From b4a17e3a28f31217c79faa160f5e6abd720da054 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Mon, 23 Sep 2024 23:53:01 -0500 Subject: Applying RAII types --- src/sdl_context.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/sdl_context.cpp (limited to 'src/sdl_context.cpp') diff --git a/src/sdl_context.cpp b/src/sdl_context.cpp new file mode 100644 index 0000000..f52bbe9 --- /dev/null +++ b/src/sdl_context.cpp @@ -0,0 +1,54 @@ +#include "sdl_context.hpp" + +#include +#include +#include + +namespace bookmouse { + +SdlContext::SdlContext() { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { + const auto* lastError = SDL_GetError(); + throw std::runtime_error{lastError}; + } + + // Decide GL+GLSL versions +#if defined(IMGUI_IMPL_OPENGL_ES2) + // GL ES 2.0 + GLSL 100 + setAttribute(SDL_GL_CONTEXT_FLAGS, 0); + setAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + setAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + setAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); +#elif defined(__APPLE__) + // GL 3.2 Core + GLSL 150 + setAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac + setAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + setAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + setAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); +#else + // GL 3.0 + GLSL 130 + setAttribute(SDL_GL_CONTEXT_FLAGS, 0); + setAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + setAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + setAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); +#endif + + // From 2.0.18: Enable native IME. +#ifdef SDL_HINT_IME_SHOW_UI + auto hintSet = SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); + assert(hintSet == SDL_TRUE); +#endif +} + +SdlContext::~SdlContext() { + SDL_Quit(); +} + +int SdlContext::setAttribute(SDL_GLattr attr, int value) const +{ + auto result = SDL_GL_SetAttribute(attr, value); + assert(result == 0); + return result; +} + +} // namespace bookmouse -- cgit v1.2.3