From 6bb305fb724a64553db11e4c51acd7a32f6c7f93 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 1 Sep 2024 07:06:39 +0200 Subject: [PATCH 1/3] Resize the surface at the correct times in examples --- examples/animation.rs | 19 ++++++++++++++----- examples/fruit.rs | 18 ++++++++++-------- examples/rectangle.rs | 22 +++++++++++++++------- examples/winit.rs | 18 +++++++++++++----- examples/winit_wrong_sized_buffer.rs | 17 +++++++++-------- 5 files changed, 61 insertions(+), 33 deletions(-) diff --git a/examples/animation.rs b/examples/animation.rs index 72b387b0..2b004f67 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -33,12 +33,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 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 f276ee9d..cad49a55 100644 --- a/examples/fruit.rs +++ b/examples/fruit.rs @@ -20,7 +20,16 @@ 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. + surface + .resize( + NonZeroU32::new(width).unwrap(), + NonZeroU32::new(height).unwrap(), + ) + .unwrap(); (window, surface) }) @@ -33,13 +42,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 7ab59175..41f8be40 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 7678fa5e..3d3f261b 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 c0396d3a..95e3e07e 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 { From 16b7f212df83aa6559f042c31e1bdb1ea844113d Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 4 Sep 2024 15:50:12 +0200 Subject: [PATCH 2/3] Fix extra brackets --- examples/animation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/animation.rs b/examples/animation.rs index 2b004f67..6df2265e 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -47,7 +47,7 @@ fn main() { } if window_id == window.id() => { let size = window.inner_size(); if let (Some(width), Some(height)) = - { (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) } + (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) { let elapsed = start.elapsed().as_secs_f64() % 1.0; From 624ff5ef872cd3ffa4868394fc3869e3ba459df5 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 4 Sep 2024 15:52:20 +0200 Subject: [PATCH 3/3] Fix note of why we call `resize` --- examples/fruit.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/fruit.rs b/examples/fruit.rs index cad49a55..cecd4afc 100644 --- a/examples/fruit.rs +++ b/examples/fruit.rs @@ -23,7 +23,8 @@ fn main() { 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. + // 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(),