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