diff --git a/engine/NBGraphics/include/NBGraphics/Window.hpp b/engine/NBGraphics/include/NBGraphics/Window.hpp index 66c4306..5acfff9 100644 --- a/engine/NBGraphics/include/NBGraphics/Window.hpp +++ b/engine/NBGraphics/include/NBGraphics/Window.hpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -31,64 +30,6 @@ class GLFWError : public Error { 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 { -public: - static bool StrictInitialization; - - static int getGLFWHint(int); - static int setGLFWHint(int, int); - static Window* getCurrent(); - - Window(const std::array, 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 getSize() const; - float getAspectRatio() const; - std::string getName() const; - - int init(); - int setWindowHint(int, int); - void setCurrent(); - void resize(const std::array); - void resize(const uint16_t x, const uint16_t y); - -protected: - static std::map WindowContexts; - static std::map GLFWHints; - static Window* _current; - static bool _glfw_init; - static bool checkKillGLFW(); - - std::map windowHints; - std::array 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; private: diff --git a/engine/NBGraphics/src/Window.cpp b/engine/NBGraphics/src/Window.cpp index 99e04b0..d0d31b1 100644 --- a/engine/NBGraphics/src/Window.cpp +++ b/engine/NBGraphics/src/Window.cpp @@ -2,20 +2,6 @@ namespace nb { -std::map Window::WindowContexts = {}; -std::map Window::GLFWHints = {}; -bool Window::StrictInitialization = true; -bool Window::_glfw_init = false; -Window* Window::_current = nullptr; -static std::map 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 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 Window::getSize() const { - return windowSize; -} - -std::string Window::getName() const { - return windowName; -} - -void Window::resize(const std::array 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 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); } diff --git a/engine/NBGraphics/tests/TestWindow.cpp b/engine/NBGraphics/tests/TestWindow.cpp index e02ed2c..ed5d614 100644 --- a/engine/NBGraphics/tests/TestWindow.cpp +++ b/engine/NBGraphics/tests/TestWindow.cpp @@ -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( GL_VERTEX_SHADER,