Skip to content

Commit

Permalink
Check window size before redrawing
Browse files Browse the repository at this point in the history
  • Loading branch information
thenlevy committed Dec 6, 2021
1 parent 26d053a commit e2d6d2d
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions examples/integration_wgpu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ use controls::Controls;
use scene::Scene;

use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport};
use iced_winit::{conversion, futures, program, winit, Clipboard, Debug, Size};
use iced_winit::{
conversion, futures, program,
winit::{self, dpi::PhysicalSize},
Clipboard, Debug, Size,
};

use futures::task::SpawnExt;
use winit::{
dpi::PhysicalPosition,
event::{Event, ModifiersState, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::Window,
};

pub fn main() {
env_logger::init();

// Initialize winit
let event_loop = EventLoop::new();
let window = winit::window::Window::new(&event_loop).unwrap();
let window = Window::new(&event_loop).unwrap();

let physical_size = window.inner_size();
let mut viewport = Viewport::with_physical_size(
Expand Down Expand Up @@ -77,6 +82,7 @@ pub fn main() {
)
};
let mut resized = false;
let mut last_processed_size = window.inner_size();

// Initialize staging belt and local pool
let mut staging_belt = wgpu::util::StagingBelt::new(5 * 1024);
Expand Down Expand Up @@ -112,11 +118,8 @@ pub fn main() {
WindowEvent::ModifiersChanged(new_modifiers) => {
modifiers = new_modifiers;
}
WindowEvent::Resized(new_size) => {
viewport = Viewport::with_physical_size(
Size::new(new_size.width, new_size.height),
window.scale_factor(),
);
WindowEvent::Resized(_) => {
process_resize(&mut viewport, &mut last_processed_size, &window);

resized = true;
}
Expand Down Expand Up @@ -156,15 +159,21 @@ pub fn main() {
}
Event::RedrawRequested(_) => {
if resized {
let size = window.inner_size();

if last_processed_size != window.inner_size() {
// This can happen when resizing the window quickly, we enter this branch
// with the wrong window size, wich could cause validations errors
process_resize(&mut viewport, &mut last_processed_size, &window);
resized = true;
window.request_redraw();
return;
}
surface.configure(
&device,
&wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: format,
width: size.width,
height: size.height,
width: last_processed_size.width,
height: last_processed_size.height,
present_mode: wgpu::PresentMode::Mailbox,
},
);
Expand All @@ -190,6 +199,7 @@ pub fn main() {
program.background_color(),
);


// Draw the scene
scene.draw(&mut render_pass);
}
Expand All @@ -213,11 +223,11 @@ pub fn main() {
frame.present();

// Update the mouse cursor
window.set_cursor_icon(
iced_winit::conversion::mouse_interaction(
state.mouse_interaction(),
),
);
window.set_cursor_icon(
iced_winit::conversion::mouse_interaction(
state.mouse_interaction(),
),
);

// And recall staging buffers
local_pool
Expand All @@ -237,8 +247,22 @@ pub fn main() {
}
},
}
window.set_min_inner_size(Some(last_processed_size).filter(|_| false)); // using the filter to infer the type of None
}
_ => {}
}
})
}

fn process_resize(
viewport: &mut Viewport,
last_processed_size: &mut PhysicalSize<u32>,
window: &Window,
) {
let new_size = window.inner_size();
*last_processed_size = window.inner_size();
*viewport = Viewport::with_physical_size(
Size::new(new_size.width, new_size.height),
window.scale_factor(),
);
}

0 comments on commit e2d6d2d

Please sign in to comment.