Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Minimal C GLFW Metal example
//
// cc glfw-metal-example.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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment