From 66e43ba3337930770d9cf156421ef9f33a4bbd62 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Mon, 5 Jan 2026 22:24:00 +0100 Subject: [PATCH] Adding glew as OpenGL initialization in a different scope --- CMakeLists.txt | 7 ++++++- include/opengl_context.hpp | 16 ++++++++++++++++ include/window.hpp | 6 +++--- src/opengl_context.cpp | 33 +++++++++++++++++++++++++++++++++ src/window.cpp | 17 ++--------------- 5 files changed, 60 insertions(+), 19 deletions(-) create mode 100644 include/opengl_context.hpp create mode 100644 src/opengl_context.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a66c26..99ea42c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,12 @@ 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 src/context_window.cpp) +add_library(${PROJECT_NAME} SHARED + src/window.cpp + src/context_window.cpp + src/opengl_context.cpp + include/opengl_context.hpp +) target_precompile_headers(${PROJECT_NAME} PUBLIC include/window.hpp include/basic_data.hpp include/context_window.hpp) target_include_directories(${PROJECT_NAME} PUBLIC $ diff --git a/include/opengl_context.hpp b/include/opengl_context.hpp new file mode 100644 index 0000000..a673fd6 --- /dev/null +++ b/include/opengl_context.hpp @@ -0,0 +1,16 @@ +// +// Created by postaron on 05/01/2026. +// + +#ifndef WINDOWGLFWGLLIB_OPENGL_CONTEXT_HPP +#define WINDOWGLFWGLLIB_OPENGL_CONTEXT_HPP + +namespace gui { + class Window; +} + +namespace opengl { + [[nodiscard]] bool init_glew(const gui::Window &win) noexcept; +} + +#endif //WINDOWGLFWGLLIB_OPENGL_CONTEXT_HPP diff --git a/include/window.hpp b/include/window.hpp index c847bd7..5f006a1 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -16,6 +16,8 @@ namespace gui { public: using modulesType = std::unordered_map; using windowPtr = GLFWwindow *; + static constexpr int default_width{ 1280 }; + static constexpr int default_height{ 720 }; private: static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods); @@ -32,8 +34,6 @@ namespace gui { modulesType modules; - bool valid{ false }; - explicit Window(const bool debugOpengl, const GLFWframebuffersizefun framebufferCallback, GLFWwindow *shared, @@ -76,7 +76,7 @@ namespace gui { void color_10_bits() noexcept; - bool check_color_depth(int color_depth) noexcept; + [[nodiscard]] bool check_color_depth(int color_depth) noexcept; void opengl_debug() noexcept; } diff --git a/src/opengl_context.cpp b/src/opengl_context.cpp new file mode 100644 index 0000000..043da1b --- /dev/null +++ b/src/opengl_context.cpp @@ -0,0 +1,33 @@ +// +// Created by postaron on 05/01/2026. +// + +#include + +#if defined( _WIN32 ) + +#include + +#endif + +#include "opengl_context.hpp" +#include + +#include "window.hpp" + + +bool opengl::init_glew(const gui::Window &win) noexcept { + win.get_context(); + if (const GLenum error = glewInit(); error != GLEW_OK) { + if (const auto error_callback = glfwSetErrorCallback(nullptr); error_callback != nullptr) { + error_callback(0, reinterpret_cast(glewGetErrorString(error))); + glfwSetErrorCallback(error_callback); + } else { + std::cerr << "Error initializing glew: " + << reinterpret_cast(glewGetErrorString(error)) + << std::endl; + } + return false; + } + return true; +} diff --git a/src/window.cpp b/src/window.cpp index acf152f..56eaa5f 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -19,23 +19,10 @@ namespace gui { glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, debugOpengl ? GLFW_TRUE : GLFW_FALSE); wwindow = std::unique_ptr( - glfwCreateWindow(720, 1280, "Billy Sheet tracker", nullptr, shared), + glfwCreateWindow(1280, 720, "Billy Sheet tracker", nullptr, shared), glfwDestroyWindow); - if (!wwindow) { - valid = false; - return; - } + glfwMakeContextCurrent(wwindow.get()); - glewExperimental = true; - 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; - } - valid = true; glfwSetFramebufferSizeCallback(wwindow.get(), framebufferCallback != nullptr ? framebufferCallback : internalFramebufferCallback);