10.1 KB、216行 生 Blame 履歴
1// Dear ImGui: standalone example application for SDL2 + OpenGL
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// **DO NOT USE THIS CODE IF YOUR CODE/ENGINE IS USING MODERN OPENGL (SHADERS, VBO, VAO, etc.)**
11// **Prefer using the code in the example_sdl2_opengl3/ folder**
12// See imgui_impl_sdl2.cpp for details.
13
14#include "imgui.h"
15#include "imgui_impl_sdl2.h"
16#include "imgui_impl_opengl2.h"
17#include <stdio.h>
18#include <SDL.h>
19#include <SDL_opengl.h>
20#ifdef _WIN32
21#include <windows.h> // SetProcessDPIAware()
22#endif
23
24// Main code
25int main(int, char**)
26{
27 // Setup SDL
28#ifdef _WIN32
29 ::SetProcessDPIAware();
30#endif
31 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
32 {
33 printf("Error: %s\n", SDL_GetError());
34 return 1;
35 }
36
37 // From 2.0.18: Enable native IME.
38#ifdef SDL_HINT_IME_SHOW_UI
39 SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
40#endif
41
42 // Setup window
43 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
44 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
45 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
46 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
47 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
48 float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
49 SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
50 SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(800 * main_scale), window_flags);
51 if (window == nullptr)
52 {
53 printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
54 return 1;
55 }
56
57 SDL_GLContext gl_context = SDL_GL_CreateContext(window);
58 SDL_GL_MakeCurrent(window, gl_context);
59 SDL_GL_SetSwapInterval(1); // Enable vsync
60
61 // Setup Dear ImGui context
62 IMGUI_CHECKVERSION();
63 ImGui::CreateContext();
64 ImGuiIO& io = ImGui::GetIO(); (void)io;
65 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
66 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
67 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
68 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
69 //io.ConfigViewportsNoAutoMerge = true;
70 //io.ConfigViewportsNoTaskBarIcon = true;
71
72 // Setup Dear ImGui style
73 ImGui::StyleColorsDark();
74 //ImGui::StyleColorsLight();
75
76 // Setup scaling
77 ImGuiStyle& style = ImGui::GetStyle();
78 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)
79 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)
80 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.
81 io.ConfigDpiScaleViewports = true; // [Experimental] Scale Dear ImGui and Platform Windows when Monitor DPI changes.
82
83 // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
84 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
85 {
86 style.WindowRounding = 0.0f;
87 style.Colors[ImGuiCol_WindowBg].w = 1.0f;
88 }
89
90 // Setup Platform/Renderer backends
91 ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
92 ImGui_ImplOpenGL2_Init();
93
94 // Load Fonts
95 // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
96 // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
97 // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
98 // - 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).
99 // - Read 'docs/FONTS.md' for more instructions and details.
100 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
101 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
102 //style.FontSizeBase = 20.0f;
103 //io.Fonts->AddFontDefaultVector();
104 //io.Fonts->AddFontDefaultBitmap();
105 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
106 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
107 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
108 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
109 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
110 //IM_ASSERT(font != nullptr);
111
112 // Our state
113 bool show_demo_window = true;
114 bool show_another_window = false;
115 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
116
117 // Main loop
118 bool done = false;
119 while (!done)
120 {
121 // Poll and handle events (inputs, window resize, etc.)
122 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
123 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
124 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
125 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
126 SDL_Event event;
127 while (SDL_PollEvent(&event))
128 {
129 ImGui_ImplSDL2_ProcessEvent(&event);
130 if (event.type == SDL_QUIT)
131 done = true;
132 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
133 done = true;
134 }
135 if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
136 {
137 SDL_Delay(10);
138 continue;
139 }
140
141 // Start the Dear ImGui frame
142 ImGui_ImplOpenGL2_NewFrame();
143 ImGui_ImplSDL2_NewFrame();
144 ImGui::NewFrame();
145
146 // 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!).
147 if (show_demo_window)
148 ImGui::ShowDemoWindow(&show_demo_window);
149
150 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
151 {
152 static float f = 0.0f;
153 static int counter = 0;
154
155 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
156
157 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
158 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
159 ImGui::Checkbox("Another Window", &show_another_window);
160
161 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
162 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
163
164 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
165 counter++;
166 ImGui::SameLine();
167 ImGui::Text("counter = %d", counter);
168
169 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
170 ImGui::End();
171 }
172
173 // 3. Show another simple window.
174 if (show_another_window)
175 {
176 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)
177 ImGui::Text("Hello from another window!");
178 if (ImGui::Button("Close Me"))
179 show_another_window = false;
180 ImGui::End();
181 }
182
183 // Rendering
184 ImGui::Render();
185 glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
186 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
187 glClear(GL_COLOR_BUFFER_BIT);
188 //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context where shaders may be bound
189 ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
190
191 // Update and Render additional Platform Windows
192 // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
193 // For this specific demo app we could also call SDL_GL_MakeCurrent(window, gl_context) directly)
194 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
195 {
196 SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
197 SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
198 ImGui::UpdatePlatformWindows();
199 ImGui::RenderPlatformWindowsDefault();
200 SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
201 }
202
203 SDL_GL_SwapWindow(window);
204 }
205
206 // Cleanup
207 ImGui_ImplOpenGL2_Shutdown();
208 ImGui_ImplSDL2_Shutdown();
209 ImGui::DestroyContext();
210
211 SDL_GL_DeleteContext(gl_context);
212 SDL_DestroyWindow(window);
213 SDL_Quit();
214
215 return 0;
216}