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