From 08f6d928cfb7fb24f95a2aac9121510eb420764c Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 24 Oct 2023 23:37:07 -0700 Subject: [PATCH] bizzare slow motion gameplay --- src/graphics_worker.rs | 7 ++++--- src/physics_worker.rs | 9 +++++---- src/setup.rs | 20 +++++++++++--------- src/window.rs | 16 ++++++++-------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/graphics_worker.rs b/src/graphics_worker.rs index 28b247f..6d125d7 100644 --- a/src/graphics_worker.rs +++ b/src/graphics_worker.rs @@ -16,14 +16,15 @@ 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<'a>( + scope:&'a std::thread::Scope<'a,'_>, mut graphics:crate::graphics::GraphicsState, mut config:wgpu::SurfaceConfiguration, surface:wgpu::Surface, device:wgpu::Device, queue:wgpu::Queue, - )->crate::compat_worker::INWorker<'a,Instruction>{ + )->crate::worker::INWorker<'a,Instruction>{ let mut resize=None; - crate::compat_worker::INWorker::new(move |ins:Instruction|{ + crate::worker::INWorker::new(scope,move |ins:Instruction|{ match ins{ Instruction::GenerateModels(indexed_model_instances)=>{ graphics.generate_models(&device,&queue,indexed_model_instances); @@ -67,4 +68,4 @@ pub fn new<'a>( } } }) -} \ No newline at end of file +} diff --git a/src/physics_worker.rs b/src/physics_worker.rs index 093d807..8b16cc0 100644 --- a/src/physics_worker.rs +++ b/src/physics_worker.rs @@ -23,11 +23,11 @@ pub enum Instruction{ //Graphics(crate::graphics_worker::Instruction), } - pub fn new(mut physics:crate::physics::PhysicsState,mut graphics_worker:crate::compat_worker::INWorker)->crate::compat_worker::QNWorker>{ + pub fn new<'a>(scope:&'a std::thread::Scope<'a,'_>,mut physics:crate::physics::PhysicsState,graphics_worker:crate::worker::INWorker<'a,crate::graphics_worker::Instruction>)->crate::worker::QNWorker<'a,TimedInstruction>{ let mut mouse_blocking=true; let mut last_mouse_time=physics.next_mouse.time; let mut timeline=std::collections::VecDeque::new(); - crate::compat_worker::QNWorker::new(move |ins:TimedInstruction|{ + crate::worker::QNWorker::new(scope,move |ins:TimedInstruction|{ if if let Some(phys_input)=match &ins.instruction{ Instruction::Input(input_instruction)=>match input_instruction{ &InputInstruction::MoveMouse(m)=>{ @@ -113,10 +113,11 @@ pub enum Instruction{ } match ins.instruction{ Instruction::Render=>{ - graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.next_mouse.pos)).unwrap(); + let _=graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.next_mouse.pos)); }, Instruction::Resize(size,user_settings)=>{ - graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap(); + //block! + graphics_worker.blocking_send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap(); }, Instruction::GenerateModels(indexed_model_instances)=>{ physics.generate_models(&indexed_model_instances); diff --git a/src/setup.rs b/src/setup.rs index 42e6618..016ed58 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -232,20 +232,22 @@ impl SetupContextSetup{ let (window,event_loop,setup_context)=self.into_split(); //dedicated thread to ping request redraw back and resize the window doesn't seem logical - - let window=crate::window::WindowContextSetup::new(&setup_context,window); - //the thread that spawns the physics thread - let window_thread=window.into_worker(setup_context); - - println!("Entering event loop..."); + //but here I am doing it let root_time=std::time::Instant::now(); - run_event_loop(event_loop,window_thread,root_time).unwrap(); + std::thread::scope(|s|{ + let window=crate::window::WindowContextSetup::new(&setup_context,window); + //the thread that spawns the physics thread + let window_thread=window.into_worker(s,setup_context); + + println!("Entering event loop..."); + run_event_loop(event_loop,window_thread,root_time).unwrap(); + }); } } fn run_event_loop( event_loop:winit::event_loop::EventLoop<()>, - mut window_thread:crate::compat_worker::QNWorker>, + window_thread:crate::worker::QNWorker>, root_time:std::time::Instant )->Result<(),winit::error::EventLoopError>{ event_loop.run(move |event,elwt|{ @@ -300,4 +302,4 @@ fn run_event_loop( _=>{} } }) -} \ No newline at end of file +} diff --git a/src/window.rs b/src/window.rs index c210de0..0323bef 100644 --- a/src/window.rs +++ b/src/window.rs @@ -16,7 +16,7 @@ struct WindowContext<'a>{ screen_size:glam::UVec2, user_settings:crate::settings::UserSettings, window:winit::window::Window, - physics_thread:crate::compat_worker::QNWorker<'a, TimedInstruction>, + physics_thread:crate::worker::QNWorker<'a,TimedInstruction>, } impl WindowContext<'_>{ @@ -194,9 +194,9 @@ impl WindowContextSetup{ } } - fn into_context<'a>(self,setup_context:crate::setup::SetupContext)->WindowContext<'a>{ + fn into_context<'a>(self,scope:&'a std::thread::Scope<'a,'_>,setup_context:crate::setup::SetupContext)->WindowContext<'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(scope,self.graphics,setup_context.config,setup_context.surface,setup_context.device,setup_context.queue); WindowContext{ manual_mouse_lock:false, mouse:crate::physics::MouseState::default(), @@ -204,13 +204,13 @@ impl WindowContextSetup{ screen_size, user_settings:self.user_settings, window:self.window, - physics_thread:crate::physics_worker::new(self.physics,graphics_thread), + physics_thread:crate::physics_worker::new(scope,self.physics,graphics_thread), } } - pub fn into_worker<'a>(self,setup_context:crate::setup::SetupContext)->crate::compat_worker::QNWorker<'a,TimedInstruction>{ - let mut window_context=self.into_context(setup_context); - crate::compat_worker::QNWorker::new(move |ins:TimedInstruction|{ + pub fn into_worker<'a>(self,scope:&'a std::thread::Scope<'a,'_>,setup_context:crate::setup::SetupContext)->crate::worker::QNWorker<'a,TimedInstruction>{ + let mut window_context=self.into_context(scope,setup_context); + crate::worker::QNWorker::new(scope,move |ins:TimedInstruction|{ match ins.instruction{ WindowInstruction::RequestRedraw=>{ window_context.window.request_redraw(); @@ -240,4 +240,4 @@ impl WindowContextSetup{ } }) } -} \ No newline at end of file +}