Skip to content

Instantly share code, notes, and snippets.

@hammanandre
Forked from gcatlin/sdl-metal-example.m
Created July 18, 2024 17:04
Show Gist options
  • Save hammanandre/af4417f62c8cd3d487e78c44cf71db31 to your computer and use it in GitHub Desktop.
Save hammanandre/af4417f62c8cd3d487e78c44cf71db31 to your computer and use it in GitHub Desktop.

Revisions

  1. @gcatlin gcatlin revised this gist Jul 14, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions sdl-metal-example.m
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    //
    // cc sdl-metal-example.m -framework SDL2 -framework Metal -framework QuartzCore
    // cc sdl-metal-example.m `sdl2-config --cflags --libs` -framework Metal -framework QuartzCore && ./a.out
    //
    #include <SDL2/SDL.h>
    #include <SDL.h>
    #import <Metal/Metal.h>
    #import <QuartzCore/CAMetalLayer.h>

    @@ -14,7 +14,6 @@ int main (int argc, char *args[])
    const CAMetalLayer *swapchain = (__bridge CAMetalLayer *)SDL_RenderGetMetalLayer(renderer);
    const id<MTLDevice> gpu = swapchain.device;
    const id<MTLCommandQueue> queue = [gpu newCommandQueue];
    SDL_DestroyRenderer(renderer);

    MTLClearColor color = MTLClearColorMake(0, 0, 0, 1);
    bool quit = false;
    @@ -46,6 +45,7 @@ int main (int argc, char *args[])
    }
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

  2. @gcatlin gcatlin renamed this gist Nov 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.m → sdl-metal-example.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // cc main.m -framework SDL2 -framework Metal -framework QuartzCore
    // cc sdl-metal-example.m -framework SDL2 -framework Metal -framework QuartzCore
    //
    #include <SDL2/SDL.h>
    #import <Metal/Metal.h>
  3. @gcatlin gcatlin revised this gist Nov 4, 2018. 1 changed file with 16 additions and 19 deletions.
    35 changes: 16 additions & 19 deletions main.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // cc main.m -framework Metal -framework QuartzCore -framework SDL2
    // cc main.m -framework SDL2 -framework Metal -framework QuartzCore
    //
    #include <SDL2/SDL.h>
    #import <Metal/Metal.h>
    @@ -9,19 +9,14 @@ int main (int argc, char *args[])
    {
    SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal");
    SDL_InitSubSystem(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("SDL Metal", -1, -1, 640, 480, 0);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    const CAMetalLayer *mtllayer = (__bridge CAMetalLayer *)SDL_RenderGetMetalLayer(renderer);
    SDL_Window *window = SDL_CreateWindow("SDL Metal", -1, -1, 640, 480, SDL_WINDOW_ALLOW_HIGHDPI);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
    const CAMetalLayer *swapchain = (__bridge CAMetalLayer *)SDL_RenderGetMetalLayer(renderer);
    const id<MTLDevice> gpu = swapchain.device;
    const id<MTLCommandQueue> queue = [gpu newCommandQueue];
    SDL_DestroyRenderer(renderer);

    const id<MTLDevice> mtldevice = mtllayer.device;
    const id<MTLCommandQueue> mtlcmdqueue = [mtldevice newCommandQueue];
    MTLRenderPassDescriptor *pass = [MTLRenderPassDescriptor renderPassDescriptor];
    MTLRenderPassColorAttachmentDescriptor *ca = pass.colorAttachments[0];
    ca.clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0);
    ca.loadAction = MTLLoadActionClear;
    ca.storeAction = MTLStoreActionStore;

    MTLClearColor color = MTLClearColorMake(0, 0, 0, 1);
    bool quit = false;
    SDL_Event e;

    @@ -33,16 +28,18 @@ int main (int argc, char *args[])
    }

    @autoreleasepool {
    id<MTLCommandBuffer> buffer = [mtlcmdqueue commandBuffer];
    id<MTLRenderCommandEncoder> encoder = [buffer renderCommandEncoderWithDescriptor:pass];
    id<CAMetalDrawable> surface = [mtllayer nextDrawable];
    id<CAMetalDrawable> surface = [swapchain nextDrawable];

    MTLRenderPassColorAttachmentDescriptor *ca = pass.colorAttachments[0];
    MTLClearColor color = ca.clearColor;
    color.red = (color.red > 1.0) ? 0 : color.red + 0.01;
    ca.clearColor = color;
    ca.texture = surface.texture;

    MTLRenderPassDescriptor *pass = [MTLRenderPassDescriptor renderPassDescriptor];
    pass.colorAttachments[0].clearColor = color;
    pass.colorAttachments[0].loadAction = MTLLoadActionClear;
    pass.colorAttachments[0].storeAction = MTLStoreActionStore;
    pass.colorAttachments[0].texture = surface.texture;

    id<MTLCommandBuffer> buffer = [queue commandBuffer];
    id<MTLRenderCommandEncoder> encoder = [buffer renderCommandEncoderWithDescriptor:pass];
    [encoder endEncoding];
    [buffer presentDrawable:surface];
    [buffer commit];
  4. @gcatlin gcatlin revised this gist Nov 2, 2018. 1 changed file with 4 additions and 10 deletions.
    14 changes: 4 additions & 10 deletions main.m
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,21 @@
    //
    // cc main.m -framework AppKit -framework Metal -framework QuartzCore -framework SDL2
    // cc main.m -framework Metal -framework QuartzCore -framework SDL2
    //
    #include <SDL2/SDL.h>
    #import <AppKit/AppKit.h>
    #import <Metal/Metal.h>
    #import <QuartzCore/CAMetalLayer.h>

    int main (int argc, char *args[])
    {
    const int w = 640;
    const int h = 480;

    SDL_SetHintWithPriority(SDL_HINT_RENDER_DRIVER, "metal", SDL_HINT_OVERRIDE);
    SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal");
    SDL_InitSubSystem(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("SDL Metal", -1, -1, w, h, 0);

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
    SDL_Window *window = SDL_CreateWindow("SDL Metal", -1, -1, 640, 480, 0);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    const CAMetalLayer *mtllayer = (__bridge CAMetalLayer *)SDL_RenderGetMetalLayer(renderer);
    SDL_DestroyRenderer(renderer);

    const id<MTLDevice> mtldevice = mtllayer.device;
    const id<MTLCommandQueue> mtlcmdqueue = [mtldevice newCommandQueue];

    MTLRenderPassDescriptor *pass = [MTLRenderPassDescriptor renderPassDescriptor];
    MTLRenderPassColorAttachmentDescriptor *ca = pass.colorAttachments[0];
    ca.clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0);
  5. @gcatlin gcatlin created this gist Nov 2, 2018.
    62 changes: 62 additions & 0 deletions main.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    //
    // cc main.m -framework AppKit -framework Metal -framework QuartzCore -framework SDL2
    //
    #include <SDL2/SDL.h>
    #import <AppKit/AppKit.h>
    #import <Metal/Metal.h>
    #import <QuartzCore/CAMetalLayer.h>

    int main (int argc, char *args[])
    {
    const int w = 640;
    const int h = 480;

    SDL_SetHintWithPriority(SDL_HINT_RENDER_DRIVER, "metal", SDL_HINT_OVERRIDE);
    SDL_InitSubSystem(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("SDL Metal", -1, -1, w, h, 0);

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
    const CAMetalLayer *mtllayer = (__bridge CAMetalLayer *)SDL_RenderGetMetalLayer(renderer);
    SDL_DestroyRenderer(renderer);

    const id<MTLDevice> mtldevice = mtllayer.device;
    const id<MTLCommandQueue> mtlcmdqueue = [mtldevice newCommandQueue];

    MTLRenderPassDescriptor *pass = [MTLRenderPassDescriptor renderPassDescriptor];
    MTLRenderPassColorAttachmentDescriptor *ca = pass.colorAttachments[0];
    ca.clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0);
    ca.loadAction = MTLLoadActionClear;
    ca.storeAction = MTLStoreActionStore;

    bool quit = false;
    SDL_Event e;

    while (!quit) {
    while (SDL_PollEvent(&e) != 0) {
    switch (e.type) {
    case SDL_QUIT: quit = true; break;
    }
    }

    @autoreleasepool {
    id<MTLCommandBuffer> buffer = [mtlcmdqueue commandBuffer];
    id<MTLRenderCommandEncoder> encoder = [buffer renderCommandEncoderWithDescriptor:pass];
    id<CAMetalDrawable> surface = [mtllayer nextDrawable];

    MTLRenderPassColorAttachmentDescriptor *ca = pass.colorAttachments[0];
    MTLClearColor color = ca.clearColor;
    color.red = (color.red > 1.0) ? 0 : color.red + 0.01;
    ca.clearColor = color;
    ca.texture = surface.texture;

    [encoder endEncoding];
    [buffer presentDrawable:surface];
    [buffer commit];
    }
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
    }