12.1 KB、266行 生 Blame 履歴
1// Dear ImGui: standalone example application for SDL2 + OpenGL
2// (SDL 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_sdl2.h"
12#include "imgui_impl_opengl3.h"
13#include <stdio.h>
14#include <SDL.h>
15#if defined(IMGUI_IMPL_OPENGL_ES2)
16#include <SDL_opengles2.h>
17#else
18#include <SDL_opengl.h>
19#endif
20#ifdef _WIN32
21#include <windows.h> // SetProcessDPIAware()
22#endif
23
24// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
25#ifdef __EMSCRIPTEN__
26#include "../libs/emscripten/emscripten_mainloop_stub.h"
27#endif
28
29// Main code
30int main(int, char**)
31{
32 // Setup SDL
33#ifdef _WIN32
34 ::SetProcessDPIAware();
35#endif
36 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
37 {
38 printf("Error: %s\n", SDL_GetError());
39 return 1;
40 }
41
42 // Decide GL+GLSL versions
43#if defined(IMGUI_IMPL_OPENGL_ES2)
44 // GL ES 2.0 + GLSL 100 (WebGL 1.0)
45 const char* glsl_version = "#version 100";
46 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
47 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
48 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
49 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
50#elif defined(IMGUI_IMPL_OPENGL_ES3)
51 // GL ES 3.0 + GLSL 300 es (WebGL 2.0)
52 const char* glsl_version = "#version 300 es";
53 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
54 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
55 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
56 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
57#elif defined(__APPLE__)
58 // GL 3.2 Core + GLSL 150
59 const char* glsl_version = "#version 150";
60 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
61 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
62 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
63 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
64#else
65 // GL 3.0 + GLSL 130
66 const char* glsl_version = "#version 130";
67 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
68 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
69 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
70 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
71#endif
72
73 // From 2.0.18: Enable native IME.
74#ifdef SDL_HINT_IME_SHOW_UI
75 SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
76#endif
77
78 // Create window with graphics context
79 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
80 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
81 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
82 float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
83 SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
84 SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
85 if (window == nullptr)
86 {
87 printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
88 return 1;
89 }
90
91 SDL_GLContext gl_context = SDL_GL_CreateContext(window);
92 if (gl_context == nullptr)
93 {
94 printf("Error: SDL_GL_CreateContext(): %s\n", SDL_GetError());
95 return 1;
96 }
97
98 SDL_GL_MakeCurrent(window, gl_context);
99 SDL_GL_SetSwapInterval(1); // Enable vsync
100
101 // Setup Dear ImGui context
102 IMGUI_CHECKVERSION();
103 ImGui::CreateContext();
104 ImGuiIO& io = ImGui::GetIO(); (void)io;
105 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
106 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
107 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
108 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
109 //io.ConfigViewportsNoAutoMerge = true;
110 //io.ConfigViewportsNoTaskBarIcon = true;
111
112 // Setup Dear ImGui style
113 ImGui::StyleColorsDark();
114 //ImGui::StyleColorsLight();
115
116 // Setup scaling
117 ImGuiStyle& style = ImGui::GetStyle();
118 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)
119 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)
120 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.
121 io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
122
123 // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
124 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
125 {
126 style.WindowRounding = 0.0f;
127 style.Colors[ImGuiCol_WindowBg].w = 1.0f;
128 }
129
130 // Setup Platform/Renderer backends
131 ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
132 ImGui_ImplOpenGL3_Init(glsl_version);
133
134 // Load Fonts
135 // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
136 // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
137 // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
138 // - 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).
139 // - Read 'docs/FONTS.md' for more instructions and details.
140 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
141 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
142 // - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
143 //style.FontSizeBase = 20.0f;
144 //io.Fonts->AddFontDefaultVector();
145 //io.Fonts->AddFontDefaultBitmap();
146 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
147 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
148 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
149 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
150 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
151 //IM_ASSERT(font != nullptr);
152
153 // Our state
154 bool show_demo_window = true;
155 bool show_another_window = false;
156 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
157
158 // Main loop
159 bool done = false;
160#ifdef __EMSCRIPTEN__
161 // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
162 // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
163 io.IniFilename = nullptr;
164 EMSCRIPTEN_MAINLOOP_BEGIN
165#else
166 while (!done)
167#endif
168 {
169 // Poll and handle events (inputs, window resize, etc.)
170 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
171 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
172 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
173 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
174 SDL_Event event;
175 while (SDL_PollEvent(&event))
176 {
177 ImGui_ImplSDL2_ProcessEvent(&event);
178 if (event.type == SDL_QUIT)
179 done = true;
180 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
181 done = true;
182 }
183 if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
184 {
185 SDL_Delay(10);
186 continue;
187 }
188
189 // Start the Dear ImGui frame
190 ImGui_ImplOpenGL3_NewFrame();
191 ImGui_ImplSDL2_NewFrame();
192 ImGui::NewFrame();
193
194 // 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!).
195 if (show_demo_window)
196 ImGui::ShowDemoWindow(&show_demo_window);
197
198 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
199 {
200 static float f = 0.0f;
201 static int counter = 0;
202
203 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
204
205 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
206 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
207 ImGui::Checkbox("Another Window", &show_another_window);
208
209 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
210 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
211
212 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
213 counter++;
214 ImGui::SameLine();
215 ImGui::Text("counter = %d", counter);
216
217 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
218 ImGui::End();
219 }
220
221 // 3. Show another simple window.
222 if (show_another_window)
223 {
224 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)
225 ImGui::Text("Hello from another window!");
226 if (ImGui::Button("Close Me"))
227 show_another_window = false;
228 ImGui::End();
229 }
230
231 // Rendering
232 ImGui::Render();
233 glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
234 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
235 glClear(GL_COLOR_BUFFER_BIT);
236 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
237
238 // Update and Render additional Platform Windows
239 // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
240 // For this specific demo app we could also call SDL_GL_MakeCurrent(window, gl_context) directly)
241 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
242 {
243 SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
244 SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
245 ImGui::UpdatePlatformWindows();
246 ImGui::RenderPlatformWindowsDefault();
247 SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
248 }
249
250 SDL_GL_SwapWindow(window);
251 }
252#ifdef __EMSCRIPTEN__
253 EMSCRIPTEN_MAINLOOP_END;
254#endif
255
256 // Cleanup
257 ImGui_ImplOpenGL3_Shutdown();
258 ImGui_ImplSDL2_Shutdown();
259 ImGui::DestroyContext();
260
261 SDL_GL_DeleteContext(gl_context);
262 SDL_DestroyWindow(window);
263 SDL_Quit();
264
265 return 0;
266}