Mild updates to Window and other pieces to match the rest

This commit is contained in:
NaifBanana 2026-06-11 04:21:53 -05:00
parent ea632614b0
commit 1efe2a0a7c
5 changed files with 66 additions and 43 deletions

View File

@ -5,30 +5,4 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <string>
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 #endif

View File

@ -9,6 +9,7 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <NBCore/Errors.hpp>
#include <NBCore/Utils.hpp> #include <NBCore/Utils.hpp>
namespace nb { namespace nb {
@ -18,9 +19,32 @@ public:
GLError(const std::string&); GLError(const std::string&);
}; };
class WindowError : public std::runtime_error { class OpenGLError : public Error<OpenGLError> {
public: using Base = Error<OpenGLError>;
WindowError(const std::string&);
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<WindowError> {
using Base = Error<WindowError>;
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 { class Window {

View File

@ -16,9 +16,25 @@ static std::map<int, int> defailt_window_hints = {
#endif #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) { int Window::getGLFWHint(int hint_key) {
if (GLFWHints.find(hint_key) == GLFWHints.end()) { 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) { int Window::setGLFWHint(int hint_key, int hint_val) {
if (Window::_glfw_init) { if (Window::_glfw_init) {
throw GLError("Cannot set GLFW hint after window is initialized."); THROW(OpenGLError(OpenGLErrorCodes::GLFW_INTIALIZED));
} else { } else {
GLFWHints[hint_key] = hint_val; 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; Window::_glfw_init = true;
} else { } else {
if (Window::StrictInitialization) { 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) { int Window::setWindowHint(int hint_key, int hint_val) {
if (_init) { if (_init) {
throw WindowError("Cannot set window hint after window is initialized."); THROW(WindowError(WindowErrorCodes::INITIALIZED_WINDOW));
} else { } else {
windowHints[hint_key] = hint_val; windowHints[hint_key] = hint_val;
} }
@ -112,7 +128,7 @@ int Window::setWindowHint(int hint_key, int hint_val) {
int Window::init() { int Window::init() {
if (!_glfw_init) { if (!_glfw_init) {
throw GLError("GLFW has not been initialized."); THROW(WindowError(WindowErrorCodes::NO_GLFW));
} }
for (const auto& hint : windowHints) { for (const auto& hint : windowHints) {
glfwWindowHint(hint.first, hint.second); glfwWindowHint(hint.first, hint.second);
@ -123,7 +139,7 @@ int Window::init() {
Window::WindowContexts.erase(window); Window::WindowContexts.erase(window);
if (Window::WindowContexts.size()==0 && Window::_glfw_init) { glfwTerminate(); } if (Window::WindowContexts.size()==0 && Window::_glfw_init) { glfwTerminate(); }
if (Window::StrictInitialization) { if (Window::StrictInitialization) {
throw WindowError("Could not create window in glfwCreateWindow()."); THROW(WindowError(WindowErrorCodes::INIT_FAILED));
} }
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
@ -133,10 +149,7 @@ int Window::init() {
if (!gladResponse) { if (!gladResponse) {
Window::checkKillGLFW(); Window::checkKillGLFW();
if (Window::StrictInitialization) { if (Window::StrictInitialization) {
throw GLError("Failed to load GLAD with \ THROW(OpenGLError(OpenGLErrorCodes::GLAD_FAILED));
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)="+\
std::to_string(gladResponse)+"."
);
} }
} }

View File

@ -9,8 +9,8 @@ if (NB_BUILD_TESTS)
target_link_libraries(TestWindow target_link_libraries(TestWindow
NBCore NBCore
NBGraphics NBGraphics
GTest::gtest_main # GTest::gtest_main
) )
gtest_discover_tests(TestWindow) # gtest_discover_tests(TestWindow)
endif() endif()

View File

@ -1,6 +1,18 @@
#include <Buffers.hpp> #include "GLLoad.hpp"
// #define _NB_AUTOLOG
#include "VertexArray.hpp"
#include "Window.hpp"
int main() { 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; return 0;
} }