Skip to content

Commit

Permalink
fix(close #11): Fix window resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
coderloff committed Aug 22, 2024
1 parent a337c93 commit 0ced6c4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
20 changes: 13 additions & 7 deletions engine/include/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
#include <iostream>
#include <Renderer.h>

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
Expand All @@ -24,16 +32,14 @@ class Window

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{};
Expand Down
12 changes: 11 additions & 1 deletion engine/src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
Expand Down
24 changes: 6 additions & 18 deletions engine/src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ 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();
return;
}

glfwMakeContextCurrent(m_Window);

glfwSetFramebufferSizeCallback(m_Window, FramebufferSizeCallback);
}

Window Window::Create()
Expand All @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 0ced6c4

Please sign in to comment.