diff options
author | HampusM <hampus@hampusmat.com> | 2023-10-07 20:05:52 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-10-07 20:05:52 +0200 |
commit | 146635292369cc8a9660139d97cd9662025bd591 (patch) | |
tree | 65da7bde1a542cdfbabfc629d486dd6a7fd18b26 | |
parent | b34b393f7b07495ca92ff5b0ce79d4c391abe0ca (diff) |
fix(engine): update viewport when framebuffer changes size
-rw-r--r-- | engine/src/lib.rs | 6 | ||||
-rw-r--r-- | engine/src/renderer/mod.rs | 20 | ||||
-rw-r--r-- | engine/src/vector.rs | 5 |
3 files changed, 22 insertions, 9 deletions
diff --git a/engine/src/lib.rs b/engine/src/lib.rs index 618bc75..ea74fd1 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -3,6 +3,8 @@ use glfw::Window; pub use glfw::WindowSize; +use crate::vector::Vec2; + mod renderer; pub mod vector; @@ -27,6 +29,10 @@ impl Engine .make_context_current() .map_err(Error::ConfigureWindowFailed)?; + window.set_framebuffer_size_callback(move |new_window_size| { + crate::renderer::set_viewport(&Vec2::ZERO, &new_window_size); + }); + crate::renderer::initialize(&window).map_err(Error::InitializeRendererFailed)?; Ok(Self { window }) diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index c34441c..9cd379d 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -1,6 +1,8 @@ use std::ffi::{c_void, CString}; use std::process::abort; +use glfw::WindowSize; + use crate::vector::Vec2; pub fn initialize(window: &glfw::Window) -> Result<(), Error> @@ -35,15 +37,7 @@ pub fn render() } } -/// Renderer error. -#[derive(Debug, thiserror::Error)] -pub enum Error -{ - #[error("Failed to get window size")] - GetWindowSizeFailed(#[source] glfw::Error), -} - -fn set_viewport(position: &Vec2<u32>, size: &crate::WindowSize) +pub fn set_viewport(position: &Vec2<u32>, size: &WindowSize) { unsafe { #[allow(clippy::cast_possible_wrap)] @@ -55,3 +49,11 @@ fn set_viewport(position: &Vec2<u32>, size: &crate::WindowSize) ); } } + +/// Renderer error. +#[derive(Debug, thiserror::Error)] +pub enum Error +{ + #[error("Failed to get window size")] + GetWindowSizeFailed(#[source] glfw::Error), +} diff --git a/engine/src/vector.rs b/engine/src/vector.rs index 92192bf..48550fa 100644 --- a/engine/src/vector.rs +++ b/engine/src/vector.rs @@ -4,3 +4,8 @@ pub struct Vec2<Value> pub x: Value, pub y: Value, } + +impl Vec2<u32> +{ + pub const ZERO: Self = Self { x: 0, y: 0 }; +} |