1// Dear ImGui: standalone example application for SDL3 + SDL_Renderer
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// Important to understand: SDL_Renderer is an _optional_ component of SDL3.
11// For a multi-platform app consider using e.g. SDL+DirectX on Windows and SDL+OpenGL on Linux/OSX.
12
13#include "imgui.h"
14#include "imgui_impl_sdl3.h"
15#include "imgui_impl_sdlrenderer3.h"
16#include <stdio.h>
17#include <SDL3/SDL.h>
18
19#ifdef __EMSCRIPTEN__
20#include "../libs/emscripten/emscripten_mainloop_stub.h"
21#endif
22
23// Main code
24int main(int, char**)
25{
26 // Setup SDL
27 // [If using SDL_MAIN_USE_CALLBACKS: all code below until the main loop starts would likely be your SDL_AppInit() function]
28 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
29 {
30 printf("Error: SDL_Init(): %s\n", SDL_GetError());
31 return -1;
32 }
33
34 // Create window with SDL_Renderer graphics context
35 Uint32 window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
36 SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+SDL_Renderer example", 1280, 720, window_flags);
37 if (window == nullptr)
38 {
39 printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
40 return -1;
41 }
42 SDL_Renderer* renderer = SDL_CreateRenderer(window, nullptr);
43 SDL_SetRenderVSync(renderer, 1);
44 if (renderer == nullptr)
45 {
46 SDL_Log("Error: SDL_CreateRenderer(): %s\n", SDL_GetError());
47 return -1;
48 }
49 SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
50 SDL_ShowWindow(window);
51
52 // Setup Dear ImGui context
53 IMGUI_CHECKVERSION();
54 ImGui::CreateContext();
55 ImGuiIO& io = ImGui::GetIO(); (void)io;
56 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
57 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
58 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
59
60 // Setup Dear ImGui style
61 ImGui::StyleColorsDark();
62 //ImGui::StyleColorsLight();
63
64 // Setup Platform/Renderer backends
65 ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
66 ImGui_ImplSDLRenderer3_Init(renderer);
67
68 // Load Fonts
69 // - 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.
70 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
71 // - 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).
72 // - 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.
73 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
74 // - Read 'docs/FONTS.md' for more instructions and details.
75 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
76 // - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
77 //io.Fonts->AddFontDefault();
78 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
79 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
80 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
81 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
82 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
83 //IM_ASSERT(font != nullptr);
84
85 // Our state
86 bool show_demo_window = true;
87 bool show_another_window = false;
88 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
89
90 // Main loop
91 bool done = false;
92#ifdef __EMSCRIPTEN__
93 // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
94 // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
95 io.IniFilename = nullptr;
96 EMSCRIPTEN_MAINLOOP_BEGIN
97#else
98 while (!done)
99#endif
100 {
101 // Poll and handle events (inputs, window resize, etc.)
102 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
103 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
104 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
105 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
106 // [If using SDL_MAIN_USE_CALLBACKS: call ImGui_ImplSDL3_ProcessEvent() from your SDL_AppEvent() function]
107 SDL_Event event;
108 while (SDL_PollEvent(&event))
109 {
110 ImGui_ImplSDL3_ProcessEvent(&event);
111 if (event.type == SDL_EVENT_QUIT)
112 done = true;
113 if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
114 done = true;
115 }
116
117 // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppIterate() function]
118 if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
119 {
120 SDL_Delay(10);
121 continue;
122 }
123
124 // Start the Dear ImGui frame
125 ImGui_ImplSDLRenderer3_NewFrame();
126 ImGui_ImplSDL3_NewFrame();
127 ImGui::NewFrame();
128
129 // 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!).
130 if (show_demo_window)
131 ImGui::ShowDemoWindow(p_open: &show_demo_window);
132
133 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
134 {
135 static float f = 0.0f;
136 static int counter = 0;
137
138 ImGui::Begin(name: "Hello, world!"); // Create a window called "Hello, world!" and append into it.
139
140 ImGui::Text(fmt: "This is some useful text."); // Display some text (you can use a format strings too)
141 ImGui::Checkbox(label: "Demo Window", v: &show_demo_window); // Edit bools storing our window open/close state
142 ImGui::Checkbox(label: "Another Window", v: &show_another_window);
143
144 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
145 ImGui::ColorEdit3(label: "clear color", col: (float*)&clear_color); // Edit 3 floats representing a color
146
147 if (ImGui::Button(label: "Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
148 counter++;
149 ImGui::SameLine();
150 ImGui::Text(fmt: "counter = %d", counter);
151
152 ImGui::Text(fmt: "Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
153 ImGui::End();
154 }
155
156 // 3. Show another simple window.
157 if (show_another_window)
158 {
159 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)
160 ImGui::Text(fmt: "Hello from another window!");
161 if (ImGui::Button(label: "Close Me"))
162 show_another_window = false;
163 ImGui::End();
164 }
165
166 // Rendering
167 ImGui::Render();
168 //SDL_RenderSetScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
169 SDL_SetRenderDrawColorFloat(renderer, clear_color.x, clear_color.y, clear_color.z, clear_color.w);
170 SDL_RenderClear(renderer);
171 ImGui_ImplSDLRenderer3_RenderDrawData(draw_data: ImGui::GetDrawData(), renderer);
172 SDL_RenderPresent(renderer);
173 }
174#ifdef __EMSCRIPTEN__
175 EMSCRIPTEN_MAINLOOP_END;
176#endif
177
178 // Cleanup
179 // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function]
180 ImGui_ImplSDLRenderer3_Shutdown();
181 ImGui_ImplSDL3_Shutdown();
182 ImGui::DestroyContext();
183
184 SDL_DestroyRenderer(renderer);
185 SDL_DestroyWindow(window);
186 SDL_Quit();
187
188 return 0;
189}
190

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

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