11.6 KB、242行 生 Blame 履歴
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
32static void glfw_error_callback(int error, const char* description)
33{
34 fprintf(stderr, "GLFW Error %d: %s\n", error, description);
35}
36
37// Main code
38int main(int, char**)
39{
40 glfwSetErrorCallback(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, 3);
68 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 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(glfwGetPrimaryMonitor()); // Valid on GLFW 3.3+ only
75 GLFWwindow* window = glfwCreateWindow((int)(1280 * main_scale), (int)(800 * main_scale), "Dear ImGui GLFW+OpenGL3 example", nullptr, nullptr);
76 if (window == nullptr)
77 return 1;
78 glfwMakeContextCurrent(window);
79 glfwSwapInterval(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(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. (in docking branch: using io.ConfigDpiScaleFonts=true automatically overrides this for every window depending on the current monitor)
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, true);
114#ifdef __EMSCRIPTEN__
115 ImGui_ImplGlfw_InstallEmscriptenCallbacks(window, "#canvas");
116#endif
117 ImGui_ImplOpenGL3_Init(glsl_version);
118
119 // Load Fonts
120 // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
121 // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
122 // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
123 // - 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).
124 // - Read 'docs/FONTS.md' for more instructions and details.
125 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
126 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
127 // - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
128 //style.FontSizeBase = 20.0f;
129 //io.Fonts->AddFontDefaultVector();
130 //io.Fonts->AddFontDefaultBitmap();
131 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
132 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
133 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
134 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
135 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
136 //IM_ASSERT(font != nullptr);
137
138 // Our state
139 bool show_demo_window = true;
140 bool show_another_window = false;
141 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
142
143 // Main loop
144#ifdef __EMSCRIPTEN__
145 // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
146 // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
147 io.IniFilename = nullptr;
148 EMSCRIPTEN_MAINLOOP_BEGIN
149#else
150 while (!glfwWindowShouldClose(window))
151#endif
152 {
153 // Poll and handle events (inputs, window resize, etc.)
154 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
155 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
156 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
157 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
158 glfwPollEvents();
159 if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0)
160 {
161 ImGui_ImplGlfw_Sleep(10);
162 continue;
163 }
164
165 // Start the Dear ImGui frame
166 ImGui_ImplOpenGL3_NewFrame();
167 ImGui_ImplGlfw_NewFrame();
168 ImGui::NewFrame();
169
170 // 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!).
171 if (show_demo_window)
172 ImGui::ShowDemoWindow(&show_demo_window);
173
174 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
175 {
176 static float f = 0.0f;
177 static int counter = 0;
178
179 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
180
181 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
182 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
183 ImGui::Checkbox("Another Window", &show_another_window);
184
185 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
186 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
187
188 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
189 counter++;
190 ImGui::SameLine();
191 ImGui::Text("counter = %d", counter);
192
193 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
194 ImGui::End();
195 }
196
197 // 3. Show another simple window.
198 if (show_another_window)
199 {
200 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)
201 ImGui::Text("Hello from another window!");
202 if (ImGui::Button("Close Me"))
203 show_another_window = false;
204 ImGui::End();
205 }
206
207 // Rendering
208 ImGui::Render();
209 int display_w, display_h;
210 glfwGetFramebufferSize(window, &display_w, &display_h);
211 glViewport(0, 0, display_w, display_h);
212 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
213 glClear(GL_COLOR_BUFFER_BIT);
214 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
215
216 // Update and Render additional Platform Windows
217 // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
218 // For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
219 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
220 {
221 GLFWwindow* backup_current_context = glfwGetCurrentContext();
222 ImGui::UpdatePlatformWindows();
223 ImGui::RenderPlatformWindowsDefault();
224 glfwMakeContextCurrent(backup_current_context);
225 }
226
227 glfwSwapBuffers(window);
228 }
229#ifdef __EMSCRIPTEN__
230 EMSCRIPTEN_MAINLOOP_END;
231#endif
232
233 // Cleanup
234 ImGui_ImplOpenGL3_Shutdown();
235 ImGui_ImplGlfw_Shutdown();
236 ImGui::DestroyContext();
237
238 glfwDestroyWindow(window);
239 glfwTerminate();
240
241 return 0;
242}