diff --git a/src/video/CMakeLists.txt b/src/video/CMakeLists.txt index 8fde9884..3df97d4d 100644 --- a/src/video/CMakeLists.txt +++ b/src/video/CMakeLists.txt @@ -3,11 +3,13 @@ project(Video) add_library(Video STATIC interface/video/Video.hpp src/Video.cpp + interface/video/ScopedImguiContext.hpp $,src/Video_PSP.cpp,> $,src/Video_Desktop.cpp,> $,src/Video_Desktop.cpp,> $,src/Video_Darwin.cpp,> $,src/Video_Android.cpp,> + $,src/ScopedImguiContext.cpp,src/ScopedImguiContext_Dummy.cpp> ) target_include_directories(Video diff --git a/src/video/interface/video/ScopedImguiContext.hpp b/src/video/interface/video/ScopedImguiContext.hpp new file mode 100644 index 00000000..e0eafc6b --- /dev/null +++ b/src/video/interface/video/ScopedImguiContext.hpp @@ -0,0 +1,9 @@ +#pragma once + +struct SDL_Window; + +class ScopedImguiContext { +public: + ScopedImguiContext(SDL_Window *window, void *gl_context); + ~ScopedImguiContext(); +}; \ No newline at end of file diff --git a/src/video/src/ScopedImguiContext.cpp b/src/video/src/ScopedImguiContext.cpp new file mode 100644 index 00000000..563cae30 --- /dev/null +++ b/src/video/src/ScopedImguiContext.cpp @@ -0,0 +1,29 @@ +#include "video/ScopedImguiContext.hpp" + +#include "imgui.h" +#include "imgui_impl_sdl2.h" +#include "imgui_impl_opengl2.h" + +#include + +ScopedImguiContext::ScopedImguiContext(SDL_Window *window, void *gl_context) { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO &io = ImGui::GetIO(); + (void) io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; + // Setup Dear ImGui style + ImGui::StyleColorsDark(); + // Setup Platform/Renderer backends + if (!ImGui_ImplSDL2_InitForOpenGL(window, gl_context) || + !ImGui_ImplOpenGL2_Init()) { + throw std::runtime_error("Failed to initialize imgui with SDL2 + OpenGL"); + } +} + +ScopedImguiContext::~ScopedImguiContext() { + ImGui_ImplOpenGL2_Shutdown(); + ImGui_ImplSDL2_Shutdown(); + ImGui::DestroyContext(); +} diff --git a/src/video/src/ScopedImguiContext_Dummy.cpp b/src/video/src/ScopedImguiContext_Dummy.cpp new file mode 100644 index 00000000..fc618e9c --- /dev/null +++ b/src/video/src/ScopedImguiContext_Dummy.cpp @@ -0,0 +1,5 @@ +#include "video/ScopedImguiContext.hpp" + +ScopedImguiContext::ScopedImguiContext(SDL_Window *window, void *gl_context) { } + +ScopedImguiContext::~ScopedImguiContext() { } diff --git a/src/video/src/Video_Darwin.cpp b/src/video/src/Video_Darwin.cpp index 441a2896..d172bdea 100644 --- a/src/video/src/Video_Darwin.cpp +++ b/src/video/src/Video_Darwin.cpp @@ -3,6 +3,7 @@ #include "time/Timestep.hpp" #include "logger/log.h" #include "glad/glad.h" +#include "video/ScopedImguiContext.hpp" #include #include @@ -12,6 +13,7 @@ struct PlatformSpecific { SDL_Window* window; void* gl_context; + std::unique_ptr imgui; }; Video::~Video() = default; // For pimpl idiom. @@ -25,6 +27,7 @@ void Video::tear_down_gl() _platform_specific->window = nullptr; _platform_specific->gl_context = nullptr; + _platform_specific->imgui = nullptr; } void Video::swap_buffers() const @@ -119,6 +122,9 @@ bool Video::setup_gl() DebugGlCall(glDisable(GL_NORMALIZE)); DebugGlCall(glDisable(GL_RESCALE_NORMAL)); + log_info("Initializing imgui"); + _platform_specific->imgui = std::make_unique(_platform_specific->window, _platform_specific->gl_context); + log_info("Exiting Video::setup_gl, success."); return true; } diff --git a/src/video/src/Video_Desktop.cpp b/src/video/src/Video_Desktop.cpp index 21fa2ed1..77dd45e2 100644 --- a/src/video/src/Video_Desktop.cpp +++ b/src/video/src/Video_Desktop.cpp @@ -3,46 +3,12 @@ #include "time/Timestep.hpp" #include "logger/log.h" #include "glad/glad.h" +#include "video/ScopedImguiContext.hpp" #include #include #include -#if defined(SPELUNKY_PSP_WITH_IMGUI) -#include "imgui.h" -#include "imgui_impl_sdl2.h" -#include "imgui_impl_opengl2.h" - -namespace { - class Imgui { - public: - Imgui(SDL_Window *window, void *gl_context) { - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO &io = ImGui::GetIO(); - (void) io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls - // Setup Dear ImGui style - ImGui::StyleColorsDark(); - // Setup Platform/Renderer backends - if (!ImGui_ImplSDL2_InitForOpenGL(window, gl_context) || - !ImGui_ImplOpenGL2_Init()) { - throw std::runtime_error("Failed to initialize imgui with SDL2 + OpenGL"); - } - } - - ~Imgui() { - ImGui_ImplOpenGL2_Shutdown(); - ImGui_ImplSDL2_Shutdown(); - ImGui::DestroyContext(); - } - }; -} -#else -struct Imgui { Imgui(SDL_Window *window, void *gl_context) {}}; -#endif - namespace { std::pair query_screen_dimensions() { @@ -59,7 +25,7 @@ struct PlatformSpecific { SDL_Window* window; void* gl_context; - std::unique_ptr imgui; + std::unique_ptr imgui; }; Video::~Video() = default; // For pimpl idiom. @@ -176,7 +142,7 @@ bool Video::setup_gl() DebugGlCall(glDisable(GL_RESCALE_NORMAL)); log_info("Initializing imgui"); - _platform_specific->imgui = std::make_unique(_platform_specific->window, _platform_specific->gl_context); + _platform_specific->imgui = std::make_unique(_platform_specific->window, _platform_specific->gl_context); log_info("Exiting Video::setup_gl, success."); return true;