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