|
@@ -8,7 +8,8 @@ use wgpu::SurfaceError;
|
|
|
use winit::{
|
|
use winit::{
|
|
|
dpi::PhysicalSize,
|
|
dpi::PhysicalSize,
|
|
|
event::*,
|
|
event::*,
|
|
|
- event_loop::{ControlFlow, EventLoop},
|
|
|
|
|
|
|
+ event_loop::EventLoop,
|
|
|
|
|
+ keyboard::{Key, NamedKey},
|
|
|
window::WindowBuilder
|
|
window::WindowBuilder
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -18,40 +19,37 @@ fn main() {
|
|
|
let path = std::env::args().nth(1)
|
|
let path = std::env::args().nth(1)
|
|
|
.expect("Run with a mesh file");
|
|
.expect("Run with a mesh file");
|
|
|
|
|
|
|
|
- let event_loop = EventLoop::new();
|
|
|
|
|
|
|
+ let event_loop = EventLoop::new()
|
|
|
|
|
+ .expect("Failed to create event loop");
|
|
|
|
|
+
|
|
|
let window = WindowBuilder::new()
|
|
let window = WindowBuilder::new()
|
|
|
.with_title("chair model viewer")
|
|
.with_title("chair model viewer")
|
|
|
.with_inner_size(PhysicalSize::new(1280, 720))
|
|
.with_inner_size(PhysicalSize::new(1280, 720))
|
|
|
- // TODO: center on display
|
|
|
|
|
.build(&event_loop)
|
|
.build(&event_loop)
|
|
|
.unwrap();
|
|
.unwrap();
|
|
|
|
|
+ let id = window.id();
|
|
|
|
|
|
|
|
let mut renderer = Renderer::new(&window, &path).block_on();
|
|
let mut renderer = Renderer::new(&window, &path).block_on();
|
|
|
|
|
|
|
|
- event_loop.run(move |event, _, control_flow| match event {
|
|
|
|
|
|
|
+ event_loop.run(move |event, elwt| match event {
|
|
|
Event::WindowEvent {
|
|
Event::WindowEvent {
|
|
|
ref event,
|
|
ref event,
|
|
|
window_id
|
|
window_id
|
|
|
- } if window_id == window.id() => {
|
|
|
|
|
|
|
+ } if window_id == id => {
|
|
|
match event {
|
|
match event {
|
|
|
// Exit when window is closed or ESC is pressed
|
|
// Exit when window is closed or ESC is pressed
|
|
|
WindowEvent::CloseRequested
|
|
WindowEvent::CloseRequested
|
|
|
| WindowEvent::KeyboardInput {
|
|
| WindowEvent::KeyboardInput {
|
|
|
- input: KeyboardInput {
|
|
|
|
|
|
|
+ event: KeyEvent {
|
|
|
state: ElementState::Pressed,
|
|
state: ElementState::Pressed,
|
|
|
- virtual_keycode: Some(VirtualKeyCode::Escape),
|
|
|
|
|
|
|
+ logical_key: Key::Named(NamedKey::Escape),
|
|
|
..
|
|
..
|
|
|
},
|
|
},
|
|
|
..
|
|
..
|
|
|
- } => *control_flow = ControlFlow::Exit,
|
|
|
|
|
|
|
+ } => elwt.exit(),
|
|
|
|
|
|
|
|
// Handle window resizing
|
|
// Handle window resizing
|
|
|
- WindowEvent::Resized(size) => {
|
|
|
|
|
- renderer.resize(*size);
|
|
|
|
|
- },
|
|
|
|
|
- WindowEvent::ScaleFactorChanged {new_inner_size, ..} => {
|
|
|
|
|
- renderer.resize(**new_inner_size);
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ WindowEvent::Resized(size) => renderer.resize(*size),
|
|
|
|
|
|
|
|
// Handle user input
|
|
// Handle user input
|
|
|
WindowEvent::CursorMoved {position, ..} => {
|
|
WindowEvent::CursorMoved {position, ..} => {
|
|
@@ -60,28 +58,22 @@ fn main() {
|
|
|
|
|
|
|
|
_ => {}
|
|
_ => {}
|
|
|
}
|
|
}
|
|
|
- },
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// Handle drawing
|
|
// Handle drawing
|
|
|
- Event::RedrawRequested(window_id) if window_id == window.id() => {
|
|
|
|
|
- match renderer.render() {
|
|
|
|
|
- Ok(_) => {},
|
|
|
|
|
- // Reconfigure the surface if lost
|
|
|
|
|
- Err(SurfaceError::Lost) => renderer.recreate(),
|
|
|
|
|
- // death
|
|
|
|
|
- Err(SurfaceError::OutOfMemory) => {
|
|
|
|
|
- eprintln!("Out of memory!");
|
|
|
|
|
- *control_flow = ControlFlow::Exit;
|
|
|
|
|
- },
|
|
|
|
|
- // All other errors should be temporary
|
|
|
|
|
- Err(e) => eprintln!("{:?}", e)
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
- Event::MainEventsCleared => {
|
|
|
|
|
- // RedrawRequested will only trigger once, unless manually requested
|
|
|
|
|
- window.request_redraw();
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ Event::AboutToWait => match renderer.render() {
|
|
|
|
|
+ Ok(_) => {},
|
|
|
|
|
+ // Reconfigure the surface if lost
|
|
|
|
|
+ Err(SurfaceError::Lost) => renderer.recreate(),
|
|
|
|
|
+ // death
|
|
|
|
|
+ Err(SurfaceError::OutOfMemory) => {
|
|
|
|
|
+ eprintln!("Out of memory!");
|
|
|
|
|
+ elwt.exit();
|
|
|
|
|
+ },
|
|
|
|
|
+ // All other errors should be temporary
|
|
|
|
|
+ Err(e) => eprintln!("{:?}", e)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
_ => {}
|
|
_ => {}
|
|
|
- });
|
|
|
|
|
|
|
+ }).expect("Failed to run event loop");
|
|
|
}
|
|
}
|