Change library to make it less buggy when destroying objects
# Conflicts: # include/window.hpp # src/window.cpp
This commit is contained in:
parent
32b6c523c3
commit
bc12874c80
5 changed files with 71 additions and 52 deletions
16
src/context_window.cpp
Normal file
16
src/context_window.cpp
Normal 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
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue