1
0
Fork 0

Rename engine to window

This commit is contained in:
Florian RICHER 2022-06-16 19:43:02 +02:00
parent 96d1e2441f
commit c2a32a3be6
4 changed files with 78 additions and 78 deletions

View file

@ -1,74 +0,0 @@
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();
}
_ => {}
},
);
}
}

View file

@ -1,6 +1,3 @@
mod engine;
pub use engine::Engine;
mod state;
pub use state::State;
@ -15,6 +12,6 @@ fn main() {
println!("Failed to start logger : {}", err);
}
let engine = Engine::new("Test 123");
let engine = render::Window::new("Test 123");
pollster::block_on(engine.run());
}

View file

@ -16,6 +16,9 @@ use wgpu::{Device, Queue};
mod mesh;
pub use mesh::Mesh;
mod window;
pub use window::Window;
pub trait Renderable {
fn initialize(&mut self, device: &Device);
fn update_instances(&mut self, queue: &Queue);

74
src/render/window.rs Normal file
View file

@ -0,0 +1,74 @@
use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
pub struct Window {
title: &'static str,
}
impl Window {
pub fn new(title: &'static str) -> Self {
Self { 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();
}
_ => {}
},
);
}
}