| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "imgui.h" |
| 10 | #include "imgui_impl_win32.h" |
| 11 | #include "imgui_impl_dx11.h" |
| 12 | #include <d3d11.h> |
| 13 | #include <tchar.h> |
| 14 | |
| 15 | |
| 16 | static ID3D11Device* g_pd3dDevice = nullptr; |
| 17 | static ID3D11DeviceContext* g_pd3dDeviceContext = nullptr; |
| 18 | static IDXGISwapChain* g_pSwapChain = nullptr; |
| 19 | static bool g_SwapChainOccluded = false; |
| 20 | static UINT g_ResizeWidth = 0, g_ResizeHeight = 0; |
| 21 | static ID3D11RenderTargetView* g_mainRenderTargetView = nullptr; |
| 22 | |
| 23 | |
| 24 | bool CreateDeviceD3D(HWND hWnd); |
| 25 | void CleanupDeviceD3D(); |
| 26 | void CreateRenderTarget(); |
| 27 | void CleanupRenderTarget(); |
| 28 | LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 29 | |
| 30 | |
| 31 | int main(int, char**) |
| 32 | { |
| 33 | |
| 34 | ImGui_ImplWin32_EnableDpiAwareness(); |
| 35 | float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY)); |
| 36 | |
| 37 | |
| 38 | WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr }; |
| 39 | ::RegisterClassExW(&wc); |
| 40 | HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX11 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr); |
| 41 | |
| 42 | |
| 43 | if (!CreateDeviceD3D(hwnd)) |
| 44 | { |
| 45 | CleanupDeviceD3D(); |
| 46 | ::UnregisterClassW(wc.lpszClassName, wc.hInstance); |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | ::ShowWindow(hwnd, SW_SHOWDEFAULT); |
| 52 | ::UpdateWindow(hwnd); |
| 53 | |
| 54 | |
| 55 | IMGUI_CHECKVERSION(); |
| 56 | ImGui::CreateContext(); |
| 57 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 58 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 59 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 60 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 61 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | ImGui::StyleColorsDark(); |
| 69 | |
| 70 | |
| 71 | |
| 72 | ImGuiStyle& style = ImGui::GetStyle(); |
| 73 | style.ScaleAllSizes(main_scale); |
| 74 | style.FontScaleDpi = main_scale; |
| 75 | io.ConfigDpiScaleFonts = true; |
| 76 | io.ConfigDpiScaleViewports = true; |
| 77 | |
| 78 | |
| 79 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 80 | { |
| 81 | style.WindowRounding = 0.0f; |
| 82 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | ImGui_ImplWin32_Init(hwnd); |
| 87 | ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext); |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | bool show_demo_window = true; |
| 109 | bool show_another_window = false; |
| 110 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 111 | |
| 112 | |
| 113 | bool done = false; |
| 114 | while (!done) |
| 115 | { |
| 116 | |
| 117 | |
| 118 | MSG msg; |
| 119 | while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE)) |
| 120 | { |
| 121 | ::TranslateMessage(&msg); |
| 122 | ::DispatchMessage(&msg); |
| 123 | if (msg.message == WM_QUIT) |
| 124 | done = true; |
| 125 | } |
| 126 | if (done) |
| 127 | break; |
| 128 | |
| 129 | |
| 130 | if (g_SwapChainOccluded && g_pSwapChain->Present(0, DXGI_PRESENT_TEST) == DXGI_STATUS_OCCLUDED) |
| 131 | { |
| 132 | ::Sleep(10); |
| 133 | continue; |
| 134 | } |
| 135 | g_SwapChainOccluded = false; |
| 136 | |
| 137 | |
| 138 | if (g_ResizeWidth != 0 && g_ResizeHeight != 0) |
| 139 | { |
| 140 | CleanupRenderTarget(); |
| 141 | g_pSwapChain->ResizeBuffers(0, g_ResizeWidth, g_ResizeHeight, DXGI_FORMAT_UNKNOWN, 0); |
| 142 | g_ResizeWidth = g_ResizeHeight = 0; |
| 143 | CreateRenderTarget(); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | ImGui_ImplDX11_NewFrame(); |
| 148 | ImGui_ImplWin32_NewFrame(); |
| 149 | ImGui::NewFrame(); |
| 150 | |
| 151 | |
| 152 | if (show_demo_window) |
| 153 | ImGui::ShowDemoWindow(&show_demo_window); |
| 154 | |
| 155 | |
| 156 | { |
| 157 | static float f = 0.0f; |
| 158 | static int counter = 0; |
| 159 | |
| 160 | ImGui::Begin("Hello, world!"); |
| 161 | |
| 162 | ImGui::Text("This is some useful text."); |
| 163 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 164 | ImGui::Checkbox("Another Window", &show_another_window); |
| 165 | |
| 166 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 167 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 168 | |
| 169 | if (ImGui::Button("Button")) |
| 170 | counter++; |
| 171 | ImGui::SameLine(); |
| 172 | ImGui::Text("counter = %d", counter); |
| 173 | |
| 174 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 175 | ImGui::End(); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | if (show_another_window) |
| 180 | { |
| 181 | ImGui::Begin("Another Window", &show_another_window); |
| 182 | ImGui::Text("Hello from another window!"); |
| 183 | if (ImGui::Button("Close Me")) |
| 184 | show_another_window = false; |
| 185 | ImGui::End(); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | ImGui::Render(); |
| 190 | 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 }; |
| 191 | g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, nullptr); |
| 192 | g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha); |
| 193 | ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); |
| 194 | |
| 195 | |
| 196 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 197 | { |
| 198 | ImGui::UpdatePlatformWindows(); |
| 199 | ImGui::RenderPlatformWindowsDefault(); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | HRESULT hr = g_pSwapChain->Present(1, 0); |
| 204 | |
| 205 | g_SwapChainOccluded = (hr == DXGI_STATUS_OCCLUDED); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | ImGui_ImplDX11_Shutdown(); |
| 210 | ImGui_ImplWin32_Shutdown(); |
| 211 | ImGui::DestroyContext(); |
| 212 | |
| 213 | CleanupDeviceD3D(); |
| 214 | ::DestroyWindow(hwnd); |
| 215 | ::UnregisterClassW(wc.lpszClassName, wc.hInstance); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | bool CreateDeviceD3D(HWND hWnd) |
| 222 | { |
| 223 | |
| 224 | |
| 225 | DXGI_SWAP_CHAIN_DESC sd; |
| 226 | ZeroMemory(&sd, sizeof(sd)); |
| 227 | sd.BufferCount = 2; |
| 228 | sd.BufferDesc.Width = 0; |
| 229 | sd.BufferDesc.Height = 0; |
| 230 | sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 231 | sd.BufferDesc.RefreshRate.Numerator = 60; |
| 232 | sd.BufferDesc.RefreshRate.Denominator = 1; |
| 233 | sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; |
| 234 | sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 235 | sd.OutputWindow = hWnd; |
| 236 | sd.SampleDesc.Count = 1; |
| 237 | sd.SampleDesc.Quality = 0; |
| 238 | sd.Windowed = TRUE; |
| 239 | sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; |
| 240 | |
| 241 | UINT createDeviceFlags = 0; |
| 242 | |
| 243 | D3D_FEATURE_LEVEL featureLevel; |
| 244 | const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, }; |
| 245 | HRESULT res = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext); |
| 246 | if (res == DXGI_ERROR_UNSUPPORTED) |
| 247 | res = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_WARP, nullptr, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext); |
| 248 | if (res != S_OK) |
| 249 | return false; |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | IDXGIFactory* pSwapChainFactory; |
| 255 | if (SUCCEEDED(g_pSwapChain->GetParent(IID_PPV_ARGS(&pSwapChainFactory)))) |
| 256 | { |
| 257 | pSwapChainFactory->MakeWindowAssociation(hWnd, DXGI_MWA_NO_ALT_ENTER); |
| 258 | pSwapChainFactory->Release(); |
| 259 | } |
| 260 | |
| 261 | CreateRenderTarget(); |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | void CleanupDeviceD3D() |
| 266 | { |
| 267 | CleanupRenderTarget(); |
| 268 | if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = nullptr; } |
| 269 | if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = nullptr; } |
| 270 | if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; } |
| 271 | } |
| 272 | |
| 273 | void CreateRenderTarget() |
| 274 | { |
| 275 | ID3D11Texture2D* pBackBuffer; |
| 276 | g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer)); |
| 277 | g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_mainRenderTargetView); |
| 278 | pBackBuffer->Release(); |
| 279 | } |
| 280 | |
| 281 | void CleanupRenderTarget() |
| 282 | { |
| 283 | if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = nullptr; } |
| 284 | } |
| 285 | |
| 286 | |
| 287 | extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 295 | { |
| 296 | if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) |
| 297 | return true; |
| 298 | |
| 299 | switch (msg) |
| 300 | { |
| 301 | case WM_SIZE: |
| 302 | if (wParam == SIZE_MINIMIZED) |
| 303 | return 0; |
| 304 | g_ResizeWidth = (UINT)LOWORD(lParam); |
| 305 | g_ResizeHeight = (UINT)HIWORD(lParam); |
| 306 | return 0; |
| 307 | case WM_SYSCOMMAND: |
| 308 | if ((wParam & 0xfff0) == SC_KEYMENU) |
| 309 | return 0; |
| 310 | break; |
| 311 | case WM_DESTROY: |
| 312 | ::PostQuitMessage(0); |
| 313 | return 0; |
| 314 | } |
| 315 | return ::DefWindowProcW(hWnd, msg, wParam, lParam); |
| 316 | } |