use winit::{ event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; pub struct Engine { title: &'static str, } impl Engine { pub fn new(title: &'static str) -> Engine { Engine { title } } pub async fn run(self) { let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title(self.title) .build(&event_loop) .unwrap(); let mut state = crate::State::new(&window).await; event_loop.run( move |event: Event<()>, _, control_flow: &mut ControlFlow| match event { Event::WindowEvent { ref event, window_id, } if window_id == window.id() => { if !state.input(&event) { match event { WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::KeyboardInput { input, .. } => match 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, .. } => { // new_inner_size is &&mut so we have to dereference it twice state.resize(**new_inner_size); } _ => {} } } } 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(); } _ => {} }, ); } }