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 Uint32 window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
72 SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+OpenGL3 example", 1280, 720, window_flags);
73 if (window == nullptr)
74 {
75 printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
76 return -1;
77 }
78 SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
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_ShowWindow(window);
89
90 // Setup Dear ImGui context
91 IMGUI_CHECKVERSION();
92 ImGui::CreateContext();
93 ImGuiIO& io = ImGui::GetIO(); (void)io;
94 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
95 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
96 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
97 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
98 //io.ConfigViewportsNoAutoMerge = true;
99 //io.ConfigViewportsNoTaskBarIcon = true;
100
101 // Setup Dear ImGui style
102 ImGui::StyleColorsDark();
103 //ImGui::StyleColorsLight();
104
105 // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
106 ImGuiStyle& style = ImGui::GetStyle();
107 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
108 {
109 style.WindowRounding = 0.0f;
110 style.Colors[ImGuiCol_WindowBg].w = 1.0f;
111 }
112
113 // Setup Platform/Renderer backends
114 ImGui_ImplSDL3_InitForOpenGL(window, gl_context);
115 ImGui_ImplOpenGL3_Init(glsl_version);
116
117 // Load Fonts
118 // - 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.
119 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
120 // - 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).
121 // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
122 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
123 // - Read 'docs/FONTS.md' for more instructions and details.
124 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
125 // - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
126 //io.Fonts->AddFontDefault();
127 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
128 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
129 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
130 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
131 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
132 //IM_ASSERT(font != nullptr);
133
134 // Our state
135 bool show_demo_window = true;
136 bool show_another_window = false;
137 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
138
139 // Main loop
140 bool done = false;
141#ifdef __EMSCRIPTEN__
142 // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
143 // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
144 io.IniFilename = nullptr;
145 EMSCRIPTEN_MAINLOOP_BEGIN
146#else
147 while (!done)
148#endif
149 {
150 // Poll and handle events (inputs, window resize, etc.)
151 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
152 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
153 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
154 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
155 // [If using SDL_MAIN_USE_CALLBACKS: call ImGui_ImplSDL3_ProcessEvent() from your SDL_AppEvent() function]
156 SDL_Event event;
157 while (SDL_PollEvent(&event))
158 {
159 ImGui_ImplSDL3_ProcessEvent(&event);
160 if (event.type == SDL_EVENT_QUIT)
161 done = true;
162 if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
163 done = true;
164 }
165
166 // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppIterate() function]
167 if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
168 {
169 SDL_Delay(10);
170 continue;
171 }
172
173 // Start the Dear ImGui frame
174 ImGui_ImplOpenGL3_NewFrame();
175 ImGui_ImplSDL3_NewFrame();
176 ImGui::NewFrame();
177
178 // 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!).
179 if (show_demo_window)
180 ImGui::ShowDemoWindow(p_open: &show_demo_window);
181
182 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
183 {
184 static float f = 0.0f;
185 static int counter = 0;
186
187 ImGui::Begin(name: "Hello, world!"); // Create a window called "Hello, world!" and append into it.
188
189 ImGui::Text(fmt: "This is some useful text."); // Display some text (you can use a format strings too)
190 ImGui::Checkbox(label: "Demo Window", v: &show_demo_window); // Edit bools storing our window open/close state
191 ImGui::Checkbox(label: "Another Window", v: &show_another_window);
192
193 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
194 ImGui::ColorEdit3(label: "clear color", col: (float*)&clear_color); // Edit 3 floats representing a color
195
196 if (ImGui::Button(label: "Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
197 counter++;
198 ImGui::SameLine();
199 ImGui::Text(fmt: "counter = %d", counter);
200
201 ImGui::Text(fmt: "Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
202 ImGui::End();
203 }
204
205 // 3. Show another simple window.
206 if (show_another_window)
207 {
208 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)
209 ImGui::Text(fmt: "Hello from another window!");
210 if (ImGui::Button(label: "Close Me"))
211 show_another_window = false;
212 ImGui::End();
213 }
214
215 // Rendering
216 ImGui::Render();
217 glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
218 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
219 glClear(GL_COLOR_BUFFER_BIT);
220 ImGui_ImplOpenGL3_RenderDrawData(draw_data: ImGui::GetDrawData());
221
222 // Update and Render additional Platform Windows
223 // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
224 // For this specific demo app we could also call SDL_GL_MakeCurrent(window, gl_context) directly)
225 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
226 {
227 SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
228 SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
229 ImGui::UpdatePlatformWindows();
230 ImGui::RenderPlatformWindowsDefault();
231 SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
232 }
233
234 SDL_GL_SwapWindow(window);
235 }
236#ifdef __EMSCRIPTEN__
237 EMSCRIPTEN_MAINLOOP_END;
238#endif
239
240 // Cleanup
241 // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function]
242 ImGui_ImplOpenGL3_Shutdown();
243 ImGui_ImplSDL3_Shutdown();
244 ImGui::DestroyContext();
245
246 SDL_GL_DestroyContext(gl_context);
247 SDL_DestroyWindow(window);
248 SDL_Quit();
249
250 return 0;
251}
252

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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