Skip to content

Commit

Permalink
Resize the surface at the correct times in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 1, 2024
1 parent d5cf875 commit 6bb305f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 33 deletions.
19 changes: 14 additions & 5 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
18 changes: 10 additions & 8 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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() {
Expand Down
22 changes: 15 additions & 7 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 13 additions & 5 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
17 changes: 9 additions & 8 deletions examples/winit_wrong_sized_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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 {
Expand Down

0 comments on commit 6bb305f

Please sign in to comment.