| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "imgui.h" |
| 10 | #include "imgui_impl_dx9.h" |
| 11 | #include "imgui_impl_win32.h" |
| 12 | #include <d3d9.h> |
| 13 | #include <tchar.h> |
| 14 | |
| 15 | |
| 16 | static LPDIRECT3D9 g_pD3D = nullptr; |
| 17 | static LPDIRECT3DDEVICE9 g_pd3dDevice = nullptr; |
| 18 | static bool g_DeviceLost = false; |
| 19 | static UINT g_ResizeWidth = 0, g_ResizeHeight = 0; |
| 20 | static D3DPRESENT_PARAMETERS g_d3dpp = {}; |
| 21 | |
| 22 | |
| 23 | bool CreateDeviceD3D(HWND hWnd); |
| 24 | void CleanupDeviceD3D(); |
| 25 | void ResetDevice(); |
| 26 | LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 27 | |
| 28 | |
| 29 | int main(int, char**) |
| 30 | { |
| 31 | |
| 32 | ImGui_ImplWin32_EnableDpiAwareness(); |
| 33 | float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY)); |
| 34 | |
| 35 | |
| 36 | WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr }; |
| 37 | ::RegisterClassExW(&wc); |
| 38 | HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX9 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr); |
| 39 | |
| 40 | |
| 41 | if (!CreateDeviceD3D(hwnd)) |
| 42 | { |
| 43 | CleanupDeviceD3D(); |
| 44 | ::UnregisterClassW(wc.lpszClassName, wc.hInstance); |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | ::ShowWindow(hwnd, SW_SHOWDEFAULT); |
| 50 | ::UpdateWindow(hwnd); |
| 51 | |
| 52 | |
| 53 | IMGUI_CHECKVERSION(); |
| 54 | ImGui::CreateContext(); |
| 55 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 56 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 57 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 58 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 59 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | ImGui::StyleColorsDark(); |
| 65 | |
| 66 | |
| 67 | |
| 68 | ImGuiStyle& style = ImGui::GetStyle(); |
| 69 | style.ScaleAllSizes(main_scale); |
| 70 | style.FontScaleDpi = main_scale; |
| 71 | io.ConfigDpiScaleFonts = true; |
| 72 | io.ConfigDpiScaleViewports = true; |
| 73 | |
| 74 | |
| 75 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 76 | { |
| 77 | style.WindowRounding = 0.0f; |
| 78 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | ImGui_ImplWin32_Init(hwnd); |
| 83 | ImGui_ImplDX9_Init(g_pd3dDevice); |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | bool show_demo_window = true; |
| 105 | bool show_another_window = false; |
| 106 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 107 | |
| 108 | |
| 109 | bool done = false; |
| 110 | while (!done) |
| 111 | { |
| 112 | |
| 113 | |
| 114 | MSG msg; |
| 115 | while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE)) |
| 116 | { |
| 117 | ::TranslateMessage(&msg); |
| 118 | ::DispatchMessage(&msg); |
| 119 | if (msg.message == WM_QUIT) |
| 120 | done = true; |
| 121 | } |
| 122 | if (done) |
| 123 | break; |
| 124 | |
| 125 | |
| 126 | if (g_DeviceLost) |
| 127 | { |
| 128 | HRESULT hr = g_pd3dDevice->TestCooperativeLevel(); |
| 129 | if (hr == D3DERR_DEVICELOST) |
| 130 | { |
| 131 | ::Sleep(10); |
| 132 | continue; |
| 133 | } |
| 134 | if (hr == D3DERR_DEVICENOTRESET) |
| 135 | ResetDevice(); |
| 136 | g_DeviceLost = false; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | if (g_ResizeWidth != 0 && g_ResizeHeight != 0) |
| 141 | { |
| 142 | g_d3dpp.BackBufferWidth = g_ResizeWidth; |
| 143 | g_d3dpp.BackBufferHeight = g_ResizeHeight; |
| 144 | g_ResizeWidth = g_ResizeHeight = 0; |
| 145 | ResetDevice(); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | ImGui_ImplDX9_NewFrame(); |
| 150 | ImGui_ImplWin32_NewFrame(); |
| 151 | ImGui::NewFrame(); |
| 152 | |
| 153 | |
| 154 | if (show_demo_window) |
| 155 | ImGui::ShowDemoWindow(&show_demo_window); |
| 156 | |
| 157 | |
| 158 | { |
| 159 | static float f = 0.0f; |
| 160 | static int counter = 0; |
| 161 | |
| 162 | ImGui::Begin("Hello, world!"); |
| 163 | |
| 164 | ImGui::Text("This is some useful text."); |
| 165 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 166 | ImGui::Checkbox("Another Window", &show_another_window); |
| 167 | |
| 168 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 169 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 170 | |
| 171 | if (ImGui::Button("Button")) |
| 172 | counter++; |
| 173 | ImGui::SameLine(); |
| 174 | ImGui::Text("counter = %d", counter); |
| 175 | |
| 176 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 177 | ImGui::End(); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | if (show_another_window) |
| 182 | { |
| 183 | ImGui::Begin("Another Window", &show_another_window); |
| 184 | ImGui::Text("Hello from another window!"); |
| 185 | if (ImGui::Button("Close Me")) |
| 186 | show_another_window = false; |
| 187 | ImGui::End(); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | ImGui::EndFrame(); |
| 192 | g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 193 | g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 194 | g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); |
| 195 | D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x*clear_color.w*255.0f), (int)(clear_color.y*clear_color.w*255.0f), (int)(clear_color.z*clear_color.w*255.0f), (int)(clear_color.w*255.0f)); |
| 196 | g_pd3dDevice->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0); |
| 197 | if (g_pd3dDevice->BeginScene() >= 0) |
| 198 | { |
| 199 | ImGui::Render(); |
| 200 | ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); |
| 201 | g_pd3dDevice->EndScene(); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 206 | { |
| 207 | ImGui::UpdatePlatformWindows(); |
| 208 | ImGui::RenderPlatformWindowsDefault(); |
| 209 | } |
| 210 | |
| 211 | HRESULT result = g_pd3dDevice->Present(nullptr, nullptr, nullptr, nullptr); |
| 212 | if (result == D3DERR_DEVICELOST) |
| 213 | g_DeviceLost = true; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | ImGui_ImplDX9_Shutdown(); |
| 218 | ImGui_ImplWin32_Shutdown(); |
| 219 | ImGui::DestroyContext(); |
| 220 | |
| 221 | CleanupDeviceD3D(); |
| 222 | ::DestroyWindow(hwnd); |
| 223 | ::UnregisterClassW(wc.lpszClassName, wc.hInstance); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | bool CreateDeviceD3D(HWND hWnd) |
| 230 | { |
| 231 | if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == nullptr) |
| 232 | return false; |
| 233 | |
| 234 | |
| 235 | ZeroMemory(&g_d3dpp, sizeof(g_d3dpp)); |
| 236 | g_d3dpp.Windowed = TRUE; |
| 237 | g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 238 | g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; |
| 239 | g_d3dpp.EnableAutoDepthStencil = TRUE; |
| 240 | g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; |
| 241 | g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; |
| 242 | |
| 243 | if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0) |
| 244 | return false; |
| 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | void CleanupDeviceD3D() |
| 250 | { |
| 251 | if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; } |
| 252 | if (g_pD3D) { g_pD3D->Release(); g_pD3D = nullptr; } |
| 253 | } |
| 254 | |
| 255 | void ResetDevice() |
| 256 | { |
| 257 | ImGui_ImplDX9_InvalidateDeviceObjects(); |
| 258 | HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp); |
| 259 | if (hr == D3DERR_INVALIDCALL) |
| 260 | IM_ASSERT(0); |
| 261 | ImGui_ImplDX9_CreateDeviceObjects(); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 273 | { |
| 274 | if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) |
| 275 | return true; |
| 276 | |
| 277 | switch (msg) |
| 278 | { |
| 279 | case WM_SIZE: |
| 280 | if (wParam == SIZE_MINIMIZED) |
| 281 | return 0; |
| 282 | g_ResizeWidth = (UINT)LOWORD(lParam); |
| 283 | g_ResizeHeight = (UINT)HIWORD(lParam); |
| 284 | return 0; |
| 285 | case WM_SYSCOMMAND: |
| 286 | if ((wParam & 0xfff0) == SC_KEYMENU) |
| 287 | return 0; |
| 288 | break; |
| 289 | case WM_DESTROY: |
| 290 | ::PostQuitMessage(0); |
| 291 | return 0; |
| 292 | } |
| 293 | return ::DefWindowProcW(hWnd, msg, wParam, lParam); |
| 294 | } |