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,34 +3,34 @@ 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() => {
if !state.input(&event) {
match event { match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput { input, .. } => match input { WindowEvent::KeyboardInput { input, .. } => match input {
@ -50,7 +50,8 @@ impl Engine {
} }
_ => {} _ => {}
} }
}, }
}
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();
} }
_ => {} _ => {}
}); },
);
} }
} }