Uhhh changes idek anymore mayn

This commit is contained in:
NaifBanana
2025-11-05 00:21:11 -06:00
parent ac1762cb67
commit a7428ef55f
19 changed files with 553 additions and 10286 deletions
View File
+58
View File
@@ -0,0 +1,58 @@
#include "Errors.hpp"
namespace nb {
// class Error
const ErrorCodeMap Error::ErrorCodes = {
{0, "Undefined error."},
{666, "Unrecognized error code."}
};
Error::Error(
const unsigned int code,
std::string msg,
std::exception* const trace,
unsigned int line,
std::string filename
) noexcept : _code{code} {
_msg = std::string(type());
_msg += ":" + std::to_string(code);
if (line && filename.size()) {
_msg += " in \'" + filename + "\':" + std::to_string(line);
}
_msg += "\n\t" + msg;
if (trace) {
_msg += "\nTraceback - " + std::string(trace->what());
}
}
Error::Error(const unsigned int code, std::exception* const trace) noexcept
: Error(code, ErrorCodeLookup<Error>(code), trace) {}
Error::Error(std::string msg, std::exception* const trace) noexcept
: Error(0, msg, trace) {}
Error::Error(const Error& cp) noexcept
: _type{cp._type}, _code{cp._code}, _msg{cp._msg} {}
unsigned int Error::code() const noexcept { return _code; }
const char* Error::type() const noexcept { return this->_type; }
const char* Error::what() const noexcept { return _msg.c_str(); }
/* constexpr const char* Error::lookup(unsigned int code) noexcept {
char* ret = new char[512];
switch(code) {
case 0:
strcpy(ret, "General ");
strcat(ret, this->type());
break;
default:
strcpy(ret, this->lookup(code));
break;
}
return ret;
} */
}
View File
+3
View File
@@ -0,0 +1,3 @@
add_library(NBWindow Window.cpp ./../../../glad/src/glad.c)
target_link_libraries(NBWindow glfw)
+170
View File
@@ -0,0 +1,170 @@
#include "Window.h"
namespace NB {
std::map<GLFWwindow*, Window*> Window::WindowContexts = {};
std::map<int, int> Window::GLFWHints = {};
bool Window::StrictInitialization = true;
bool Window::_glfw_init = false;
Window* Window::_current = nullptr;
static std::map<int, int> defailt_window_hints = {
{GLFW_CONTEXT_VERSION_MAJOR, 3},
{GLFW_CONTEXT_VERSION_MINOR, 1},
{GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE}
#ifdef _NB_GL_DEBUG_ON
, {GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE}
#endif
};
GLError::GLError(const std::string& msg) : std::runtime_error(msg) {}
WindowError::WindowError(const std::string &msg) : std::runtime_error(msg) {}
int Window::getGLFWHint(int hint_key) {
if (GLFWHints.find(hint_key) == GLFWHints.end()) {
return 0;
} else {
return GLFWHints.at(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.");
} else {
GLFWHints[hint_key] = hint_val;
}
return GLFWHints[hint_key];
}
Window* Window::getCurrent() {
return _current;
}
bool Window::checkKillGLFW() {
if(Window::WindowContexts.size() == 0 && _glfw_init) {
glfwTerminate();
Window::_glfw_init = false;
return true;
}
return false;
}
Window::Window(const uint16_t x, const uint16_t y, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) {
if (!Window::_glfw_init) {
for (const auto& hint : Window::GLFWHints) {
glfwInitHint(hint.first, hint.second);
}
int glfwResponse = glfwInit();
if (glfwResponse) {
Window::_glfw_init = true;
} else {
if (Window::StrictInitialization) {
throw GLError("Failed to load GLFW with glfwInit()="+std::to_string(glfwResponse)+".");
}
}
}
windowSize = {x, y};
_aspect_ratio = float(x)/float(y);
windowName = std::string(initName);
monitor = initMonitor;
shareWindow = initWindow;
for (const auto& hint: defailt_window_hints) {
windowHints[hint.first] = hint.second;
}
}
Window::Window(const std::array<uint16_t, 2> initSize, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) :
Window(initSize[0], initSize[1], initName, initMonitor, initWindow){}
Window::~Window() {
glfwSetWindowShouldClose(window, true);
glfwDestroyWindow(window);
Window::WindowContexts.erase(window);
if (Window::WindowContexts.size()==0) {
glfwTerminate();
}
}
void Window::setCurrent() {
_current = this;
glfwMakeContextCurrent(window);
}
int Window::getWindowHint(int hint_key) const {
if (windowHints.find(hint_key) == windowHints.end()) {
return 0;
} else {
return windowHints.at(hint_key);
}
}
int Window::setWindowHint(int hint_key, int hint_val) {
if (_init) {
throw WindowError("Cannot set window hint after window is initialized.");
} else {
windowHints[hint_key] = hint_val;
}
return windowHints[hint_key];
}
int Window::init() {
if (!_glfw_init) {
throw GLError("GLFW has not been initialized.");
}
for (const auto& hint : windowHints) {
glfwWindowHint(hint.first, hint.second);
}
window = glfwCreateWindow(windowSize[0], windowSize[1], windowName.c_str(), monitor, shareWindow);
if (window == NULL) {
Window::WindowContexts.erase(window);
if (Window::WindowContexts.size()==0 && Window::_glfw_init) { glfwTerminate(); }
if (Window::StrictInitialization) {
throw WindowError("Could not create window in glfwCreateWindow().");
}
}
glfwMakeContextCurrent(window);
Window::WindowContexts[window] = this;
gladResponse = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
if (!gladResponse) {
Window::checkKillGLFW();
if (Window::StrictInitialization) {
throw GLError("Failed to load GLAD with \
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)="+\
std::to_string(gladResponse)+"."
);
}
}
_init = true;
glViewport(0, 0, windowSize[0], windowSize[1]);
return gladResponse;
}
GLFWwindow* Window::getWindow() const {
return window;
}
float Window::getAspectRatio() const { return _aspect_ratio; }
std::array<uint16_t, 2> Window::getSize() const {
return windowSize;
}
std::string Window::getName() const {
return windowName;
}
void Window::resize(const std::array<uint16_t, 2> newSize) {
windowSize = newSize;
_aspect_ratio = float(newSize[0]) / float(newSize[1]);
glViewport(0, 0, windowSize[0], windowSize[1]);
glfwSetWindowSize(window, windowSize[0], windowSize[1]);
}
void Window::resize(const uint16_t x, const uint16_t y) { resize({x, y}); }
};
-91
View File
@@ -1,91 +0,0 @@
#include "Window.h"
namespace NB {
std::atomic<uint8_t> Window::windowCount(0);
Window::Window(const uint16_t x, const uint16_t y, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) {
if (windowCount == 0) {
glfwInit();
}
windowSize = {x, y};
windowName = std::string(initName);
monitor = initMonitor;
shareWindow = initWindow;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(windowSize[0], windowSize[0], windowName.c_str(), monitor, shareWindow);
if (window == NULL) {
std::cout << "NB::NBWINDOW::COULD NOT CREATE WINDOW(" << windowName << ")\n";
if (windowCount == 0) { glfwTerminate(); }
ready = false;
} else {
windowCount++;
ready = true;
}
}
Window::Window(const std::array<uint16_t, 2> initSize, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) :
Window(initSize[0], initSize[1], initName, initMonitor, initWindow){}
Window::~Window() {
glfwSetWindowShouldClose(window, true);
glfwDestroyWindow(window);
windowCount--;
if (windowCount == 0) {
glfwTerminate();
}
}
int Window::init() {
if (!ready) {
std::cout << "NB::NBWINDOW::NOT READY\n";
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cout << "NB::NBWINDOW::COULD NOT INITIALIZE GLAD\n";
if (windowCount == 0) { glfwTerminate(); }
return -1;
}
glViewport(0, 0, windowSize[0], windowSize[1]);
running=true;
return 0;
}
GLFWwindow* Window::getWindow() const {
return window;
}
std::array<uint16_t, 2> Window::getSize() const {
return windowSize;
}
std::string Window::getName() const {
return windowName;
}
void Window::resize(const std::array<uint16_t, 2> newSize) {
windowSize = newSize;
if (running) {
glViewport(0, 0, windowSize[0], windowSize[1]);
glfwSetWindowSize(window, windowSize[0], windowSize[1]);
}
}
void Window::resize(const uint16_t x, const uint16_t y) {
windowSize = {x, y};
if (running) {
glViewport(0, 0, windowSize[0], windowSize[1]);
glfwSetWindowSize(window, windowSize[0], windowSize[1]);
}
}
};