Change library to make it less buggy when destroying objects

# Conflicts:
#	include/window.hpp
#	src/window.cpp
This commit is contained in:
Pcornat 2025-06-06 17:11:10 +02:00
parent 32b6c523c3
commit bc12874c80
Signed by: Pcornat
GPG key ID: E0326CC678A00BDD
5 changed files with 71 additions and 52 deletions

View file

@ -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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
INTERFACE $<INSTALL_INTERFACE:include/windowGlfwGlLib>

View file

@ -0,0 +1,22 @@
#ifndef SCRATCHBSDF_CONTEXT_WINDOW_HPP
#define SCRATCHBSDF_CONTEXT_WINDOW_HPP
#include <GLFW/glfw3.h>
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

View file

@ -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<GLFWwindow, decltype(&delete_glfw_window)> wwindow{ nullptr, delete_glfw_window };
std::unique_ptr<GLFWwindow, decltype(&glfwDestroyWindow)> wwindow{ nullptr, glfwDestroyWindow };
modulesType modules;
bool valid{ false };
explicit Window(const bool debugOpengl,
const GLFWframebuffersizefun framebufferCallback,
GLFWwindow *shared,
std::initializer_list<modulesType::value_type> 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<Window> create_window(
const GLFWerrorfun errorCallback,
GLFWframebuffersizefun framebufferCallback,
const bool debugOpengl,
GLFWwindow *shared,
std::initializer_list<modulesType::value_type> initializer) noexcept;
~Window() noexcept;
~Window() noexcept = default;
[[nodiscard]] const std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)> &get_window() const noexcept;
[[nodiscard]] const std::unique_ptr<GLFWwindow, decltype(&glfwDestroyWindow)> &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;

16
src/context_window.cpp Normal file
View file

@ -0,0 +1,16 @@
#include "context_window.hpp"
#include <GLFW/glfw3.h>
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

View file

@ -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<GLFWwindow, decltype(&delete_glfw_window)>(
wwindow = std::unique_ptr<GLFWwindow, decltype(&glfwDestroyWindow)>(
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<const char *>(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<GLFWwindow, decltype(&Window::delete_glfw_window)> &Window::get_window() const noexcept {
const std::unique_ptr<GLFWwindow, decltype(&glfwDestroyWindow)> &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> Window::create_window(
const GLFWerrorfun errorCallback,
const GLFWframebuffersizefun framebufferCallback,
const bool debugOpengl,
GLFWwindow *shared,
const std::initializer_list<modulesType::value_type> 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 {