From 75f958348c99108f6122cfa3a4553e381a2599a7 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 24 Oct 2023 20:46:07 -0700 Subject: [PATCH] LIFETIMES --- src/compat_worker.rs | 12 ++++++------ src/graphics.rs | 1 - src/graphics_worker.rs | 19 ++++++++++--------- src/physics_worker.rs | 8 ++++---- src/run.rs | 22 ++++++++-------------- src/settings.rs | 7 ++++++- src/setup.rs | 10 +++++----- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/compat_worker.rs b/src/compat_worker.rs index bf703ba..d41ea3e 100644 --- a/src/compat_worker.rs +++ b/src/compat_worker.rs @@ -1,13 +1,13 @@ -pub type QNWorker=CompatNWorker; -pub type INWorker=CompatNWorker; +pub type QNWorker<'a,Task>=CompatNWorker<'a,Task>; +pub type INWorker<'a,Task>=CompatNWorker<'a,Task>; -pub struct CompatNWorker{ +pub struct CompatNWorker<'a,Task>{ data:std::marker::PhantomData, - f:Box, + f:Box, } -impl CompatNWorker{ - pub fn new(f:impl FnMut(Task)+'static)->Self{ +impl<'a,Task> CompatNWorker<'a,Task>{ + pub fn new(f:impl FnMut(Task)+'a)->CompatNWorker<'a,Task>{ Self{ data:std::marker::PhantomData, f:Box::new(f), diff --git a/src/graphics.rs b/src/graphics.rs index 5d4455b..d993258 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -872,7 +872,6 @@ impl GraphicsState{ pub fn resize( &mut self, device:&wgpu::Device, - queue:&wgpu::Queue, config:&wgpu::SurfaceConfiguration, user_settings:&crate::settings::UserSettings, ) { diff --git a/src/graphics_worker.rs b/src/graphics_worker.rs index 81e58e1..d4bdae2 100644 --- a/src/graphics_worker.rs +++ b/src/graphics_worker.rs @@ -2,7 +2,7 @@ pub enum Instruction{ Render(crate::physics::PhysicsOutputState,crate::integer::Time,glam::IVec2), //UpdateModel(crate::graphics::ModelUpdate), - Resize(winit::dpi::PhysicalSize), + Resize(winit::dpi::PhysicalSize,crate::settings::UserSettings), } //Ideally the graphics thread worker description is: @@ -14,26 +14,27 @@ WorkerDescription{ */ //up to three frames in flight, dropping new frame requests when all three are busy, and dropping output frames when one renders out of order -pub fn new( +pub fn new<'a>( mut graphics:crate::graphics::GraphicsState, mut config:wgpu::SurfaceConfiguration, surface:wgpu::Surface, - device:&wgpu::Device, - queue:&wgpu::Queue, - )->crate::compat_worker::INWorker{ + device:wgpu::Device, + queue:wgpu::Queue, + )->crate::compat_worker::INWorker<'a,Instruction>{ crate::compat_worker::INWorker::new(move |ins:Instruction|{ match ins{ - Instruction::Resize(size)=>{ + Instruction::Resize(size,user_settings)=>{ config.width=size.width.max(1); config.height=size.height.max(1); - surface.configure(device,&config); + surface.configure(&device,&config); + graphics.resize(&device,&config,&user_settings); } Instruction::Render(physics_output,predicted_time,mouse_pos)=>{ //this has to go deeper somehow let frame=match surface.get_current_texture(){ Ok(frame)=>frame, Err(_)=>{ - surface.configure(device,&config); + surface.configure(&device,&config); surface .get_current_texture() .expect("Failed to acquire next surface texture!") @@ -44,7 +45,7 @@ pub fn new( ..wgpu::TextureViewDescriptor::default() }); - graphics.render(&view,device,queue,physics_output,predicted_time,mouse_pos); + graphics.render(&view,&device,&queue,physics_output,predicted_time,mouse_pos); frame.present(); } diff --git a/src/physics_worker.rs b/src/physics_worker.rs index 6d06148..f3b764f 100644 --- a/src/physics_worker.rs +++ b/src/physics_worker.rs @@ -17,7 +17,7 @@ pub enum InputInstruction { pub enum Instruction{ Input(InputInstruction), Render, - Resize(winit::dpi::PhysicalSize), + Resize(winit::dpi::PhysicalSize,crate::settings::UserSettings), //Graphics(crate::graphics_worker::Instruction), } @@ -61,7 +61,7 @@ pub enum Instruction{ &InputInstruction::Zoom(s)=>Some(PhysicsInputInstruction::SetZoom(s)), InputInstruction::Reset=>Some(PhysicsInputInstruction::Reset), }, - Instruction::Resize(_)=>Some(PhysicsInputInstruction::Idle), + Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle), Instruction::Render=>Some(PhysicsInputInstruction::Idle), }{ //non-mouse event @@ -111,8 +111,8 @@ pub enum Instruction{ Instruction::Render=>{ graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.next_mouse.pos)).unwrap(); }, - Instruction::Resize(size)=>{ - graphics_worker.send(crate::graphics_worker::Instruction::Resize(size)).unwrap(); + Instruction::Resize(size,user_settings)=>{ + graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap(); }, _=>(), } diff --git a/src/run.rs b/src/run.rs index 01678c5..2d5858f 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,6 +1,5 @@ -use crate::physics::PhysicsInstruction; -use crate::physics_worker::InputInstruction; use crate::instruction::TimedInstruction; +use crate::physics_worker::InputInstruction; pub enum RunInstruction{ Resize(winit::dpi::PhysicalSize), @@ -11,18 +10,16 @@ pub enum RunInstruction{ } //holds thread handles to dispatch to -struct RunContext{ +struct RunContext<'a>{ manual_mouse_lock:bool, mouse:crate::physics::MouseState,//std::sync::Arc> - device:wgpu::Device, - queue:wgpu::Queue, screen_size:glam::UVec2, user_settings:crate::settings::UserSettings, window:winit::window::Window, - physics_thread:crate::compat_worker::QNWorker>, + physics_thread:crate::compat_worker::QNWorker<'a, TimedInstruction>, } -impl RunContext{ +impl RunContext<'_>{ fn get_middle_of_screen(&self)->winit::dpi::PhysicalPosition{ winit::dpi::PhysicalPosition::new(self.screen_size.x as f32/2.0, self.screen_size.y as f32/2.0) } @@ -121,7 +118,6 @@ impl RunContext{ }).unwrap(); } }, - _=>(), } }, _=>(), @@ -200,23 +196,21 @@ impl RunContextSetup{ } } - fn into_context(self,setup_context:crate::setup::SetupContext)->RunContext{ + fn into_context<'a>(self,setup_context:crate::setup::SetupContext)->RunContext<'a>{ let screen_size=glam::uvec2(setup_context.config.width,setup_context.config.height); - let graphics_thread=crate::graphics_worker::new(self.graphics,setup_context.config,setup_context.surface,&setup_context.device,&setup_context.queue); + let graphics_thread=crate::graphics_worker::new(self.graphics,setup_context.config,setup_context.surface,setup_context.device,setup_context.queue); RunContext{ manual_mouse_lock:false, mouse:crate::physics::MouseState::default(), //make sure to update this!!!!! screen_size, - device:setup_context.device, - queue:setup_context.queue, user_settings:self.user_settings, window:self.window, physics_thread:crate::physics_worker::new(self.physics,graphics_thread), } } - pub fn into_worker(self,setup_context:crate::setup::SetupContext)->crate::compat_worker::QNWorker>{ + pub fn into_worker<'a>(self,setup_context:crate::setup::SetupContext)->crate::compat_worker::QNWorker<'a,TimedInstruction>{ let mut run_context=self.into_context(setup_context); crate::compat_worker::QNWorker::new(move |ins:TimedInstruction|{ match ins.instruction{ @@ -233,7 +227,7 @@ impl RunContextSetup{ run_context.physics_thread.send( TimedInstruction{ time:ins.time, - instruction:crate::physics_worker::Instruction::Resize(size) + instruction:crate::physics_worker::Instruction::Resize(size,run_context.user_settings.clone()) } ).unwrap(); } diff --git a/src/settings.rs b/src/settings.rs index 24ac840..d8ad48c 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,11 +1,14 @@ use crate::integer::{Ratio64,Ratio64Vec2}; +#[derive(Clone)] struct Ratio{ ratio:f64, } +#[derive(Clone)] enum DerivedFov{ FromScreenAspect, FromAspect(Ratio), } +#[derive(Clone)] enum Fov{ Exactly{x:f64,y:f64}, SpecifyXDeriveY{x:f64,y:DerivedFov}, @@ -16,9 +19,11 @@ impl Default for Fov{ Fov::SpecifyYDeriveX{x:DerivedFov::FromScreenAspect,y:1.0} } } +#[derive(Clone)] enum DerivedSensitivity{ FromRatio(Ratio64), } +#[derive(Clone)] enum Sensitivity{ Exactly{x:Ratio64,y:Ratio64}, SpecifyXDeriveY{x:Ratio64,y:DerivedSensitivity}, @@ -30,7 +35,7 @@ impl Default for Sensitivity{ } } -#[derive(Default)] +#[derive(Default,Clone)] pub struct UserSettings{ fov:Fov, sensitivity:Sensitivity, diff --git a/src/setup.rs b/src/setup.rs index f1b0555..9252eb5 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -244,7 +244,7 @@ impl SetupContextSetup{ // }; match event{ winit::event::Event::AboutToWait=>{ - run_thread.send(TimedInstruction{time,instruction:RunInstruction::RequestRedraw}); + run_thread.send(TimedInstruction{time,instruction:RunInstruction::RequestRedraw}).unwrap(); } winit::event::Event::WindowEvent { event: @@ -257,7 +257,7 @@ impl SetupContextSetup{ window_id:_, } => { println!("Resizing to {:?}",size); - run_thread.send(TimedInstruction{time,instruction:RunInstruction::Resize(size)}); + run_thread.send(TimedInstruction{time,instruction:RunInstruction::Resize(size)}).unwrap(); } winit::event::Event::WindowEvent{event,..}=>match event{ winit::event::WindowEvent::KeyboardInput{ @@ -273,17 +273,17 @@ impl SetupContextSetup{ elwt.exit(); } winit::event::WindowEvent::RedrawRequested=>{ - run_thread.send(TimedInstruction{time,instruction:RunInstruction::Render}); + run_thread.send(TimedInstruction{time,instruction:RunInstruction::Render}).unwrap(); } _=>{ - run_thread.send(TimedInstruction{time,instruction:RunInstruction::WindowEvent(event)}); + run_thread.send(TimedInstruction{time,instruction:RunInstruction::WindowEvent(event)}).unwrap(); } }, winit::event::Event::DeviceEvent{ event, .. } => { - run_thread.send(TimedInstruction{time,instruction:RunInstruction::DeviceEvent(event)}); + run_thread.send(TimedInstruction{time,instruction:RunInstruction::DeviceEvent(event)}).unwrap(); }, _=>{} }