From 416446206f5d846297663622ae341810face4f2a Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:54:36 -0500 Subject: [PATCH] Made window hinting/param system easier --- engine/NBCore/include/NBCore/TypeTraits.hpp | 4 ++ .../NBGraphics/include/NBGraphics/Window.hpp | 51 ++++++++++++----- engine/NBGraphics/src/Window.cpp | 56 +------------------ engine/NBGraphics/tests/TestWindow.cpp | 5 +- 4 files changed, 46 insertions(+), 70 deletions(-) diff --git a/engine/NBCore/include/NBCore/TypeTraits.hpp b/engine/NBCore/include/NBCore/TypeTraits.hpp index 5542cf2..1ac3dfa 100644 --- a/engine/NBCore/include/NBCore/TypeTraits.hpp +++ b/engine/NBCore/include/NBCore/TypeTraits.hpp @@ -3,6 +3,7 @@ #define _NB_TYPE_TRAITS #include +#include namespace nb { @@ -29,6 +30,9 @@ namespace detail { }; } // namespace detail +template +std::pair KeyVal(K key, V val) { return std::pair(key, val); } + enum TypeCodes { UINT8, UINT16, UINT32, UINT64, INT8, INT16, INT32, INT64, FLOAT16, FLOAT32, DOUBLE64, BOOL, NONE diff --git a/engine/NBGraphics/include/NBGraphics/Window.hpp b/engine/NBGraphics/include/NBGraphics/Window.hpp index 7bb5956..d5d605d 100644 --- a/engine/NBGraphics/include/NBGraphics/Window.hpp +++ b/engine/NBGraphics/include/NBGraphics/Window.hpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include @@ -126,21 +128,26 @@ class OGLWindow { static thread_local GLFWwindow* _currWindow; static thread_local const GLFWwindow* _currArena; - static void SetHintInteger(const std::array&); - static void SetHintString(const std::tuple&); - + template + static void SetHint(std::pair hint) { + if constexpr ( std::is_same_v ) { + glfwWindowHintString(std::get<0>(hint), std::get<1>(hint).c_str()); + } else if constexpr ( std::is_same_v ) { + glfwWindowHint(std::get<0>(hint), std::get<1>(hint)); + } + } + std::string _title; GLFWwindow* _window_ptr; const GLFWwindow* _arena; OGLWindow( - unsigned int, - unsigned int, + unsigned int x, + unsigned int y, const std::string&, - const std::vector>& integerWindowHints, - const std::vector>& stringWindowHints={}, GLFWwindow* sharingContext = NULL ); + GLFWwindow* makeCurrent() const; void detachWindow(); @@ -154,21 +161,37 @@ class OGLWindow { static GLFWwindow* MakeCurrent(const std::shared_ptr&); static inline GLFWwindow* DropCurrent() { return MakeCurrent(nullptr); } static void DestroyWindow(std::shared_ptr&); + template static std::shared_ptr CreateWindow( unsigned int x, unsigned int y, const std::string& title, - const std::vector>& integerHints={}, - const std::vector>& stringHints={} - ); + const std::pair&... windowHints + ) { + (SetHint(std::pair(windowHints)),... ); + return std::shared_ptr(new OGLWindow( + x, + y, + title, + nullptr + ) ); + } + template static std::shared_ptr CreateSharedWindow( unsigned int x, unsigned int y, const std::string& title, - std::shared_ptr sharingWindow, - const std::vector>& integerHints={}, - const std::vector>& stringHints={} - ); + std::shared_ptr sharingWindow = NULL, + const std::pair&... windowHints + ) { + (SetHint(std::pair(windowHints)),... ); + return std::shared_ptr(new OGLWindow( + x, + y, + title, + sharingWindow->_window_ptr + ) ); + } OGLWindow(OGLWindow&&) = delete; OGLWindow(const OGLWindow&) = delete; diff --git a/engine/NBGraphics/src/Window.cpp b/engine/NBGraphics/src/Window.cpp index 5c28671..e3b9006 100644 --- a/engine/NBGraphics/src/Window.cpp +++ b/engine/NBGraphics/src/Window.cpp @@ -48,16 +48,6 @@ bool OGLWindow::TerminateGLFW() { return !IsGLFWInitialized(); } -void OGLWindow::SetHintInteger(const std::array& hint) { - CheckForMainThread(); - glfwWindowHint(hint[0], hint[1]); -} - -void OGLWindow::SetHintString(const std::tuple& hint) { - CheckForMainThread(); - glfwWindowHintString(std::get<0>(hint), std::get<1>(hint).c_str()); -} - GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr& window) { auto tmp = _currWindow; if (window) { @@ -71,56 +61,14 @@ GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr& window) { return tmp; } -std::shared_ptr OGLWindow::CreateWindow( - unsigned int x, - unsigned int y, - const std::string& title, - const std::vector>& integerHints, - const std::vector>& stringHints -) { - return std::shared_ptr( new OGLWindow( - x, - y, - title, - integerHints, - stringHints - ) ); -} - -std::shared_ptr OGLWindow::CreateSharedWindow( - unsigned int x, - unsigned int y, - const std::string& title, - std::shared_ptr sharingWindow, - const std::vector>& integerHints, - const std::vector>& stringHints -) { - return std::shared_ptr( new OGLWindow( - x, - y, - title, - integerHints, - stringHints, - sharingWindow->getWindowPtr() - ) ); -} - OGLWindow::OGLWindow( unsigned int x, unsigned int y, - const std::string& title, - const std::vector>& integerHints, - const std::vector>& stringHints, + const std::string&, GLFWwindow* sharingContext -) : _title(title) { +) { CheckForMainThread(); InitGLFW(); - for (const auto& hint : integerHints) { - SetHintInteger(hint); - } - for (const auto& hint : stringHints) { - SetHintString(hint); - } _window_ptr = glfwCreateWindow( x, y, diff --git a/engine/NBGraphics/tests/TestWindow.cpp b/engine/NBGraphics/tests/TestWindow.cpp index a3c815e..920896b 100644 --- a/engine/NBGraphics/tests/TestWindow.cpp +++ b/engine/NBGraphics/tests/TestWindow.cpp @@ -3,6 +3,7 @@ #include "stb_image.h" #include +#include #include #include #include @@ -17,8 +18,8 @@ int main() { 400, 400, "Howdy!", - {{GLFW_CONTEXT_VERSION_MAJOR, 3}, - {GLFW_CONTEXT_VERSION_MINOR, 3}} + nb::KeyVal(GLFW_CONTEXT_VERSION_MAJOR, 3), + nb::KeyVal(GLFW_CONTEXT_VERSION_MINOR, 3) ); nb::OGLWindow::MakeCurrent(window);