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 | GLFWwindow* window = glfwCreateWindow(width: 1280, height: 720, title: "Dear ImGui GLFW+OpenGL3 example" , monitor: nullptr, share: nullptr); |
75 | if (window == nullptr) |
76 | return 1; |
77 | glfwMakeContextCurrent(window); |
78 | glfwSwapInterval(interval: 1); // Enable vsync |
79 | |
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 | //io.ConfigViewportsNoAutoMerge = true; |
89 | //io.ConfigViewportsNoTaskBarIcon = true; |
90 | |
91 | // Setup Dear ImGui style |
92 | ImGui::StyleColorsDark(); |
93 | //ImGui::StyleColorsLight(); |
94 | |
95 | // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones. |
96 | ImGuiStyle& style = ImGui::GetStyle(); |
97 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
98 | { |
99 | style.WindowRounding = 0.0f; |
100 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
101 | } |
102 | |
103 | // Setup Platform/Renderer backends |
104 | ImGui_ImplGlfw_InitForOpenGL(window, install_callbacks: true); |
105 | #ifdef __EMSCRIPTEN__ |
106 | ImGui_ImplGlfw_InstallEmscriptenCallbacks(window, "#canvas" ); |
107 | #endif |
108 | ImGui_ImplOpenGL3_Init(glsl_version); |
109 | |
110 | // Load Fonts |
111 | // - 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. |
112 | // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. |
113 | // - 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). |
114 | // - 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. |
115 | // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering. |
116 | // - Read 'docs/FONTS.md' for more instructions and details. |
117 | // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! |
118 | // - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details. |
119 | //io.Fonts->AddFontDefault(); |
120 | //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f); |
121 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); |
122 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); |
123 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); |
124 | //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese()); |
125 | //IM_ASSERT(font != nullptr); |
126 | |
127 | // Our state |
128 | bool show_demo_window = true; |
129 | bool show_another_window = false; |
130 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
131 | |
132 | // Main loop |
133 | #ifdef __EMSCRIPTEN__ |
134 | // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file. |
135 | // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage. |
136 | io.IniFilename = nullptr; |
137 | EMSCRIPTEN_MAINLOOP_BEGIN |
138 | #else |
139 | while (!glfwWindowShouldClose(window)) |
140 | #endif |
141 | { |
142 | // Poll and handle events (inputs, window resize, etc.) |
143 | // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. |
144 | // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
145 | // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
146 | // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. |
147 | glfwPollEvents(); |
148 | if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0) |
149 | { |
150 | ImGui_ImplGlfw_Sleep(milliseconds: 10); |
151 | continue; |
152 | } |
153 | |
154 | // Start the Dear ImGui frame |
155 | ImGui_ImplOpenGL3_NewFrame(); |
156 | ImGui_ImplGlfw_NewFrame(); |
157 | ImGui::NewFrame(); |
158 | |
159 | // 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!). |
160 | if (show_demo_window) |
161 | ImGui::ShowDemoWindow(p_open: &show_demo_window); |
162 | |
163 | // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. |
164 | { |
165 | static float f = 0.0f; |
166 | static int counter = 0; |
167 | |
168 | ImGui::Begin(name: "Hello, world!" ); // Create a window called "Hello, world!" and append into it. |
169 | |
170 | ImGui::Text(fmt: "This is some useful text." ); // Display some text (you can use a format strings too) |
171 | ImGui::Checkbox(label: "Demo Window" , v: &show_demo_window); // Edit bools storing our window open/close state |
172 | ImGui::Checkbox(label: "Another Window" , v: &show_another_window); |
173 | |
174 | 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 |
175 | ImGui::ColorEdit3(label: "clear color" , col: (float*)&clear_color); // Edit 3 floats representing a color |
176 | |
177 | if (ImGui::Button(label: "Button" )) // Buttons return true when clicked (most widgets return true when edited/activated) |
178 | counter++; |
179 | ImGui::SameLine(); |
180 | ImGui::Text(fmt: "counter = %d" , counter); |
181 | |
182 | ImGui::Text(fmt: "Application average %.3f ms/frame (%.1f FPS)" , 1000.0f / io.Framerate, io.Framerate); |
183 | ImGui::End(); |
184 | } |
185 | |
186 | // 3. Show another simple window. |
187 | if (show_another_window) |
188 | { |
189 | 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) |
190 | ImGui::Text(fmt: "Hello from another window!" ); |
191 | if (ImGui::Button(label: "Close Me" )) |
192 | show_another_window = false; |
193 | ImGui::End(); |
194 | } |
195 | |
196 | // Rendering |
197 | ImGui::Render(); |
198 | int display_w, display_h; |
199 | glfwGetFramebufferSize(window, width: &display_w, height: &display_h); |
200 | glViewport(x: 0, y: 0, width: display_w, height: display_h); |
201 | 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); |
202 | glClear(GL_COLOR_BUFFER_BIT); |
203 | ImGui_ImplOpenGL3_RenderDrawData(draw_data: ImGui::GetDrawData()); |
204 | |
205 | // Update and Render additional Platform Windows |
206 | // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere. |
207 | // For this specific demo app we could also call glfwMakeContextCurrent(window) directly) |
208 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
209 | { |
210 | GLFWwindow* backup_current_context = glfwGetCurrentContext(); |
211 | ImGui::UpdatePlatformWindows(); |
212 | ImGui::RenderPlatformWindowsDefault(); |
213 | glfwMakeContextCurrent(window: backup_current_context); |
214 | } |
215 | |
216 | glfwSwapBuffers(window); |
217 | } |
218 | #ifdef __EMSCRIPTEN__ |
219 | EMSCRIPTEN_MAINLOOP_END; |
220 | #endif |
221 | |
222 | // Cleanup |
223 | ImGui_ImplOpenGL3_Shutdown(); |
224 | ImGui_ImplGlfw_Shutdown(); |
225 | ImGui::DestroyContext(); |
226 | |
227 | glfwDestroyWindow(window); |
228 | glfwTerminate(); |
229 | |
230 | return 0; |
231 | } |
232 | |