Update
This commit is contained in:
parent
4a7246cbd8
commit
9962782e0f
1 changed files with 29 additions and 27 deletions
|
@ -3,54 +3,55 @@ mod state;
|
|||
use winit::{
|
||||
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
|
||||
event_loop::{ControlFlow, EventLoop},
|
||||
window::{WindowBuilder},
|
||||
window::WindowBuilder,
|
||||
};
|
||||
|
||||
pub struct Engine {
|
||||
title: &'static str
|
||||
title: &'static str,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn new(title: &'static str) -> Engine {
|
||||
Engine {
|
||||
title
|
||||
}
|
||||
Engine { title }
|
||||
}
|
||||
|
||||
pub async fn run(self) {
|
||||
let event_loop = EventLoop::new();
|
||||
let window = WindowBuilder::new()
|
||||
.with_title(self.title)
|
||||
.build(&event_loop).unwrap();
|
||||
.build(&event_loop)
|
||||
.unwrap();
|
||||
|
||||
let mut state = state::State::new(&window).await;
|
||||
|
||||
event_loop
|
||||
.run(move |event: Event<()>, _, control_flow: &mut ControlFlow | match event {
|
||||
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,
|
||||
} 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);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
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() {
|
||||
|
@ -69,6 +70,7 @@ impl Engine {
|
|||
window.request_redraw();
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue