10 KB、203行 生 Blame 履歴
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, "GLFW Error %d: %s\n", error, description);
33}
34
35// Main code
36int main(int, char**)
37{
38 glfwSetErrorCallback(glfw_error_callback);
39 if (!glfwInit())
40 return 1;
41
42 // Create window with graphics context
43 float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only
44 GLFWwindow* window = glfwCreateWindow((int)(1280 * main_scale), (int)(800 * main_scale), "Dear ImGui GLFW+OpenGL2 example", nullptr, nullptr);
45 if (window == nullptr)
46 return 1;
47 glfwMakeContextCurrent(window);
48 glfwSwapInterval(1); // Enable vsync
49
50 // Setup Dear ImGui context
51 IMGUI_CHECKVERSION();
52 ImGui::CreateContext();
53 ImGuiIO& io = ImGui::GetIO(); (void)io;
54 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
55 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
56 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
57 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
58 //io.ConfigViewportsNoAutoMerge = true;
59 //io.ConfigViewportsNoTaskBarIcon = true;
60
61 // Setup Dear ImGui style
62 ImGui::StyleColorsDark();
63 //ImGui::StyleColorsLight();
64
65 // Setup scaling
66 ImGuiStyle& style = ImGui::GetStyle();
67 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)
68 style.FontScaleDpi = main_scale; // Set initial font scale. (in docking branch: using io.ConfigDpiScaleFonts=true automatically overrides this for every window depending on the current monitor)
69 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.
70 io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
71
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
79 // Setup Platform/Renderer backends
80 ImGui_ImplGlfw_InitForOpenGL(window, true);
81 ImGui_ImplOpenGL2_Init();
82
83 // Load Fonts
84 // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
85 // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
86 // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
87 // - If a file cannot be loaded, AddFont functions will return a nullptr. Please handle those errors in your code (e.g. use an assertion, display an error and quit).
88 // - Read 'docs/FONTS.md' for more instructions and details.
89 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
90 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
91 //style.FontSizeBase = 20.0f;
92 //io.Fonts->AddFontDefaultVector();
93 //io.Fonts->AddFontDefaultBitmap();
94 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
95 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
96 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
97 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
98 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
99 //IM_ASSERT(font != nullptr);
100
101 // Our state
102 bool show_demo_window = true;
103 bool show_another_window = false;
104 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
105
106 // Main loop
107 while (!glfwWindowShouldClose(window))
108 {
109 // Poll and handle events (inputs, window resize, etc.)
110 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
111 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
112 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
113 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
114 glfwPollEvents();
115 if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0)
116 {
117 ImGui_ImplGlfw_Sleep(10);
118 continue;
119 }
120
121 // Start the Dear ImGui frame
122 ImGui_ImplOpenGL2_NewFrame();
123 ImGui_ImplGlfw_NewFrame();
124 ImGui::NewFrame();
125
126 // 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!).
127 if (show_demo_window)
128 ImGui::ShowDemoWindow(&show_demo_window);
129
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
135 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
136
137 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
138 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
139 ImGui::Checkbox("Another Window", &show_another_window);
140
141 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
142 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
143
144 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
145 counter++;
146 ImGui::SameLine();
147 ImGui::Text("counter = %d", counter);
148
149 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
150 ImGui::End();
151 }
152
153 // 3. Show another simple window.
154 if (show_another_window)
155 {
156 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)
157 ImGui::Text("Hello from another window!");
158 if (ImGui::Button("Close Me"))
159 show_another_window = false;
160 ImGui::End();
161 }
162
163 // Rendering
164 ImGui::Render();
165 int display_w, display_h;
166 glfwGetFramebufferSize(window, &display_w, &display_h);
167 glViewport(0, 0, display_w, display_h);
168 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
169 glClear(GL_COLOR_BUFFER_BIT);
170
171 // If you are using this code with non-legacy OpenGL header/contexts (which you should not, prefer using imgui_impl_opengl3.cpp!!),
172 // you may need to backup/reset/restore other state, e.g. for current shader using the commented lines below.
173 //GLint last_program;
174 //glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
175 //glUseProgram(0);
176 ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
177 //glUseProgram(last_program);
178
179 // Update and Render additional Platform Windows
180 // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
181 // For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
182 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
183 {
184 GLFWwindow* backup_current_context = glfwGetCurrentContext();
185 ImGui::UpdatePlatformWindows();
186 ImGui::RenderPlatformWindowsDefault();
187 glfwMakeContextCurrent(backup_current_context);
188 }
189
190 glfwMakeContextCurrent(window);
191 glfwSwapBuffers(window);
192 }
193
194 // Cleanup
195 ImGui_ImplOpenGL2_Shutdown();
196 ImGui_ImplGlfw_Shutdown();
197 ImGui::DestroyContext();
198
199 glfwDestroyWindow(window);
200 glfwTerminate();
201
202 return 0;
203}