Nuked old window class outta orbit!

This commit is contained in:
NaifBanana
2026-10-11 13:32:47 -05:00
parent f49650744a
commit e76ef32c45
3 changed files with 4 additions and 234 deletions
@@ -6,7 +6,6 @@
#include <NBGraphics/OGLObjects.hpp>
#include <array>
#include <map>
#include <memory>
#include <string>
#include <vector>
@@ -31,64 +30,6 @@ class GLFWError : public Error<GLFWError> {
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 {
public:
static bool StrictInitialization;
static int getGLFWHint(int);
static int setGLFWHint(int, int);
static Window* getCurrent();
Window(const std::array<uint16_t, 2>, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
Window(const uint16_t, const uint16_t, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
~Window();
int getWindowHint(int) const;
GLFWwindow* getWindow() const;
std::array<uint16_t, 2> getSize() const;
float getAspectRatio() const;
std::string getName() const;
int init();
int setWindowHint(int, int);
void setCurrent();
void resize(const std::array<uint16_t, 2>);
void resize(const uint16_t x, const uint16_t y);
protected:
static std::map<GLFWwindow*, Window*> WindowContexts;
static std::map<int, int> GLFWHints;
static Window* _current;
static bool _glfw_init;
static bool checkKillGLFW();
std::map<int, int> windowHints;
std::array<uint16_t, 2> windowSize;
float _aspect_ratio;
std::string windowName;
GLFWwindow* window;
GLFWmonitor* monitor;
GLFWwindow* shareWindow;
bool _init = false;
int gladResponse = -1;
};
class OGLObjectArena;
class OGLWindow {
using GLFWHint = std::array<int, 2>;
private:
+3 -175
View File
@@ -2,20 +2,6 @@
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
};
using GLFWCodes = GLFWError::Codes;
const std::string GLFWError::type = "nb::GLFWError";
const ErrorCodeMap GLFWError::ErrorMessages = {
@@ -27,167 +13,6 @@ const ErrorCodeMap GLFWError::ErrorMessages = {
{GLFWCodes::WINDOW_FAILED, "Failed to initialize window"}
};
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"}
};
// class Window
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(GLFWError(GLFWCodes::GLFW_INTIALIZED));
} 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(GLFWError(GLFWCodes::INIT_FAILED));
}
}
}
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(WindowErrorCodes::INITIALIZED_WINDOW));
} else {
windowHints[hint_key] = hint_val;
}
return windowHints[hint_key];
}
int Window::init() {
if (!_glfw_init) {
THROW(WindowError(WindowErrorCodes::NO_GLFW));
}
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(WindowErrorCodes::INIT_FAILED));
}
}
glfwMakeContextCurrent(window);
Window::WindowContexts[window] = this;
gladResponse = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
if (!gladResponse) {
Window::checkKillGLFW();
if (Window::StrictInitialization) {
THROW(GLFWError(GLFWCodes::GLAD_FAILED));
}
}
_init = true;
OPENGL_CALL(
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]);
OPENGL_CALL(
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}); }
// class OGLWindow
std::atomic<bool> OGLWindow::_glfwInitialized = false;
@@ -272,9 +97,12 @@ OGLWindow::OGLWindow(
THROW(GLFWError(GLFWError::Codes::WINDOW_FAILED));
}
_arena = _window_ptr;
auto tmp = glfwGetCurrentContext();
glfwMakeContextCurrent(_window_ptr);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
THROW(GLFWError(GLFWError::Codes::GLAD_FAILED));
}
glfwMakeContextCurrent(tmp);
OGLWindow::_count.fetch_add(1);
}
+1
View File
@@ -19,6 +19,7 @@ int main() {
{{GLFW_CONTEXT_VERSION_MAJOR, 3},
{GLFW_CONTEXT_VERSION_MINOR, 3}}
);
nb::OGLWindow::MakeCurrent(window);
auto vert = std::make_shared<nb::Shader>(
GL_VERTEX_SHADER,