From bc12874c801888eb60a2790eac4273e732636c7d Mon Sep 17 00:00:00 2001 From: Pcornat Date: Fri, 6 Jun 2025 17:11:10 +0200 Subject: [PATCH] Change library to make it less buggy when destroying objects # Conflicts: # include/window.hpp # src/window.cpp --- CMakeLists.txt | 4 +-- include/context_window.hpp | 22 +++++++++++++++ include/window.hpp | 26 ++++++++---------- src/context_window.cpp | 16 +++++++++++ src/window.cpp | 55 ++++++++++++++------------------------ 5 files changed, 71 insertions(+), 52 deletions(-) create mode 100644 include/context_window.hpp create mode 100644 src/context_window.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f46007..4a66c26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,8 +54,8 @@ elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") set(LINKER_OPTIMIZED_OPTIONS ${LINKER_OPTIMIZED_OPTIONS} -fdevirtualize-at-ltrans) endif () -add_library(${PROJECT_NAME} SHARED src/window.cpp include/window.hpp include/basic_data.hpp) -target_precompile_headers(${PROJECT_NAME} PUBLIC include/window.hpp include/basic_data.hpp) +add_library(${PROJECT_NAME} SHARED src/window.cpp src/context_window.cpp) +target_precompile_headers(${PROJECT_NAME} PUBLIC include/window.hpp include/basic_data.hpp include/context_window.hpp) target_include_directories(${PROJECT_NAME} PUBLIC $ INTERFACE $ diff --git a/include/context_window.hpp b/include/context_window.hpp new file mode 100644 index 0000000..b7fee92 --- /dev/null +++ b/include/context_window.hpp @@ -0,0 +1,22 @@ +#ifndef SCRATCHBSDF_CONTEXT_WINDOW_HPP +#define SCRATCHBSDF_CONTEXT_WINDOW_HPP + +#include + +namespace window { + class ContextWindow final { + private: + bool init{ false }; + + public: + ContextWindow() noexcept = delete; + + explicit ContextWindow(const GLFWerrorfun error_clbk) noexcept; + + ~ContextWindow() noexcept; + + [[nodiscard]] inline bool is_init() const { return init; } + }; +} // window + +#endif //SCRATCHBSDF_CONTEXT_WINDOW_HPP diff --git a/include/window.hpp b/include/window.hpp index 1510091..c847bd7 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -28,39 +28,33 @@ namespace gui { static void window_pos_callback(GLFWwindow *window, int xpos, int ypos); - static void delete_glfw_window(GLFWwindow *glfWwindow) { - glfwDestroyWindow(glfWwindow); - glfwTerminate(); - } - - static std::uint_fast8_t count_instance; - - std::unique_ptr wwindow{ nullptr, delete_glfw_window }; + std::unique_ptr wwindow{ nullptr, glfwDestroyWindow }; modulesType modules; + bool valid{ false }; + explicit Window(const bool debugOpengl, const GLFWframebuffersizefun framebufferCallback, GLFWwindow *shared, std::initializer_list initializer); public: - [[nodiscard]] static bool init_glfw(GLFWerrorfun errorCallback) noexcept; - Window() noexcept = delete; - Window(Window &&window) noexcept = default; + Window(const Window &) = delete; + + Window(Window &&window) noexcept; [[nodiscard]] static std::optional create_window( - const GLFWerrorfun errorCallback, GLFWframebuffersizefun framebufferCallback, const bool debugOpengl, GLFWwindow *shared, std::initializer_list initializer) noexcept; - ~Window() noexcept; + ~Window() noexcept = default; - [[nodiscard]] const std::unique_ptr &get_window() const noexcept; + [[nodiscard]] const std::unique_ptr &get_window() const noexcept; [[nodiscard]] bool should_close() const noexcept; @@ -75,7 +69,9 @@ namespace gui { */ void get_context() const noexcept; - Window &operator=(Window &&other) noexcept = default; + Window &operator=(const Window &) = delete; + + Window &operator=(Window &&other) noexcept; }; void color_10_bits() noexcept; diff --git a/src/context_window.cpp b/src/context_window.cpp new file mode 100644 index 0000000..8f81541 --- /dev/null +++ b/src/context_window.cpp @@ -0,0 +1,16 @@ +#include "context_window.hpp" +#include + +namespace window { + ContextWindow::~ContextWindow() noexcept { + glfwTerminate(); + } + + ContextWindow::ContextWindow(const GLFWerrorfun error_clbk) noexcept { + glfwSetErrorCallback(error_clbk); + if (glfwInit() == GLFW_FALSE) { + init = false; + } + init = true; + } +} // window diff --git a/src/window.cpp b/src/window.cpp index 7d7d401..f695e84 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -8,10 +8,6 @@ static void internalFramebufferCallback([[maybe_unused]] GLFWwindow *glfWwindow, } namespace gui { - static bool init = false; - - std::uint_fast8_t Window::count_instance = 0; - Window::Window(const bool debugOpengl, const GLFWframebuffersizefun framebufferCallback, GLFWwindow *shared, @@ -19,26 +15,24 @@ namespace gui { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, debugOpengl ? GLFW_TRUE : GLFW_FALSE); - wwindow = std::unique_ptr( + wwindow = std::unique_ptr( glfwCreateWindow(720, 1280, "Billy Sheet tracker", nullptr, shared), - delete_glfw_window - ); + glfwDestroyWindow); if (!wwindow) { - glfwTerminate(); - init = false; - count_instance = 0; + valid = false; return; } glfwMakeContextCurrent(wwindow.get()); glewExperimental = true; - if (glewInit() != GLEW_OK) { - wwindow.reset(nullptr); - glfwTerminate(); - init = false; - count_instance = 0; + if (const GLenum error = glewInit(); error != GLEW_OK) { + valid = false; + if (const auto error_callback = glfwSetErrorCallback(nullptr); error_callback != nullptr) { + error_callback(0, reinterpret_cast(glewGetErrorString(error))); + glfwSetErrorCallback(error_callback); + } return; } - ++count_instance; + valid = true; glfwSetFramebufferSizeCallback(wwindow.get(), framebufferCallback != nullptr ? framebufferCallback : internalFramebufferCallback); @@ -51,13 +45,7 @@ namespace gui { glfwSetWindowUserPointer(wwindow.get(), &modules); } - Window::~Window() noexcept { - if (count_instance > 0) { - --count_instance; - } - } - - const std::unique_ptr &Window::get_window() const noexcept { + const std::unique_ptr &Window::get_window() const noexcept { return wwindow; } @@ -111,30 +99,27 @@ namespace gui { glfwMakeContextCurrent(wwindow.get()); } + Window &Window::operator=(Window &&other) noexcept { + wwindow = std::move(other.wwindow); + modules = std::move(other.modules); + glfwSetWindowUserPointer(wwindow.get(), &modules); + return *this; + } + std::optional Window::create_window( - const GLFWerrorfun errorCallback, const GLFWframebuffersizefun framebufferCallback, const bool debugOpengl, GLFWwindow *shared, const std::initializer_list initializer ) noexcept { - if (!init) { - glfwSetErrorCallback(errorCallback); - if (glfwInit() == GLFW_FALSE) { - return std::nullopt; - } - init = true; - } if (Window win{ debugOpengl, framebufferCallback, shared, initializer }; win.get_window()) { return win; } return std::nullopt; } - bool Window::init_glfw(const GLFWerrorfun errorCallback) noexcept { - glfwSetErrorCallback(errorCallback); - init = glfwInit() == GLFW_TRUE; - return init; + Window::Window(Window &&window) noexcept : wwindow(std::move(window.wwindow)), modules(std::move(window.modules)) { + glfwSetWindowUserPointer(wwindow.get(), &modules); } void color_10_bits() noexcept {