1
0
Fork 0
This commit is contained in:
Florian RICHER 2022-06-08 17:19:28 +02:00
parent 4a7246cbd8
commit 9962782e0f

View file

@ -3,54 +3,55 @@ mod state;
use winit::{ use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::{WindowBuilder}, window::WindowBuilder,
}; };
pub struct Engine { pub struct Engine {
title: &'static str title: &'static str,
} }
impl Engine { impl Engine {
pub fn new(title: &'static str) -> Engine { pub fn new(title: &'static str) -> Engine {
Engine { Engine { title }
title
}
} }
pub async fn run(self) { pub async fn run(self) {
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
let window = WindowBuilder::new() let window = WindowBuilder::new()
.with_title(self.title) .with_title(self.title)
.build(&event_loop).unwrap(); .build(&event_loop)
.unwrap();
let mut state = state::State::new(&window).await; let mut state = state::State::new(&window).await;
event_loop event_loop.run(
.run(move |event: Event<()>, _, control_flow: &mut ControlFlow | match event { move |event: Event<()>, _, control_flow: &mut ControlFlow| match event {
Event::WindowEvent { Event::WindowEvent {
ref event, ref event,
window_id, window_id,
} if window_id == window.id() => if !state.input(&event) { } if window_id == window.id() => {
match event { if !state.input(&event) {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, match event {
WindowEvent::KeyboardInput { input, .. } => match input { WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
KeyboardInput { WindowEvent::KeyboardInput { input, .. } => match input {
state: ElementState::Pressed, KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape), state: ElementState::Pressed,
.. virtual_keycode: Some(VirtualKeyCode::Escape),
} => *control_flow = ControlFlow::Exit, ..
} => *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);
}
_ => {} _ => {}
},
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() => { Event::RedrawRequested(window_id) if window_id == window.id() => {
state.update(); state.update();
match state.render() { match state.render() {
@ -69,6 +70,7 @@ impl Engine {
window.request_redraw(); window.request_redraw();
} }
_ => {} _ => {}
}); },
);
} }
} }