| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | #include "imgui.h" |
| 11 | #include "imgui_impl_sdl3.h" |
| 12 | #include "imgui_impl_metal.h" |
| 13 | #include <stdio.h> |
| 14 | #include <SDL3/SDL.h> |
| 15 | |
| 16 | #import <Metal/Metal.h> |
| 17 | #import <QuartzCore/QuartzCore.h> |
| 18 | |
| 19 | |
| 20 | int main(int, char**) |
| 21 | { |
| 22 | |
| 23 | |
| 24 | if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) |
| 25 | { |
| 26 | printf("Error: SDL_Init(): %s\n", SDL_GetError()); |
| 27 | return 1; |
| 28 | } |
| 29 | |
| 30 | |
| 31 | float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); |
| 32 | SDL_WindowFlags window_flags = SDL_WINDOW_METAL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY; |
| 33 | SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+Metal example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags); |
| 34 | if (window == nullptr) |
| 35 | { |
| 36 | printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); |
| 37 | return 1; |
| 38 | } |
| 39 | SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); |
| 40 | SDL_ShowWindow(window); |
| 41 | |
| 42 | |
| 43 | id<MTLDevice> metalDevice = MTLCreateSystemDefaultDevice(); |
| 44 | if (!metalDevice) |
| 45 | { |
| 46 | printf("Error: failed to create Metal device.\n"); |
| 47 | SDL_DestroyWindow(window); |
| 48 | SDL_Quit(); |
| 49 | return 1; |
| 50 | } |
| 51 | SDL_MetalView view = SDL_Metal_CreateView(window); |
| 52 | CAMetalLayer* layer = (__bridge CAMetalLayer*)SDL_Metal_GetLayer(view); |
| 53 | layer.device = metalDevice; |
| 54 | layer.pixelFormat = MTLPixelFormatBGRA8Unorm; |
| 55 | |
| 56 | id<MTLCommandQueue> commandQueue = [layer.device newCommandQueue]; |
| 57 | MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor new]; |
| 58 | |
| 59 | |
| 60 | IMGUI_CHECKVERSION(); |
| 61 | ImGui::CreateContext(); |
| 62 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 63 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 64 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 65 | |
| 66 | |
| 67 | ImGui::StyleColorsDark(); |
| 68 | |
| 69 | |
| 70 | |
| 71 | ImGuiStyle& style = ImGui::GetStyle(); |
| 72 | style.ScaleAllSizes(main_scale); |
| 73 | style.FontScaleDpi = main_scale; |
| 74 | |
| 75 | |
| 76 | ImGui_ImplMetal_Init(layer.device); |
| 77 | ImGui_ImplSDL3_InitForMetal(window); |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | bool show_demo_window = true; |
| 99 | bool show_another_window = false; |
| 100 | float clear_color[4] = { 0.45f, 0.55f, 0.60f, 1.00f }; |
| 101 | |
| 102 | |
| 103 | bool done = false; |
| 104 | while (!done) |
| 105 | { |
| 106 | @autoreleasepool |
| 107 | { |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | SDL_Event event; |
| 115 | while (SDL_PollEvent(&event)) |
| 116 | { |
| 117 | ImGui_ImplSDL3_ProcessEvent(&event); |
| 118 | if (event.type == SDL_EVENT_QUIT) |
| 119 | done = true; |
| 120 | if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) |
| 121 | done = true; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) |
| 126 | { |
| 127 | SDL_Delay(10); |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | int width, height; |
| 132 | SDL_GetWindowSizeInPixels(window, &width, &height); |
| 133 | |
| 134 | layer.drawableSize = CGSizeMake(width, height); |
| 135 | id<CAMetalDrawable> drawable = [layer nextDrawable]; |
| 136 | |
| 137 | id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer]; |
| 138 | renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(clear_color[0] * clear_color[3], clear_color[1] * clear_color[3], clear_color[2] * clear_color[3], clear_color[3]); |
| 139 | renderPassDescriptor.colorAttachments[0].texture = drawable.texture; |
| 140 | renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear; |
| 141 | renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore; |
| 142 | id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; |
| 143 | [renderEncoder pushDebugGroup:@"ImGui demo"]; |
| 144 | |
| 145 | |
| 146 | ImGui_ImplMetal_NewFrame(renderPassDescriptor); |
| 147 | ImGui_ImplSDL3_NewFrame(); |
| 148 | ImGui::NewFrame(); |
| 149 | |
| 150 | |
| 151 | if (show_demo_window) |
| 152 | ImGui::ShowDemoWindow(&show_demo_window); |
| 153 | |
| 154 | |
| 155 | { |
| 156 | static float f = 0.0f; |
| 157 | static int counter = 0; |
| 158 | |
| 159 | ImGui::Begin("Hello, world!"); |
| 160 | |
| 161 | ImGui::Text("This is some useful text."); |
| 162 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 163 | ImGui::Checkbox("Another Window", &show_another_window); |
| 164 | |
| 165 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 166 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 167 | |
| 168 | if (ImGui::Button("Button")) |
| 169 | counter++; |
| 170 | ImGui::SameLine(); |
| 171 | ImGui::Text("counter = %d", counter); |
| 172 | |
| 173 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 174 | ImGui::End(); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | if (show_another_window) |
| 179 | { |
| 180 | ImGui::Begin("Another Window", &show_another_window); |
| 181 | ImGui::Text("Hello from another window!"); |
| 182 | if (ImGui::Button("Close Me")) |
| 183 | show_another_window = false; |
| 184 | ImGui::End(); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | ImGui::Render(); |
| 189 | ImDrawData* draw_data = ImGui::GetDrawData(); |
| 190 | ImGui_ImplMetal_RenderDrawData(draw_data, commandBuffer, renderEncoder); |
| 191 | |
| 192 | [renderEncoder popDebugGroup]; |
| 193 | [renderEncoder endEncoding]; |
| 194 | |
| 195 | [commandBuffer presentDrawable:drawable]; |
| 196 | [commandBuffer commit]; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | ImGui_ImplMetal_Shutdown(); |
| 203 | ImGui_ImplSDL3_Shutdown(); |
| 204 | ImGui::DestroyContext(); |
| 205 | |
| 206 | SDL_DestroyWindow(window); |
| 207 | SDL_Quit(); |
| 208 | |
| 209 | return 0; |
| 210 | } |