| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | #include "imgui.h" |
| 11 | #include "imgui_impl_sdl2.h" |
| 12 | #include "imgui_impl_opengl3.h" |
| 13 | #include <stdio.h> |
| 14 | #include <SDL.h> |
| 15 | #if defined(IMGUI_IMPL_OPENGL_ES2) |
| 16 | #include <SDL_opengles2.h> |
| 17 | #else |
| 18 | #include <SDL_opengl.h> |
| 19 | #endif |
| 20 | #ifdef _WIN32 |
| 21 | #include <windows.h> |
| 22 | #endif |
| 23 | |
| 24 | |
| 25 | #ifdef __EMSCRIPTEN__ |
| 26 | #include "../libs/emscripten/emscripten_mainloop_stub.h" |
| 27 | #endif |
| 28 | |
| 29 | |
| 30 | int main(int, char**) |
| 31 | { |
| 32 | |
| 33 | #ifdef _WIN32 |
| 34 | ::SetProcessDPIAware(); |
| 35 | #endif |
| 36 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) |
| 37 | { |
| 38 | printf("Error: %s\n", SDL_GetError()); |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | #if defined(IMGUI_IMPL_OPENGL_ES2) |
| 44 | |
| 45 | const char* glsl_version = "#version 100"; |
| 46 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); |
| 47 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); |
| 48 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); |
| 49 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
| 50 | #elif defined(IMGUI_IMPL_OPENGL_ES3) |
| 51 | |
| 52 | const char* glsl_version = "#version 300 es"; |
| 53 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); |
| 54 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); |
| 55 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 56 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
| 57 | #elif defined(__APPLE__) |
| 58 | |
| 59 | const char* glsl_version = "#version 150"; |
| 60 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); |
| 61 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 62 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 63 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); |
| 64 | #else |
| 65 | |
| 66 | const char* glsl_version = "#version 130"; |
| 67 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); |
| 68 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 69 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 70 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
| 71 | #endif |
| 72 | |
| 73 | |
| 74 | #ifdef SDL_HINT_IME_SHOW_UI |
| 75 | SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); |
| 76 | #endif |
| 77 | |
| 78 | |
| 79 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 80 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
| 81 | SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); |
| 82 | float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0); |
| 83 | SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); |
| 84 | SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(1280 * main_scale), (int)(800 * main_scale), window_flags); |
| 85 | if (window == nullptr) |
| 86 | { |
| 87 | printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | SDL_GLContext gl_context = SDL_GL_CreateContext(window); |
| 92 | if (gl_context == nullptr) |
| 93 | { |
| 94 | printf("Error: SDL_GL_CreateContext(): %s\n", SDL_GetError()); |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | SDL_GL_MakeCurrent(window, gl_context); |
| 99 | SDL_GL_SetSwapInterval(1); |
| 100 | |
| 101 | |
| 102 | IMGUI_CHECKVERSION(); |
| 103 | ImGui::CreateContext(); |
| 104 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 105 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 106 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 107 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 108 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | ImGui::StyleColorsDark(); |
| 114 | |
| 115 | |
| 116 | |
| 117 | ImGuiStyle& style = ImGui::GetStyle(); |
| 118 | style.ScaleAllSizes(main_scale); |
| 119 | style.FontScaleDpi = main_scale; |
| 120 | io.ConfigDpiScaleFonts = true; |
| 121 | io.ConfigDpiScaleViewports = true; |
| 122 | |
| 123 | |
| 124 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 125 | { |
| 126 | style.WindowRounding = 0.0f; |
| 127 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | ImGui_ImplSDL2_InitForOpenGL(window, gl_context); |
| 132 | ImGui_ImplOpenGL3_Init(glsl_version); |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | bool show_demo_window = true; |
| 155 | bool show_another_window = false; |
| 156 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 157 | |
| 158 | |
| 159 | bool done = false; |
| 160 | #ifdef __EMSCRIPTEN__ |
| 161 | |
| 162 | |
| 163 | io.IniFilename = nullptr; |
| 164 | EMSCRIPTEN_MAINLOOP_BEGIN |
| 165 | #else |
| 166 | while (!done) |
| 167 | #endif |
| 168 | { |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | SDL_Event event; |
| 175 | while (SDL_PollEvent(&event)) |
| 176 | { |
| 177 | ImGui_ImplSDL2_ProcessEvent(&event); |
| 178 | if (event.type == SDL_QUIT) |
| 179 | done = true; |
| 180 | if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) |
| 181 | done = true; |
| 182 | } |
| 183 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) |
| 184 | { |
| 185 | SDL_Delay(10); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | ImGui_ImplOpenGL3_NewFrame(); |
| 191 | ImGui_ImplSDL2_NewFrame(); |
| 192 | ImGui::NewFrame(); |
| 193 | |
| 194 | |
| 195 | if (show_demo_window) |
| 196 | ImGui::ShowDemoWindow(&show_demo_window); |
| 197 | |
| 198 | |
| 199 | { |
| 200 | static float f = 0.0f; |
| 201 | static int counter = 0; |
| 202 | |
| 203 | ImGui::Begin("Hello, world!"); |
| 204 | |
| 205 | ImGui::Text("This is some useful text."); |
| 206 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 207 | ImGui::Checkbox("Another Window", &show_another_window); |
| 208 | |
| 209 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 210 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 211 | |
| 212 | if (ImGui::Button("Button")) |
| 213 | counter++; |
| 214 | ImGui::SameLine(); |
| 215 | ImGui::Text("counter = %d", counter); |
| 216 | |
| 217 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 218 | ImGui::End(); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | if (show_another_window) |
| 223 | { |
| 224 | ImGui::Begin("Another Window", &show_another_window); |
| 225 | ImGui::Text("Hello from another window!"); |
| 226 | if (ImGui::Button("Close Me")) |
| 227 | show_another_window = false; |
| 228 | ImGui::End(); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | ImGui::Render(); |
| 233 | glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); |
| 234 | glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); |
| 235 | glClear(GL_COLOR_BUFFER_BIT); |
| 236 | ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 242 | { |
| 243 | SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow(); |
| 244 | SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext(); |
| 245 | ImGui::UpdatePlatformWindows(); |
| 246 | ImGui::RenderPlatformWindowsDefault(); |
| 247 | SDL_GL_MakeCurrent(backup_current_window, backup_current_context); |
| 248 | } |
| 249 | |
| 250 | SDL_GL_SwapWindow(window); |
| 251 | } |
| 252 | #ifdef __EMSCRIPTEN__ |
| 253 | EMSCRIPTEN_MAINLOOP_END; |
| 254 | #endif |
| 255 | |
| 256 | |
| 257 | ImGui_ImplOpenGL3_Shutdown(); |
| 258 | ImGui_ImplSDL2_Shutdown(); |
| 259 | ImGui::DestroyContext(); |
| 260 | |
| 261 | SDL_GL_DeleteContext(gl_context); |
| 262 | SDL_DestroyWindow(window); |
| 263 | SDL_Quit(); |
| 264 | |
| 265 | return 0; |
| 266 | } |