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)(720 * 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(scale_factor: 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. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
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 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.
126 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
127 // - 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).
128 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
129 // - Read 'docs/FONTS.md' for more instructions and details.
130 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
131 // - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
132 //style.FontSizeBase = 20.0f;
133 //io.Fonts->AddFontDefault();
134 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
135 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
136 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
137 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
138 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
139 //IM_ASSERT(font != nullptr);
140
141 // Our state
142 bool show_demo_window = true;
143 bool show_another_window = false;
144 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
145
146 // Main loop
147 bool done = false;
148#ifdef __EMSCRIPTEN__
149 // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
150 // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
151 io.IniFilename = nullptr;
152 EMSCRIPTEN_MAINLOOP_BEGIN
153#else
154 while (!done)
155#endif
156 {
157 // Poll and handle events (inputs, window resize, etc.)
158 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
159 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
160 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
161 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
162 // [If using SDL_MAIN_USE_CALLBACKS: call ImGui_ImplSDL3_ProcessEvent() from your SDL_AppEvent() function]
163 SDL_Event event;
164 while (SDL_PollEvent(&event))
165 {
166 ImGui_ImplSDL3_ProcessEvent(&event);
167 if (event.type == SDL_EVENT_QUIT)
168 done = true;
169 if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
170 done = true;
171 }
172
173 // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppIterate() function]
174 if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
175 {
176 SDL_Delay(10);
177 continue;
178 }
179
180 // Start the Dear ImGui frame
181 ImGui_ImplOpenGL3_NewFrame();
182 ImGui_ImplSDL3_NewFrame();
183 ImGui::NewFrame();
184
185 // 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!).
186 if (show_demo_window)
187 ImGui::ShowDemoWindow(p_open: &show_demo_window);
188
189 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
190 {
191 static float f = 0.0f;
192 static int counter = 0;
193
194 ImGui::Begin(name: "Hello, world!"); // Create a window called "Hello, world!" and append into it.
195
196 ImGui::Text(fmt: "This is some useful text."); // Display some text (you can use a format strings too)
197 ImGui::Checkbox(label: "Demo Window", v: &show_demo_window); // Edit bools storing our window open/close state
198 ImGui::Checkbox(label: "Another Window", v: &show_another_window);
199
200 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
201 ImGui::ColorEdit3(label: "clear color", col: (float*)&clear_color); // Edit 3 floats representing a color
202
203 if (ImGui::Button(label: "Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
204 counter++;
205 ImGui::SameLine();
206 ImGui::Text(fmt: "counter = %d", counter);
207
208 ImGui::Text(fmt: "Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
209 ImGui::End();
210 }
211
212 // 3. Show another simple window.
213 if (show_another_window)
214 {
215 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)
216 ImGui::Text(fmt: "Hello from another window!");
217 if (ImGui::Button(label: "Close Me"))
218 show_another_window = false;
219 ImGui::End();
220 }
221
222 // Rendering
223 ImGui::Render();
224 glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
225 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
226 glClear(GL_COLOR_BUFFER_BIT);
227 ImGui_ImplOpenGL3_RenderDrawData(draw_data: ImGui::GetDrawData());
228
229 // Update and Render additional Platform Windows
230 // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
231 // For this specific demo app we could also call SDL_GL_MakeCurrent(window, gl_context) directly)
232 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
233 {
234 SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
235 SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
236 ImGui::UpdatePlatformWindows();
237 ImGui::RenderPlatformWindowsDefault();
238 SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
239 }
240
241 SDL_GL_SwapWindow(window);
242 }
243#ifdef __EMSCRIPTEN__
244 EMSCRIPTEN_MAINLOOP_END;
245#endif
246
247 // Cleanup
248 // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function]
249 ImGui_ImplOpenGL3_Shutdown();
250 ImGui_ImplSDL3_Shutdown();
251 ImGui::DestroyContext();
252
253 SDL_GL_DestroyContext(gl_context);
254 SDL_DestroyWindow(window);
255 SDL_Quit();
256
257 return 0;
258}
259

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