main.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Dear ImGui: standalone example application for SDL2 + OpenGL
  2. // (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
  3. // Learn about Dear ImGui:
  4. // - FAQ https://dearimgui.com/faq
  5. // - Getting Started https://dearimgui.com/getting-started
  6. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  7. // - Introduction, links and more at the top of imgui.cpp
  8. // **DO NOT USE THIS CODE IF YOUR CODE/ENGINE IS USING MODERN OPENGL (SHADERS, VBO, VAO, etc.)**
  9. // **Prefer using the code in the example_sdl2_opengl3/ folder**
  10. // See imgui_impl_sdl2.cpp for details.
  11. #include "imgui.h"
  12. #include "imgui_impl_sdl2.h"
  13. #include "imgui_impl_opengl2.h"
  14. #include <stdio.h>
  15. #include <SDL.h>
  16. #include <SDL_opengl.h>
  17. #ifdef _WIN32
  18. #include <windows.h> // SetProcessDPIAware()
  19. #endif
  20. // Main code
  21. int main(int, char**)
  22. {
  23. // Setup SDL
  24. #ifdef _WIN32
  25. ::SetProcessDPIAware();
  26. #endif
  27. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
  28. {
  29. printf("Error: %s\n", SDL_GetError());
  30. return -1;
  31. }
  32. // From 2.0.18: Enable native IME.
  33. #ifdef SDL_HINT_IME_SHOW_UI
  34. SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
  35. #endif
  36. // Setup window
  37. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  38. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  39. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  40. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  41. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  42. float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
  43. SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  44. SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(720 * main_scale), window_flags);
  45. if (window == nullptr)
  46. {
  47. printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
  48. return -1;
  49. }
  50. SDL_GLContext gl_context = SDL_GL_CreateContext(window);
  51. SDL_GL_MakeCurrent(window, gl_context);
  52. SDL_GL_SetSwapInterval(1); // Enable vsync
  53. // Setup Dear ImGui context
  54. IMGUI_CHECKVERSION();
  55. ImGui::CreateContext();
  56. ImGuiIO& io = ImGui::GetIO(); (void)io;
  57. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  58. io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
  59. io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
  60. io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
  61. //io.ConfigViewportsNoAutoMerge = true;
  62. //io.ConfigViewportsNoTaskBarIcon = true;
  63. // Setup Dear ImGui style
  64. ImGui::StyleColorsDark();
  65. //ImGui::StyleColorsLight();
  66. // Setup scaling
  67. ImGuiStyle& style = ImGui::GetStyle();
  68. 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)
  69. style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
  70. io.ConfigDpiScaleFonts = true; // [Experimental] Automatically overwrite style.FontScaleDpi in Begin() when Monitor DPI changes. This will scale fonts but _NOT_ scale sizes/padding for now.
  71. io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
  72. // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
  73. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  74. {
  75. style.WindowRounding = 0.0f;
  76. style.Colors[ImGuiCol_WindowBg].w = 1.0f;
  77. }
  78. // Setup Platform/Renderer backends
  79. ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
  80. ImGui_ImplOpenGL2_Init();
  81. // Load Fonts
  82. // - 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.
  83. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  84. // - 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).
  85. // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
  86. // - Read 'docs/FONTS.md' for more instructions and details.
  87. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  88. //style.FontSizeBase = 20.0f;
  89. //io.Fonts->AddFontDefault();
  90. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
  91. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
  92. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
  93. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
  94. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
  95. //IM_ASSERT(font != nullptr);
  96. // Our state
  97. bool show_demo_window = true;
  98. bool show_another_window = false;
  99. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  100. // Main loop
  101. bool done = false;
  102. while (!done)
  103. {
  104. // Poll and handle events (inputs, window resize, etc.)
  105. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  106. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  107. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  108. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  109. SDL_Event event;
  110. while (SDL_PollEvent(&event))
  111. {
  112. ImGui_ImplSDL2_ProcessEvent(&event);
  113. if (event.type == SDL_QUIT)
  114. done = true;
  115. if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
  116. done = true;
  117. }
  118. if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
  119. {
  120. SDL_Delay(10);
  121. continue;
  122. }
  123. // Start the Dear ImGui frame
  124. ImGui_ImplOpenGL2_NewFrame();
  125. ImGui_ImplSDL2_NewFrame();
  126. ImGui::NewFrame();
  127. // 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!).
  128. if (show_demo_window)
  129. ImGui::ShowDemoWindow(&show_demo_window);
  130. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
  131. {
  132. static float f = 0.0f;
  133. static int counter = 0;
  134. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  135. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  136. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  137. ImGui::Checkbox("Another Window", &show_another_window);
  138. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  139. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  140. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  141. counter++;
  142. ImGui::SameLine();
  143. ImGui::Text("counter = %d", counter);
  144. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
  145. ImGui::End();
  146. }
  147. // 3. Show another simple window.
  148. if (show_another_window)
  149. {
  150. 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)
  151. ImGui::Text("Hello from another window!");
  152. if (ImGui::Button("Close Me"))
  153. show_another_window = false;
  154. ImGui::End();
  155. }
  156. // Rendering
  157. ImGui::Render();
  158. glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
  159. glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
  160. glClear(GL_COLOR_BUFFER_BIT);
  161. //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context where shaders may be bound
  162. ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
  163. // Update and Render additional Platform Windows
  164. // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
  165. // For this specific demo app we could also call SDL_GL_MakeCurrent(window, gl_context) directly)
  166. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  167. {
  168. SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
  169. SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
  170. ImGui::UpdatePlatformWindows();
  171. ImGui::RenderPlatformWindowsDefault();
  172. SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
  173. }
  174. SDL_GL_SwapWindow(window);
  175. }
  176. // Cleanup
  177. ImGui_ImplOpenGL2_Shutdown();
  178. ImGui_ImplSDL2_Shutdown();
  179. ImGui::DestroyContext();
  180. SDL_GL_DeleteContext(gl_context);
  181. SDL_DestroyWindow(window);
  182. SDL_Quit();
  183. return 0;
  184. }