From 0ced6c452f920194956b90e215f4dc238eaca6e1 Mon Sep 17 00:00:00 2001 From: Huseyn Ismayilov Date: Thu, 22 Aug 2024 20:34:22 +0400 Subject: [PATCH] fix(close #11): Fix window resizing --- engine/include/Window.h | 20 +++++++++++++------- engine/src/Renderer.cpp | 12 +++++++++++- engine/src/Window.cpp | 24 ++++++------------------ 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/engine/include/Window.h b/engine/include/Window.h index de758b0..10b6aca 100644 --- a/engine/include/Window.h +++ b/engine/include/Window.h @@ -6,13 +6,21 @@ #include #include +struct WindowSize +{ + int Width, Height; + + WindowSize(): Width(0), Height(0){} + WindowSize(int width, int height): Width(width), Height(height){} +}; + struct WindowData { std::string Title; - int Width, Height; + WindowSize Size; - WindowData(): Title("Ferx Engine"), Width(900), Height(600){} - WindowData(const std::string& title, int width, int height): Title(title), Width(width), Height(height){}; + WindowData(): Title("Ferx Engine"), Size(900, 600){} + WindowData(const std::string& title, int width, int height): Title(title), Size(width, height){}; }; class Window @@ -24,16 +32,14 @@ public: static Window Create(); static Window Create(const std::string& title, int width, int height); - static void FramebufferSizeCallback(GLFWwindow* window, int width, int height); void Init(); GLFWwindow* GetWindow() const; const std::string& GetTitle() const; - int GetWidth() const; - int GetHeight() const; + WindowSize GetSize(); - void Shutdown(); + void Shutdown() const; private: GLFWwindow* m_Window{}; diff --git a/engine/src/Renderer.cpp b/engine/src/Renderer.cpp index 2f6c144..deff9e5 100644 --- a/engine/src/Renderer.cpp +++ b/engine/src/Renderer.cpp @@ -20,6 +20,15 @@ void Renderer::Init() LoadShaders(); SetupBuffers(); + + glfwSetWindowSizeCallback(Engine::Get().GetWindow().GetWindow(), [](GLFWwindow* window, int width, int height) + { + SetupBuffers(); + }); + glfwSetFramebufferSizeCallback(Engine::Get().GetWindow().GetWindow(), [](GLFWwindow* window, int width, int height) + { + glViewport(0, 0, width, height); + }); } RendererData Renderer::GetData() @@ -41,7 +50,8 @@ void Renderer::SetupBuffers() }; s_Data.m_FBO = new FrameBuffer(); - s_Data.m_FBO->AttachTexture(Engine::Get().GetWindow().GetWidth(), Engine::Get().GetWindow().GetHeight()); + WindowSize windowSize = Engine::Get().GetWindow().GetSize(); + s_Data.m_FBO->AttachTexture(windowSize.Width, windowSize.Height); s_Data.m_VAO = new VertexArray(); s_Data.m_VBO = new VertexBuffer(); diff --git a/engine/src/Window.cpp b/engine/src/Window.cpp index 3c7a9a0..56769ce 100644 --- a/engine/src/Window.cpp +++ b/engine/src/Window.cpp @@ -29,7 +29,7 @@ void Window::Init() glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - m_Window = glfwCreateWindow(m_Data.Width, m_Data.Height, m_Data.Title.c_str(), nullptr, nullptr); + m_Window = glfwCreateWindow(m_Data.Size.Width, m_Data.Size.Height, m_Data.Title.c_str(), nullptr, nullptr); if (!m_Window) { std::cerr << "Failed to create GLFW window" << std::endl; glfwTerminate(); @@ -37,8 +37,6 @@ void Window::Init() } glfwMakeContextCurrent(m_Window); - - glfwSetFramebufferSizeCallback(m_Window, FramebufferSizeCallback); } Window Window::Create() @@ -51,11 +49,6 @@ Window Window::Create(const std::string& title, int width, int height) return Window{title, width, height}; } -void Window::FramebufferSizeCallback(GLFWwindow* window, int width, int height) -{ - glViewport(0, 0, width, height); -} - GLFWwindow* Window::GetWindow() const { return m_Window; @@ -66,18 +59,13 @@ const std::string& Window::GetTitle() const return m_Data.Title; } -int Window::GetWidth() const +WindowSize Window::GetSize() { - return m_Data.Width; + glfwGetWindowSize(m_Window, &m_Data.Size.Width, &m_Data.Size.Height); + return m_Data.Size; } -int Window::GetHeight() const -{ - return m_Data.Height; -} - - -void Window::Shutdown() +void Window::Shutdown() const { glfwDestroyWindow(m_Window); -} \ No newline at end of file +}