From c2a32a3be6aac511992c872b65022ae29dd639da Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Thu, 16 Jun 2022 19:43:02 +0200 Subject: [PATCH] Rename engine to window --- src/engine.rs | 74 -------------------------------------------- src/main.rs | 5 +-- src/render/mod.rs | 3 ++ src/render/window.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 78 deletions(-) delete mode 100644 src/engine.rs create mode 100644 src/render/window.rs diff --git a/src/engine.rs b/src/engine.rs deleted file mode 100644 index 578e28e..0000000 --- a/src/engine.rs +++ /dev/null @@ -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(); - } - _ => {} - }, - ); - } -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3540f2b..45f46e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()); } diff --git a/src/render/mod.rs b/src/render/mod.rs index 5739d00..03d8a7b 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -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); diff --git a/src/render/window.rs b/src/render/window.rs new file mode 100644 index 0000000..4edb999 --- /dev/null +++ b/src/render/window.rs @@ -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(); + } + _ => {} + }, + ); + } +}