main.cpp 9.4 KB

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