|
| 1 | +#include "backends/imgui_impl_opengl3.h" |
| 2 | +#include "backends/imgui_impl_sdl2.h" |
| 3 | +#include "gui.hpp" |
| 4 | +#include "imgui.h" |
| 5 | +#include "output.hpp" |
| 6 | + |
| 7 | +#include <SDL.h> |
| 8 | +#include <SDL_opengl.h> |
| 9 | +#include <array> |
| 10 | + |
| 11 | +using namespace steppable; |
| 12 | +using namespace steppable::gui::__internals; |
| 13 | + |
| 14 | +int main() |
| 15 | +{ |
| 16 | + // Setup SDL |
| 17 | + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) |
| 18 | + { |
| 19 | + steppable::output::error("Error: {0}\n", std::string(SDL_GetError())); |
| 20 | + return -1; |
| 21 | + } |
| 22 | + |
| 23 | + // Decide GL+GLSL versions |
| 24 | +#if defined(IMGUI_IMPL_OPENGL_ES2) |
| 25 | + // GL ES 2.0 + GLSL 100 |
| 26 | + const char* glsl_version = "#version 100"; |
| 27 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); |
| 28 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); |
| 29 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); |
| 30 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
| 31 | +#elif defined(__APPLE__) |
| 32 | + // GL 3.2 Core + GLSL 150 |
| 33 | + const char* glsl_version = "#version 150"; |
| 34 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac |
| 35 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 36 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 37 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); |
| 38 | +#else |
| 39 | + // GL 3.0 + GLSL 130 |
| 40 | + const char* glsl_version = "#version 130"; |
| 41 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); |
| 42 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 43 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 44 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
| 45 | +#endif |
| 46 | + |
| 47 | + // From 2.0.18: Enable native IME. |
| 48 | + SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); |
| 49 | + |
| 50 | + // Create window with graphics context |
| 51 | + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 52 | + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
| 53 | + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); |
| 54 | + auto window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); |
| 55 | + SDL_Window* window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags); |
| 56 | + if (window == nullptr) |
| 57 | + { |
| 58 | + printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); |
| 59 | + return -1; |
| 60 | + } |
| 61 | + |
| 62 | + SDL_GLContext gl_context = SDL_GL_CreateContext(window); |
| 63 | + SDL_GL_MakeCurrent(window, gl_context); |
| 64 | + SDL_GL_SetSwapInterval(1); // Enable vsync |
| 65 | + |
| 66 | + // Setup Dear ImGui context |
| 67 | + IMGUI_CHECKVERSION(); |
| 68 | + ImGui::CreateContext(); |
| 69 | + ImGuiIO& io = ImGui::GetIO(); |
| 70 | + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls |
| 71 | + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 72 | + |
| 73 | + // Setup Platform/Renderer backends |
| 74 | + ImGui_ImplSDL2_InitForOpenGL(window, gl_context); |
| 75 | + ImGui_ImplOpenGL3_Init(glsl_version); |
| 76 | + |
| 77 | + bool done = false; |
| 78 | + ImVec4 clear_color = ImVec4(0.22, 0.22, 0.22, 1.00); |
| 79 | + std::array<char, 100> buf{}; |
| 80 | + loadFonts(&io); |
| 81 | + |
| 82 | + while (not done) |
| 83 | + { |
| 84 | + // Poll and handle events (inputs, window resize, etc.) |
| 85 | + // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your |
| 86 | + // inputs. |
| 87 | + // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or |
| 88 | + // clear/overwrite your copy of the mouse data. |
| 89 | + // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or |
| 90 | + // clear/overwrite your copy of the keyboard data. Generally you may always pass all inputs to dear imgui, and |
| 91 | + // hide them from your application based on those two flags. |
| 92 | + SDL_Event event; |
| 93 | + while (SDL_PollEvent(&event) != 0) |
| 94 | + { |
| 95 | + ImGui_ImplSDL2_ProcessEvent(&event); |
| 96 | + if (event.type == SDL_QUIT) |
| 97 | + done = true; |
| 98 | + if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && |
| 99 | + event.window.windowID == SDL_GetWindowID(window)) |
| 100 | + done = true; |
| 101 | + } |
| 102 | + |
| 103 | + // Start the Dear ImGui frame |
| 104 | + ImGui_ImplOpenGL3_NewFrame(); |
| 105 | + ImGui_ImplSDL2_NewFrame(); |
| 106 | + ImGui::NewFrame(); |
| 107 | + |
| 108 | + ImGui::Begin("My Window"); |
| 109 | + ImGui::Text("Testing!"); |
| 110 | + ImGui::InputText("Input", buf.data(), buf.size()); |
| 111 | + ImGui::End(); |
| 112 | + |
| 113 | + if (isDarkModeEnabled()) |
| 114 | + { |
| 115 | + glClearColor(0.22, 0.22, 0.22, 1.0); |
| 116 | + ImGui::StyleColorsDark(); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + glClearColor(0.95, 0.95, 0.95, 1.0); |
| 121 | + ImGui::StyleColorsLight(); |
| 122 | + } |
| 123 | + |
| 124 | + // Rendering |
| 125 | + ImGui::Render(); |
| 126 | + glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); |
| 127 | + glClear(GL_COLOR_BUFFER_BIT); |
| 128 | + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); |
| 129 | + SDL_GL_SwapWindow(window); |
| 130 | + } |
| 131 | + |
| 132 | + // Cleanup |
| 133 | + ImGui_ImplOpenGL3_Shutdown(); |
| 134 | + ImGui_ImplSDL2_Shutdown(); |
| 135 | + ImGui::DestroyContext(); |
| 136 | + |
| 137 | + SDL_GL_DeleteContext(gl_context); |
| 138 | + SDL_DestroyWindow(window); |
| 139 | + SDL_Quit(); |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
0 commit comments