From 4cad20da09f2616c847491c9829dff016570bbee Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 23 Oct 2023 14:55:48 -0700 Subject: [PATCH] wip: move stuff out of event_loop --- src/graphics_context.rs | 98 ++++++++++++++++++++++++++--------------- src/main.rs | 2 - src/window.rs | 11 +++-- 3 files changed, 69 insertions(+), 42 deletions(-) diff --git a/src/graphics_context.rs b/src/graphics_context.rs index d37f0bc..98471bd 100644 --- a/src/graphics_context.rs +++ b/src/graphics_context.rs @@ -1,3 +1,5 @@ +use crate::instruction::TimedInstruction; + fn optional_features() -> wgpu::Features { wgpu::Features::empty() } @@ -197,6 +199,53 @@ pub fn setup(title:&str)->GraphicsContextSetup{ } } +enum RunInstruction{ + Resize(winit::dpi::PhysicalSize), + WindowEvent(winit::event::WindowEvent), + DeviceEvent(winit::event::DeviceEvent), + Render, +} + +impl GraphicsContext{ + fn into_worker(self,mut global_state:crate::GlobalState)->crate::worker::QNWorker>{ + crate::worker::QNWorker::new(move |ins:TimedInstruction|{ + match ins.instruction{ + RunInstruction::WindowEvent(window_event)=>{ + global_state.window_event(window_event); + }, + RunInstruction::DeviceEvent(device_event)=>{ + global_state.device_event(device_event); + }, + RunInstruction::Resize(size)=>{ + self.config.width=size.width.max(1); + self.config.height=size.height.max(1); + global_state.graphics.resize(&self.device,&self.config); + self.surface.configure(&self.device,&self.config); + } + RunInstruction::Render=>{ + let frame=match self.surface.get_current_texture(){ + Ok(frame)=>frame, + Err(_)=>{ + self.surface.configure(&self.device,&self.config); + self.surface + .get_current_texture() + .expect("Failed to acquire next surface texture!") + } + }; + let view=frame.texture.create_view(&wgpu::TextureViewDescriptor{ + format:Some(self.config.view_formats[0]), + ..wgpu::TextureViewDescriptor::default() + }); + + global_state.graphics.render(&view,&self.device,&self.queue); + + frame.present(); + } + } + }) + } +} + struct GraphicsContextSetup{ window:winit::window::Window, event_loop:winit::event_loop::EventLoop<()>, @@ -216,8 +265,15 @@ impl GraphicsContextSetup{ pub fn start(self,mut global_state:crate::GlobalState){ let (window,event_loop,graphics_context)=self.into_split(); + //dedicated thread to pigh request redraw back and resize the window doesn't seem logical + + //physics and graphics render thread + let run_thread=graphics_context.into_worker(global_state); + println!("Entering render loop..."); + let root_time=std::time::Instant::now(); event_loop.run(move |event,_,control_flow|{ + let time=crate::integer::Time::from_nanos(root_time.elapsed().as_nanos() as i64); *control_flow=if cfg!(feature="metal-auto-capture"){ winit::event_loop::ControlFlow::Exit }else{ @@ -232,9 +288,9 @@ impl GraphicsContextSetup{ winit::event::WindowEvent::Resized(size) | winit::event::WindowEvent::ScaleFactorChanged { new_inner_size:&mut size, - .. + scale_factor:_, }, - .. + window_id:_, } => { // Once winit is fixed, the detection conditions here can be removed. // https://github.com/rust-windowing/winit/issues/2876 @@ -249,10 +305,7 @@ impl GraphicsContextSetup{ ); }else{ println!("Resizing to {:?}",size); - graphics_context.config.width=size.width.max(1); - graphics_context.config.height=size.height.max(1); - window.resize(&graphics_context); - graphics_context.surface.configure(&graphics_context.device,&graphics_context.config); + run_thread.send(TimedInstruction{time,instruction:RunInstruction::Resize(size)}); } } winit::event::Event::WindowEvent{event,..}=>match event{ @@ -268,45 +321,18 @@ impl GraphicsContextSetup{ |winit::event::WindowEvent::CloseRequested=>{ *control_flow=winit::event_loop::ControlFlow::Exit; } - winit::event::WindowEvent::KeyboardInput{ - input: - winit::event::KeyboardInput{ - virtual_keycode:Some(winit::event::VirtualKeyCode::Scroll), - state: winit::event::ElementState::Pressed, - .. - }, - .. - }=>{ - println!("{:#?}",graphics_context.instance.generate_report()); - } _=>{ - global_state.update(event); + run_thread.send(TimedInstruction{time,instruction:RunInstruction::WindowEvent(event)}); } }, winit::event::Event::DeviceEvent{ event, .. } => { - global_state.device_event(event); + run_thread.send(TimedInstruction{time,instruction:RunInstruction::DeviceEvent(event)}); }, winit::event::Event::RedrawRequested(_)=>{ - let frame=match graphics_context.surface.get_current_texture(){ - Ok(frame)=>frame, - Err(_)=>{ - graphics_context.surface.configure(&graphics_context.device,&graphics_context.config); - graphics_context.surface - .get_current_texture() - .expect("Failed to acquire next surface texture!") - } - }; - let view=frame.texture.create_view(&wgpu::TextureViewDescriptor{ - format:Some(graphics_context.config.view_formats[0]), - ..wgpu::TextureViewDescriptor::default() - }); - - graphics.render(&view,&graphics_context.device,&graphics_context.queue); - - frame.present(); + //send } _=>{} } diff --git a/src/main.rs b/src/main.rs index ac6d72f..bd5d030 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,6 @@ mod graphics_context; pub struct GlobalState{ - start_time: std::time::Instant, manual_mouse_lock:bool, mouse:std::sync::Arc>, user_settings:settings::UserSettings, @@ -159,7 +158,6 @@ impl GlobalState { //3. forget let mut state=GlobalState{ - start_time:Instant::now(), manual_mouse_lock:false, mouse:physics::MouseState::default(), user_settings, diff --git a/src/window.rs b/src/window.rs index fc9fa46..82790bd 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,3 +1,6 @@ +pub enum WindowInstruction{ + Resize(), +} pub struct WindowState{ //ok } @@ -5,8 +8,7 @@ impl WindowState{ fn resize(&mut self); fn render(&self); - fn update(&mut self, window: &winit::window::Window, event: winit::event::WindowEvent) { - let time=integer::Time::from_nanos(self.start_time.elapsed().as_nanos() as i64); + fn window_event(&mut self, window: &winit::window::Window, event: winit::event::WindowEvent) { match event { winit::event::WindowEvent::DroppedFile(path)=>{ std::thread::spawn(move ||{ @@ -102,8 +104,6 @@ impl WindowState{ } fn device_event(&mut self, window: &winit::window::Window, event: winit::event::DeviceEvent) { - //there's no way this is the best way get a timestamp. - let time=integer::Time::from_nanos(self.start_time.elapsed().as_nanos() as i64); match event { winit::event::DeviceEvent::MouseMotion { delta,//these (f64,f64) are integers on my machine @@ -149,4 +149,7 @@ impl WindowState{ } builder.build(event_loop) } + pub fn into_thread(window:winit::window::Window){ + // + } } \ No newline at end of file