Last active
June 6, 2023 12:31
-
-
Save TheSpydog/e8c6ce4854cb2ebdc1a69fb3dd5c978c to your computer and use it in GitHub Desktop.
SDL_Metal Clear Screen Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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