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
|
||||||
|
}
|
58
src/lib.rs
58
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,28 +181,28 @@ 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:
|
KeyboardInput {
|
||||||
KeyboardInput {
|
state: ElementState::Pressed,
|
||||||
state: ElementState::Pressed,
|
virtual_keycode: Some(VirtualKeyCode::Escape),
|
||||||
virtual_keycode: Some(VirtualKeyCode::Escape),
|
..
|
||||||
..
|
},
|
||||||
},
|
..
|
||||||
..
|
} => *control_flow = ControlFlow::Exit,
|
||||||
} => *control_flow = ControlFlow::Exit,
|
WindowEvent::Resized(physical_size) => {
|
||||||
WindowEvent::Resized(physical_size) => {
|
renderer.resize(*physical_size);
|
||||||
renderer.resize(*physical_size);
|
|
||||||
}
|
|
||||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
|
||||||
renderer.resize(**new_inner_size);
|
|
||||||
}
|
|
||||||
_ => { renderer.input(&base_event); }
|
|
||||||
}
|
}
|
||||||
}
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||||
|
renderer.resize(**new_inner_size);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,4 +2,4 @@ use tuto1::run;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
async_std::task::block_on(run());
|
async_std::task::block_on(run());
|
||||||
}
|
}
|
||||||
|
|
|
@ -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};
|
||||||
|
|
|
@ -56,4 +56,4 @@ pub fn create_render_pipeline(
|
||||||
// indicates how many array layers the attachments will have.
|
// indicates how many array layers the attachments will have.
|
||||||
multiview: None,
|
multiview: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
fn input(&mut self, event: &Event<()>) -> bool;
|
Self: Sized;
|
||||||
fn update(&mut self, queue: &Queue, dt: instant::Duration);
|
fn resize(
|
||||||
fn render(&mut self, view: &TextureView, encoder: &mut CommandEncoder) -> Result<(), wgpu::SurfaceError>;
|
&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>;
|
||||||
|
}
|
||||||
|
|
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,26 +75,26 @@ impl Renderer {
|
||||||
self.surface.configure(&self.device, &self.config);
|
self.surface.configure(&self.device, &self.config);
|
||||||
|
|
||||||
if let Some(state) = self.state.as_mut() {
|
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 {
|
pub fn input(&mut self, event: &Event<()>) -> bool {
|
||||||
if let Some(state) = self.state.as_mut() {
|
if let Some(state) = self.state.as_mut() {
|
||||||
return state.input(event);
|
return state.input(event);
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, dt: instant::Duration) {
|
pub fn update(&mut self, dt: instant::Duration) {
|
||||||
if let Some(state) = self.state.as_mut() {
|
if let Some(state) = self.state.as_mut() {
|
||||||
state.update(&self.queue, dt);
|
state.update(&self.queue, dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
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
|
let view = output
|
||||||
.texture
|
.texture
|
||||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
@ -106,7 +106,7 @@ impl Renderer {
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(state) = self.state.as_mut() {
|
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()));
|
self.queue.submit(iter::once(encoder.finish()));
|
||||||
|
@ -116,6 +116,6 @@ impl Renderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_state(&mut self, state: Option<Box<dyn State>>) {
|
pub fn set_state(&mut self, state: Option<Box<dyn State>>) {
|
||||||
self.state = state;
|
self.state = state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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