Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize the surface at the correct time in examples #236

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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
19 changes: 11 additions & 8 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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() {
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();
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
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