2
0

main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Dear ImGui: standalone example application for DirectX 9
  2. // Learn about Dear ImGui:
  3. // - FAQ https://dearimgui.com/faq
  4. // - Getting Started https://dearimgui.com/getting-started
  5. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  6. // - Introduction, links and more at the top of imgui.cpp
  7. #include "imgui.h"
  8. #include "imgui_impl_dx9.h"
  9. #include "imgui_impl_win32.h"
  10. #include <d3d9.h>
  11. #include <tchar.h>
  12. // Data
  13. static LPDIRECT3D9 g_pD3D = nullptr;
  14. static LPDIRECT3DDEVICE9 g_pd3dDevice = nullptr;
  15. static bool g_DeviceLost = false;
  16. static UINT g_ResizeWidth = 0, g_ResizeHeight = 0;
  17. static D3DPRESENT_PARAMETERS g_d3dpp = {};
  18. // Forward declarations of helper functions
  19. bool CreateDeviceD3D(HWND hWnd);
  20. void CleanupDeviceD3D();
  21. void ResetDevice();
  22. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  23. // Main code
  24. int main(int, char**)
  25. {
  26. // Make process DPI aware and obtain main monitor scale
  27. ImGui_ImplWin32_EnableDpiAwareness();
  28. float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY));
  29. // Create application window
  30. WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
  31. ::RegisterClassExW(&wc);
  32. 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);
  33. // Initialize Direct3D
  34. if (!CreateDeviceD3D(hwnd))
  35. {
  36. CleanupDeviceD3D();
  37. ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
  38. return 1;
  39. }
  40. // Show the window
  41. ::ShowWindow(hwnd, SW_SHOWDEFAULT);
  42. ::UpdateWindow(hwnd);
  43. // Setup Dear ImGui context
  44. IMGUI_CHECKVERSION();
  45. ImGui::CreateContext();
  46. ImGuiIO& io = ImGui::GetIO(); (void)io;
  47. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  48. io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
  49. // Setup Dear ImGui style
  50. ImGui::StyleColorsDark();
  51. //ImGui::StyleColorsLight();
  52. // Setup scaling
  53. ImGuiStyle& style = ImGui::GetStyle();
  54. style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
  55. style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
  56. // Setup Platform/Renderer backends
  57. ImGui_ImplWin32_Init(hwnd);
  58. ImGui_ImplDX9_Init(g_pd3dDevice);
  59. // Load Fonts
  60. // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
  61. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  62. // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
  63. // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
  64. // - Read 'docs/FONTS.md' for more instructions and details.
  65. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  66. //style.FontSizeBase = 20.0f;
  67. //io.Fonts->AddFontDefault();
  68. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
  69. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
  70. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
  71. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
  72. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
  73. //IM_ASSERT(font != nullptr);
  74. // Our state
  75. bool show_demo_window = true;
  76. bool show_another_window = false;
  77. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  78. // Main loop
  79. bool done = false;
  80. while (!done)
  81. {
  82. // Poll and handle messages (inputs, window resize, etc.)
  83. // See the WndProc() function below for our to dispatch events to the Win32 backend.
  84. MSG msg;
  85. while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
  86. {
  87. ::TranslateMessage(&msg);
  88. ::DispatchMessage(&msg);
  89. if (msg.message == WM_QUIT)
  90. done = true;
  91. }
  92. if (done)
  93. break;
  94. // Handle lost D3D9 device
  95. if (g_DeviceLost)
  96. {
  97. HRESULT hr = g_pd3dDevice->TestCooperativeLevel();
  98. if (hr == D3DERR_DEVICELOST)
  99. {
  100. ::Sleep(10);
  101. continue;
  102. }
  103. if (hr == D3DERR_DEVICENOTRESET)
  104. ResetDevice();
  105. g_DeviceLost = false;
  106. }
  107. // Handle window resize (we don't resize directly in the WM_SIZE handler)
  108. if (g_ResizeWidth != 0 && g_ResizeHeight != 0)
  109. {
  110. g_d3dpp.BackBufferWidth = g_ResizeWidth;
  111. g_d3dpp.BackBufferHeight = g_ResizeHeight;
  112. g_ResizeWidth = g_ResizeHeight = 0;
  113. ResetDevice();
  114. }
  115. // Start the Dear ImGui frame
  116. ImGui_ImplDX9_NewFrame();
  117. ImGui_ImplWin32_NewFrame();
  118. ImGui::NewFrame();
  119. // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
  120. if (show_demo_window)
  121. ImGui::ShowDemoWindow(&show_demo_window);
  122. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
  123. {
  124. static float f = 0.0f;
  125. static int counter = 0;
  126. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  127. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  128. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  129. ImGui::Checkbox("Another Window", &show_another_window);
  130. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  131. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  132. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  133. counter++;
  134. ImGui::SameLine();
  135. ImGui::Text("counter = %d", counter);
  136. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
  137. ImGui::End();
  138. }
  139. // 3. Show another simple window.
  140. if (show_another_window)
  141. {
  142. ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
  143. ImGui::Text("Hello from another window!");
  144. if (ImGui::Button("Close Me"))
  145. show_another_window = false;
  146. ImGui::End();
  147. }
  148. // Rendering
  149. ImGui::EndFrame();
  150. g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
  151. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
  152. g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
  153. 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));
  154. g_pd3dDevice->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
  155. if (g_pd3dDevice->BeginScene() >= 0)
  156. {
  157. ImGui::Render();
  158. ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
  159. g_pd3dDevice->EndScene();
  160. }
  161. HRESULT result = g_pd3dDevice->Present(nullptr, nullptr, nullptr, nullptr);
  162. if (result == D3DERR_DEVICELOST)
  163. g_DeviceLost = true;
  164. }
  165. // Cleanup
  166. ImGui_ImplDX9_Shutdown();
  167. ImGui_ImplWin32_Shutdown();
  168. ImGui::DestroyContext();
  169. CleanupDeviceD3D();
  170. ::DestroyWindow(hwnd);
  171. ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
  172. return 0;
  173. }
  174. // Helper functions
  175. bool CreateDeviceD3D(HWND hWnd)
  176. {
  177. if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == nullptr)
  178. return false;
  179. // Create the D3DDevice
  180. ZeroMemory(&g_d3dpp, sizeof(g_d3dpp));
  181. g_d3dpp.Windowed = TRUE;
  182. g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  183. g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Need to use an explicit format with alpha if needing per-pixel alpha composition.
  184. g_d3dpp.EnableAutoDepthStencil = TRUE;
  185. g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  186. g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync
  187. //g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate
  188. if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0)
  189. return false;
  190. return true;
  191. }
  192. void CleanupDeviceD3D()
  193. {
  194. if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; }
  195. if (g_pD3D) { g_pD3D->Release(); g_pD3D = nullptr; }
  196. }
  197. void ResetDevice()
  198. {
  199. ImGui_ImplDX9_InvalidateDeviceObjects();
  200. HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp);
  201. if (hr == D3DERR_INVALIDCALL)
  202. IM_ASSERT(0);
  203. ImGui_ImplDX9_CreateDeviceObjects();
  204. }
  205. // Forward declare message handler from imgui_impl_win32.cpp
  206. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  207. // Win32 message handler
  208. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  209. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  210. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  211. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  212. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  213. {
  214. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  215. return true;
  216. switch (msg)
  217. {
  218. case WM_SIZE:
  219. if (wParam == SIZE_MINIMIZED)
  220. return 0;
  221. g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize
  222. g_ResizeHeight = (UINT)HIWORD(lParam);
  223. return 0;
  224. case WM_SYSCOMMAND:
  225. if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  226. return 0;
  227. break;
  228. case WM_DESTROY:
  229. ::PostQuitMessage(0);
  230. return 0;
  231. }
  232. return ::DefWindowProcW(hWnd, msg, wParam, lParam);
  233. }