1// Dear ImGui: standalone example application for GLFW + OpenGL2, using legacy fixed 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// **DO NOT USE THIS CODE IF YOUR CODE/ENGINE IS USING MODERN OPENGL (SHADERS, VBO, VAO, etc.)**
11// **Prefer using the code in the example_glfw_opengl2/ folder**
12// See imgui_impl_glfw.cpp for details.
13
14#include "imgui.h"
15#include "imgui_impl_glfw.h"
16#include "imgui_impl_opengl2.h"
17#include <stdio.h>
18#ifdef __APPLE__
19#define GL_SILENCE_DEPRECATION
20#endif
21#include <GLFW/glfw3.h>
22
23// [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers.
24// To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma.
25// 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.
26#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
27#pragma comment(lib, "legacy_stdio_definitions")
28#endif
29
30static void glfw_error_callback(int error, const char* description)
31{
32 fprintf(stderr, format: "GLFW Error %d: %s\n", error, description);
33}
34
35// Main code
36int main(int, char**)
37{
38 glfwSetErrorCallback(cbfun: glfw_error_callback);
39 if (!glfwInit())
40 return 1;
41
42 // Create window with graphics context
43 GLFWwindow* window = glfwCreateWindow(width: 1280, height: 720, title: "Dear ImGui GLFW+OpenGL2 example", monitor: nullptr, share: nullptr);
44 if (window == nullptr)
45 return 1;
46 glfwMakeContextCurrent(window);
47 glfwSwapInterval(interval: 1); // Enable vsync
48
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 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
56 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
57 //io.ConfigViewportsNoAutoMerge = true;
58 //io.ConfigViewportsNoTaskBarIcon = true;
59
60 // Setup Dear ImGui style
61 ImGui::StyleColorsDark();
62 //ImGui::StyleColorsLight();
63
64 // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
65 ImGuiStyle& style = ImGui::GetStyle();
66 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
67 {
68 style.WindowRounding = 0.0f;
69 style.Colors[ImGuiCol_WindowBg].w = 1.0f;
70 }
71
72 // Setup Platform/Renderer backends
73 ImGui_ImplGlfw_InitForOpenGL(window, install_callbacks: true);
74 ImGui_ImplOpenGL2_Init();
75
76 // Load Fonts
77 // - 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.
78 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
79 // - 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).
80 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
81 // - Read 'docs/FONTS.md' for more instructions and details.
82 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
83 //style.FontSizeBase = 20.0f;
84 //io.Fonts->AddFontDefault();
85 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
86 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
87 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
88 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
89 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
90 //IM_ASSERT(font != nullptr);
91
92 // Our state
93 bool show_demo_window = true;
94 bool show_another_window = false;
95 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
96
97 // Main loop
98 while (!glfwWindowShouldClose(window))
99 {
100 // Poll and handle events (inputs, window resize, etc.)
101 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
102 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
103 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
104 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
105 glfwPollEvents();
106 if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0)
107 {
108 ImGui_ImplGlfw_Sleep(milliseconds: 10);
109 continue;
110 }
111
112 // Start the Dear ImGui frame
113 ImGui_ImplOpenGL2_NewFrame();
114 ImGui_ImplGlfw_NewFrame();
115 ImGui::NewFrame();
116
117 // 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!).
118 if (show_demo_window)
119 ImGui::ShowDemoWindow(p_open: &show_demo_window);
120
121 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
122 {
123 static float f = 0.0f;
124 static int counter = 0;
125
126 ImGui::Begin(name: "Hello, world!"); // Create a window called "Hello, world!" and append into it.
127
128 ImGui::Text(fmt: "This is some useful text."); // Display some text (you can use a format strings too)
129 ImGui::Checkbox(label: "Demo Window", v: &show_demo_window); // Edit bools storing our window open/close state
130 ImGui::Checkbox(label: "Another Window", v: &show_another_window);
131
132 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
133 ImGui::ColorEdit3(label: "clear color", col: (float*)&clear_color); // Edit 3 floats representing a color
134
135 if (ImGui::Button(label: "Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
136 counter++;
137 ImGui::SameLine();
138 ImGui::Text(fmt: "counter = %d", counter);
139
140 ImGui::Text(fmt: "Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
141 ImGui::End();
142 }
143
144 // 3. Show another simple window.
145 if (show_another_window)
146 {
147 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)
148 ImGui::Text(fmt: "Hello from another window!");
149 if (ImGui::Button(label: "Close Me"))
150 show_another_window = false;
151 ImGui::End();
152 }
153
154 // Rendering
155 ImGui::Render();
156 int display_w, display_h;
157 glfwGetFramebufferSize(window, width: &display_w, height: &display_h);
158 glViewport(x: 0, y: 0, width: display_w, height: display_h);
159 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);
160 glClear(GL_COLOR_BUFFER_BIT);
161
162 // If you are using this code with non-legacy OpenGL header/contexts (which you should not, prefer using imgui_impl_opengl3.cpp!!),
163 // you may need to backup/reset/restore other state, e.g. for current shader using the commented lines below.
164 //GLint last_program;
165 //glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
166 //glUseProgram(0);
167 ImGui_ImplOpenGL2_RenderDrawData(draw_data: ImGui::GetDrawData());
168 //glUseProgram(last_program);
169
170 // Update and Render additional Platform Windows
171 // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
172 // For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
173 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
174 {
175 GLFWwindow* backup_current_context = glfwGetCurrentContext();
176 ImGui::UpdatePlatformWindows();
177 ImGui::RenderPlatformWindowsDefault();
178 glfwMakeContextCurrent(window: backup_current_context);
179 }
180
181 glfwMakeContextCurrent(window);
182 glfwSwapBuffers(window);
183 }
184
185 // Cleanup
186 ImGui_ImplOpenGL2_Shutdown();
187 ImGui_ImplGlfw_Shutdown();
188 ImGui::DestroyContext();
189
190 glfwDestroyWindow(window);
191 glfwTerminate();
192
193 return 0;
194}
195

source code of imgui/examples/example_glfw_opengl2/main.cpp