79 lines
2.7 KiB
Rust
79 lines
2.7 KiB
Rust
use winit::{
|
|
event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode},
|
|
event_loop::{ControlFlow, EventLoop},
|
|
window::WindowBuilder,
|
|
dpi::LogicalSize
|
|
};
|
|
|
|
mod app;
|
|
use app::App;
|
|
|
|
fn main() {
|
|
// Create event loop
|
|
let events = EventLoop::new();
|
|
|
|
// Spawn window
|
|
let window = WindowBuilder::new()
|
|
.with_title("Test")
|
|
.with_inner_size(LogicalSize::new(1280.0, 720.0))
|
|
.build(&events)
|
|
.expect("Failed to create window");
|
|
|
|
// Create app
|
|
let mut app = futures::executor::block_on(App::new(&window));
|
|
|
|
// Run event loop
|
|
events.run(move |e, _, c| {
|
|
match e {
|
|
// Window event
|
|
Event::WindowEvent { ref event, .. } => if !app.input(event) {
|
|
match event {
|
|
// Window closed
|
|
WindowEvent::CloseRequested => {
|
|
*c = ControlFlow::Exit;
|
|
}
|
|
// Keyboard input
|
|
WindowEvent::KeyboardInput { input, .. } => {
|
|
match input {
|
|
// Escape key pressed
|
|
KeyboardInput { state: ElementState::Pressed, virtual_keycode: Some(VirtualKeyCode::Escape), .. } => {
|
|
*c = ControlFlow::Exit;
|
|
}
|
|
// Other keys
|
|
_ => ()
|
|
}
|
|
}
|
|
// Window resized
|
|
WindowEvent::Resized(physical_size) => {
|
|
app.resize(Some(*physical_size));
|
|
}
|
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
|
app.resize(Some(**new_inner_size));
|
|
}
|
|
// Other window events
|
|
_ => ()
|
|
}
|
|
}
|
|
// Request draw
|
|
Event::MainEventsCleared => {
|
|
window.request_redraw();
|
|
}
|
|
// Draw
|
|
Event::RedrawRequested(_) => {
|
|
app.update();
|
|
match app.render() {
|
|
Ok(_) => {}
|
|
// Recreate the swap_chain if lost
|
|
Err(wgpu::SwapChainError::Lost) => app.resize(None),
|
|
// The system is out of memory, we should probably quit
|
|
Err(wgpu::SwapChainError::OutOfMemory) => *c = ControlFlow::Exit,
|
|
// All other errors (Outdated, Timeout) should be resolved by the next frame
|
|
Err(e) => eprintln!("{:?}", e),
|
|
}
|
|
}
|
|
// Other events
|
|
_ => ()
|
|
}
|
|
});
|
|
}
|