main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Dear ImGui: standalone example application for Win32 + OpenGL 3
  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. // Support function for multi-viewports
  30. // Unlike most other backend combination, we need specific hooks to combine Win32+OpenGL.
  31. // We could in theory decide to support Win32-specific code in OpenGL backend via e.g. an hypothetical ImGui_ImplOpenGL3_InitForRawWin32().
  32. static void Hook_Renderer_CreateWindow(ImGuiViewport* viewport)
  33. {
  34. assert(viewport->RendererUserData == NULL);
  35. WGL_WindowData* data = IM_NEW(WGL_WindowData);
  36. CreateDeviceWGL((HWND)viewport->PlatformHandle, data);
  37. viewport->RendererUserData = data;
  38. }
  39. static void Hook_Renderer_DestroyWindow(ImGuiViewport* viewport)
  40. {
  41. if (viewport->RendererUserData != NULL)
  42. {
  43. WGL_WindowData* data = (WGL_WindowData*)viewport->RendererUserData;
  44. CleanupDeviceWGL((HWND)viewport->PlatformHandle, data);
  45. IM_DELETE(data);
  46. viewport->RendererUserData = NULL;
  47. }
  48. }
  49. static void Hook_Platform_RenderWindow(ImGuiViewport* viewport, void*)
  50. {
  51. // Activate the platform window DC in the OpenGL rendering context
  52. if (WGL_WindowData* data = (WGL_WindowData*)viewport->RendererUserData)
  53. wglMakeCurrent(data->hDC, g_hRC);
  54. }
  55. static void Hook_Renderer_SwapBuffers(ImGuiViewport* viewport, void*)
  56. {
  57. if (WGL_WindowData* data = (WGL_WindowData*)viewport->RendererUserData)
  58. ::SwapBuffers(data->hDC);
  59. }
  60. // Main code
  61. int main(int, char**)
  62. {
  63. // Create application window
  64. //ImGui_ImplWin32_EnableDpiAwareness();
  65. WNDCLASSEXW wc = { sizeof(wc), CS_OWNDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
  66. ::RegisterClassExW(&wc);
  67. HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+OpenGL3 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
  68. // Initialize OpenGL
  69. if (!CreateDeviceWGL(hwnd, &g_MainWindow))
  70. {
  71. CleanupDeviceWGL(hwnd, &g_MainWindow);
  72. ::DestroyWindow(hwnd);
  73. ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
  74. return 1;
  75. }
  76. wglMakeCurrent(g_MainWindow.hDC, g_hRC);
  77. // Show the window
  78. ::ShowWindow(hwnd, SW_SHOWDEFAULT);
  79. ::UpdateWindow(hwnd);
  80. // Setup Dear ImGui context
  81. IMGUI_CHECKVERSION();
  82. ImGui::CreateContext();
  83. ImGuiIO& io = ImGui::GetIO(); (void)io;
  84. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  85. io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
  86. io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
  87. io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
  88. // Setup Dear ImGui style
  89. ImGui::StyleColorsDark();
  90. //ImGui::StyleColorsClassic();
  91. // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
  92. ImGuiStyle& style = ImGui::GetStyle();
  93. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  94. {
  95. style.WindowRounding = 0.0f;
  96. style.Colors[ImGuiCol_WindowBg].w = 1.0f;
  97. }
  98. // Setup Platform/Renderer backends
  99. ImGui_ImplWin32_InitForOpenGL(hwnd);
  100. ImGui_ImplOpenGL3_Init();
  101. // Win32+GL needs specific hooks for viewport, as there are specific things needed to tie Win32 and GL api.
  102. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  103. {
  104. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  105. IM_ASSERT(platform_io.Renderer_CreateWindow == NULL);
  106. IM_ASSERT(platform_io.Renderer_DestroyWindow == NULL);
  107. IM_ASSERT(platform_io.Renderer_SwapBuffers == NULL);
  108. IM_ASSERT(platform_io.Platform_RenderWindow == NULL);
  109. platform_io.Renderer_CreateWindow = Hook_Renderer_CreateWindow;
  110. platform_io.Renderer_DestroyWindow = Hook_Renderer_DestroyWindow;
  111. platform_io.Renderer_SwapBuffers = Hook_Renderer_SwapBuffers;
  112. platform_io.Platform_RenderWindow = Hook_Platform_RenderWindow;
  113. }
  114. // Load Fonts
  115. // - 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.
  116. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  117. // - 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).
  118. // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
  119. // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
  120. // - Read 'docs/FONTS.md' for more instructions and details.
  121. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  122. //io.Fonts->AddFontDefault();
  123. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
  124. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  125. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  126. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  127. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
  128. //IM_ASSERT(font != nullptr);
  129. // Our state
  130. bool show_demo_window = true;
  131. bool show_another_window = false;
  132. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  133. // Main loop
  134. bool done = false;
  135. while (!done)
  136. {
  137. // Poll and handle messages (inputs, window resize, etc.)
  138. // See the WndProc() function below for our to dispatch events to the Win32 backend.
  139. MSG msg;
  140. while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
  141. {
  142. ::TranslateMessage(&msg);
  143. ::DispatchMessage(&msg);
  144. if (msg.message == WM_QUIT)
  145. done = true;
  146. }
  147. if (done)
  148. break;
  149. if (::IsIconic(hwnd))
  150. {
  151. ::Sleep(10);
  152. continue;
  153. }
  154. // Start the Dear ImGui frame
  155. ImGui_ImplOpenGL3_NewFrame();
  156. ImGui_ImplWin32_NewFrame();
  157. ImGui::NewFrame();
  158. // 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!).
  159. if (show_demo_window)
  160. ImGui::ShowDemoWindow(&show_demo_window);
  161. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
  162. {
  163. static float f = 0.0f;
  164. static int counter = 0;
  165. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  166. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  167. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  168. ImGui::Checkbox("Another Window", &show_another_window);
  169. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  170. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  171. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  172. counter++;
  173. ImGui::SameLine();
  174. ImGui::Text("counter = %d", counter);
  175. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
  176. ImGui::End();
  177. }
  178. // 3. Show another simple window.
  179. if (show_another_window)
  180. {
  181. 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)
  182. ImGui::Text("Hello from another window!");
  183. if (ImGui::Button("Close Me"))
  184. show_another_window = false;
  185. ImGui::End();
  186. }
  187. // Rendering
  188. ImGui::Render();
  189. glViewport(0, 0, g_Width, g_Height);
  190. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  191. glClear(GL_COLOR_BUFFER_BIT);
  192. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  193. // Update and Render additional Platform Windows
  194. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  195. {
  196. ImGui::UpdatePlatformWindows();
  197. ImGui::RenderPlatformWindowsDefault();
  198. // Restore the OpenGL rendering context to the main window DC, since platform windows might have changed it.
  199. wglMakeCurrent(g_MainWindow.hDC, g_hRC);
  200. }
  201. // Present
  202. ::SwapBuffers(g_MainWindow.hDC);
  203. }
  204. ImGui_ImplOpenGL3_Shutdown();
  205. ImGui_ImplWin32_Shutdown();
  206. ImGui::DestroyContext();
  207. CleanupDeviceWGL(hwnd, &g_MainWindow);
  208. wglDeleteContext(g_hRC);
  209. ::DestroyWindow(hwnd);
  210. ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
  211. return 0;
  212. }
  213. // Helper functions
  214. bool CreateDeviceWGL(HWND hWnd, WGL_WindowData* data)
  215. {
  216. HDC hDc = ::GetDC(hWnd);
  217. PIXELFORMATDESCRIPTOR pfd = { 0 };
  218. pfd.nSize = sizeof(pfd);
  219. pfd.nVersion = 1;
  220. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  221. pfd.iPixelType = PFD_TYPE_RGBA;
  222. pfd.cColorBits = 32;
  223. const int pf = ::ChoosePixelFormat(hDc, &pfd);
  224. if (pf == 0)
  225. return false;
  226. if (::SetPixelFormat(hDc, pf, &pfd) == FALSE)
  227. return false;
  228. ::ReleaseDC(hWnd, hDc);
  229. data->hDC = ::GetDC(hWnd);
  230. if (!g_hRC)
  231. g_hRC = wglCreateContext(data->hDC);
  232. return true;
  233. }
  234. void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data)
  235. {
  236. wglMakeCurrent(nullptr, nullptr);
  237. ::ReleaseDC(hWnd, data->hDC);
  238. }
  239. // Forward declare message handler from imgui_impl_win32.cpp
  240. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  241. // Win32 message handler
  242. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  243. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  244. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  245. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  246. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  247. {
  248. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  249. return true;
  250. switch (msg)
  251. {
  252. case WM_SIZE:
  253. if (wParam != SIZE_MINIMIZED)
  254. {
  255. g_Width = LOWORD(lParam);
  256. g_Height = HIWORD(lParam);
  257. }
  258. return 0;
  259. case WM_SYSCOMMAND:
  260. if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  261. return 0;
  262. break;
  263. case WM_DESTROY:
  264. ::PostQuitMessage(0);
  265. return 0;
  266. }
  267. return ::DefWindowProcW(hWnd, msg, wParam, lParam);
  268. }