| 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 |
| 24 | int 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 | float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()); |
| 36 | SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY; |
| 37 | SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+SDL_Renderer example", (int)(1280 * main_scale), (int)(800 * main_scale), window_flags); |
| 38 | if (window == nullptr) |
| 39 | { |
| 40 | printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); |
| 41 | return 1; |
| 42 | } |
| 43 | SDL_Renderer* renderer = SDL_CreateRenderer(window, nullptr); |
| 44 | SDL_SetRenderVSync(renderer, 1); |
| 45 | if (renderer == nullptr) |
| 46 | { |
| 47 | SDL_Log("Error: SDL_CreateRenderer(): %s\n", SDL_GetError()); |
| 48 | return 1; |
| 49 | } |
| 50 | SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); |
| 51 | SDL_ShowWindow(window); |
| 52 | |
| 53 | // Setup Dear ImGui context |
| 54 | IMGUI_CHECKVERSION(); |
| 55 | ImGui::CreateContext(); |
| 56 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 57 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls |
| 58 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls |
| 59 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking |
| 60 | |
| 61 | // Setup Dear ImGui style |
| 62 | ImGui::StyleColorsDark(); |
| 63 | //ImGui::StyleColorsLight(); |
| 64 | |
| 65 | // Setup scaling |
| 66 | ImGuiStyle& style = ImGui::GetStyle(); |
| 67 | 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) |
| 68 | 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) |
| 69 | //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. |
| 70 | //io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes. |
| 71 | |
| 72 | // Setup Platform/Renderer backends |
| 73 | ImGui_ImplSDL3_InitForSDLRenderer(window, renderer); |
| 74 | ImGui_ImplSDLRenderer3_Init(renderer); |
| 75 | |
| 76 | // Load Fonts |
| 77 | // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap(). |
| 78 | // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold. |
| 79 | // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them. |
| 80 | // - 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). |
| 81 | // - Read 'docs/FONTS.md' for more instructions and details. |
| 82 | // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering. |
| 83 | // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! |
| 84 | //style.FontSizeBase = 20.0f; |
| 85 | //io.Fonts->AddFontDefaultVector(); |
| 86 | //io.Fonts->AddFontDefaultBitmap(); |
| 87 | //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf"); |
| 88 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf"); |
| 89 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf"); |
| 90 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf"); |
| 91 | //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf"); |
| 92 | //IM_ASSERT(font != nullptr); |
| 93 | |
| 94 | // Our state |
| 95 | bool show_demo_window = true; |
| 96 | bool show_another_window = false; |
| 97 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 98 | |
| 99 | // Main loop |
| 100 | bool done = false; |
| 101 | #ifdef __EMSCRIPTEN__ |
| 102 | // For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file. |
| 103 | // You may manually call LoadIniSettingsFromMemory() to load settings from your own storage. |
| 104 | io.IniFilename = nullptr; |
| 105 | EMSCRIPTEN_MAINLOOP_BEGIN |
| 106 | #else |
| 107 | while (!done) |
| 108 | #endif |
| 109 | { |
| 110 | // Poll and handle events (inputs, window resize, etc.) |
| 111 | // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. |
| 112 | // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
| 113 | // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
| 114 | // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. |
| 115 | // [If using SDL_MAIN_USE_CALLBACKS: call ImGui_ImplSDL3_ProcessEvent() from your SDL_AppEvent() function] |
| 116 | SDL_Event event; |
| 117 | while (SDL_PollEvent(&event)) |
| 118 | { |
| 119 | ImGui_ImplSDL3_ProcessEvent(&event); |
| 120 | if (event.type == SDL_EVENT_QUIT) |
| 121 | done = true; |
| 122 | if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) |
| 123 | done = true; |
| 124 | } |
| 125 | |
| 126 | // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppIterate() function] |
| 127 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) |
| 128 | { |
| 129 | SDL_Delay(10); |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | // Start the Dear ImGui frame |
| 134 | ImGui_ImplSDLRenderer3_NewFrame(); |
| 135 | ImGui_ImplSDL3_NewFrame(); |
| 136 | ImGui::NewFrame(); |
| 137 | |
| 138 | // 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!). |
| 139 | if (show_demo_window) |
| 140 | ImGui::ShowDemoWindow(&show_demo_window); |
| 141 | |
| 142 | // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. |
| 143 | { |
| 144 | static float f = 0.0f; |
| 145 | static int counter = 0; |
| 146 | |
| 147 | ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. |
| 148 | |
| 149 | ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) |
| 150 | ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state |
| 151 | ImGui::Checkbox("Another Window", &show_another_window); |
| 152 | |
| 153 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f |
| 154 | ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color |
| 155 | |
| 156 | if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) |
| 157 | counter++; |
| 158 | ImGui::SameLine(); |
| 159 | ImGui::Text("counter = %d", counter); |
| 160 | |
| 161 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 162 | ImGui::End(); |
| 163 | } |
| 164 | |
| 165 | // 3. Show another simple window. |
| 166 | if (show_another_window) |
| 167 | { |
| 168 | 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) |
| 169 | ImGui::Text("Hello from another window!"); |
| 170 | if (ImGui::Button("Close Me")) |
| 171 | show_another_window = false; |
| 172 | ImGui::End(); |
| 173 | } |
| 174 | |
| 175 | // Rendering |
| 176 | ImGui::Render(); |
| 177 | SDL_SetRenderScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); |
| 178 | SDL_SetRenderDrawColorFloat(renderer, clear_color.x, clear_color.y, clear_color.z, clear_color.w); |
| 179 | SDL_RenderClear(renderer); |
| 180 | ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer); |
| 181 | SDL_RenderPresent(renderer); |
| 182 | } |
| 183 | #ifdef __EMSCRIPTEN__ |
| 184 | EMSCRIPTEN_MAINLOOP_END; |
| 185 | #endif |
| 186 | |
| 187 | // Cleanup |
| 188 | // [If using SDL_MAIN_USE_CALLBACKS: all code below would likely be your SDL_AppQuit() function] |
| 189 | ImGui_ImplSDLRenderer3_Shutdown(); |
| 190 | ImGui_ImplSDL3_Shutdown(); |
| 191 | ImGui::DestroyContext(); |
| 192 | |
| 193 | SDL_DestroyRenderer(renderer); |
| 194 | SDL_DestroyWindow(window); |
| 195 | SDL_Quit(); |
| 196 | |
| 197 | return 0; |
| 198 | } |