feat: add window resize support
Added width/height tracking to the window class via a GLFW resize callback. The callback stores the current size and forwards it to a user-provided std::function. Added context::set_size() to update the GL viewport when the window is resized. Changed scene::render() to accept (int width, int height) parameters so scenes can compute the correct aspect ratio for their projection matrix instead of hardcoding 1280/720. Fixed parameter shadowing in resize_callback_impl (glfw_window instead of window).
This commit is contained in:
@@ -34,6 +34,9 @@ context::context(window const& win) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_valid = true;
|
m_valid = true;
|
||||||
|
|
||||||
|
// Set initial viewport to match window size
|
||||||
|
glViewport(0, 0, win.width(), win.height());
|
||||||
}
|
}
|
||||||
|
|
||||||
context::~context() {}
|
context::~context() {}
|
||||||
@@ -42,4 +45,8 @@ auto context::valid() const -> bool {
|
|||||||
return m_valid;
|
return m_valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto context::set_size(int width, int height) -> void {
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ public:
|
|||||||
~context();
|
~context();
|
||||||
|
|
||||||
auto valid() const -> bool;
|
auto valid() const -> bool;
|
||||||
|
auto set_size(int width, int height) -> void;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_valid = false;
|
bool m_valid = false;
|
||||||
|
|||||||
+1
-1
@@ -8,6 +8,6 @@ auto scene::init() -> bool {
|
|||||||
|
|
||||||
auto scene::update(float) -> void {}
|
auto scene::update(float) -> void {}
|
||||||
|
|
||||||
auto scene::render() -> void {}
|
auto scene::render(int, int) -> void {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ public:
|
|||||||
|
|
||||||
virtual auto init() -> bool;
|
virtual auto init() -> bool;
|
||||||
virtual auto update(float delta_time) -> void;
|
virtual auto update(float delta_time) -> void;
|
||||||
virtual auto render() -> void;
|
virtual auto render(int width, int height) -> void;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,12 @@ window::window(std::string title, int width, int height) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_width = width;
|
||||||
|
m_height = height;
|
||||||
|
|
||||||
|
glfwSetWindowUserPointer(m_window, this);
|
||||||
|
glfwSetWindowSizeCallback(m_window, resize_callback_impl);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
auto hwnd = glfwGetWin32Window(m_window);
|
auto hwnd = glfwGetWin32Window(m_window);
|
||||||
BOOL use_dark_mode = TRUE;
|
BOOL use_dark_mode = TRUE;
|
||||||
@@ -94,6 +100,29 @@ auto window::stop() -> void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto window::width() const -> int {
|
||||||
|
return m_width;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto window::height() const -> int {
|
||||||
|
return m_height;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto window::on_resize(resize_callback cb) -> void {
|
||||||
|
m_resize_cb = cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto window::resize_callback_impl(GLFWwindow* glfw_window, int width, int height) -> void {
|
||||||
|
auto* self = static_cast<window*>(glfwGetWindowUserPointer(glfw_window));
|
||||||
|
if (self) {
|
||||||
|
self->m_width = width;
|
||||||
|
self->m_height = height;
|
||||||
|
if (self->m_resize_cb) {
|
||||||
|
self->m_resize_cb(width, height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto window::screenshot(std::string filepath) const -> bool {
|
auto window::screenshot(std::string filepath) const -> bool {
|
||||||
if (!m_window || !valid()) {
|
if (!m_window || !valid()) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#define GLFW_INCLUDE_NONE
|
#define GLFW_INCLUDE_NONE
|
||||||
@@ -19,13 +20,22 @@ public:
|
|||||||
auto raw() const -> GLFWwindow*;
|
auto raw() const -> GLFWwindow*;
|
||||||
auto stop() -> void;
|
auto stop() -> void;
|
||||||
auto screenshot(std::string filepath = "screenshot.png") const -> bool;
|
auto screenshot(std::string filepath = "screenshot.png") const -> bool;
|
||||||
|
auto width() const -> int;
|
||||||
|
auto height() const -> int;
|
||||||
|
|
||||||
|
using resize_callback = std::function<void(int width, int height)>;
|
||||||
|
auto on_resize(resize_callback cb) -> void;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GLFWwindow* m_window = nullptr;
|
GLFWwindow* m_window = nullptr;
|
||||||
|
int m_width = 0;
|
||||||
|
int m_height = 0;
|
||||||
|
resize_callback m_resize_cb;
|
||||||
bool m_glfw_initialized = false;
|
bool m_glfw_initialized = false;
|
||||||
|
|
||||||
static auto init_glfw() -> bool;
|
static auto init_glfw() -> bool;
|
||||||
static auto terminate_glfw() -> void;
|
static auto terminate_glfw() -> void;
|
||||||
|
static auto resize_callback_impl(GLFWwindow* glfw_window, int width, int height) -> void;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,11 @@ auto main(int argc, char const* argv[]) -> int {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wire up resize callback
|
||||||
|
win.on_resize([&ctx](int width, int height) {
|
||||||
|
ctx.set_size(width, height);
|
||||||
|
});
|
||||||
|
|
||||||
auto cube_scn = cbt::scenes::cube();
|
auto cube_scn = cbt::scenes::cube();
|
||||||
auto sphere_scn = cbt::scenes::sphere();
|
auto sphere_scn = cbt::scenes::sphere();
|
||||||
|
|
||||||
@@ -118,7 +123,7 @@ auto main(int argc, char const* argv[]) -> int {
|
|||||||
prev = now;
|
prev = now;
|
||||||
|
|
||||||
active_scene->update(dt);
|
active_scene->update(dt);
|
||||||
active_scene->render();
|
active_scene->render(win.width(), win.height());
|
||||||
|
|
||||||
win.swap_buffers();
|
win.swap_buffers();
|
||||||
win.poll_events();
|
win.poll_events();
|
||||||
|
|||||||
+2
-2
@@ -23,14 +23,14 @@ auto cube::init() -> bool {
|
|||||||
|
|
||||||
auto cube::update(float) -> void {}
|
auto cube::update(float) -> void {}
|
||||||
|
|
||||||
auto cube::render() -> void {
|
auto cube::render(int width, int height) -> void {
|
||||||
auto now = std::chrono::steady_clock::now();
|
auto now = std::chrono::steady_clock::now();
|
||||||
auto elapsed = std::chrono::duration<float>(now - m_start).count();
|
auto elapsed = std::chrono::duration<float>(now - m_start).count();
|
||||||
|
|
||||||
glClearColor(0.15f, 0.15f, 0.2f, 1.0f);
|
glClearColor(0.15f, 0.15f, 0.2f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
auto aspect = 1280.0f / 720.0f;
|
auto aspect = float(width) / float(height);
|
||||||
auto proj = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 100.0f);
|
auto proj = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 100.0f);
|
||||||
auto view = glm::translate(glm::mat4{1.0f}, glm::vec3{0.0f, 0.0f, -3.0f});
|
auto view = glm::translate(glm::mat4{1.0f}, glm::vec3{0.0f, 0.0f, -3.0f});
|
||||||
auto model = glm::rotate(glm::mat4{1.0f}, elapsed, glm::vec3{1.0f, 0.5f, 0.3f});
|
auto model = glm::rotate(glm::mat4{1.0f}, elapsed, glm::vec3{1.0f, 0.5f, 0.3f});
|
||||||
|
|||||||
+1
-1
@@ -16,7 +16,7 @@ public:
|
|||||||
cube();
|
cube();
|
||||||
auto init() -> bool override;
|
auto init() -> bool override;
|
||||||
auto update(float delta_time) -> void override;
|
auto update(float delta_time) -> void override;
|
||||||
auto render() -> void override;
|
auto render(int width, int height) -> void override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
opengl::shader m_prog;
|
opengl::shader m_prog;
|
||||||
|
|||||||
+2
-2
@@ -25,14 +25,14 @@ auto sphere::init() -> bool {
|
|||||||
|
|
||||||
auto sphere::update(float) -> void {}
|
auto sphere::update(float) -> void {}
|
||||||
|
|
||||||
auto sphere::render() -> void {
|
auto sphere::render(int width, int height) -> void {
|
||||||
auto now = std::chrono::steady_clock::now();
|
auto now = std::chrono::steady_clock::now();
|
||||||
auto elapsed = std::chrono::duration<float>(now - m_start).count();
|
auto elapsed = std::chrono::duration<float>(now - m_start).count();
|
||||||
|
|
||||||
glClearColor(0.15f, 0.15f, 0.2f, 1.0f);
|
glClearColor(0.15f, 0.15f, 0.2f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
auto aspect = 1280.0f / 720.0f;
|
auto aspect = float(width) / float(height);
|
||||||
auto proj = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 100.0f);
|
auto proj = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 100.0f);
|
||||||
auto view = glm::translate(glm::mat4{1.0f}, glm::vec3{0.0f, 0.0f, -4.0f});
|
auto view = glm::translate(glm::mat4{1.0f}, glm::vec3{0.0f, 0.0f, -4.0f});
|
||||||
auto model = glm::rotate(glm::mat4{1.0f}, elapsed, glm::vec3{1.0f, 0.3f, 0.2f});
|
auto model = glm::rotate(glm::mat4{1.0f}, elapsed, glm::vec3{1.0f, 0.3f, 0.2f});
|
||||||
|
|||||||
+1
-1
@@ -16,7 +16,7 @@ public:
|
|||||||
sphere();
|
sphere();
|
||||||
auto init() -> bool override;
|
auto init() -> bool override;
|
||||||
auto update(float delta_time) -> void override;
|
auto update(float delta_time) -> void override;
|
||||||
auto render() -> void override;
|
auto render(int width, int height) -> void override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
opengl::shader m_prog;
|
opengl::shader m_prog;
|
||||||
|
|||||||
Reference in New Issue
Block a user