| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #include "imgui.h" |
| 15 | #include "imgui_impl_sdl3.h" |
| 16 | #include "imgui_impl_sdlgpu3.h" |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <SDL3/SDL.h> |
| 20 | |
| 21 | |
| 22 | #ifdef __EMSCRIPTEN__ |
| 23 | #include "../libs/emscripten/emscripten_mainloop_stub.h" |
| 24 | #endif |
| 25 | |
| 26 | |
| 27 | int main(int, char**) |
| 28 | { |
| 29 | |
| 30 | |
| 31 | if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) |
| 32 | { |
| 33 | printf("Error: SDL_Init(): %s\n", SDL_GetError()); |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); |
| 39 | SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY; |
| 40 | SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+SDL_GPU example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags); |
| 41 | if (window == nullptr) |
| 42 | { |
| 43 | printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); |
| 44 | return 1; |
| 45 | } |
| 46 | SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); |
| 47 | SDL_ShowWindow(window); |
| 48 | |
| 49 | |
| 50 | SDL_GPUDevice* gpu_device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_MSL | SDL_GPU_SHADERFORMAT_METALLIB, true, nullptr); |
| 51 | if (gpu_device == nullptr) |
| 52 | { |
| 53 | printf("Error: SDL_CreateGPUDevice(): %s\n", SDL_GetError()); |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | if (!SDL_ClaimWindowForGPUDevice(gpu_device, window)) |
| 59 | { |
| 60 | printf("Error: SDL_ClaimWindowForGPUDevice(): %s\n", SDL_GetError()); |
| 61 | return 1; |
| 62 | } |
| 63 | SDL_SetGPUSwapchainParameters(gpu_device, window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, SDL_GPU_PRESENTMODE_VSYNC); |
| 64 | |
| 65 | |
| 66 | IMGUI_CHECKVERSION(); |
| 67 | ImGui::CreateContext(); |
| 68 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 69 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 70 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 71 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 72 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; |
| 73 | |
| 74 | |
| 75 | ImGui::StyleColorsDark(); |
| 76 | |
| 77 | |
| 78 | |
| 79 | ImGuiStyle& style = ImGui::GetStyle(); |
| 80 | style.ScaleAllSizes(main_scale); |
| 81 | style.FontScaleDpi = main_scale; |
| 82 | io.ConfigDpiScaleFonts = true; |
| 83 | io.ConfigDpiScaleViewports = true; |
| 84 | |
| 85 | |
| 86 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 87 | { |
| 88 | style.WindowRounding = 0.0f; |
| 89 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | ImGui_ImplSDL3_InitForSDLGPU(window); |
| 94 | ImGui_ImplSDLGPU3_InitInfo init_info = {}; |
| 95 | init_info.Device = gpu_device; |
| 96 | init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpu_device, window); |
| 97 | init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1; |
| 98 | init_info.SwapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR; |
| 99 | init_info.PresentMode = SDL_GPU_PRESENTMODE_VSYNC; |
| 100 | ImGui_ImplSDLGPU3_Init(&init_info); |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | bool show_demo_window = true; |
| 122 | bool show_another_window = false; |
| 123 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 124 | |
| 125 | |
| 126 | bool done = false; |
| 127 | while (!done) |
| 128 | { |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | SDL_Event event; |
| 136 | while (SDL_PollEvent(&event)) |
| 137 | { |
| 138 | ImGui_ImplSDL3_ProcessEvent(&event); |
| 139 | if (event.type == SDL_EVENT_QUIT) |
| 140 | done = true; |
| 141 | if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) |
| 142 | done = true; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) |
| 147 | { |
| 148 | SDL_Delay(10); |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | ImGui_ImplSDLGPU3_NewFrame(); |
| 154 | ImGui_ImplSDL3_NewFrame(); |
| 155 | ImGui::NewFrame(); |
| 156 | |
| 157 | |
| 158 | if (show_demo_window) |
| 159 | ImGui::ShowDemoWindow(&show_demo_window); |
| 160 | |
| 161 | |
| 162 | { |
| 163 | static float f = 0.0f; |
| 164 | static int counter = 0; |
| 165 | |
| 166 | ImGui::Begin("Hello, world!"); |
| 167 | |
| 168 | ImGui::Text("This is some useful text."); |
| 169 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 170 | ImGui::Checkbox("Another Window", &show_another_window); |
| 171 | |
| 172 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 173 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 174 | |
| 175 | if (ImGui::Button("Button")) |
| 176 | counter++; |
| 177 | ImGui::SameLine(); |
| 178 | ImGui::Text("counter = %d", counter); |
| 179 | |
| 180 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 181 | ImGui::End(); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | if (show_another_window) |
| 186 | { |
| 187 | ImGui::Begin("Another Window", &show_another_window); |
| 188 | ImGui::Text("Hello from another window!"); |
| 189 | if (ImGui::Button("Close Me")) |
| 190 | show_another_window = false; |
| 191 | ImGui::End(); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | ImGui::Render(); |
| 196 | ImDrawData* draw_data = ImGui::GetDrawData(); |
| 197 | const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f); |
| 198 | |
| 199 | SDL_GPUCommandBuffer* command_buffer = SDL_AcquireGPUCommandBuffer(gpu_device); |
| 200 | |
| 201 | SDL_GPUTexture* swapchain_texture; |
| 202 | SDL_WaitAndAcquireGPUSwapchainTexture(command_buffer, window, &swapchain_texture, nullptr, nullptr); |
| 203 | |
| 204 | if (swapchain_texture != nullptr && !is_minimized) |
| 205 | { |
| 206 | |
| 207 | ImGui_ImplSDLGPU3_PrepareDrawData(draw_data, command_buffer); |
| 208 | |
| 209 | |
| 210 | SDL_GPUColorTargetInfo target_info = {}; |
| 211 | target_info.texture = swapchain_texture; |
| 212 | target_info.clear_color = SDL_FColor { clear_color.x, clear_color.y, clear_color.z, clear_color.w }; |
| 213 | target_info.load_op = SDL_GPU_LOADOP_CLEAR; |
| 214 | target_info.store_op = SDL_GPU_STOREOP_STORE; |
| 215 | target_info.mip_level = 0; |
| 216 | target_info.layer_or_depth_plane = 0; |
| 217 | target_info.cycle = false; |
| 218 | SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(command_buffer, &target_info, 1, nullptr); |
| 219 | |
| 220 | |
| 221 | ImGui_ImplSDLGPU3_RenderDrawData(draw_data, command_buffer, render_pass); |
| 222 | |
| 223 | SDL_EndGPURenderPass(render_pass); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 228 | { |
| 229 | ImGui::UpdatePlatformWindows(); |
| 230 | ImGui::RenderPlatformWindowsDefault(); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | SDL_SubmitGPUCommandBuffer(command_buffer); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | |
| 239 | SDL_WaitForGPUIdle(gpu_device); |
| 240 | ImGui_ImplSDL3_Shutdown(); |
| 241 | ImGui_ImplSDLGPU3_Shutdown(); |
| 242 | ImGui::DestroyContext(); |
| 243 | |
| 244 | SDL_ReleaseWindowFromGPUDevice(gpu_device, window); |
| 245 | SDL_DestroyGPUDevice(gpu_device); |
| 246 | SDL_DestroyWindow(window); |
| 247 | SDL_Quit(); |
| 248 | |
| 249 | return 0; |
| 250 | } |