From 898783ca80086353b8d216739cbd3a8def6872b5 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Tue, 21 Jun 2022 22:37:45 +0200 Subject: [PATCH] Reformat --- .vscode/settings.json | 3 ++ src/lib.rs | 58 +++++++++++++++------------- src/main.rs | 2 +- src/render/mod.rs | 2 +- src/render/pipelines/utils.rs | 2 +- src/render/renderer/default_state.rs | 9 ++--- src/render/renderer/mod.rs | 25 ++++++++---- src/render/renderer/renderer.rs | 20 +++++----- src/resources.rs | 3 +- 9 files changed, 71 insertions(+), 53 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e3f3c6c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.tabSize": 4 +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 08f7199..c855e4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc, ops::Deref}; +use std::{ops::Deref, sync::Arc}; use cgmath::prelude::*; use winit::{ @@ -9,7 +9,7 @@ use winit::{ #[cfg(target_arch = "wasm32")] use wasm_bindgen::prelude::*; -use crate::render::{Renderer, DefaultState, State}; +use crate::render::{DefaultState, Renderer, State}; mod camera; mod model; @@ -168,7 +168,9 @@ pub async fn run() { let mut renderer = Arc::from(Renderer::new(&window).await); let default_state = Box::from(DefaultState::new(renderer.deref())); - Arc::get_mut(&mut renderer).unwrap().set_state(Some(default_state)); + Arc::get_mut(&mut renderer) + .unwrap() + .set_state(Some(default_state)); let mut last_render_time = instant::Instant::now(); event_loop.run(move |base_event, _, control_flow| { *control_flow = ControlFlow::Poll; @@ -179,28 +181,28 @@ pub async fn run() { Event::WindowEvent { ref event, window_id, - } if window_id == window.id() && !renderer.input(&base_event) => { - match event { - #[cfg(not(target_arch="wasm32"))] - WindowEvent::CloseRequested - | WindowEvent::KeyboardInput { - input: - KeyboardInput { - state: ElementState::Pressed, - virtual_keycode: Some(VirtualKeyCode::Escape), - .. - }, - .. - } => *control_flow = ControlFlow::Exit, - WindowEvent::Resized(physical_size) => { - renderer.resize(*physical_size); - } - WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { - renderer.resize(**new_inner_size); - } - _ => { renderer.input(&base_event); } + } if window_id == window.id() && !renderer.input(&base_event) => match event { + #[cfg(not(target_arch = "wasm32"))] + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { + input: + KeyboardInput { + state: ElementState::Pressed, + virtual_keycode: Some(VirtualKeyCode::Escape), + .. + }, + .. + } => *control_flow = ControlFlow::Exit, + WindowEvent::Resized(physical_size) => { + renderer.resize(*physical_size); } - } + WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { + renderer.resize(**new_inner_size); + } + _ => { + renderer.input(&base_event); + } + }, Event::RedrawRequested(window_id) if window_id == window.id() => { let now = instant::Instant::now(); let dt = now - last_render_time; @@ -209,14 +211,18 @@ pub async fn run() { match renderer.render() { Ok(_) => {} // Reconfigure the surface if it's lost or outdated - Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => renderer.resize(renderer.size), + Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => { + renderer.resize(renderer.size) + } // The system is out of memory, we should probably quit Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit, // We're ignoring timeouts Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"), } } - _ => { renderer.input(&base_event); } + _ => { + renderer.input(&base_event); + } } }); } diff --git a/src/main.rs b/src/main.rs index 66a139e..1fe1616 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,4 +2,4 @@ use tuto1::run; fn main() { async_std::task::block_on(run()); -} \ No newline at end of file +} diff --git a/src/render/mod.rs b/src/render/mod.rs index c35b407..9c10d9d 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -4,4 +4,4 @@ pub use pipelines::utils::create_render_pipeline; pub use pipelines::{GlobalBindLayout, Pipelines}; mod renderer; -pub use renderer::{Renderer, DefaultState, State}; \ No newline at end of file +pub use renderer::{DefaultState, Renderer, State}; diff --git a/src/render/pipelines/utils.rs b/src/render/pipelines/utils.rs index 28ac4db..3fe2ce5 100644 --- a/src/render/pipelines/utils.rs +++ b/src/render/pipelines/utils.rs @@ -56,4 +56,4 @@ pub fn create_render_pipeline( // indicates how many array layers the attachments will have. multiview: None, }) -} \ No newline at end of file +} diff --git a/src/render/renderer/default_state.rs b/src/render/renderer/default_state.rs index 9fcacf8..133af6f 100644 --- a/src/render/renderer/default_state.rs +++ b/src/render/renderer/default_state.rs @@ -222,18 +222,15 @@ impl super::State for DefaultState { fn input(&mut self, event: &Event<()>) -> bool { match event { Event::DeviceEvent { - event: DeviceEvent::MouseMotion{ delta, }, + event: DeviceEvent::MouseMotion { delta }, .. } => { if self.mouse_pressed { self.camera_controller.process_mouse(delta.0, delta.1); } true - }, - Event::WindowEvent { - ref event, - .. - } => match event { + } + Event::WindowEvent { ref event, .. } => match event { WindowEvent::KeyboardInput { input: KeyboardInput { diff --git a/src/render/renderer/mod.rs b/src/render/renderer/mod.rs index 2dfbe98..acd2399 100644 --- a/src/render/renderer/mod.rs +++ b/src/render/renderer/mod.rs @@ -5,13 +5,24 @@ pub use renderer::Renderer; mod default_state; pub use default_state::DefaultState; -use wgpu::{TextureView, CommandEncoder, Queue}; +use wgpu::{CommandEncoder, Queue, TextureView}; use winit::event::Event; pub trait State { - fn new(renderer: &Renderer) -> Self where Self: Sized; - fn resize(&mut self, device: &wgpu::Device, config: &wgpu::SurfaceConfiguration, new_size: winit::dpi::PhysicalSize); - fn input(&mut self, event: &Event<()>) -> bool; - fn update(&mut self, queue: &Queue, dt: instant::Duration); - fn render(&mut self, view: &TextureView, encoder: &mut CommandEncoder) -> Result<(), wgpu::SurfaceError>; -} \ No newline at end of file + fn new(renderer: &Renderer) -> Self + where + Self: Sized; + fn resize( + &mut self, + device: &wgpu::Device, + config: &wgpu::SurfaceConfiguration, + new_size: winit::dpi::PhysicalSize, + ); + fn input(&mut self, event: &Event<()>) -> bool; + fn update(&mut self, queue: &Queue, dt: instant::Duration); + fn render( + &mut self, + view: &TextureView, + encoder: &mut CommandEncoder, + ) -> Result<(), wgpu::SurfaceError>; +} diff --git a/src/render/renderer/renderer.rs b/src/render/renderer/renderer.rs index f8096d9..267aedd 100644 --- a/src/render/renderer/renderer.rs +++ b/src/render/renderer/renderer.rs @@ -10,7 +10,7 @@ pub struct Renderer { pub queue: wgpu::Queue, pub size: winit::dpi::PhysicalSize, pub config: wgpu::SurfaceConfiguration, - state: Option> + state: Option>, } impl Renderer { @@ -63,7 +63,7 @@ impl Renderer { queue, config, size, - state: None + state: None, } } @@ -75,26 +75,26 @@ impl Renderer { self.surface.configure(&self.device, &self.config); if let Some(state) = self.state.as_mut() { - state.resize(&self.device, &self.config, new_size); + state.resize(&self.device, &self.config, new_size); } } } pub fn input(&mut self, event: &Event<()>) -> bool { if let Some(state) = self.state.as_mut() { - return state.input(event); + return state.input(event); } false } pub fn update(&mut self, dt: instant::Duration) { - if let Some(state) = self.state.as_mut() { - state.update(&self.queue, dt); - } + if let Some(state) = self.state.as_mut() { + state.update(&self.queue, dt); + } } pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> { - let output = self.surface.get_current_texture()?; + let output = self.surface.get_current_texture()?; let view = output .texture .create_view(&wgpu::TextureViewDescriptor::default()); @@ -106,7 +106,7 @@ impl Renderer { }); if let Some(state) = self.state.as_mut() { - state.render(&view, &mut encoder)?; + state.render(&view, &mut encoder)?; } self.queue.submit(iter::once(encoder.finish())); @@ -116,6 +116,6 @@ impl Renderer { } pub fn set_state(&mut self, state: Option>) { - self.state = state; + self.state = state; } } diff --git a/src/resources.rs b/src/resources.rs index 9748884..e4c2abb 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -13,7 +13,8 @@ fn format_url(file_name: &str) -> reqwest::Url { "{}/{}/", location.origin().unwrap(), option_env!("RES_PATH").unwrap_or("res"), - )).unwrap(); + )) + .unwrap(); base.join(file_name).unwrap() }