From 91db82d468cd052cb10f2311e2a1c9265fb92c9e Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 4 Sep 2024 18:06:53 +0200 Subject: [PATCH] Resize the surface at the correct time in examples (#236) --- examples/animation.rs | 17 +++++++++++++---- examples/fruit.rs | 19 +++++++++++-------- examples/rectangle.rs | 22 +++++++++++++++------- examples/winit.rs | 18 +++++++++++++----- examples/winit_wrong_sized_buffer.rs | 17 +++++++++-------- 5 files changed, 61 insertions(+), 32 deletions(-) diff --git a/examples/animation.rs b/examples/animation.rs index 72b387b..6df2265 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -31,14 +31,24 @@ fn main() { elwt.set_control_flow(ControlFlow::Poll); match event { + Event::WindowEvent { + window_id, + event: WindowEvent::Resized(size), + } if window_id == window.id() => { + if let (Some(width), Some(height)) = + (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) + { + surface.resize(width, height).unwrap(); + } + } Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested, } if window_id == window.id() => { - if let (Some(width), Some(height)) = { - let size = window.inner_size(); + let size = window.inner_size(); + if let (Some(width), Some(height)) = (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) - } { + { let elapsed = start.elapsed().as_secs_f64() % 1.0; if (width.get(), height.get()) != *old_size { @@ -48,7 +58,6 @@ fn main() { let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)]; - surface.resize(width, height).unwrap(); let mut buffer = surface.buffer_mut().unwrap(); buffer.copy_from_slice(frame); buffer.present().unwrap(); diff --git a/examples/fruit.rs b/examples/fruit.rs index f276ee9..cecd4af 100644 --- a/examples/fruit.rs +++ b/examples/fruit.rs @@ -20,7 +20,17 @@ fn main() { }); let context = softbuffer::Context::new(window.clone()).unwrap(); - let surface = softbuffer::Surface::new(&context, window.clone()).unwrap(); + let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap(); + + // Intentionally only set the size of the surface once, at creation. + // This is needed if the window chooses to ignore the size we passed in above, and for the + // platforms softbuffer supports that don't yet extract the size from the window. + surface + .resize( + NonZeroU32::new(width).unwrap(), + NonZeroU32::new(height).unwrap(), + ) + .unwrap(); (window, surface) }) @@ -33,13 +43,6 @@ fn main() { window_id, event: WindowEvent::RedrawRequested, } if window_id == window.id() => { - surface - .resize( - NonZeroU32::new(fruit.width()).unwrap(), - NonZeroU32::new(fruit.height()).unwrap(), - ) - .unwrap(); - let mut buffer = surface.buffer_mut().unwrap(); let width = fruit.width() as usize; for (x, y, pixel) in fruit.pixels() { diff --git a/examples/rectangle.rs b/examples/rectangle.rs index 7ab5917..41f8be4 100644 --- a/examples/rectangle.rs +++ b/examples/rectangle.rs @@ -45,16 +45,24 @@ fn main() { match event { Event::WindowEvent { window_id, - event: WindowEvent::RedrawRequested, + event: WindowEvent::Resized(size), } if window_id == window.id() => { - // Grab the window's client area dimensions - if let (Some(width), Some(height)) = { - let size = window.inner_size(); + if let (Some(width), Some(height)) = (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) - } { - // Resize surface if needed + { + // Resize surface surface.resize(width, height).unwrap(); - + } + } + Event::WindowEvent { + window_id, + event: WindowEvent::RedrawRequested, + } if window_id == window.id() => { + // Grab the window's client area dimensions, and ensure they're valid + let size = window.inner_size(); + if let (Some(width), Some(height)) = + (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) + { // Draw something in the window let mut buffer = surface.buffer_mut().unwrap(); redraw( diff --git a/examples/winit.rs b/examples/winit.rs index 7678fa5..3d3f261 100644 --- a/examples/winit.rs +++ b/examples/winit.rs @@ -24,14 +24,22 @@ fn main() { match event { Event::WindowEvent { window_id, - event: WindowEvent::RedrawRequested, + event: WindowEvent::Resized(size), } if window_id == window.id() => { - if let (Some(width), Some(height)) = { - let size = window.inner_size(); + if let (Some(width), Some(height)) = (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) - } { + { surface.resize(width, height).unwrap(); - + } + } + Event::WindowEvent { + window_id, + event: WindowEvent::RedrawRequested, + } if window_id == window.id() => { + let size = window.inner_size(); + if let (Some(width), Some(height)) = + (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) + { let mut buffer = surface.buffer_mut().unwrap(); for y in 0..height.get() { for x in 0..width.get() { diff --git a/examples/winit_wrong_sized_buffer.rs b/examples/winit_wrong_sized_buffer.rs index c0396d3..95e3e07 100644 --- a/examples/winit_wrong_sized_buffer.rs +++ b/examples/winit_wrong_sized_buffer.rs @@ -16,7 +16,15 @@ fn main() { let window = winit_app::make_window(elwt, |w| w); let context = softbuffer::Context::new(window.clone()).unwrap(); - let surface = softbuffer::Surface::new(&context, window.clone()).unwrap(); + let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap(); + + // Intentionally set the size of the surface to something different than the size of the window. + surface + .resize( + NonZeroU32::new(BUFFER_WIDTH as u32).unwrap(), + NonZeroU32::new(BUFFER_HEIGHT as u32).unwrap(), + ) + .unwrap(); (window, surface) }) @@ -29,13 +37,6 @@ fn main() { window_id, event: WindowEvent::RedrawRequested, } if window_id == window.id() => { - surface - .resize( - NonZeroU32::new(BUFFER_WIDTH as u32).unwrap(), - NonZeroU32::new(BUFFER_HEIGHT as u32).unwrap(), - ) - .unwrap(); - let mut buffer = surface.buffer_mut().unwrap(); for y in 0..BUFFER_HEIGHT { for x in 0..BUFFER_WIDTH {