summaryrefslogtreecommitdiff
path: root/src/sdl_context.cpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-09-23 23:53:01 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-09-23 23:53:01 -0500
commitb4a17e3a28f31217c79faa160f5e6abd720da054 (patch)
tree9e95862b192aa6c14b86a820ef77f290e46969c8 /src/sdl_context.cpp
parentcae8b633fc8723bcc35944298335ad48844d2bf0 (diff)
Applying RAII types
Diffstat (limited to 'src/sdl_context.cpp')
-rw-r--r--src/sdl_context.cpp54
1 files changed, 54 insertions, 0 deletions
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 <SDL.h>
+#include <cassert>
+#include <stdexcept>
+
+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