Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SDL_Metal Clear Screen Example
/* Requires >= SDL 2.0.11 (not yet released)
* Also requires a patch that has not yet been merged:
* https://bugzilla.libsdl.org/show_bug.cgi?id=4796
*/
#include <SDL.h>
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
int main(int argc, char **argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow(
"Metal Clear Screen Example",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
480,
SDL_WINDOW_METAL | SDL_ALLOW_HIGHDPI
);
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
SDL_MetalView view = SDL_Metal_CreateView(window);
CAMetalLayer *layer = SDL_Metal_GetLayer(view);
layer.device = device;
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
id<MTLCommandQueue> commandQueue = [device newCommandQueue];
SDL_Event e;
int quit = 0;
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = 1;
}
}
id<CAMetalDrawable> drawable = [layer nextDrawable];
MTLRenderPassDescriptor *passDesc = [MTLRenderPassDescriptor renderPassDescriptor];
passDesc.colorAttachments[0].texture = drawable.texture;
passDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
passDesc.colorAttachments[0].storeAction = MTLStoreActionStore;
passDesc.colorAttachments[0].clearColor = MTLClearColorMake(1.0, 0.0, 1.0, 1.0);
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
id<MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor:passDesc];
[encoder endEncoding];
[commandBuffer presentDrawable:drawable];
[commandBuffer commit];
SDL_Delay(16);
}
SDL_Metal_DestroyView(view);
SDL_DestroyWindow(window);
SDL_Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.