Skip to content

Instantly share code, notes, and snippets.

@gcatlin
Last active October 13, 2025 01:15
Show Gist options
  • Select an option

  • Save gcatlin/987be74e2d58da96093a7598f3fbfb27 to your computer and use it in GitHub Desktop.

Select an option

Save gcatlin/987be74e2d58da96093a7598f3fbfb27 to your computer and use it in GitHub Desktop.

Revisions

  1. gcatlin revised this gist Dec 14, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion glfw-metal-example.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // cc glfw-metal-example.m -lGLFW -framework AppKit -framework Metal -framework QuartzCore
    // cc glfw-metal-example.m `pkg-config --cflags --libs glfw3` -framework AppKit -framework Metal -framework QuartzCore
    //
    #define GLFW_INCLUDE_NONE
    #define GLFW_EXPOSE_NATIVE_COCOA
  2. gcatlin revised this gist May 22, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion glfw-metal-example.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // cc sdl-glfw-exampl.m -lGLFW -framework AppKit -framework Metal -framework QuartzCore
    // cc glfw-metal-example.m -lGLFW -framework AppKit -framework Metal -framework QuartzCore
    //
    #define GLFW_INCLUDE_NONE
    #define GLFW_EXPOSE_NATIVE_COCOA
  3. gcatlin renamed this gist Nov 29, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. gcatlin created this gist Nov 4, 2018.
    63 changes: 63 additions & 0 deletions sdl-glfw-example.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    //
    // cc sdl-glfw-exampl.m -lGLFW -framework AppKit -framework Metal -framework QuartzCore
    //
    #define GLFW_INCLUDE_NONE
    #define GLFW_EXPOSE_NATIVE_COCOA
    #include <GLFW/glfw3.h>
    #include <GLFW/glfw3native.h>

    #import <Metal/Metal.h>
    #import <QuartzCore/CAMetalLayer.h>

    static void quit(GLFWwindow *window, int key, int scancode, int action, int mods)
    {
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
    glfwSetWindowShouldClose(window, GLFW_TRUE);
    }
    }

    int main(void)
    {
    const id<MTLDevice> gpu = MTLCreateSystemDefaultDevice();
    const id<MTLCommandQueue> queue = [gpu newCommandQueue];
    CAMetalLayer *swapchain = [CAMetalLayer layer];
    swapchain.device = gpu;
    swapchain.opaque = YES;

    glfwInit();
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    GLFWwindow *window = glfwCreateWindow(640, 480, "GLFW Metal", NULL, NULL);
    NSWindow *nswindow = glfwGetCocoaWindow(window);
    nswindow.contentView.layer = swapchain;
    nswindow.contentView.wantsLayer = YES;

    glfwSetKeyCallback(window, quit);
    MTLClearColor color = MTLClearColorMake(0, 0, 0, 1);

    while (!glfwWindowShouldClose(window)) {
    glfwPollEvents();

    @autoreleasepool {
    color.red = (color.red > 1.0) ? 0 : color.red + 0.01;

    id<CAMetalDrawable> surface = [swapchain nextDrawable];

    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];
    }
    }

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
    }