65 lines
2.5 KiB
Rust
65 lines
2.5 KiB
Rust
|
use super::state::State;
|
||
|
use winit::{
|
||
|
event::*,
|
||
|
event_loop::{ControlFlow, EventLoop},
|
||
|
window::WindowBuilder,
|
||
|
};
|
||
|
|
||
|
pub async fn run() {
|
||
|
let event_loop = EventLoop::new();
|
||
|
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||
|
let mut state = State::new(&window).await;
|
||
|
|
||
|
// Event loop
|
||
|
event_loop.run(move |event, _, control_flow| {
|
||
|
match event {
|
||
|
Event::WindowEvent {
|
||
|
ref event,
|
||
|
window_id,
|
||
|
} if window_id == window.id() => {
|
||
|
if !state.input(event) {
|
||
|
// UPDATED!
|
||
|
match event {
|
||
|
WindowEvent::CloseRequested
|
||
|
| WindowEvent::KeyboardInput {
|
||
|
input:
|
||
|
KeyboardInput {
|
||
|
state: ElementState::Pressed,
|
||
|
virtual_keycode: Some(VirtualKeyCode::Escape),
|
||
|
..
|
||
|
},
|
||
|
..
|
||
|
} => *control_flow = ControlFlow::Exit,
|
||
|
WindowEvent::Resized(physical_size) => {
|
||
|
state.resize(*physical_size);
|
||
|
}
|
||
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||
|
state.resize(**new_inner_size);
|
||
|
}
|
||
|
WindowEvent::CursorMoved { position, .. } => {}
|
||
|
_ => {}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
||
|
state.update();
|
||
|
match state.render() {
|
||
|
Ok(_) => {}
|
||
|
// Reconfigure the surface if lost
|
||
|
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
|
||
|
// The system is out of memory, we should probably quit
|
||
|
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||
|
// All other errors (Outdated, Timeout) should be resolved by the next frame
|
||
|
Err(e) => eprintln!("{:?}", e),
|
||
|
}
|
||
|
}
|
||
|
Event::MainEventsCleared => {
|
||
|
// RedrawRequested will only trigger once, unless we manually
|
||
|
// request it.
|
||
|
window.request_redraw();
|
||
|
}
|
||
|
_ => {}
|
||
|
}
|
||
|
});
|
||
|
}
|