Skip to content

Commit

Permalink
Moved Imgui context creation into a dedicated file; Initializing imgu…
Browse files Browse the repository at this point in the history
…i context on Darwin
  • Loading branch information
dbeef committed Aug 25, 2024
1 parent 0cae815 commit 6e386db
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 37 deletions.
2 changes: 2 additions & 0 deletions src/video/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ project(Video)
add_library(Video STATIC
interface/video/Video.hpp
src/Video.cpp
interface/video/ScopedImguiContext.hpp
$<IF:$<BOOL:${SPELUNKY_PSP_PLATFORM_PSP}>,src/Video_PSP.cpp,>
$<IF:$<BOOL:${SPELUNKY_PSP_PLATFORM_WINDOWS}>,src/Video_Desktop.cpp,>
$<IF:$<BOOL:${SPELUNKY_PSP_PLATFORM_LINUX}>,src/Video_Desktop.cpp,>
$<IF:$<BOOL:${SPELUNKY_PSP_PLATFORM_DARWIN}>,src/Video_Darwin.cpp,>
$<IF:$<BOOL:${SPELUNKY_PSP_PLATFORM_ANDROID}>,src/Video_Android.cpp,>
$<IF:$<BOOL:${SPELUNKY_PSP_WITH_IMGUI}>,src/ScopedImguiContext.cpp,src/ScopedImguiContext_Dummy.cpp>
)

target_include_directories(Video
Expand Down
9 changes: 9 additions & 0 deletions src/video/interface/video/ScopedImguiContext.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

struct SDL_Window;

class ScopedImguiContext {
public:
ScopedImguiContext(SDL_Window *window, void *gl_context);
~ScopedImguiContext();
};
29 changes: 29 additions & 0 deletions src/video/src/ScopedImguiContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "video/ScopedImguiContext.hpp"

#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_opengl2.h"

#include <stdexcept>

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();
}
5 changes: 5 additions & 0 deletions src/video/src/ScopedImguiContext_Dummy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "video/ScopedImguiContext.hpp"

ScopedImguiContext::ScopedImguiContext(SDL_Window *window, void *gl_context) { }

ScopedImguiContext::~ScopedImguiContext() { }
6 changes: 6 additions & 0 deletions src/video/src/Video_Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "time/Timestep.hpp"
#include "logger/log.h"
#include "glad/glad.h"
#include "video/ScopedImguiContext.hpp"

#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
Expand All @@ -12,6 +13,7 @@ struct PlatformSpecific
{
SDL_Window* window;
void* gl_context;
std::unique_ptr<ScopedImguiContext> imgui;
};

Video::~Video() = default; // For pimpl idiom.
Expand All @@ -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
Expand Down Expand Up @@ -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<ScopedImguiContext>(_platform_specific->window, _platform_specific->gl_context);

log_info("Exiting Video::setup_gl, success.");
return true;
}
Expand Down
40 changes: 3 additions & 37 deletions src/video/src/Video_Desktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,12 @@
#include "time/Timestep.hpp"
#include "logger/log.h"
#include "glad/glad.h"
#include "video/ScopedImguiContext.hpp"

#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <stdexcept>

#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<int, int> query_screen_dimensions() {
Expand All @@ -59,7 +25,7 @@ struct PlatformSpecific
{
SDL_Window* window;
void* gl_context;
std::unique_ptr<Imgui> imgui;
std::unique_ptr<ScopedImguiContext> imgui;
};

Video::~Video() = default; // For pimpl idiom.
Expand Down Expand Up @@ -176,7 +142,7 @@ bool Video::setup_gl()
DebugGlCall(glDisable(GL_RESCALE_NORMAL));

log_info("Initializing imgui");
_platform_specific->imgui = std::make_unique<Imgui>(_platform_specific->window, _platform_specific->gl_context);
_platform_specific->imgui = std::make_unique<ScopedImguiContext>(_platform_specific->window, _platform_specific->gl_context);

log_info("Exiting Video::setup_gl, success.");
return true;
Expand Down

0 comments on commit 6e386db

Please sign in to comment.