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