main.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // dear imgui: standalone example application for OpenGL3 with Winapi
  2. // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
  3. #include "imgui.h"
  4. #include "imgui_impl_opengl3.h"
  5. #include "imgui_impl_win32.h"
  6. #include <windows.h>
  7. #include <GL/GL.h>
  8. #include <tchar.h>
  9. // Data stored per platform window
  10. struct RendererData
  11. {
  12. HDC hDC;
  13. };
  14. // Data
  15. HGLRC g_hRC;
  16. RendererData g_MainWindow;
  17. static int g_Width;
  18. static int g_Height;
  19. // Forward declarations of helper functions
  20. static bool CreateDeviceOpenGL3(HWND hWnd, RendererData* data);
  21. static void CleanupDeviceOpenGL3(HWND hWnd, RendererData* data);
  22. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  23. // Main code
  24. int main(int, char**)
  25. {
  26. // Create application window
  27. //ImGui_ImplWin32_EnableDpiAwareness();
  28. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_OWNDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("ImGui Example"), NULL };
  29. ::RegisterClassEx(&wc);
  30. HWND hwnd = ::CreateWindow(wc.lpszClassName, _T("Dear ImGui Winapi+OpenGL3 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
  31. // Initialize OpenGL3
  32. if (!CreateDeviceOpenGL3(hwnd, &g_MainWindow))
  33. {
  34. CleanupDeviceOpenGL3(hwnd, &g_MainWindow);
  35. ::DestroyWindow(hwnd);
  36. ::UnregisterClass(wc.lpszClassName, wc.hInstance);
  37. return 1;
  38. }
  39. wglMakeCurrent(g_MainWindow.hDC, g_hRC);
  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::StyleColorsClassic();
  52. // Setup Platform/Renderer bindings
  53. ImGui_ImplWin32_InitForOpenGL(hwnd);
  54. ImGui_ImplOpenGL3_Init();
  55. // Load Fonts
  56. // - 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.
  57. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  58. // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
  59. // - 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.
  60. // - Read 'docs/FONTS.md' for more instructions and details.
  61. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  62. //io.Fonts->AddFontDefault();
  63. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  64. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  65. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  66. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
  67. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  68. //IM_ASSERT(font != NULL);
  69. // Our state
  70. bool show_demo_window = true;
  71. bool show_another_window = false;
  72. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  73. // Main loop
  74. MSG msg;
  75. ZeroMemory(&msg, sizeof(msg));
  76. while (msg.message != WM_QUIT)
  77. {
  78. // Poll and handle messages (inputs, window resize, etc.)
  79. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  80. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  81. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  82. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  83. if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  84. {
  85. ::TranslateMessage(&msg);
  86. ::DispatchMessage(&msg);
  87. continue;
  88. }
  89. // Start the Dear ImGui frame
  90. ImGui_ImplOpenGL3_NewFrame();
  91. ImGui_ImplWin32_NewFrame();
  92. ImGui::NewFrame();
  93. // 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!).
  94. if (show_demo_window)
  95. ImGui::ShowDemoWindow(&show_demo_window);
  96. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
  97. {
  98. static float f = 0.0f;
  99. static int counter = 0;
  100. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  101. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  102. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  103. ImGui::Checkbox("Another Window", &show_another_window);
  104. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  105. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  106. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  107. counter++;
  108. ImGui::SameLine();
  109. ImGui::Text("counter = %d", counter);
  110. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  111. ImGui::End();
  112. }
  113. // 3. Show another simple window.
  114. if (show_another_window)
  115. {
  116. 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)
  117. ImGui::Text("Hello from another window!");
  118. if (ImGui::Button("Close Me"))
  119. show_another_window = false;
  120. ImGui::End();
  121. }
  122. // Rendering
  123. ImGui::Render();
  124. glViewport(0, 0, g_Width, g_Height);
  125. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  126. glClear(GL_COLOR_BUFFER_BIT);
  127. // If you are using this code with non-legacy OpenGL header/contexts (which you should not, prefer using imgui_impl_opengl3.cpp!!),
  128. // you may need to backup/reset/restore current shader using the commented lines below.
  129. //GLint last_program;
  130. //glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
  131. //glUseProgram(0);
  132. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  133. //glUseProgram(last_program);
  134. SwapBuffers(g_MainWindow.hDC);
  135. }
  136. ImGui_ImplOpenGL3_Shutdown();
  137. ImGui_ImplWin32_Shutdown();
  138. ImGui::DestroyContext();
  139. CleanupDeviceOpenGL3(hwnd, &g_MainWindow);
  140. wglDeleteContext(g_hRC);
  141. ::DestroyWindow(hwnd);
  142. ::UnregisterClass(wc.lpszClassName, wc.hInstance);
  143. return 0;
  144. }
  145. static bool ActivateOpenGL3(HWND hWnd)
  146. {
  147. HDC hDc = GetDC(hWnd);
  148. PIXELFORMATDESCRIPTOR pfd = { 0 };
  149. pfd.nSize = sizeof(pfd);
  150. pfd.nVersion = 1;
  151. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  152. pfd.iPixelType = PFD_TYPE_RGBA;
  153. pfd.cColorBits = 32;
  154. int pf = ChoosePixelFormat(hDc, &pfd);
  155. if (pf == 0)
  156. {
  157. return false;
  158. }
  159. if (SetPixelFormat(hDc, pf, &pfd) == FALSE)
  160. {
  161. return false;
  162. }
  163. ReleaseDC(hWnd, hDc);
  164. return true;
  165. }
  166. // Helper functions
  167. static bool CreateDeviceOpenGL3(HWND hWnd, RendererData* data)
  168. {
  169. if (!ActivateOpenGL3(hWnd))
  170. return false;
  171. data->hDC = GetDC(hWnd);
  172. if (!g_hRC)
  173. {
  174. g_hRC = wglCreateContext(data->hDC);
  175. }
  176. return true;
  177. }
  178. static void CleanupDeviceOpenGL3(HWND hWnd, RendererData* data)
  179. {
  180. wglMakeCurrent(NULL, NULL);
  181. ReleaseDC(hWnd, data->hDC);
  182. }
  183. // Forward declare message handler from imgui_impl_win32.cpp
  184. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  185. // Win32 message handler
  186. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  187. {
  188. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  189. return true;
  190. switch (msg)
  191. {
  192. case WM_SIZE:
  193. if (wParam != SIZE_MINIMIZED)
  194. {
  195. g_Width = LOWORD(lParam);
  196. g_Height = HIWORD(lParam);
  197. }
  198. return 0;
  199. case WM_SYSCOMMAND:
  200. if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  201. return 0;
  202. break;
  203. case WM_DESTROY:
  204. ::PostQuitMessage(0);
  205. return 0;
  206. }
  207. return ::DefWindowProc(hWnd, msg, wParam, lParam);
  208. }