Reformat
This commit is contained in:
parent
0145968267
commit
898783ca80
9 changed files with 71 additions and 53 deletions
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"editor.tabSize": 4
|
||||
}
|
24
src/lib.rs
24
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,8 +181,7 @@ pub async fn run() {
|
|||
Event::WindowEvent {
|
||||
ref event,
|
||||
window_id,
|
||||
} if window_id == window.id() && !renderer.input(&base_event) => {
|
||||
match event {
|
||||
} if window_id == window.id() && !renderer.input(&base_event) => match event {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
WindowEvent::CloseRequested
|
||||
| WindowEvent::KeyboardInput {
|
||||
|
@ -198,9 +199,10 @@ pub async fn run() {
|
|||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||
renderer.resize(**new_inner_size);
|
||||
}
|
||||
_ => { renderer.input(&base_event); }
|
||||
}
|
||||
_ => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -4,4 +4,4 @@ pub use pipelines::utils::create_render_pipeline;
|
|||
pub use pipelines::{GlobalBindLayout, Pipelines};
|
||||
|
||||
mod renderer;
|
||||
pub use renderer::{Renderer, DefaultState, State};
|
||||
pub use renderer::{DefaultState, Renderer, State};
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<u32>);
|
||||
fn new(renderer: &Renderer) -> Self
|
||||
where
|
||||
Self: Sized;
|
||||
fn resize(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
config: &wgpu::SurfaceConfiguration,
|
||||
new_size: winit::dpi::PhysicalSize<u32>,
|
||||
);
|
||||
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>;
|
||||
fn render(
|
||||
&mut self,
|
||||
view: &TextureView,
|
||||
encoder: &mut CommandEncoder,
|
||||
) -> Result<(), wgpu::SurfaceError>;
|
||||
}
|
|
@ -10,7 +10,7 @@ pub struct Renderer {
|
|||
pub queue: wgpu::Queue,
|
||||
pub size: winit::dpi::PhysicalSize<u32>,
|
||||
pub config: wgpu::SurfaceConfiguration,
|
||||
state: Option<Box<dyn State>>
|
||||
state: Option<Box<dyn State>>,
|
||||
}
|
||||
|
||||
impl Renderer {
|
||||
|
@ -63,7 +63,7 @@ impl Renderer {
|
|||
queue,
|
||||
config,
|
||||
size,
|
||||
state: None
|
||||
state: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue