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
|
||||||
|
}
|
26
src/lib.rs
26
src/lib.rs
|
@ -1,4 +1,4 @@
|
||||||
use std::{sync::Arc, ops::Deref};
|
use std::{ops::Deref, sync::Arc};
|
||||||
|
|
||||||
use cgmath::prelude::*;
|
use cgmath::prelude::*;
|
||||||
use winit::{
|
use winit::{
|
||||||
|
@ -9,7 +9,7 @@ use winit::{
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
use crate::render::{Renderer, DefaultState, State};
|
use crate::render::{DefaultState, Renderer, State};
|
||||||
|
|
||||||
mod camera;
|
mod camera;
|
||||||
mod model;
|
mod model;
|
||||||
|
@ -168,7 +168,9 @@ pub async fn run() {
|
||||||
|
|
||||||
let mut renderer = Arc::from(Renderer::new(&window).await);
|
let mut renderer = Arc::from(Renderer::new(&window).await);
|
||||||
let default_state = Box::from(DefaultState::new(renderer.deref()));
|
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();
|
let mut last_render_time = instant::Instant::now();
|
||||||
event_loop.run(move |base_event, _, control_flow| {
|
event_loop.run(move |base_event, _, control_flow| {
|
||||||
*control_flow = ControlFlow::Poll;
|
*control_flow = ControlFlow::Poll;
|
||||||
|
@ -179,9 +181,8 @@ pub async fn run() {
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
ref event,
|
ref event,
|
||||||
window_id,
|
window_id,
|
||||||
} if window_id == window.id() && !renderer.input(&base_event) => {
|
} if window_id == window.id() && !renderer.input(&base_event) => match event {
|
||||||
match event {
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
#[cfg(not(target_arch="wasm32"))]
|
|
||||||
WindowEvent::CloseRequested
|
WindowEvent::CloseRequested
|
||||||
| WindowEvent::KeyboardInput {
|
| WindowEvent::KeyboardInput {
|
||||||
input:
|
input:
|
||||||
|
@ -198,9 +199,10 @@ pub async fn run() {
|
||||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||||
renderer.resize(**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() => {
|
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
||||||
let now = instant::Instant::now();
|
let now = instant::Instant::now();
|
||||||
let dt = now - last_render_time;
|
let dt = now - last_render_time;
|
||||||
|
@ -209,14 +211,18 @@ pub async fn run() {
|
||||||
match renderer.render() {
|
match renderer.render() {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
// Reconfigure the surface if it's lost or outdated
|
// 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
|
// The system is out of memory, we should probably quit
|
||||||
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||||||
// We're ignoring timeouts
|
// We're ignoring timeouts
|
||||||
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
|
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};
|
pub use pipelines::{GlobalBindLayout, Pipelines};
|
||||||
|
|
||||||
mod renderer;
|
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 {
|
fn input(&mut self, event: &Event<()>) -> bool {
|
||||||
match event {
|
match event {
|
||||||
Event::DeviceEvent {
|
Event::DeviceEvent {
|
||||||
event: DeviceEvent::MouseMotion{ delta, },
|
event: DeviceEvent::MouseMotion { delta },
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
if self.mouse_pressed {
|
if self.mouse_pressed {
|
||||||
self.camera_controller.process_mouse(delta.0, delta.1);
|
self.camera_controller.process_mouse(delta.0, delta.1);
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
},
|
}
|
||||||
Event::WindowEvent {
|
Event::WindowEvent { ref event, .. } => match event {
|
||||||
ref event,
|
|
||||||
..
|
|
||||||
} => match event {
|
|
||||||
WindowEvent::KeyboardInput {
|
WindowEvent::KeyboardInput {
|
||||||
input:
|
input:
|
||||||
KeyboardInput {
|
KeyboardInput {
|
||||||
|
|
|
@ -5,13 +5,24 @@ pub use renderer::Renderer;
|
||||||
mod default_state;
|
mod default_state;
|
||||||
pub use default_state::DefaultState;
|
pub use default_state::DefaultState;
|
||||||
|
|
||||||
use wgpu::{TextureView, CommandEncoder, Queue};
|
use wgpu::{CommandEncoder, Queue, TextureView};
|
||||||
use winit::event::Event;
|
use winit::event::Event;
|
||||||
|
|
||||||
pub trait State {
|
pub trait State {
|
||||||
fn new(renderer: &Renderer) -> Self where Self: Sized;
|
fn new(renderer: &Renderer) -> Self
|
||||||
fn resize(&mut self, device: &wgpu::Device, config: &wgpu::SurfaceConfiguration, new_size: winit::dpi::PhysicalSize<u32>);
|
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 input(&mut self, event: &Event<()>) -> bool;
|
||||||
fn update(&mut self, queue: &Queue, dt: instant::Duration);
|
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 queue: wgpu::Queue,
|
||||||
pub size: winit::dpi::PhysicalSize<u32>,
|
pub size: winit::dpi::PhysicalSize<u32>,
|
||||||
pub config: wgpu::SurfaceConfiguration,
|
pub config: wgpu::SurfaceConfiguration,
|
||||||
state: Option<Box<dyn State>>
|
state: Option<Box<dyn State>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Renderer {
|
impl Renderer {
|
||||||
|
@ -63,7 +63,7 @@ impl Renderer {
|
||||||
queue,
|
queue,
|
||||||
config,
|
config,
|
||||||
size,
|
size,
|
||||||
state: None
|
state: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,8 @@ fn format_url(file_name: &str) -> reqwest::Url {
|
||||||
"{}/{}/",
|
"{}/{}/",
|
||||||
location.origin().unwrap(),
|
location.origin().unwrap(),
|
||||||
option_env!("RES_PATH").unwrap_or("res"),
|
option_env!("RES_PATH").unwrap_or("res"),
|
||||||
)).unwrap();
|
))
|
||||||
|
.unwrap();
|
||||||
base.join(file_name).unwrap()
|
base.join(file_name).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue