| 1 | // Dear ImGui: standalone example application for GLFW + OpenGL 3, using programmable pipeline |
| 2 | // (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.) |
| 3 | |
| 4 | // Learn about Dear ImGui: |
| 5 | // - FAQ https://dearimgui.com/faq |
| 6 | // - Getting Started https://dearimgui.com/getting-started |
| 7 | // - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 8 | // - Introduction, links and more at the top of imgui.cpp |
| 9 | |
| 10 | #include "imgui.h" |
| 11 | #include "imgui_impl_glfw.h" |
| 12 | #include "imgui_impl_opengl3.h" |
| 13 | #include <stdio.h> |
| 14 | #define GL_SILENCE_DEPRECATION |
| 15 | #if defined(IMGUI_IMPL_OPENGL_ES2) |
| 16 | #include <GLES2/gl2.h> |
| 17 | #endif |
| 18 | #include <GLFW/glfw3.h> // Will drag system OpenGL headers |
| 19 | |
| 20 | // [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers. |
| 21 | // To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma. |
| 22 | // Your own project should not be affected, as you are likely to link with a newer binary of GLFW that is adequate for your version of Visual Studio. |
| 23 | #if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) |
| 24 | #pragma comment(lib, "legacy_stdio_definitions") |
| 25 | #endif |
| 26 | |
| 27 | // This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details. |
| 28 | #ifdef __EMSCRIPTEN__ |
| 29 | #include "../libs/emscripten/emscripten_mainloop_stub.h" |
| 30 | #endif |
| 31 | |
| 32 | static void glfw_error_callback(int error, const char* description) |
| 33 | { |
| 34 | fprintf(stderr, format: "GLFW Error %d: %s\n" , error, description); |
| 35 | } |
| 36 | |
| 37 | // Main code |
| 38 | int main(int, char**) |
| 39 | { |
| 40 | glfwSetErrorCallback(cbfun: glfw_error_callback); |
| 41 | if (!glfwInit()) |
| 42 | return 1; |
| 43 | |
| 44 | // Decide GL+GLSL versions |
| 45 | #if defined(IMGUI_IMPL_OPENGL_ES2) |
| 46 | // GL ES 2.0 + GLSL 100 (WebGL 1.0) |
| 47 | const char* glsl_version = "#version 100" ; |
| 48 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); |
| 49 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); |
| 50 | glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); |
| 51 | #elif defined(IMGUI_IMPL_OPENGL_ES3) |
| 52 | // GL ES 3.0 + GLSL 300 es (WebGL 2.0) |
| 53 | const char* glsl_version = "#version 300 es" ; |
| 54 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); |
| 55 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); |
| 56 | glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); |
| 57 | #elif defined(__APPLE__) |
| 58 | // GL 3.2 + GLSL 150 |
| 59 | const char* glsl_version = "#version 150" ; |
| 60 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); |
| 61 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); |
| 62 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only |
| 63 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac |
| 64 | #else |
| 65 | // GL 3.0 + GLSL 130 |
| 66 | const char* glsl_version = "#version 130" ; |
| 67 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, value: 3); |
| 68 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, value: 0); |
| 69 | //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only |
| 70 | //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only |
| 71 | #endif |
| 72 | |
| 73 | // Create window with graphics context |
| 74 | float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(monitor: glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only |
| 75 | GLFWwindow* window = glfwCreateWindow(width: (int)(1280 * main_scale), height: (int)(800 * main_scale), title: "Dear ImGui GLFW+OpenGL3 example" , monitor: nullptr, share: nullptr); |
| 76 | if (window == nullptr) |
| 77 | return 1; |
| 78 | glfwMakeContextCurrent(window); |
| 79 | glfwSwapInterval(interval: 1); // Enable vsync |
| 80 | |
| 81 | // Setup Dear ImGui context |
| 82 | IMGUI_CHECKVERSION(); |
| 83 | ImGui::CreateContext(); |
| 84 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 85 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls |
| 86 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls |
| 87 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking |
| 88 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows |
| 89 | //io.ConfigViewportsNoAutoMerge = true; |
| 90 | //io.ConfigViewportsNoTaskBarIcon = true; |
| 91 | |
| 92 | // Setup Dear ImGui style |
| 93 | ImGui::StyleColorsDark(); |
| 94 | //ImGui::StyleColorsLight(); |
| 95 | |
| 96 | // Setup scaling |
| 97 | ImGuiStyle& style = ImGui::GetStyle(); |
| 98 | style.ScaleAllSizes(scale_factor: main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again) |
| 99 | style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose) |
| 100 | #if GLFW_VERSION_MAJOR >= 3 && GLFW_VERSION_MINOR >= 3 |
| 101 | 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. |
| 102 | io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes. |
| 103 | #endif |
| 104 | |
| 105 | // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones. |
| 106 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 107 | { |
| 108 | style.WindowRounding = 0.0f; |
| 109 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
| 110 | } |
| 111 | |
| 112 | // Setup Platform/Renderer backends |
| 113 | ImGui_ImplGlfw_InitForOpenGL(window, install_callbacks: true); |
| 114 | #ifdef __EMSCRIPTEN__ |
| 115 | ImGui_ImplGlfw_InstallEmscriptenCallbacks(window, "#canvas" ); |
| 116 | #endif |
| 117 | ImGui_ImplOpenGL3_Init(glsl_version); |
| 118 | |
| 119 | // Load Fonts |
| 120 | // - 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. |
| 121 | // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. |
| 122 | // - 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). |
| 123 | // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering. |
| 124 | // - Read 'docs/FONTS.md' for more instructions and details. |
| 125 | // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! |
| 126 | // - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details. |
| 127 | //style.FontSizeBase = 20.0f; |
| 128 | //io.Fonts->AddFontDefault(); |
| 129 | //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf"); |
| 130 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf"); |
| 131 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf"); |
| 132 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf"); |
| 133 | //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf"); |
| 134 | //IM_ASSERT(font != nullptr); |
| 135 | |
| 136 | // Our state |
| 137 | bool show_demo_window = true; |
| 138 | bool show_another_window = false; |
| 139 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 140 | |
| 141 | // Main loop |
| 142 | #ifdef __EMSCRIPTEN__ |
| 143 | // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file. |
| 144 | // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage. |
| 145 | io.IniFilename = nullptr; |
| 146 | EMSCRIPTEN_MAINLOOP_BEGIN |
| 147 | #else |
| 148 | while (!glfwWindowShouldClose(window)) |
| 149 | #endif |
| 150 | { |
| 151 | // Poll and handle events (inputs, window resize, etc.) |
| 152 | // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. |
| 153 | // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
| 154 | // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
| 155 | // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. |
| 156 | glfwPollEvents(); |
| 157 | if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0) |
| 158 | { |
| 159 | ImGui_ImplGlfw_Sleep(milliseconds: 10); |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | // Start the Dear ImGui frame |
| 164 | ImGui_ImplOpenGL3_NewFrame(); |
| 165 | ImGui_ImplGlfw_NewFrame(); |
| 166 | ImGui::NewFrame(); |
| 167 | |
| 168 | // 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!). |
| 169 | if (show_demo_window) |
| 170 | ImGui::ShowDemoWindow(p_open: &show_demo_window); |
| 171 | |
| 172 | // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. |
| 173 | { |
| 174 | static float f = 0.0f; |
| 175 | static int counter = 0; |
| 176 | |
| 177 | ImGui::Begin(name: "Hello, world!" ); // Create a window called "Hello, world!" and append into it. |
| 178 | |
| 179 | ImGui::Text(fmt: "This is some useful text." ); // Display some text (you can use a format strings too) |
| 180 | ImGui::Checkbox(label: "Demo Window" , v: &show_demo_window); // Edit bools storing our window open/close state |
| 181 | ImGui::Checkbox(label: "Another Window" , v: &show_another_window); |
| 182 | |
| 183 | ImGui::SliderFloat(label: "float" , v: &f, v_min: 0.0f, v_max: 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f |
| 184 | ImGui::ColorEdit3(label: "clear color" , col: (float*)&clear_color); // Edit 3 floats representing a color |
| 185 | |
| 186 | if (ImGui::Button(label: "Button" )) // Buttons return true when clicked (most widgets return true when edited/activated) |
| 187 | counter++; |
| 188 | ImGui::SameLine(); |
| 189 | ImGui::Text(fmt: "counter = %d" , counter); |
| 190 | |
| 191 | ImGui::Text(fmt: "Application average %.3f ms/frame (%.1f FPS)" , 1000.0f / io.Framerate, io.Framerate); |
| 192 | ImGui::End(); |
| 193 | } |
| 194 | |
| 195 | // 3. Show another simple window. |
| 196 | if (show_another_window) |
| 197 | { |
| 198 | ImGui::Begin(name: "Another Window" , p_open: &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) |
| 199 | ImGui::Text(fmt: "Hello from another window!" ); |
| 200 | if (ImGui::Button(label: "Close Me" )) |
| 201 | show_another_window = false; |
| 202 | ImGui::End(); |
| 203 | } |
| 204 | |
| 205 | // Rendering |
| 206 | ImGui::Render(); |
| 207 | int display_w, display_h; |
| 208 | glfwGetFramebufferSize(window, width: &display_w, height: &display_h); |
| 209 | glViewport(x: 0, y: 0, width: display_w, height: display_h); |
| 210 | glClearColor(red: clear_color.x * clear_color.w, green: clear_color.y * clear_color.w, blue: clear_color.z * clear_color.w, alpha: clear_color.w); |
| 211 | glClear(GL_COLOR_BUFFER_BIT); |
| 212 | ImGui_ImplOpenGL3_RenderDrawData(draw_data: ImGui::GetDrawData()); |
| 213 | |
| 214 | // Update and Render additional Platform Windows |
| 215 | // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere. |
| 216 | // For this specific demo app we could also call glfwMakeContextCurrent(window) directly) |
| 217 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 218 | { |
| 219 | GLFWwindow* backup_current_context = glfwGetCurrentContext(); |
| 220 | ImGui::UpdatePlatformWindows(); |
| 221 | ImGui::RenderPlatformWindowsDefault(); |
| 222 | glfwMakeContextCurrent(window: backup_current_context); |
| 223 | } |
| 224 | |
| 225 | glfwSwapBuffers(window); |
| 226 | } |
| 227 | #ifdef __EMSCRIPTEN__ |
| 228 | EMSCRIPTEN_MAINLOOP_END; |
| 229 | #endif |
| 230 | |
| 231 | // Cleanup |
| 232 | ImGui_ImplOpenGL3_Shutdown(); |
| 233 | ImGui_ImplGlfw_Shutdown(); |
| 234 | ImGui::DestroyContext(); |
| 235 | |
| 236 | glfwDestroyWindow(window); |
| 237 | glfwTerminate(); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |