diff options
Diffstat (limited to 'source/gui.cpp')
-rw-r--r-- | source/gui.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/source/gui.cpp b/source/gui.cpp new file mode 100644 index 0000000..15beb90 --- /dev/null +++ b/source/gui.cpp @@ -0,0 +1,92 @@ +/** + * @file gui.cpp + * @brief Contains code for GUI-related logic. + * + * Copyright (C) 2021 Clyne Sullivan + * + * Distributed under the GNU GPL v3 or later. You should have received a copy of + * the GNU General Public License along with this program. + * If not, see <https://www.gnu.org/licenses/>. + */ + +#include "imgui.h" +#include "backends/imgui_impl_sdl.h" +#include "backends/imgui_impl_opengl2.h" + +#include "config.h" + +#include <SDL2/SDL.h> +#include <SDL2/SDL_opengl.h> + +ImFont *font = nullptr; +static ImGuiIO *io = nullptr; +static SDL_Window *window = nullptr; +static decltype(SDL_GL_CreateContext(nullptr)) gl_context; + +bool guiInitialize() +{ + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + printf("Error: %s\n", SDL_GetError()); + return false; + } + + // Setup window + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); + window = SDL_CreateWindow("stmdsp gui", + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + WINDOW_WIDTH, WINDOW_HEIGHT, + SDL_WINDOW_OPENGL /*| SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI*/); + gl_context = SDL_GL_CreateContext(window); + SDL_GL_MakeCurrent(window, gl_context); + SDL_GL_SetSwapInterval(1); // Enable vsync + + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + io = &ImGui::GetIO(); + io->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + font = io->Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf", 20); + ImGui::StyleColorsLight(); + + ImGui_ImplSDL2_InitForOpenGL(window, gl_context); + ImGui_ImplOpenGL2_Init(); + + return true; +} + +void guiRender(void (*func)()) +{ + glViewport(0, 0, (int)io->DisplaySize.x, (int)io->DisplaySize.y); + glClearColor(1, 1, 1, 1); + glClear(GL_COLOR_BUFFER_BIT); + func(); + SDL_GL_SwapWindow(window); +} + +void guiHandleEvents(bool& done) +{ + SDL_Event event; + for (SDL_Event event; SDL_PollEvent(&event);) { + ImGui_ImplSDL2_ProcessEvent(&event); + if (event.type == SDL_QUIT) + done = true; + if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) + done = true; + } +} + +void guiShutdown() +{ + ImGui_ImplOpenGL2_Shutdown(); + ImGui_ImplSDL2_Shutdown(); + ImGui::DestroyContext(); + + SDL_GL_DeleteContext(gl_context); + SDL_DestroyWindow(window); + SDL_Quit(); +} + |