1
0
Fork 0
This commit is contained in:
Florian RICHER 2022-06-21 22:37:45 +02:00
parent 0145968267
commit 898783ca80
9 changed files with 71 additions and 53 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"editor.tabSize": 4
}

View file

@ -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);
}
}
});
}

View file

@ -2,4 +2,4 @@ use tuto1::run;
fn main() {
async_std::task::block_on(run());
}
}

View file

@ -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};

View file

@ -56,4 +56,4 @@ pub fn create_render_pipeline(
// indicates how many array layers the attachments will have.
multiview: None,
})
}
}

View file

@ -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 {

View file

@ -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 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 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>;
}

View file

@ -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,
}
}
@ -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<Box<dyn State>>) {
self.state = state;
self.state = state;
}
}

View file

@ -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()
}