From f49650744a3792157a097ca7d6727fb71931671c Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Wed, 9 Sep 2026 12:18:35 -0500 Subject: [PATCH] Lots of stuff, ending with Window restruct --- .../NBGraphics/include/NBGraphics/Buffers.hpp | 16 +- .../NBGraphics/include/NBGraphics/Image.hpp | 23 ++- .../include/NBGraphics/OGLObjects.hpp | 20 ++- .../include/NBGraphics/ProgramPipeline.hpp | 10 +- .../include/NBGraphics/Textures.hpp | 61 +++++-- .../NBGraphics/include/NBGraphics/Window.hpp | 69 +++++++- engine/NBGraphics/src/Buffers.cpp | 15 ++ engine/NBGraphics/src/OGLObjects.cpp | 23 ++- engine/NBGraphics/src/ProgramPipeline.cpp | 24 +++ engine/NBGraphics/src/Textures.cpp | 27 ++++ engine/NBGraphics/src/Window.cpp | 151 +++++++++++++++++- engine/NBGraphics/tests/TestWindow.cpp | 13 +- 12 files changed, 412 insertions(+), 40 deletions(-) diff --git a/engine/NBGraphics/include/NBGraphics/Buffers.hpp b/engine/NBGraphics/include/NBGraphics/Buffers.hpp index 7096a0b..968ee17 100644 --- a/engine/NBGraphics/include/NBGraphics/Buffers.hpp +++ b/engine/NBGraphics/include/NBGraphics/Buffers.hpp @@ -74,12 +74,12 @@ class Buffer : public OpenGLObject { if (!_id) { OPENGL_CALL(glGenBuffers(1, &_id)); } - bind(); + this->bind(); return _id; } virtual void remove() override { if (_id) { - unbind(); + this->unbind(); OPENGL_CALL(glDeleteBuffers(1, &_id)); _id = 0; } @@ -160,6 +160,18 @@ class ArrayBuffer : public Buffer { } }; +class UniformBuffer : public Buffer { + protected: + GLuint _index; + + public: + UniformBuffer(); + void bind() const override; + GLuint bufferIndex() const; + GLuint bufferIndex(GLuint); + +}; + class ElementBuffer : protected Buffer { protected: GLenum _glslType; diff --git a/engine/NBGraphics/include/NBGraphics/Image.hpp b/engine/NBGraphics/include/NBGraphics/Image.hpp index c754871..74bc916 100644 --- a/engine/NBGraphics/include/NBGraphics/Image.hpp +++ b/engine/NBGraphics/include/NBGraphics/Image.hpp @@ -158,6 +158,8 @@ class Image> { using Codes = ImageError::Codes; bool _data_managed; T* _data; + Image(unsigned int x, unsigned int y, T* data, bool managed) + : width(x), height(y), _data(data), _data_managed(managed) {} void clear() { if (_data_managed) { delete[] _data; @@ -171,19 +173,21 @@ class Image> { static constexpr size_t NumberChannels = sizeof...(Channels); const unsigned int width; const unsigned int height; - static Image&& CreateImageStorage( + static Image CreateImageStorage( unsigned int width, unsigned int height, T* data=nullptr ) { unsigned int data_size = NumberChannels*width*height*sizeof(T); T* data_ptr = new T[data_size]; - auto& ret = Image(width, height, data_ptr); + Image ret(width, height, data_ptr, true); if (data) { std::memcpy(data_ptr, data, data_size); + } else { + std::memset(data_ptr, 0x00, data_size); } ret._data_managed = true; - return std::move(ret); + return ret; } Image(unsigned int x, unsigned int y, T* data=nullptr) : width(x), height(y), _data(data), _data_managed(false) {} @@ -201,8 +205,13 @@ class Image> { } const Error<> sizeErr = sizeCompare(cpy); if (sizeErr.code!=1) { THROW(sizeErr); } - _data = cpy._data; - return this; + if (!cpy._data_managed) { + _data = cpy._data; + _data_managed = false; + } else { + *this = cpy.copy(); + } + return *this; } Image& operator=(Image&& mv) { if (_data_managed) { @@ -213,7 +222,7 @@ class Image> { _data = mv._data; _data_managed = mv._data_managed; mv._data_managed = false; - return this; + return *this; } virtual ~Image() { @@ -221,7 +230,7 @@ class Image> { } const T* data() const { return _data; } - Image&& copy() const { + Image copy() const { return CreateImageStorage(width, height, _data); } PixelReference at(unsigned int x, unsigned int y) { diff --git a/engine/NBGraphics/include/NBGraphics/OGLObjects.hpp b/engine/NBGraphics/include/NBGraphics/OGLObjects.hpp index 4171b78..9a5ab98 100644 --- a/engine/NBGraphics/include/NBGraphics/OGLObjects.hpp +++ b/engine/NBGraphics/include/NBGraphics/OGLObjects.hpp @@ -32,6 +32,7 @@ class OpenGLError : public Error { static const std::string type; static const ErrorCodeMap ErrorMessages; static OpenGLError status(); + static OpenGLError status(const std::string& note); }; class OpenGLObjectError : public Error { @@ -41,7 +42,7 @@ class OpenGLObjectError : public Error { using Base::Base; enum Codes : unsigned int { - UNDEFINED, HANGING_OBJECT, INVALID_OBJECT + UNDEFINED, HANGING_OBJECT, INVALID_OBJECT, INVALID_CONTEXT }; static const std::string type; @@ -60,13 +61,16 @@ class OpenGLObject { virtual void unbind() const = 0; GLuint id() const { return _id; } + protected: using Codes = OpenGLObjectError::Codes; - OpenGLObject() = default; + OpenGLObject(); + void checkObjectContextValidity(); virtual GLuint declare() = 0; virtual void remove() = 0; + const GLFWwindow* const _arena; GLuint _id = 0; }; @@ -80,21 +84,21 @@ class OpenGLObject { unsigned int line, Fn fn ) { - auto check_ = [file, line]() { + auto check_ = [file, line](const std::string& note="") { const auto no_err = nb::OpenGLError::NO_ERROR; - auto stat = nb::OpenGLError::status(); + auto stat = nb::OpenGLError::status(note); if (stat.code != no_err) { THROW_W_CODE_LOC(file, line, stat); } }; if constexpr(std::is_same_v, void>) { - check_(); + check_("Pre-call state"); fn(); - check_(); + check_("Post-call state"); } else { - check_(); + check_("Pre-call state"); auto ret = fn(); - check_(); + check_("Post-call state"); return ret; } } diff --git a/engine/NBGraphics/include/NBGraphics/ProgramPipeline.hpp b/engine/NBGraphics/include/NBGraphics/ProgramPipeline.hpp index 9507bd4..ea44f70 100644 --- a/engine/NBGraphics/include/NBGraphics/ProgramPipeline.hpp +++ b/engine/NBGraphics/include/NBGraphics/ProgramPipeline.hpp @@ -9,6 +9,7 @@ #include #include +#include #include namespace nb { @@ -112,17 +113,22 @@ class Program : public OpenGLObject { Program& operator=(Program&&) = delete; operator bool(); ~Program() { remove(); } - virtual void bind() const override { + void bind() const override { OPENGL_CALL(glUseProgram(_id)); } - virtual void unbind() const override { /* TODO: Some warning of some kind perhaps*/ } + void unbind() const override { /* TODO: Some warning of some kind perhaps*/ } GLint status(GLenum) const; std::string log() const; + void setUniform(const std::string& blockname, std::shared_ptr); + void setUniformBinding(const std::string& blockname, GLuint index); + GLuint getUniformBinding(const std::string& blockname) const; + friend Base; protected: using Base::_id; GLint _success; + std::unordered_map _uniformBindings; virtual GLuint declare() override { if (!_id) { _id = OPENGL_CALL(glCreateProgram()); diff --git a/engine/NBGraphics/include/NBGraphics/Textures.hpp b/engine/NBGraphics/include/NBGraphics/Textures.hpp index 07ffe0f..22ae169 100644 --- a/engine/NBGraphics/include/NBGraphics/Textures.hpp +++ b/engine/NBGraphics/include/NBGraphics/Textures.hpp @@ -24,11 +24,11 @@ class RenderTarget : public OpenGLObject { using Base = OpenGLObject; using Base::_id; RenderTarget(GLenum target) : Target(target) {} - RenderTarget(RenderTarget&& rhs) - : Target(rhs.Target), Base(std::move(rhs)) {} public: using Base::id; + RenderTarget(RenderTarget&& mv); + RenderTarget& operator=(RenderTarget&& mv); const GLenum Target; }; @@ -57,8 +57,10 @@ class Texture : public RenderTarget { public: using Base::id; using Base::Target; - - virtual void bind() const override { + Texture(Texture&& mv); + Texture& operator=(Texture&& mv); + + void bind() const override { if (_id) { OPENGL_CALL(glBindTexture(Target, _id)); } else { @@ -68,7 +70,7 @@ class Texture : public RenderTarget { )); } } - virtual void unbind() const override { + void unbind() const override { OPENGL_CALL(glBindTexture(Target, 0)); } @@ -119,13 +121,6 @@ struct OGLPixelFormat; template struct OGLPixelFormat>; -template<> -struct OGLPixelFormat> { - static constexpr GLenum glBase = GL_RGB; - static constexpr GLenum glFormat = GL_RGB8UI; - static constexpr GLenum glData = GL_UNSIGNED_BYTE; -}; - template<> struct OGLPixelFormat> { static constexpr GLenum glBase = GL_RGBA; @@ -133,6 +128,27 @@ struct OGLPixelFormat> { static constexpr GLenum glData = GL_UNSIGNED_BYTE; }; +template<> +struct OGLPixelFormat> { + static constexpr GLenum glBase = GL_RGBA; + static constexpr GLenum glFormat = GL_RGBA32F; + static constexpr GLenum glData = GL_FLOAT; +}; + +template<> +struct OGLPixelFormat> { + static constexpr GLenum glBase = GL_RGB; + static constexpr GLenum glFormat = GL_RGB8UI; + static constexpr GLenum glData = GL_UNSIGNED_BYTE; +}; + +template<> +struct OGLPixelFormat> { + static constexpr GLenum glBase = GL_RGB; + static constexpr GLenum glFormat = GL_RGB32F; + static constexpr GLenum glData = GL_FLOAT; +}; + template<> struct OGLPixelFormat> { static constexpr GLenum glBase = GL_RG; @@ -140,6 +156,13 @@ struct OGLPixelFormat> { static constexpr GLenum glData = GL_FLOAT; }; +template<> +struct OGLPixelFormat> { + static constexpr GLenum glBase = GL_RED; + static constexpr GLenum glFormat = GL_R32F; + static constexpr GLenum glData = GL_FLOAT; +}; + template class Texture2D; @@ -226,5 +249,19 @@ class Texture1D> : public Texture { } }; +template +class Texture1DArray; + +template +class Texture1DArray> : public Texture { + protected: + + public: + using Texture::generateMipmaps; + Texture1DArray() : Texture(GL_TEXTURE_1D_ARRAY) { + + } +}; + } // namespace nb #endif // _NB_TEXTURES \ No newline at end of file diff --git a/engine/NBGraphics/include/NBGraphics/Window.hpp b/engine/NBGraphics/include/NBGraphics/Window.hpp index ec85d68..66c4306 100644 --- a/engine/NBGraphics/include/NBGraphics/Window.hpp +++ b/engine/NBGraphics/include/NBGraphics/Window.hpp @@ -7,7 +7,9 @@ #include #include +#include #include +#include #include #include @@ -21,7 +23,8 @@ class GLFWError : public Error { using Base::Base; enum Codes : unsigned int { - UNDEFINED, INIT_FAILED, GLFW_INTIALIZED, GLAD_FAILED + UNDEFINED, INIT_FAILED, GLFW_INTIALIZED, GLAD_FAILED, + NOT_MAIN, WINDOW_FAILED }; static const std::string type; @@ -84,5 +87,69 @@ protected: int gladResponse = -1; }; +class OGLObjectArena; + +class OGLWindow { + using GLFWHint = std::array; + private: + static inline void CheckForMainThread() { + if (nb::CurrentTID != nb::MainTID) { + THROW(GLFWError(GLFWError::Codes::NOT_MAIN)); + } + } + + protected: + static std::atomic _count; + static std::atomic _glfwInitialized; + static thread_local std::shared_ptr _currWindow; + static thread_local GLFWwindow* _currArena; + + std::string _title; + GLFWwindow* _window_ptr; + GLFWwindow* _arena; + + static void SetHintInteger(const std::array&); + static void SetHintString(const std::tuple&); + + OGLWindow( + unsigned int, + unsigned int, + const std::string&, + const std::vector>&, + const std::vector>& + ); + + public: + static inline bool IsGLFWInitialized() { return _glfwInitialized; } + static bool InitGLFW(const std::vector& hints={}); + static bool TerminateGLFW(); + static inline std::shared_ptr getCurrentWindow() { return _currWindow; } + static inline const GLFWwindow* getCurrentObjectArena() { return _currArena; } + static std::shared_ptr MakeCurrent(const std::shared_ptr&); + static inline std::shared_ptr DropCurrent() { return MakeCurrent(nullptr); } + static std::shared_ptr CreateWindow( + unsigned int x, + unsigned int y, + const std::string& title, + const std::vector>& integerHints={}, + const std::vector>& stringHints={} + ); + OGLWindow(OGLWindow&&) = delete; + OGLWindow(const OGLWindow&) = delete; + ~OGLWindow(); + inline bool operator==(const OGLWindow& rhs) { return _window_ptr==rhs._window_ptr; } + + + int getWindowAttribute(int) const; + inline GLFWwindow* getWindowPtr() const { return _window_ptr; } + inline std::string getTitle() const { return _title; } + std::array getSize() const; + std::shared_ptr makeSharedContext(); + + void makeCurrent() const; + void setSize(unsigned int x, unsigned int y); + void setTitle(const std::string&); +}; + }; #endif \ No newline at end of file diff --git a/engine/NBGraphics/src/Buffers.cpp b/engine/NBGraphics/src/Buffers.cpp index fa8bf09..7d28d2a 100644 --- a/engine/NBGraphics/src/Buffers.cpp +++ b/engine/NBGraphics/src/Buffers.cpp @@ -74,6 +74,21 @@ void Buffer::subdata(const ByteVector& data_, GLintptr offset_) { subdata(data_.data(), size_); } +// class UniformBuffer +UniformBuffer::UniformBuffer() : Buffer(GL_UNIFORM_BUFFER) {} + +void UniformBuffer::bind() const { + Buffer::bind(); + OPENGL_CALL(glBindBufferBase(GL_UNIFORM_BUFFER, _index, id())); +} + +GLuint UniformBuffer::bufferIndex() const { return _index; } + +GLuint UniformBuffer::bufferIndex(GLuint idx) { + return (_index = idx); +} + +// class ElementBuffer ElementBuffer::ElementBuffer() : Buffer(GL_ELEMENT_ARRAY_BUFFER) {} ByteVector ElementBuffer::data() const { diff --git a/engine/NBGraphics/src/OGLObjects.cpp b/engine/NBGraphics/src/OGLObjects.cpp index 383c108..189a85d 100644 --- a/engine/NBGraphics/src/OGLObjects.cpp +++ b/engine/NBGraphics/src/OGLObjects.cpp @@ -1,4 +1,5 @@ #include +#include #ifndef GLENUMSTRPAIR #define GLENUMSTRPAIR(x) {x, #x} @@ -10,7 +11,8 @@ const std::string OpenGLObjectError::type = "nb::OpenGLObjectError"; const ErrorCodeMap OpenGLObjectError::ErrorMessages = { { OpenGLObjectError::Codes::UNDEFINED, "Error" }, {OpenGLObjectError::Codes::HANGING_OBJECT, "Attempting to leave a hanging OpenGL object"}, - {OpenGLObjectError::Codes::INVALID_OBJECT, "Attempting operation with invalid object"} + {OpenGLObjectError::Codes::INVALID_OBJECT, "Attempting operation with invalid object"}, + {OpenGLObjectError::Codes::INVALID_CONTEXT, "Object not valid with current OpengGL context"} }; const std::string OpenGLError::type = "nb::OpenGLError"; @@ -30,11 +32,28 @@ OpenGLError OpenGLError::status() { return OpenGLError(glGetError()); } -OpenGLObject::OpenGLObject(OpenGLObject&& rval) { +OpenGLError OpenGLError::status(const std::string& note) { + return OpenGLError(glGetError(), note); +} + + +// class OpenGLObject + +void OpenGLObject::checkObjectContextValidity() { + if (_arena != OGLWindow::getCurrentObjectArena()) { + THROW(OpenGLObjectError(OpenGLObjectError::INVALID_CONTEXT)); + } +} + +OpenGLObject::OpenGLObject(OpenGLObject&& rval) : _arena(rval._arena) { + checkObjectContextValidity(); *this = std::move(rval); } +OpenGLObject::OpenGLObject() : _arena(OGLWindow::getCurrentObjectArena()) {} + OpenGLObject& OpenGLObject::operator=(OpenGLObject&& rhs) { + rhs.checkObjectContextValidity(); if (_id) { THROW(OpenGLObjectError(Codes::HANGING_OBJECT)); } _id = rhs._id; rhs._id = 0; diff --git a/engine/NBGraphics/src/ProgramPipeline.cpp b/engine/NBGraphics/src/ProgramPipeline.cpp index 2dfe6e4..937e763 100644 --- a/engine/NBGraphics/src/ProgramPipeline.cpp +++ b/engine/NBGraphics/src/ProgramPipeline.cpp @@ -15,6 +15,8 @@ const ErrorCodeMap ProgramError::ErrorMessages = { {ProgramErrCodes::LINKING_ERROR, "Linker error"} }; +// class Shader + Shader::Shader(GLenum target_, const std::string& source_) : Shader(target_, std::vector({source_})) {} @@ -54,6 +56,8 @@ std::string Shader::log() const { return ret; } +// class Program + Program::Program(SharedVector shaders_) { declare(); for (auto shad_ptr : shaders_) { @@ -83,6 +87,26 @@ std::string Program::log() const { return ret; } +void Program::setUniform(const std::string& blockname, std::shared_ptr buffer) { + declare(); + auto idx = OPENGL_CALL(glGetUniformBlockIndex(id(), blockname.data())); + if (idx == GL_INVALID_INDEX) { + THROW("something"); + } + this->setUniformBinding(blockname, buffer->bufferIndex()); + OPENGL_CALL(glUniformBlockBinding( + id(), + idx, + _uniformBindings[blockname] + )); +} +void Program::setUniformBinding(const std::string& blockname, GLuint index) { + _uniformBindings[blockname] = index; +} + +GLuint Program::getUniformBinding(const std::string& blockname) const { + return _uniformBindings.at(blockname); +} } \ No newline at end of file diff --git a/engine/NBGraphics/src/Textures.cpp b/engine/NBGraphics/src/Textures.cpp index e16de71..9ae3508 100644 --- a/engine/NBGraphics/src/Textures.cpp +++ b/engine/NBGraphics/src/Textures.cpp @@ -2,6 +2,33 @@ namespace nb { +// class RenderTarget + +RenderTarget::RenderTarget(RenderTarget&& mv) : Target(mv.Target) { + *this = std::move(mv); +} + +RenderTarget& RenderTarget::operator=(RenderTarget&& mv) { + if (mv.Target != Target) { + THROW(OpenGLObjectError( + OpenGLObjectError::INVALID_OBJECT, + "Cannot assign RenderTarget of different types." + )); + } + Base::operator=(std::move(mv)); + return *this; +} + +// class Texture Texture::Texture(GLenum target_) : Base(target_) {} +Texture::Texture(Texture&& mv) : Base(mv.Target) { + *this = std::move(mv); +} + +Texture& Texture::operator=(Texture&& mv) { + Base::operator=(std::move(mv)); + return *this; +} + } // namespace nb \ No newline at end of file diff --git a/engine/NBGraphics/src/Window.cpp b/engine/NBGraphics/src/Window.cpp index 398d17f..99e04b0 100644 --- a/engine/NBGraphics/src/Window.cpp +++ b/engine/NBGraphics/src/Window.cpp @@ -22,7 +22,9 @@ const ErrorCodeMap GLFWError::ErrorMessages = { {GLFWCodes::UNDEFINED, "Error"}, {GLFWCodes::INIT_FAILED, "GLFW initialization failed"}, {GLFWCodes::GLFW_INTIALIZED, "GLFW has already been initialized"}, - {GLFWCodes::GLAD_FAILED, "GLAD initialization failed"} + {GLFWCodes::GLAD_FAILED, "GLAD initialization failed"}, + {GLFWCodes::NOT_MAIN, "Action can only be done on main thread"}, + {GLFWCodes::WINDOW_FAILED, "Failed to initialize window"} }; using WindowErrorCodes = WindowError::Codes; @@ -34,6 +36,9 @@ const ErrorCodeMap WindowError::ErrorMessages = { {WindowErrorCodes::INIT_FAILED, "Could not intialized window"} }; + +// class Window + int Window::getGLFWHint(int hint_key) { if (GLFWHints.find(hint_key) == GLFWHints.end()) { return 0; @@ -183,4 +188,148 @@ void Window::resize(const std::array newSize) { void Window::resize(const uint16_t x, const uint16_t y) { resize({x, y}); } +// class OGLWindow + +std::atomic OGLWindow::_glfwInitialized = false; +thread_local std::shared_ptr OGLWindow::_currWindow = nullptr; +thread_local GLFWwindow* OGLWindow::_currArena = nullptr; +std::atomic OGLWindow::_count = 0; + +bool OGLWindow::InitGLFW(const std::vector& hints) { + if (nb::MainTID != GetTID()) { + THROW(GLFWError(GLFWError::NOT_MAIN)); + } + if (!IsGLFWInitialized()) { + for (const auto& hint : hints) { + glfwInitHint(hint[0], hint[1]); + } + if (glfwInit()) { + OGLWindow::_glfwInitialized.store(true, std::memory_order_release); + } else { + THROW(GLFWError(GLFWError::INIT_FAILED)); + } + } + return IsGLFWInitialized(); +} + +bool OGLWindow::TerminateGLFW() { + if (nb::MainTID != GetTID()) { + THROW(GLFWError(GLFWError::NOT_MAIN)); + } + if (IsGLFWInitialized()) { + glfwTerminate(); + OGLWindow::_glfwInitialized.store(false, std::memory_order_release); + } + 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()); +} + +std::shared_ptr OGLWindow::MakeCurrent(const std::shared_ptr& window) { + auto tmp = _currWindow; + _currWindow = window; + if (window) { + glfwMakeContextCurrent(_currWindow->_window_ptr); + _currArena = window->_arena; + } else { + glfwMakeContextCurrent(nullptr); + _currArena = nullptr; + } + return tmp; +} + +OGLWindow::OGLWindow( + unsigned int x, + unsigned int y, + const std::string& title, + const std::vector>& integerHints, + const std::vector>& stringHints +) : _title(title) { + CheckForMainThread(); + InitGLFW(); + for (const auto& hint : integerHints) { + SetHintInteger(hint); + } + for (const auto& hint : stringHints) { + SetHintString(hint); + } + _window_ptr = glfwCreateWindow( + x, + y, + _title.c_str(), + NULL, + NULL + ); + if (_window_ptr == NULL) { + THROW(GLFWError(GLFWError::Codes::WINDOW_FAILED)); + } + _arena = _window_ptr; + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + THROW(GLFWError(GLFWError::Codes::GLAD_FAILED)); + } + OGLWindow::_count.fetch_add(1); +} + +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 + ) ); +} + +OGLWindow::~OGLWindow() { + CheckForMainThread(); + if (_window_ptr) { + if (_window_ptr == _currWindow->_window_ptr) { + _currWindow = nullptr; + _currArena = nullptr; + } + glfwDestroyWindow(_window_ptr); + _count.fetch_add(-1); + if (!_count.load()) { + TerminateGLFW(); + } + } +} + +int OGLWindow::getWindowAttribute(int attr) const { + CheckForMainThread(); + return glfwGetWindowAttrib(_window_ptr, attr); +} + +std::array OGLWindow::getSize() const { + CheckForMainThread(); + int x, y; + glfwGetWindowSize(_window_ptr, &x, &y); + return {x, y}; +} + +void OGLWindow::setSize(unsigned int x, unsigned int y) { + CheckForMainThread(); + glfwSetWindowSize(_window_ptr, x, y); +} + +void OGLWindow::setTitle(const std::string& name) { + CheckForMainThread(); + _title = name; + glfwSetWindowTitle(_window_ptr, _title.c_str()); +} + }; \ No newline at end of file diff --git a/engine/NBGraphics/tests/TestWindow.cpp b/engine/NBGraphics/tests/TestWindow.cpp index 97f433e..e02ed2c 100644 --- a/engine/NBGraphics/tests/TestWindow.cpp +++ b/engine/NBGraphics/tests/TestWindow.cpp @@ -12,10 +12,13 @@ int main() { LOG("Howdy!"); - nb::Window window(400, 400, "Hello!"); - window.setWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - window.setWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - window.init(); + auto window = nb::OGLWindow::CreateWindow( + 400, + 400, + "Howdy!", + {{GLFW_CONTEXT_VERSION_MAJOR, 3}, + {GLFW_CONTEXT_VERSION_MINOR, 3}} + ); auto vert = std::make_shared( GL_VERTEX_SHADER, @@ -66,7 +69,7 @@ int main() { }, indxs); LOG(prog.log()); - GLFWwindow* window_ptr = window.getWindow(); + GLFWwindow* window_ptr = window->getWindowPtr(); while(!glfwWindowShouldClose(window_ptr)) { OPENGL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f)); OPENGL_CALL(glClear(GL_COLOR_BUFFER_BIT));