| 1 | // Dear ImGui: standalone example application for SDL2 + 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 SDL2. |
| 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_sdl2.h" |
| 15 | #include "imgui_impl_sdlrenderer2.h" |
| 16 | #include <stdio.h> |
| 17 | #include <SDL.h> |
| 18 | #ifdef _WIN32 |
| 19 | #include <windows.h> // SetProcessDPIAware() |
| 20 | #endif |
| 21 | |
| 22 | #if !SDL_VERSION_ATLEAST(2,0,17) |
| 23 | #error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function |
| 24 | #endif |
| 25 | |
| 26 | // Main code |
| 27 | int main(int, char**) |
| 28 | { |
| 29 | // Setup SDL |
| 30 | #ifdef _WIN32 |
| 31 | ::SetProcessDPIAware(); |
| 32 | #endif |
| 33 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) |
| 34 | { |
| 35 | printf(format: "Error: %s\n" , SDL_GetError()); |
| 36 | return -1; |
| 37 | } |
| 38 | |
| 39 | // From 2.0.18: Enable native IME. |
| 40 | #ifdef SDL_HINT_IME_SHOW_UI |
| 41 | SDL_SetHint(SDL_HINT_IME_SHOW_UI, value: "1" ); |
| 42 | #endif |
| 43 | |
| 44 | // Create window with SDL_Renderer graphics context |
| 45 | float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(display_index: 0); |
| 46 | SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); |
| 47 | SDL_Window* window = SDL_CreateWindow(title: "Dear ImGui SDL2+SDL_Renderer example" , SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w: (int)(1280 * main_scale), h: (int)(720 * main_scale), flags: window_flags); |
| 48 | if (window == nullptr) |
| 49 | { |
| 50 | printf(format: "Error: SDL_CreateWindow(): %s\n" , SDL_GetError()); |
| 51 | return -1; |
| 52 | } |
| 53 | SDL_Renderer* renderer = SDL_CreateRenderer(window, index: -1, flags: SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED); |
| 54 | if (renderer == nullptr) |
| 55 | { |
| 56 | SDL_Log(fmt: "Error creating SDL_Renderer!" ); |
| 57 | return -1; |
| 58 | } |
| 59 | //SDL_RendererInfo info; |
| 60 | //SDL_GetRendererInfo(renderer, &info); |
| 61 | //SDL_Log("Current SDL_Renderer: %s", info.name); |
| 62 | |
| 63 | // Setup Dear ImGui context |
| 64 | IMGUI_CHECKVERSION(); |
| 65 | ImGui::CreateContext(); |
| 66 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 67 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls |
| 68 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls |
| 69 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking |
| 70 | |
| 71 | // Setup Dear ImGui style |
| 72 | ImGui::StyleColorsDark(); |
| 73 | //ImGui::StyleColorsLight(); |
| 74 | |
| 75 | // Setup scaling |
| 76 | ImGuiStyle& style = ImGui::GetStyle(); |
| 77 | 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) |
| 78 | style.FontScaleDpi = main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose) |
| 79 | 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. |
| 80 | io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes. |
| 81 | |
| 82 | // Setup Platform/Renderer backends |
| 83 | ImGui_ImplSDL2_InitForSDLRenderer(window, renderer); |
| 84 | ImGui_ImplSDLRenderer2_Init(renderer); |
| 85 | |
| 86 | // Load Fonts |
| 87 | // - 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. |
| 88 | // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. |
| 89 | // - 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). |
| 90 | // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering. |
| 91 | // - Read 'docs/FONTS.md' for more instructions and details. |
| 92 | // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! |
| 93 | //style.FontSizeBase = 20.0f; |
| 94 | //io.Fonts->AddFontDefault(); |
| 95 | //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf"); |
| 96 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf"); |
| 97 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf"); |
| 98 | //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf"); |
| 99 | //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf"); |
| 100 | //IM_ASSERT(font != nullptr); |
| 101 | |
| 102 | // Our state |
| 103 | bool show_demo_window = true; |
| 104 | bool show_another_window = false; |
| 105 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 106 | |
| 107 | // Main loop |
| 108 | bool done = false; |
| 109 | while (!done) |
| 110 | { |
| 111 | // Poll and handle events (inputs, window resize, etc.) |
| 112 | // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. |
| 113 | // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
| 114 | // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
| 115 | // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. |
| 116 | SDL_Event event; |
| 117 | while (SDL_PollEvent(event: &event)) |
| 118 | { |
| 119 | ImGui_ImplSDL2_ProcessEvent(event: &event); |
| 120 | if (event.type == SDL_QUIT) |
| 121 | done = true; |
| 122 | if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) |
| 123 | done = true; |
| 124 | } |
| 125 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) |
| 126 | { |
| 127 | SDL_Delay(ms: 10); |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | // Start the Dear ImGui frame |
| 132 | ImGui_ImplSDLRenderer2_NewFrame(); |
| 133 | ImGui_ImplSDL2_NewFrame(); |
| 134 | ImGui::NewFrame(); |
| 135 | |
| 136 | // 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!). |
| 137 | if (show_demo_window) |
| 138 | ImGui::ShowDemoWindow(p_open: &show_demo_window); |
| 139 | |
| 140 | // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. |
| 141 | { |
| 142 | static float f = 0.0f; |
| 143 | static int counter = 0; |
| 144 | |
| 145 | ImGui::Begin(name: "Hello, world!" ); // Create a window called "Hello, world!" and append into it. |
| 146 | |
| 147 | ImGui::Text(fmt: "This is some useful text." ); // Display some text (you can use a format strings too) |
| 148 | ImGui::Checkbox(label: "Demo Window" , v: &show_demo_window); // Edit bools storing our window open/close state |
| 149 | ImGui::Checkbox(label: "Another Window" , v: &show_another_window); |
| 150 | |
| 151 | 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 |
| 152 | ImGui::ColorEdit3(label: "clear color" , col: (float*)&clear_color); // Edit 3 floats representing a color |
| 153 | |
| 154 | if (ImGui::Button(label: "Button" )) // Buttons return true when clicked (most widgets return true when edited/activated) |
| 155 | counter++; |
| 156 | ImGui::SameLine(); |
| 157 | ImGui::Text(fmt: "counter = %d" , counter); |
| 158 | |
| 159 | ImGui::Text(fmt: "Application average %.3f ms/frame (%.1f FPS)" , 1000.0f / io.Framerate, io.Framerate); |
| 160 | ImGui::End(); |
| 161 | } |
| 162 | |
| 163 | // 3. Show another simple window. |
| 164 | if (show_another_window) |
| 165 | { |
| 166 | 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) |
| 167 | ImGui::Text(fmt: "Hello from another window!" ); |
| 168 | if (ImGui::Button(label: "Close Me" )) |
| 169 | show_another_window = false; |
| 170 | ImGui::End(); |
| 171 | } |
| 172 | |
| 173 | // Rendering |
| 174 | ImGui::Render(); |
| 175 | SDL_RenderSetScale(renderer, scaleX: io.DisplayFramebufferScale.x, scaleY: io.DisplayFramebufferScale.y); |
| 176 | SDL_SetRenderDrawColor(renderer, r: (Uint8)(clear_color.x * 255), g: (Uint8)(clear_color.y * 255), b: (Uint8)(clear_color.z * 255), a: (Uint8)(clear_color.w * 255)); |
| 177 | SDL_RenderClear(renderer); |
| 178 | ImGui_ImplSDLRenderer2_RenderDrawData(draw_data: ImGui::GetDrawData(), renderer); |
| 179 | SDL_RenderPresent(renderer); |
| 180 | } |
| 181 | |
| 182 | // Cleanup |
| 183 | ImGui_ImplSDLRenderer2_Shutdown(); |
| 184 | ImGui_ImplSDL2_Shutdown(); |
| 185 | ImGui::DestroyContext(); |
| 186 | |
| 187 | SDL_DestroyRenderer(renderer); |
| 188 | SDL_DestroyWindow(window); |
| 189 | SDL_Quit(); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |