| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | #include "imgui.h" |
| 11 | #include "imgui_impl_sdl3.h" |
| 12 | #include "imgui_impl_dx11.h" |
| 13 | #include <d3d11.h> |
| 14 | #include <stdio.h> |
| 15 | #include <SDL3/SDL.h> |
| 16 | |
| 17 | |
| 18 | static ID3D11Device* g_pd3dDevice = nullptr; |
| 19 | static ID3D11DeviceContext* g_pd3dDeviceContext = nullptr; |
| 20 | static IDXGISwapChain* g_pSwapChain = nullptr; |
| 21 | static ID3D11RenderTargetView* g_mainRenderTargetView = nullptr; |
| 22 | |
| 23 | |
| 24 | bool CreateDeviceD3D(HWND hWnd); |
| 25 | void CleanupDeviceD3D(); |
| 26 | void CreateRenderTarget(); |
| 27 | void CleanupRenderTarget(); |
| 28 | |
| 29 | |
| 30 | int main(int, char**) |
| 31 | { |
| 32 | |
| 33 | |
| 34 | if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) |
| 35 | { |
| 36 | printf("Error: SDL_Init(): %s\n", SDL_GetError()); |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); |
| 42 | SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY; |
| 43 | SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+DirectX11 example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags); |
| 44 | if (window == nullptr) |
| 45 | { |
| 46 | printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | SDL_PropertiesID props = SDL_GetWindowProperties(window); |
| 51 | HWND hwnd = (HWND)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr); |
| 52 | |
| 53 | |
| 54 | if (!CreateDeviceD3D(hwnd)) |
| 55 | { |
| 56 | CleanupDeviceD3D(); |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); |
| 61 | SDL_ShowWindow(window); |
| 62 | |
| 63 | |
| 64 | IMGUI_CHECKVERSION(); |
| 65 | ImGui::CreateContext(); |
| 66 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 67 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 68 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 69 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 70 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; |
| 71 | |
| 72 | |
| 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_InitForD3D(window); |
| 94 | ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext); |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | bool show_demo_window = true; |
| 116 | bool show_another_window = false; |
| 117 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 118 | |
| 119 | |
| 120 | bool done = false; |
| 121 | while (!done) |
| 122 | { |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | SDL_Event event; |
| 130 | while (SDL_PollEvent(&event)) |
| 131 | { |
| 132 | ImGui_ImplSDL3_ProcessEvent(&event); |
| 133 | if (event.type == SDL_EVENT_QUIT) |
| 134 | done = true; |
| 135 | if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) |
| 136 | done = true; |
| 137 | if (event.type == SDL_EVENT_WINDOW_RESIZED && event.window.windowID == SDL_GetWindowID(window)) |
| 138 | { |
| 139 | |
| 140 | CleanupRenderTarget(); |
| 141 | g_pSwapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0); |
| 142 | CreateRenderTarget(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) |
| 148 | { |
| 149 | SDL_Delay(10); |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | ImGui_ImplDX11_NewFrame(); |
| 155 | ImGui_ImplSDL3_NewFrame(); |
| 156 | ImGui::NewFrame(); |
| 157 | |
| 158 | |
| 159 | if (show_demo_window) |
| 160 | ImGui::ShowDemoWindow(&show_demo_window); |
| 161 | |
| 162 | |
| 163 | { |
| 164 | static float f = 0.0f; |
| 165 | static int counter = 0; |
| 166 | |
| 167 | ImGui::Begin("Hello, world!"); |
| 168 | |
| 169 | ImGui::Text("This is some useful text."); |
| 170 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 171 | ImGui::Checkbox("Another Window", &show_another_window); |
| 172 | |
| 173 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 174 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 175 | |
| 176 | if (ImGui::Button("Button")) |
| 177 | counter++; |
| 178 | ImGui::SameLine(); |
| 179 | ImGui::Text("counter = %d", counter); |
| 180 | |
| 181 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 182 | ImGui::End(); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | if (show_another_window) |
| 187 | { |
| 188 | ImGui::Begin("Another Window", &show_another_window); |
| 189 | ImGui::Text("Hello from another window!"); |
| 190 | if (ImGui::Button("Close Me")) |
| 191 | show_another_window = false; |
| 192 | ImGui::End(); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | ImGui::Render(); |
| 197 | const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w }; |
| 198 | g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, nullptr); |
| 199 | g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha); |
| 200 | ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); |
| 201 | |
| 202 | |
| 203 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 204 | { |
| 205 | ImGui::UpdatePlatformWindows(); |
| 206 | ImGui::RenderPlatformWindowsDefault(); |
| 207 | } |
| 208 | |
| 209 | g_pSwapChain->Present(1, 0); |
| 210 | |
| 211 | } |
| 212 | |
| 213 | |
| 214 | |
| 215 | ImGui_ImplDX11_Shutdown(); |
| 216 | ImGui_ImplSDL3_Shutdown(); |
| 217 | ImGui::DestroyContext(); |
| 218 | |
| 219 | CleanupDeviceD3D(); |
| 220 | SDL_DestroyWindow(window); |
| 221 | SDL_Quit(); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | bool CreateDeviceD3D(HWND hWnd) |
| 228 | { |
| 229 | |
| 230 | |
| 231 | DXGI_SWAP_CHAIN_DESC sd; |
| 232 | ZeroMemory(&sd, sizeof(sd)); |
| 233 | sd.BufferCount = 2; |
| 234 | sd.BufferDesc.Width = 0; |
| 235 | sd.BufferDesc.Height = 0; |
| 236 | sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 237 | sd.BufferDesc.RefreshRate.Numerator = 60; |
| 238 | sd.BufferDesc.RefreshRate.Denominator = 1; |
| 239 | sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; |
| 240 | sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 241 | sd.OutputWindow = hWnd; |
| 242 | sd.SampleDesc.Count = 1; |
| 243 | sd.SampleDesc.Quality = 0; |
| 244 | sd.Windowed = TRUE; |
| 245 | sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; |
| 246 | |
| 247 | UINT createDeviceFlags = 0; |
| 248 | |
| 249 | D3D_FEATURE_LEVEL featureLevel; |
| 250 | const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, }; |
| 251 | HRESULT res = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext); |
| 252 | if (res == DXGI_ERROR_UNSUPPORTED) |
| 253 | res = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_WARP, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext); |
| 254 | if (res != S_OK) |
| 255 | return false; |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | IDXGIFactory* pSwapChainFactory; |
| 261 | if (SUCCEEDED(g_pSwapChain->GetParent(IID_PPV_ARGS(&pSwapChainFactory)))) |
| 262 | { |
| 263 | pSwapChainFactory->MakeWindowAssociation(hWnd, DXGI_MWA_NO_ALT_ENTER); |
| 264 | pSwapChainFactory->Release(); |
| 265 | } |
| 266 | |
| 267 | CreateRenderTarget(); |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | void CleanupDeviceD3D() |
| 272 | { |
| 273 | CleanupRenderTarget(); |
| 274 | if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = nullptr; } |
| 275 | if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = nullptr; } |
| 276 | if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; } |
| 277 | } |
| 278 | |
| 279 | void CreateRenderTarget() |
| 280 | { |
| 281 | ID3D11Texture2D* pBackBuffer; |
| 282 | g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer)); |
| 283 | g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_mainRenderTargetView); |
| 284 | pBackBuffer->Release(); |
| 285 | } |
| 286 | |
| 287 | void CleanupRenderTarget() |
| 288 | { |
| 289 | if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = nullptr; } |
| 290 | } |