9 KB、194行 生 Blame 履歴
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
27int 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("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, "1");
42#endif
43
44 // Create window with SDL_Renderer graphics context
45 float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
46 SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
47 SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+SDL_Renderer example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
48 if (window == nullptr)
49 {
50 printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
51 return 1;
52 }
53 SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
54 if (renderer == nullptr)
55 {
56 SDL_Log("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(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. (in docking branch: using io.ConfigDpiScaleFonts=true automatically overrides this for every window depending on the current monitor)
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 fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
88 // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
89 // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
90 // - 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).
91 // - Read 'docs/FONTS.md' for more instructions and details.
92 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
93 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
94 //style.FontSizeBase = 20.0f;
95 //io.Fonts->AddFontDefaultVector();
96 //io.Fonts->AddFontDefaultBitmap();
97 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
98 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
99 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
100 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
101 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
102 //IM_ASSERT(font != nullptr);
103
104 // Our state
105 bool show_demo_window = true;
106 bool show_another_window = false;
107 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
108
109 // Main loop
110 bool done = false;
111 while (!done)
112 {
113 // Poll and handle events (inputs, window resize, etc.)
114 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
115 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
116 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
117 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
118 SDL_Event event;
119 while (SDL_PollEvent(&event))
120 {
121 ImGui_ImplSDL2_ProcessEvent(&event);
122 if (event.type == SDL_QUIT)
123 done = true;
124 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
125 done = true;
126 }
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_ImplSDLRenderer2_NewFrame();
135 ImGui_ImplSDL2_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_RenderSetScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
178 SDL_SetRenderDrawColor(renderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), (Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));
179 SDL_RenderClear(renderer);
180 ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), renderer);
181 SDL_RenderPresent(renderer);
182 }
183
184 // Cleanup
185 ImGui_ImplSDLRenderer2_Shutdown();
186 ImGui_ImplSDL2_Shutdown();
187 ImGui::DestroyContext();
188
189 SDL_DestroyRenderer(renderer);
190 SDL_DestroyWindow(window);
191 SDL_Quit();
192
193 return 0;
194}