Compare commits

...

2 Commits

Author SHA1 Message Date
580bbf2cc5 schedule frames at a fixed interval 2023-11-08 18:20:50 -08:00
08f6d928cf bizzare slow motion gameplay 2023-11-08 18:20:49 -08:00
4 changed files with 39 additions and 25 deletions

View File

@ -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 //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>( pub fn new<'a>(
scope:&'a std::thread::Scope<'a,'_>,
mut graphics:crate::graphics::GraphicsState, mut graphics:crate::graphics::GraphicsState,
mut config:wgpu::SurfaceConfiguration, mut config:wgpu::SurfaceConfiguration,
surface:wgpu::Surface, surface:wgpu::Surface,
device:wgpu::Device, device:wgpu::Device,
queue:wgpu::Queue, queue:wgpu::Queue,
)->crate::compat_worker::INWorker<'a,Instruction>{ )->crate::worker::INWorker<'a,Instruction>{
let mut resize=None; let mut resize=None;
crate::compat_worker::INWorker::new(move |ins:Instruction|{ crate::worker::INWorker::new(scope,move |ins:Instruction|{
match ins{ match ins{
Instruction::GenerateModels(indexed_model_instances)=>{ Instruction::GenerateModels(indexed_model_instances)=>{
graphics.generate_models(&device,&queue,indexed_model_instances); graphics.generate_models(&device,&queue,indexed_model_instances);

View File

@ -23,11 +23,11 @@ pub enum Instruction{
//Graphics(crate::graphics_worker::Instruction), //Graphics(crate::graphics_worker::Instruction),
} }
pub fn new(mut physics:crate::physics::PhysicsState,mut graphics_worker:crate::compat_worker::INWorker<crate::graphics_worker::Instruction>)->crate::compat_worker::QNWorker<TimedInstruction<Instruction>>{ 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<Instruction>>{
let mut mouse_blocking=true; let mut mouse_blocking=true;
let mut last_mouse_time=physics.next_mouse.time; let mut last_mouse_time=physics.next_mouse.time;
let mut timeline=std::collections::VecDeque::new(); let mut timeline=std::collections::VecDeque::new();
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{ crate::worker::QNWorker::new(scope,move |ins:TimedInstruction<Instruction>|{
if if let Some(phys_input)=match &ins.instruction{ if if let Some(phys_input)=match &ins.instruction{
Instruction::Input(input_instruction)=>match input_instruction{ Instruction::Input(input_instruction)=>match input_instruction{
&InputInstruction::MoveMouse(m)=>{ &InputInstruction::MoveMouse(m)=>{
@ -113,10 +113,11 @@ pub enum Instruction{
} }
match ins.instruction{ match ins.instruction{
Instruction::Render=>{ 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)=>{ 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)=>{ Instruction::GenerateModels(indexed_model_instances)=>{
physics.generate_models(&indexed_model_instances); physics.generate_models(&indexed_model_instances);

View File

@ -232,20 +232,32 @@ impl SetupContextSetup{
let (window,event_loop,setup_context)=self.into_split(); let (window,event_loop,setup_context)=self.into_split();
//dedicated thread to ping request redraw back and resize the window doesn't seem logical //dedicated thread to ping request redraw back and resize the window doesn't seem logical
//but here I am doing it
let root_time=std::time::Instant::now();
std::thread::scope(|s|{
let window=crate::window::WindowContextSetup::new(&setup_context,window); let window=crate::window::WindowContextSetup::new(&setup_context,window);
//the thread that spawns the physics thread //the thread that spawns the physics thread
let window_thread=window.into_worker(setup_context); let window_thread=window.into_worker(s,setup_context);
//schedule frames at 165fps
let event_loop_proxy=event_loop.create_proxy();
s.spawn(move ||{
loop{
std::thread::sleep(std::time::Duration::from_nanos(1_000_000_000/165));
event_loop_proxy.send_event(()).ok();
}
});
println!("Entering event loop..."); println!("Entering event loop...");
let root_time=std::time::Instant::now();
run_event_loop(event_loop,window_thread,root_time).unwrap(); run_event_loop(event_loop,window_thread,root_time).unwrap();
});
} }
} }
fn run_event_loop( fn run_event_loop(
event_loop:winit::event_loop::EventLoop<()>, event_loop:winit::event_loop::EventLoop<()>,
mut window_thread:crate::compat_worker::QNWorker<TimedInstruction<WindowInstruction>>, window_thread:crate::worker::QNWorker<TimedInstruction<WindowInstruction>>,
root_time:std::time::Instant root_time:std::time::Instant
)->Result<(),winit::error::EventLoopError>{ )->Result<(),winit::error::EventLoopError>{
event_loop.run(move |event,elwt|{ event_loop.run(move |event,elwt|{
@ -256,7 +268,7 @@ fn run_event_loop(
// winit::event_loop::ControlFlow::Poll // winit::event_loop::ControlFlow::Poll
// }; // };
match event{ match event{
winit::event::Event::AboutToWait=>{ winit::event::Event::UserEvent(())=>{
window_thread.send(TimedInstruction{time,instruction:WindowInstruction::RequestRedraw}).unwrap(); window_thread.send(TimedInstruction{time,instruction:WindowInstruction::RequestRedraw}).unwrap();
} }
winit::event::Event::WindowEvent { winit::event::Event::WindowEvent {

View File

@ -16,7 +16,7 @@ struct WindowContext<'a>{
screen_size:glam::UVec2, screen_size:glam::UVec2,
user_settings:crate::settings::UserSettings, user_settings:crate::settings::UserSettings,
window:winit::window::Window, window:winit::window::Window,
physics_thread:crate::compat_worker::QNWorker<'a, TimedInstruction<crate::physics_worker::Instruction>>, physics_thread:crate::worker::QNWorker<'a,TimedInstruction<crate::physics_worker::Instruction>>,
} }
impl WindowContext<'_>{ 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 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{ WindowContext{
manual_mouse_lock:false, manual_mouse_lock:false,
mouse:crate::physics::MouseState::default(), mouse:crate::physics::MouseState::default(),
@ -204,13 +204,13 @@ impl WindowContextSetup{
screen_size, screen_size,
user_settings:self.user_settings, user_settings:self.user_settings,
window:self.window, 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<WindowInstruction>>{ pub fn into_worker<'a>(self,scope:&'a std::thread::Scope<'a,'_>,setup_context:crate::setup::SetupContext)->crate::worker::QNWorker<'a,TimedInstruction<WindowInstruction>>{
let mut window_context=self.into_context(setup_context); let mut window_context=self.into_context(scope,setup_context);
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<WindowInstruction>|{ crate::worker::QNWorker::new(scope,move |ins:TimedInstruction<WindowInstruction>|{
match ins.instruction{ match ins.instruction{
WindowInstruction::RequestRedraw=>{ WindowInstruction::RequestRedraw=>{
window_context.window.request_redraw(); window_context.window.request_redraw();