1
0
Fork 0

[State] Refactor event

This commit is contained in:
Florian RICHER 2022-06-21 22:27:05 +02:00
parent 7f2a43700a
commit 0145968267
4 changed files with 65 additions and 54 deletions

View file

@ -170,21 +170,16 @@ pub async fn run() {
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 |event, _, control_flow| { event_loop.run(move |base_event, _, control_flow| {
*control_flow = ControlFlow::Poll; *control_flow = ControlFlow::Poll;
let renderer = Arc::get_mut(&mut renderer).unwrap(); let renderer = Arc::get_mut(&mut renderer).unwrap();
match event {
match base_event {
Event::MainEventsCleared => window.request_redraw(), Event::MainEventsCleared => window.request_redraw(),
// Event::DeviceEvent {
// event: DeviceEvent::MouseMotion{ delta, },
// .. // We're not using device_id currently
// } => if state.mouse_pressed {
// state.camera_controller.process_mouse(delta.0, delta.1)
// }
Event::WindowEvent { Event::WindowEvent {
ref event, ref event,
window_id, window_id,
} if window_id == window.id() && !renderer.input(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
@ -203,7 +198,7 @@ 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); }
} }
} }
Event::RedrawRequested(window_id) if window_id == window.id() => { Event::RedrawRequested(window_id) if window_id == window.id() => {
@ -221,7 +216,7 @@ pub async fn run() {
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"), Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
} }
} }
_ => {} _ => { renderer.input(&base_event); }
} }
}); });
} }

View file

@ -1,7 +1,7 @@
use cgmath::prelude::*; use cgmath::prelude::*;
use rayon::prelude::*; use rayon::prelude::*;
use wgpu::util::DeviceExt; use wgpu::{util::DeviceExt, Queue};
use winit::event::{ElementState, KeyboardInput, MouseButton, WindowEvent}; use winit::event::{DeviceEvent, ElementState, Event, KeyboardInput, MouseButton, WindowEvent};
use crate::{ use crate::{
camera, camera,
@ -208,47 +208,64 @@ impl super::State for DefaultState {
} }
} }
fn resize(&mut self, renderer: &Renderer, new_size: winit::dpi::PhysicalSize<u32>) { fn resize(
&mut self,
device: &wgpu::Device,
config: &wgpu::SurfaceConfiguration,
new_size: winit::dpi::PhysicalSize<u32>,
) {
self.projection.resize(new_size.width, new_size.height); self.projection.resize(new_size.width, new_size.height);
self.depth_texture = texture::Texture::create_depth_texture( self.depth_texture =
&renderer.device, texture::Texture::create_depth_texture(&device, &config, "depth_texture");
&renderer.config,
"depth_texture",
);
} }
fn input(&mut self, renderer: &Renderer, event: &winit::event::WindowEvent) -> bool { fn input(&mut self, event: &Event<()>) -> bool {
match event { match event {
WindowEvent::KeyboardInput { Event::DeviceEvent {
input: event: DeviceEvent::MouseMotion{ delta, },
KeyboardInput {
virtual_keycode: Some(key),
state,
..
},
..
} => self.camera_controller.process_keyboard(*key, *state),
WindowEvent::MouseWheel { delta, .. } => {
self.camera_controller.process_scroll(delta);
true
}
WindowEvent::MouseInput {
button: MouseButton::Left,
state,
.. ..
} => { } => {
self.mouse_pressed = *state == ElementState::Pressed; if self.mouse_pressed {
self.camera_controller.process_mouse(delta.0, delta.1);
}
true true
} },
Event::WindowEvent {
ref event,
..
} => match event {
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(key),
state,
..
},
..
} => self.camera_controller.process_keyboard(*key, *state),
WindowEvent::MouseWheel { delta, .. } => {
self.camera_controller.process_scroll(delta);
true
}
WindowEvent::MouseInput {
button: MouseButton::Left,
state,
..
} => {
self.mouse_pressed = *state == ElementState::Pressed;
true
}
_ => false,
},
_ => false, _ => false,
} }
} }
fn update(&mut self, renderer: &Renderer, dt: instant::Duration) { fn update(&mut self, queue: &Queue, dt: instant::Duration) {
self.camera_controller.update_camera(&mut self.camera, dt); self.camera_controller.update_camera(&mut self.camera, dt);
self.camera_uniform self.camera_uniform
.update_view_proj(&self.camera, &self.projection); .update_view_proj(&self.camera, &self.projection);
renderer.queue.write_buffer( queue.write_buffer(
&self.camera_buffer, &self.camera_buffer,
0, 0,
bytemuck::cast_slice(&[self.camera_uniform]), bytemuck::cast_slice(&[self.camera_uniform]),
@ -260,7 +277,7 @@ impl super::State for DefaultState {
(cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) (cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0))
* old_position) * old_position)
.into(); .into();
renderer.queue.write_buffer( queue.write_buffer(
&self.light_buffer, &self.light_buffer,
0, 0,
bytemuck::cast_slice(&[self.light_uniform]), bytemuck::cast_slice(&[self.light_uniform]),
@ -269,7 +286,6 @@ impl super::State for DefaultState {
fn render( fn render(
&mut self, &mut self,
renderer: &Renderer,
view: &wgpu::TextureView, view: &wgpu::TextureView,
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
) -> Result<(), wgpu::SurfaceError> { ) -> Result<(), wgpu::SurfaceError> {

View file

@ -5,13 +5,13 @@ pub use renderer::Renderer;
mod default_state; mod default_state;
pub use default_state::DefaultState; pub use default_state::DefaultState;
use wgpu::{TextureView, CommandEncoder}; use wgpu::{TextureView, CommandEncoder, Queue};
use winit::event::WindowEvent; use winit::event::Event;
pub trait State { pub trait State {
fn new(renderer: &Renderer) -> Self where Self: Sized; fn new(renderer: &Renderer) -> Self where Self: Sized;
fn resize(&mut self, renderer: &Renderer, new_size: winit::dpi::PhysicalSize<u32>); fn resize(&mut self, device: &wgpu::Device, config: &wgpu::SurfaceConfiguration, new_size: winit::dpi::PhysicalSize<u32>);
fn input(&mut self, renderer: &Renderer, event: &WindowEvent) -> bool; fn input(&mut self, event: &Event<()>) -> bool;
fn update(&mut self, renderer: &Renderer, dt: instant::Duration); fn update(&mut self, queue: &Queue, dt: instant::Duration);
fn render(&mut self, renderer: &Renderer, view: &TextureView, encoder: &mut CommandEncoder) -> Result<(), wgpu::SurfaceError>; fn render(&mut self, view: &TextureView, encoder: &mut CommandEncoder) -> Result<(), wgpu::SurfaceError>;
} }

View file

@ -1,6 +1,6 @@
use std::iter; use std::iter;
use winit::{event::WindowEvent, window::Window}; use winit::{event::Event, window::Window};
use super::State; use super::State;
@ -75,21 +75,21 @@ 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(new_size); state.resize(&self.device, &self.config, new_size);
} }
} }
} }
pub fn input(&mut self, event: &WindowEvent) -> 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(dt); state.update(&self.queue, dt);
} }
} }
@ -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()));