| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 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> |
| 22 | #endif |
| 23 | |
| 24 | |
| 25 | int main(int, char**) |
| 26 | { |
| 27 | |
| 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 | |
| 38 | #ifdef SDL_HINT_IME_SHOW_UI |
| 39 | SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); |
| 40 | #endif |
| 41 | |
| 42 | |
| 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); |
| 60 | |
| 61 | |
| 62 | IMGUI_CHECKVERSION(); |
| 63 | ImGui::CreateContext(); |
| 64 | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| 65 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 66 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 67 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 68 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | ImGui::StyleColorsDark(); |
| 74 | |
| 75 | |
| 76 | |
| 77 | ImGuiStyle& style = ImGui::GetStyle(); |
| 78 | style.ScaleAllSizes(main_scale); |
| 79 | style.FontScaleDpi = main_scale; |
| 80 | io.ConfigDpiScaleFonts = true; |
| 81 | io.ConfigDpiScaleViewports = true; |
| 82 | |
| 83 | |
| 84 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
| 85 | { |
| 86 | style.WindowRounding = 0.0f; |
| 87 | style.Colors[ImGuiCol_WindowBg].w = 1.0f; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | ImGui_ImplSDL2_InitForOpenGL(window, gl_context); |
| 92 | ImGui_ImplOpenGL2_Init(); |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 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 | |
| 118 | bool done = false; |
| 119 | while (!done) |
| 120 | { |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 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 | |
| 142 | ImGui_ImplOpenGL2_NewFrame(); |
| 143 | ImGui_ImplSDL2_NewFrame(); |
| 144 | ImGui::NewFrame(); |
| 145 | |
| 146 | |
| 147 | if (show_demo_window) |
| 148 | ImGui::ShowDemoWindow(&show_demo_window); |
| 149 | |
| 150 | |
| 151 | { |
| 152 | static float f = 0.0f; |
| 153 | static int counter = 0; |
| 154 | |
| 155 | ImGui::Begin("Hello, world!"); |
| 156 | |
| 157 | ImGui::Text("This is some useful text."); |
| 158 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 159 | ImGui::Checkbox("Another Window", &show_another_window); |
| 160 | |
| 161 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 162 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 163 | |
| 164 | if (ImGui::Button("Button")) |
| 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 | |
| 174 | if (show_another_window) |
| 175 | { |
| 176 | ImGui::Begin("Another Window", &show_another_window); |
| 177 | ImGui::Text("Hello from another window!"); |
| 178 | if (ImGui::Button("Close Me")) |
| 179 | show_another_window = false; |
| 180 | ImGui::End(); |
| 181 | } |
| 182 | |
| 183 | |
| 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 | |
| 189 | ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData()); |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 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 | |
| 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 | } |