LIFETIMES

This commit is contained in:
Quaternions 2023-10-24 20:46:07 -07:00
parent b5a0cf7045
commit 75f958348c
7 changed files with 39 additions and 40 deletions

View File

@ -1,13 +1,13 @@
pub type QNWorker<Task>=CompatNWorker<Task>;
pub type INWorker<Task>=CompatNWorker<Task>;
pub type QNWorker<'a,Task>=CompatNWorker<'a,Task>;
pub type INWorker<'a,Task>=CompatNWorker<'a,Task>;
pub struct CompatNWorker<Task>{
pub struct CompatNWorker<'a,Task>{
data:std::marker::PhantomData<Task>,
f:Box<dyn FnMut(Task)>,
f:Box<dyn FnMut(Task)+'a>,
}
impl<Task> CompatNWorker<Task>{
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),

View File

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

View File

@ -2,7 +2,7 @@
pub enum Instruction{
Render(crate::physics::PhysicsOutputState,crate::integer::Time,glam::IVec2),
//UpdateModel(crate::graphics::ModelUpdate),
Resize(winit::dpi::PhysicalSize<u32>),
Resize(winit::dpi::PhysicalSize<u32>,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<Instruction>{
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();
}

View File

@ -17,7 +17,7 @@ pub enum InputInstruction {
pub enum Instruction{
Input(InputInstruction),
Render,
Resize(winit::dpi::PhysicalSize<u32>),
Resize(winit::dpi::PhysicalSize<u32>,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();
},
_=>(),
}

View File

@ -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<u32>),
@ -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<std::sync::Mutex<>>
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<TimedInstruction<crate::physics_worker::Instruction>>,
physics_thread:crate::compat_worker::QNWorker<'a, TimedInstruction<crate::physics_worker::Instruction>>,
}
impl RunContext{
impl RunContext<'_>{
fn get_middle_of_screen(&self)->winit::dpi::PhysicalPosition<f32>{
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<TimedInstruction<RunInstruction>>{
pub fn into_worker<'a>(self,setup_context:crate::setup::SetupContext)->crate::compat_worker::QNWorker<'a,TimedInstruction<RunInstruction>>{
let mut run_context=self.into_context(setup_context);
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<RunInstruction>|{
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();
}

View File

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

View File

@ -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();
},
_=>{}
}