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/demo.cpp | 77 +++++++++---------------------------------------- src/demo.hpp | 4 +-- src/gl_context.cpp | 32 ++++++++++++++++++++ src/gl_context.hpp | 31 ++++++++++++++++++++ src/imgui_context.cpp | 14 +++++++++ src/imgui_context.hpp | 16 ++++++++++ src/main.cpp | 2 +- src/sdl_context.cpp | 54 ++++++++++++++++++++++++++++++++++ src/sdl_context.hpp | 34 ++++++++++++++++++++++ src/sdl_main_window.cpp | 34 ++++++++++++++++++++++ src/sdl_main_window.hpp | 31 ++++++++++++++++++++ 11 files changed, 263 insertions(+), 66 deletions(-) create mode 100644 src/gl_context.cpp create mode 100644 src/gl_context.hpp create mode 100644 src/imgui_context.cpp create mode 100644 src/imgui_context.hpp create mode 100644 src/sdl_context.cpp create mode 100644 src/sdl_context.hpp create mode 100644 src/sdl_main_window.cpp create mode 100644 src/sdl_main_window.hpp (limited to 'src') diff --git a/src/demo.cpp b/src/demo.cpp index c571006..8e4181a 100644 --- a/src/demo.cpp +++ b/src/demo.cpp @@ -1,6 +1,10 @@ #include "demo.hpp" +#include "sdl_context.hpp" +#include "sdl_main_window.hpp" #include "stb_image.h" +#include "gl_context.hpp" +#include "imgui_context.hpp" #include #include @@ -13,7 +17,7 @@ #include #include -namespace bookworm { +namespace bookmouse { // Simple helper function to load an image into a OpenGL texture with common settings bool LoadTextureFromMemory(const void* data, size_t data_size, GLuint* out_texture, int* out_width, int* out_height) @@ -93,63 +97,13 @@ bool LoadTextureFromFile(const fud::String& filename, GLuint* out_texture, int* int demo(const fud::String& m_filename) { - // Setup SDL - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { - printf("Error: %s\n", SDL_GetError()); - return -1; - } - - // Decide GL+GLSL versions -#if defined(IMGUI_IMPL_OPENGL_ES2) - // GL ES 2.0 + GLSL 100 - const char* glsl_version = "#version 100"; - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); -#elif defined(__APPLE__) - // GL 3.2 Core + GLSL 150 - const char* glsl_version = "#version 150"; - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); -#else - // GL 3.0 + GLSL 130 - const char* glsl_version = "#version 130"; - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); -#endif - - // From 2.0.18: Enable native IME. -#ifdef SDL_HINT_IME_SHOW_UI - SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); -#endif + SdlContext sdlContext{}; + SdlMainWindow mainWindow{sdlContext}; // Create window with graphics context - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); - SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | - SDL_WINDOW_ALLOW_HIGHDPI); - SDL_Window* window = SDL_CreateWindow( - "Dear ImGui SDL2+OpenGL3 example", - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, - 1280, - 720, - window_flags); - if (window == nullptr) { - printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); - return -1; - } - SDL_GLContext gl_context = SDL_GL_CreateContext(window); - SDL_GL_MakeCurrent(window, gl_context); - SDL_GL_SetSwapInterval(1); // Enable vsync + GlContext glContext{mainWindow}; // Setup Dear ImGui context IMGUI_CHECKVERSION(); @@ -164,8 +118,8 @@ int demo(const fud::String& m_filename) // ImGui::StyleColorsLight(); // Setup Platform/Renderer backends - ImGui_ImplSDL2_InitForOpenGL(window, gl_context); - ImGui_ImplOpenGL3_Init(glsl_version); + ImGui_ImplSDL2_InitForOpenGL(mainWindow.window(), glContext.context()); + ImGui_ImplOpenGL3_Init(sdlContext.glslVersion()); int my_image_width = 0; int my_image_height = 0; @@ -226,10 +180,10 @@ int demo(const fud::String& m_filename) if (event.type == SDL_QUIT) done = true; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && - event.window.windowID == SDL_GetWindowID(window)) + event.window.windowID == SDL_GetWindowID(mainWindow.window())) done = true; } - if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) { + if (SDL_GetWindowFlags(mainWindow.window()) & SDL_WINDOW_MINIMIZED) { SDL_Delay(10); continue; } @@ -302,7 +256,7 @@ int demo(const fud::String& m_filename) clear_color.w); glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - SDL_GL_SwapWindow(window); + SDL_GL_SwapWindow(mainWindow.window()); } #ifdef __EMSCRIPTEN__ EMSCRIPTEN_MAINLOOP_END; @@ -313,12 +267,9 @@ int demo(const fud::String& m_filename) ImGui_ImplSDL2_Shutdown(); ImGui::DestroyContext(); - SDL_GL_DeleteContext(gl_context); - SDL_DestroyWindow(window); - SDL_Quit(); // return ImageResult::okay(image); return 0; } -} // namespace bookworm +} // namespace bookmouse diff --git a/src/demo.hpp b/src/demo.hpp index 0219af0..dcf9589 100644 --- a/src/demo.hpp +++ b/src/demo.hpp @@ -3,10 +3,10 @@ #include -namespace bookworm { +namespace bookmouse { int demo(const fud::String& m_filename); -} // namespace bookworm +} // namespace bookmouse #endif diff --git a/src/gl_context.cpp b/src/gl_context.cpp new file mode 100644 index 0000000..2858e22 --- /dev/null +++ b/src/gl_context.cpp @@ -0,0 +1,32 @@ +#include "gl_context.hpp" +#include + +namespace bookmouse { + +GlContext::GlContext(SdlMainWindow& mainWindow) +{ + m_context = SDL_GL_CreateContext(mainWindow.window()); + if (m_context == nullptr) { + auto error = SDL_GetError(); + throw std::runtime_error{error}; + } + + auto status = SDL_GL_MakeCurrent(mainWindow.window(), m_context); + if (status != 0) { + auto error = SDL_GetError(); + throw std::runtime_error{error}; + } + status = SDL_GL_SetSwapInterval(1); // Enable vsync + if (status != 0) { + auto error = SDL_GetError(); + throw std::runtime_error{error}; + } +} + +GlContext::~GlContext() +{ + SDL_GL_DeleteContext(m_context); +} + + +} // namespace bookmouse diff --git a/src/gl_context.hpp b/src/gl_context.hpp new file mode 100644 index 0000000..367e50d --- /dev/null +++ b/src/gl_context.hpp @@ -0,0 +1,31 @@ +#ifndef GL_CONTEXT_HPP +#define GL_CONTEXT_HPP + +#include "sdl_main_window.hpp" + +#include + +namespace bookmouse { + +class GlContext { + public: + explicit GlContext(SdlMainWindow& mainWindow); + GlContext(const GlContext&) = delete; + GlContext(GlContext&&) = delete; + ~GlContext(); + + GlContext& operator=(const GlContext&) = delete; + GlContext& operator=(GlContext&&) = delete; + + constexpr SDL_GLContext context() + { + return m_context; + } + + private: + SDL_GLContext m_context{nullptr}; +}; + +} // namespace bookmouse + +#endif diff --git a/src/imgui_context.cpp b/src/imgui_context.cpp new file mode 100644 index 0000000..7d972fd --- /dev/null +++ b/src/imgui_context.cpp @@ -0,0 +1,14 @@ +#include "imgui_context.hpp" + +namespace bookmouse { + +ImguiContext::ImguiContext() +{ +} + +ImguiContext::~ImguiContext() +{ +} + + +} // namespace bookmouse diff --git a/src/imgui_context.hpp b/src/imgui_context.hpp new file mode 100644 index 0000000..a625865 --- /dev/null +++ b/src/imgui_context.hpp @@ -0,0 +1,16 @@ +#ifndef IMGUI_CONTEXT_HPP +#define IMGUI_CONTEXT_HPP + +namespace bookmouse { + +class ImguiContext { + public: + ImguiContext(); + ~ImguiContext(); + + private: +}; + +} // namespace bookmouse + +#endif diff --git a/src/main.cpp b/src/main.cpp index 90aef42..5b4413e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) setupLogging(); fud::String filename{"Excel Saga Vol. 01/000.jpg"}; - return bookworm::demo(filename); + return bookmouse::demo(filename); #if 0 bookmouse::BookmouseApp bookmouse{}; 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 diff --git a/src/sdl_context.hpp b/src/sdl_context.hpp new file mode 100644 index 0000000..7b96df7 --- /dev/null +++ b/src/sdl_context.hpp @@ -0,0 +1,34 @@ +#ifndef SDL_CONTEXT_HPP +#define SDL_CONTEXT_HPP + +#include + +namespace bookmouse { + +class SdlContext { + public: + SdlContext() noexcept(false); + ~SdlContext(); + + int setAttribute(SDL_GLattr attr, int value) const; + + consteval const char* glslVersion() + { +#if defined(IMGUI_IMPL_OPENGL_ES2) + // GL ES 2.0 + GLSL 100 + return "#version 100"; +#elif defined(__APPLE__) + // GL 3.2 Core + GLSL 150 + return "#version 150"; +#else + // GL 3.0 + GLSL 130 + return "#version 130"; +#endif + } + + private: +}; + +} // namespace bookmouse + +#endif diff --git a/src/sdl_main_window.cpp b/src/sdl_main_window.cpp new file mode 100644 index 0000000..54b5388 --- /dev/null +++ b/src/sdl_main_window.cpp @@ -0,0 +1,34 @@ +#include "sdl_main_window.hpp" + +#include + +namespace bookmouse { + +SdlMainWindow::SdlMainWindow(const SdlContext& sdlContext) +{ + sdlContext.setAttribute(SDL_GL_DOUBLEBUFFER, 1); + sdlContext.setAttribute(SDL_GL_DEPTH_SIZE, 24); + sdlContext.setAttribute(SDL_GL_STENCIL_SIZE, 8); + + SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | + SDL_WINDOW_ALLOW_HIGHDPI); + m_window = SDL_CreateWindow( + "Dear ImGui SDL2+OpenGL3 example", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + 1280, + 720, + window_flags); + + if (m_window == nullptr) { + const auto* lastError = SDL_GetError(); + throw std::runtime_error{lastError}; + } +} + +SdlMainWindow::~SdlMainWindow() +{ + SDL_DestroyWindow(m_window); +} + +} // namespace bookmouse diff --git a/src/sdl_main_window.hpp b/src/sdl_main_window.hpp new file mode 100644 index 0000000..fbb9dd4 --- /dev/null +++ b/src/sdl_main_window.hpp @@ -0,0 +1,31 @@ +#ifndef SDL_MAIN_WINDOW_HPP +#define SDL_MAIN_WINDOW_HPP + +#include "sdl_context.hpp" + +#include + +namespace bookmouse { + +class SdlMainWindow { + public: + explicit SdlMainWindow(const SdlContext& sdlContext) noexcept(false); + SdlMainWindow(const SdlMainWindow&) = delete; + SdlMainWindow(SdlMainWindow&&) = delete; + ~SdlMainWindow(); + + SdlMainWindow& operator=(const SdlMainWindow&) = delete; + SdlMainWindow& operator=(SdlMainWindow&&) = delete; + + constexpr SDL_Window* window() + { + return m_window; + } + + private: + SDL_Window* m_window{nullptr}; +}; + +} // namespace bookmouse + +#endif -- cgit v1.2.3