From cda9950d33f6aaadaa912310d2d39b0cf976e86e Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Thu, 11 Jun 2026 04:21:53 -0500 Subject: [PATCH] Mild updates to Window and other pieces to match the rest --- engine/NBGraphics/GLLoad.hpp | 26 ------------------- engine/NBGraphics/Window.hpp | 30 +++++++++++++++++++--- engine/NBGraphics/src/Window.cpp | 35 ++++++++++++++++++-------- engine/NBGraphics/tests/CMakeLists.txt | 4 +-- engine/NBGraphics/tests/TestWindow.cpp | 14 ++++++++++- 5 files changed, 66 insertions(+), 43 deletions(-) diff --git a/engine/NBGraphics/GLLoad.hpp b/engine/NBGraphics/GLLoad.hpp index 7989fb3..8158ecd 100644 --- a/engine/NBGraphics/GLLoad.hpp +++ b/engine/NBGraphics/GLLoad.hpp @@ -5,30 +5,4 @@ #include #include -#include - -namespace NB { - -static unsigned int __NB_GL_DEBUG_ERROR_CODE__; - -static std::string formatDebugString(const std::string& msg, const std::string& file="", int line=-1) { - std::string ret = ""; - if (file != "") { - ret += "In file " + file; - if (line >= 0) { - ret += " at line " + std::to_string(line); - } - ret += ":\n\t"; - } - return ret + msg; -} - -} - -#define NB_GL_DEBUG_THROW(cmd, when) while(NB::__NB_GL_DEBUG_ERROR_CODE__=glGetError()){\ - std::cout << "[GL ERROR]: " << NB::__NB_GL_DEBUG_ERROR_CODE__;\ - std::cout << " " << when << " " << cmd << "\n";\ -} -#define NB_GL_DEBUG(cmd) NB_GL_DEBUG_THROW(#cmd, "before"); cmd; NB_GL_DEBUG_THROW(#cmd, "after"); - #endif \ No newline at end of file diff --git a/engine/NBGraphics/Window.hpp b/engine/NBGraphics/Window.hpp index 88e84df..36e1d4b 100644 --- a/engine/NBGraphics/Window.hpp +++ b/engine/NBGraphics/Window.hpp @@ -9,6 +9,7 @@ #include #include +#include #include namespace nb { @@ -18,9 +19,32 @@ public: GLError(const std::string&); }; -class WindowError : public std::runtime_error { -public: - WindowError(const std::string&); +class OpenGLError : public Error { + using Base = Error; + + public: + using Base::Base; + + enum Codes : unsigned int { + UNDEFINED, INIT_FAILED, GLFW_INTIALIZED, GLAD_FAILED + }; + + static const std::string type; + static const ErrorCodeMap ErrorMessages; +}; + +class WindowError : public Error { + using Base = Error; + + public: + using Base::Base; + + enum Codes : unsigned int { + UNDEFINED, INITIALIZED_WINDOW, NO_GLFW, INIT_FAILED + }; + + static const std::string type; + static const ErrorCodeMap ErrorMessages; }; class Window { diff --git a/engine/NBGraphics/src/Window.cpp b/engine/NBGraphics/src/Window.cpp index adceaf7..5229af1 100644 --- a/engine/NBGraphics/src/Window.cpp +++ b/engine/NBGraphics/src/Window.cpp @@ -16,9 +16,25 @@ static std::map defailt_window_hints = { #endif }; -GLError::GLError(const std::string& msg) : std::runtime_error(msg) {} +using OpenGLErrorCodes = OpenGLError::Codes; +const std::string OpenGLError::type = "nb::OpenGLError"; +const ErrorCodeMap OpenGLError::ErrorMessages = { + {OpenGLErrorCodes::UNDEFINED, "Error"}, + {OpenGLErrorCodes::INIT_FAILED, "GLFW initialization failed"}, + {OpenGLErrorCodes::GLFW_INTIALIZED, "GLFW has already been initialized"}, + {OpenGLErrorCodes::GLAD_FAILED, "GLAD initialization failed"} +}; -WindowError::WindowError(const std::string &msg) : std::runtime_error(msg) {} +using WindowErrorCodes = WindowError::Codes; +const std::string WindowError::type = "nb::WindowError"; +const ErrorCodeMap WindowError::ErrorMessages = { + {WindowErrorCodes::UNDEFINED, "Error"}, + {WindowErrorCodes::INITIALIZED_WINDOW, "Window already initialized"}, + {WindowErrorCodes::NO_GLFW, "GLFW has not been initialized"}, + {WindowErrorCodes::INIT_FAILED, "Could not intialized window"} +}; + +GLError::GLError(const std::string& msg) : std::runtime_error(msg) {} int Window::getGLFWHint(int hint_key) { if (GLFWHints.find(hint_key) == GLFWHints.end()) { @@ -30,7 +46,7 @@ int Window::getGLFWHint(int hint_key) { int Window::setGLFWHint(int hint_key, int hint_val) { if (Window::_glfw_init) { - throw GLError("Cannot set GLFW hint after window is initialized."); + THROW(OpenGLError(OpenGLErrorCodes::GLFW_INTIALIZED)); } else { GLFWHints[hint_key] = hint_val; } @@ -60,7 +76,7 @@ Window::Window(const uint16_t x, const uint16_t y, const char* initName, GLFWmon Window::_glfw_init = true; } else { if (Window::StrictInitialization) { - throw GLError("Failed to load GLFW with glfwInit()="+std::to_string(glfwResponse)+"."); + THROW(OpenGLError(OpenGLErrorCodes::INIT_FAILED)); } } } @@ -103,7 +119,7 @@ int Window::getWindowHint(int hint_key) const { int Window::setWindowHint(int hint_key, int hint_val) { if (_init) { - throw WindowError("Cannot set window hint after window is initialized."); + THROW(WindowError(WindowErrorCodes::INITIALIZED_WINDOW)); } else { windowHints[hint_key] = hint_val; } @@ -112,7 +128,7 @@ int Window::setWindowHint(int hint_key, int hint_val) { int Window::init() { if (!_glfw_init) { - throw GLError("GLFW has not been initialized."); + THROW(WindowError(WindowErrorCodes::NO_GLFW)); } for (const auto& hint : windowHints) { glfwWindowHint(hint.first, hint.second); @@ -123,7 +139,7 @@ int Window::init() { Window::WindowContexts.erase(window); if (Window::WindowContexts.size()==0 && Window::_glfw_init) { glfwTerminate(); } if (Window::StrictInitialization) { - throw WindowError("Could not create window in glfwCreateWindow()."); + THROW(WindowError(WindowErrorCodes::INIT_FAILED)); } } glfwMakeContextCurrent(window); @@ -133,10 +149,7 @@ int Window::init() { if (!gladResponse) { Window::checkKillGLFW(); if (Window::StrictInitialization) { - throw GLError("Failed to load GLAD with \ - gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)="+\ - std::to_string(gladResponse)+"." - ); + THROW(OpenGLError(OpenGLErrorCodes::GLAD_FAILED)); } } diff --git a/engine/NBGraphics/tests/CMakeLists.txt b/engine/NBGraphics/tests/CMakeLists.txt index e352a10..62ecefc 100644 --- a/engine/NBGraphics/tests/CMakeLists.txt +++ b/engine/NBGraphics/tests/CMakeLists.txt @@ -9,8 +9,8 @@ if (NB_BUILD_TESTS) target_link_libraries(TestWindow NBCore NBGraphics - GTest::gtest_main +# GTest::gtest_main ) - gtest_discover_tests(TestWindow) +# gtest_discover_tests(TestWindow) endif() \ No newline at end of file diff --git a/engine/NBGraphics/tests/TestWindow.cpp b/engine/NBGraphics/tests/TestWindow.cpp index d7df069..03a7c0e 100644 --- a/engine/NBGraphics/tests/TestWindow.cpp +++ b/engine/NBGraphics/tests/TestWindow.cpp @@ -1,6 +1,18 @@ -#include +#include "GLLoad.hpp" +// #define _NB_AUTOLOG +#include "VertexArray.hpp" +#include "Window.hpp" + int main() { + nb::logger.log("Howdy!"); + nb::Window window(200, 200, "Hello!"); + window.setWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + window.setWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + window.init(); + + GLFWwindow* window_ptr = window.getWindow(); + while(!glfwWindowShouldClose(window_ptr)) {} return 0; } \ No newline at end of file