From 5ad62fff05064a6a025a9b947ebab05ed7770e6c Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:21 +0100 Subject: feat(render): handle window resize and close events again --- stockton-render/src/lib.rs | 4 ++++ stockton-render/src/window.rs | 32 +++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) (limited to 'stockton-render') diff --git a/stockton-render/src/lib.rs b/stockton-render/src/lib.rs index 7cea5e7..4d10ff8 100644 --- a/stockton-render/src/lib.rs +++ b/stockton-render/src/lib.rs @@ -87,6 +87,10 @@ impl<'a> Renderer<'a> { self.context.draw_vertices(map, &faces).unwrap(); } } + + fn resize(&mut self) { + unsafe { self.context.handle_surface_change().unwrap() }; + } } /// A system that just renders the world. diff --git a/stockton-render/src/window.rs b/stockton-render/src/window.rs index ab19a6a..6c4cecd 100644 --- a/stockton-render/src/window.rs +++ b/stockton-render/src/window.rs @@ -18,18 +18,40 @@ use crate::Renderer; use winit::event::Event as WinitEvent; +use winit::event::WindowEvent as WinitWindowEvent; +use winit::event_loop::ControlFlow; -pub struct WindowEvent {} +#[derive(Debug, Clone, Copy)] +pub enum WindowEvent { + SizeChanged, + CloseRequested, +} impl WindowEvent { - pub fn from(_winit_event: &WinitEvent<()>) -> WindowEvent { + pub fn from(winit_event: &WinitEvent<()>) -> Option { // TODO - WindowEvent {} + match winit_event { + WinitEvent::WindowEvent { event, .. } => match event { + WinitWindowEvent::CloseRequested => Some(WindowEvent::CloseRequested), + WinitWindowEvent::Resized(_) => Some(WindowEvent::SizeChanged), + _ => None, + }, + _ => None, + } } } #[system] /// A system to process the window events sent to renderer by the winit event loop. -pub fn process_window_events(#[resource] _renderer: &mut Renderer<'static>) { - println!("processing window events..."); +pub fn process_window_events(#[resource] renderer: &mut Renderer<'static>) { + while let Ok(event) = renderer.window_events.try_recv() { + match event { + WindowEvent::SizeChanged => renderer.resize(), + WindowEvent::CloseRequested => { + let mut flow = renderer.update_control_flow.write().unwrap(); + // TODO: Let everything know this is our last frame + *flow = ControlFlow::Exit; + } + }; + } } -- cgit v1.2.3