main.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Dear ImGui: standalone example application for Windows API + OpenGL
  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. // This is provided for completeness, however it is strongly recommended you use OpenGL with SDL or GLFW.
  8. #include "imgui.h"
  9. #include "imgui_impl_opengl3.h"
  10. #include "imgui_impl_win32.h"
  11. #ifndef WIN32_LEAN_AND_MEAN
  12. #define WIN32_LEAN_AND_MEAN
  13. #endif
  14. #include <windows.h>
  15. #include <GL/gl.h>
  16. #include <tchar.h>
  17. // Data stored per platform window
  18. struct WGL_WindowData { HDC hDC; };
  19. // Data
  20. static HGLRC g_hRC;
  21. static WGL_WindowData g_MainWindow;
  22. static int g_Width;
  23. static int g_Height;
  24. // Forward declarations of helper functions
  25. bool CreateDeviceWGL(HWND hWnd, WGL_WindowData* data);
  26. void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data);
  27. void ResetDeviceWGL();
  28. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  29. // Main code
  30. int main(int, char**)
  31. {
  32. // Make process DPI aware and obtain main monitor scale
  33. ImGui_ImplWin32_EnableDpiAwareness();
  34. float main_scale = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY));
  35. // Create application window
  36. WNDCLASSEXW wc = { sizeof(wc), CS_OWNDC, 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 Win32+OpenGL3 Example", WS_OVERLAPPEDWINDOW, 100, 100, (int)(1280 * main_scale), (int)(800 * main_scale), nullptr, nullptr, wc.hInstance, nullptr);
  39. // Initialize OpenGL
  40. if (!CreateDeviceWGL(hwnd, &g_MainWindow))
  41. {
  42. CleanupDeviceWGL(hwnd, &g_MainWindow);
  43. ::DestroyWindow(hwnd);
  44. ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
  45. return 1;
  46. }
  47. wglMakeCurrent(g_MainWindow.hDC, g_hRC);
  48. // Show the window
  49. ::ShowWindow(hwnd, SW_SHOWDEFAULT);
  50. ::UpdateWindow(hwnd);
  51. // Setup Dear ImGui context
  52. IMGUI_CHECKVERSION();
  53. ImGui::CreateContext();
  54. ImGuiIO& io = ImGui::GetIO(); (void)io;
  55. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  56. io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
  57. // Setup Dear ImGui style
  58. ImGui::StyleColorsDark();
  59. //ImGui::StyleColorsClassic();
  60. // Setup scaling
  61. ImGuiStyle& style = ImGui::GetStyle();
  62. 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)
  63. style.FontScaleDpi = main_scale; // Set initial font scale. (in docking branch: using io.ConfigDpiScaleFonts=true automatically overrides this for every window depending on the current monitor)
  64. // Setup Platform/Renderer backends
  65. ImGui_ImplWin32_InitForOpenGL(hwnd);
  66. ImGui_ImplOpenGL3_Init();
  67. // Load Fonts
  68. // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
  69. // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
  70. // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
  71. // - If a file cannot be loaded, AddFont functions will return a nullptr. Please handle those errors in your code (e.g. use an assertion, display an error and quit).
  72. // - Read 'docs/FONTS.md' for more instructions and details.
  73. // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
  74. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  75. //style.FontSizeBase = 20.0f;
  76. //io.Fonts->AddFontDefaultVector();
  77. //io.Fonts->AddFontDefaultBitmap();
  78. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
  79. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
  80. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
  81. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
  82. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
  83. //IM_ASSERT(font != nullptr);
  84. // Our state
  85. bool show_demo_window = true;
  86. bool show_another_window = false;
  87. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  88. // Main loop
  89. bool done = false;
  90. while (!done)
  91. {
  92. // Poll and handle messages (inputs, window resize, etc.)
  93. // See the WndProc() function below for our to dispatch events to the Win32 backend.
  94. MSG msg;
  95. while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
  96. {
  97. ::TranslateMessage(&msg);
  98. ::DispatchMessage(&msg);
  99. if (msg.message == WM_QUIT)
  100. done = true;
  101. }
  102. if (done)
  103. break;
  104. if (::IsIconic(hwnd))
  105. {
  106. ::Sleep(10);
  107. continue;
  108. }
  109. // Start the Dear ImGui frame
  110. ImGui_ImplOpenGL3_NewFrame();
  111. ImGui_ImplWin32_NewFrame();
  112. ImGui::NewFrame();
  113. // 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!).
  114. if (show_demo_window)
  115. ImGui::ShowDemoWindow(&show_demo_window);
  116. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
  117. {
  118. static float f = 0.0f;
  119. static int counter = 0;
  120. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  121. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  122. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  123. ImGui::Checkbox("Another Window", &show_another_window);
  124. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  125. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  126. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  127. counter++;
  128. ImGui::SameLine();
  129. ImGui::Text("counter = %d", counter);
  130. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
  131. ImGui::End();
  132. }
  133. // 3. Show another simple window.
  134. if (show_another_window)
  135. {
  136. 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)
  137. ImGui::Text("Hello from another window!");
  138. if (ImGui::Button("Close Me"))
  139. show_another_window = false;
  140. ImGui::End();
  141. }
  142. // Rendering
  143. ImGui::Render();
  144. glViewport(0, 0, g_Width, g_Height);
  145. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  146. glClear(GL_COLOR_BUFFER_BIT);
  147. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  148. // Present
  149. ::SwapBuffers(g_MainWindow.hDC);
  150. }
  151. ImGui_ImplOpenGL3_Shutdown();
  152. ImGui_ImplWin32_Shutdown();
  153. ImGui::DestroyContext();
  154. CleanupDeviceWGL(hwnd, &g_MainWindow);
  155. wglDeleteContext(g_hRC);
  156. ::DestroyWindow(hwnd);
  157. ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
  158. return 0;
  159. }
  160. // Helper functions
  161. bool CreateDeviceWGL(HWND hWnd, WGL_WindowData* data)
  162. {
  163. HDC hDc = ::GetDC(hWnd);
  164. PIXELFORMATDESCRIPTOR pfd = { 0 };
  165. pfd.nSize = sizeof(pfd);
  166. pfd.nVersion = 1;
  167. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  168. pfd.iPixelType = PFD_TYPE_RGBA;
  169. pfd.cColorBits = 32;
  170. const int pf = ::ChoosePixelFormat(hDc, &pfd);
  171. if (pf == 0)
  172. return false;
  173. if (::SetPixelFormat(hDc, pf, &pfd) == FALSE)
  174. return false;
  175. ::ReleaseDC(hWnd, hDc);
  176. data->hDC = ::GetDC(hWnd);
  177. if (!g_hRC)
  178. g_hRC = wglCreateContext(data->hDC);
  179. return true;
  180. }
  181. void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data)
  182. {
  183. wglMakeCurrent(nullptr, nullptr);
  184. ::ReleaseDC(hWnd, data->hDC);
  185. }
  186. // Forward declare message handler from imgui_impl_win32.cpp
  187. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  188. // Win32 message handler
  189. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  190. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  191. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  192. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  193. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  194. {
  195. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  196. return true;
  197. switch (msg)
  198. {
  199. case WM_SIZE:
  200. if (wParam != SIZE_MINIMIZED)
  201. {
  202. g_Width = LOWORD(lParam);
  203. g_Height = HIWORD(lParam);
  204. }
  205. return 0;
  206. case WM_SYSCOMMAND:
  207. if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  208. return 0;
  209. break;
  210. case WM_DESTROY:
  211. ::PostQuitMessage(0);
  212. return 0;
  213. }
  214. return ::DefWindowProcW(hWnd, msg, wParam, lParam);
  215. }