wip: move stuff out of event_loop

This commit is contained in:
Quaternions 2023-10-23 14:55:48 -07:00
parent 7805c4db6d
commit 4cad20da09
3 changed files with 69 additions and 42 deletions

View File

@ -1,3 +1,5 @@
use crate::instruction::TimedInstruction;
fn optional_features() -> wgpu::Features { fn optional_features() -> wgpu::Features {
wgpu::Features::empty() wgpu::Features::empty()
} }
@ -197,6 +199,53 @@ pub fn setup(title:&str)->GraphicsContextSetup{
} }
} }
enum RunInstruction{
Resize(winit::dpi::PhysicalSize<u32>),
WindowEvent(winit::event::WindowEvent),
DeviceEvent(winit::event::DeviceEvent),
Render,
}
impl GraphicsContext{
fn into_worker(self,mut global_state:crate::GlobalState)->crate::worker::QNWorker<TimedInstruction<RunInstruction>>{
crate::worker::QNWorker::new(move |ins:TimedInstruction<RunInstruction>|{
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{ struct GraphicsContextSetup{
window:winit::window::Window, window:winit::window::Window,
event_loop:winit::event_loop::EventLoop<()>, event_loop:winit::event_loop::EventLoop<()>,
@ -216,8 +265,15 @@ impl GraphicsContextSetup{
pub fn start(self,mut global_state:crate::GlobalState){ pub fn start(self,mut global_state:crate::GlobalState){
let (window,event_loop,graphics_context)=self.into_split(); 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..."); println!("Entering render loop...");
let root_time=std::time::Instant::now();
event_loop.run(move |event,_,control_flow|{ 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"){ *control_flow=if cfg!(feature="metal-auto-capture"){
winit::event_loop::ControlFlow::Exit winit::event_loop::ControlFlow::Exit
}else{ }else{
@ -232,9 +288,9 @@ impl GraphicsContextSetup{
winit::event::WindowEvent::Resized(size) winit::event::WindowEvent::Resized(size)
| winit::event::WindowEvent::ScaleFactorChanged { | winit::event::WindowEvent::ScaleFactorChanged {
new_inner_size:&mut size, new_inner_size:&mut size,
.. scale_factor:_,
}, },
.. window_id:_,
} => { } => {
// Once winit is fixed, the detection conditions here can be removed. // Once winit is fixed, the detection conditions here can be removed.
// https://github.com/rust-windowing/winit/issues/2876 // https://github.com/rust-windowing/winit/issues/2876
@ -249,10 +305,7 @@ impl GraphicsContextSetup{
); );
}else{ }else{
println!("Resizing to {:?}",size); println!("Resizing to {:?}",size);
graphics_context.config.width=size.width.max(1); run_thread.send(TimedInstruction{time,instruction:RunInstruction::Resize(size)});
graphics_context.config.height=size.height.max(1);
window.resize(&graphics_context);
graphics_context.surface.configure(&graphics_context.device,&graphics_context.config);
} }
} }
winit::event::Event::WindowEvent{event,..}=>match event{ winit::event::Event::WindowEvent{event,..}=>match event{
@ -268,45 +321,18 @@ impl GraphicsContextSetup{
|winit::event::WindowEvent::CloseRequested=>{ |winit::event::WindowEvent::CloseRequested=>{
*control_flow=winit::event_loop::ControlFlow::Exit; *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{ winit::event::Event::DeviceEvent{
event, event,
.. ..
} => { } => {
global_state.device_event(event); run_thread.send(TimedInstruction{time,instruction:RunInstruction::DeviceEvent(event)});
}, },
winit::event::Event::RedrawRequested(_)=>{ winit::event::Event::RedrawRequested(_)=>{
let frame=match graphics_context.surface.get_current_texture(){ //send
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();
} }
_=>{} _=>{}
} }

View File

@ -22,7 +22,6 @@ mod graphics_context;
pub struct GlobalState{ pub struct GlobalState{
start_time: std::time::Instant,
manual_mouse_lock:bool, manual_mouse_lock:bool,
mouse:std::sync::Arc<std::sync::Mutex<physics::MouseState>>, mouse:std::sync::Arc<std::sync::Mutex<physics::MouseState>>,
user_settings:settings::UserSettings, user_settings:settings::UserSettings,
@ -159,7 +158,6 @@ impl GlobalState {
//3. forget //3. forget
let mut state=GlobalState{ let mut state=GlobalState{
start_time:Instant::now(),
manual_mouse_lock:false, manual_mouse_lock:false,
mouse:physics::MouseState::default(), mouse:physics::MouseState::default(),
user_settings, user_settings,

View File

@ -1,3 +1,6 @@
pub enum WindowInstruction{
Resize(),
}
pub struct WindowState{ pub struct WindowState{
//ok //ok
} }
@ -5,8 +8,7 @@ impl WindowState{
fn resize(&mut self); fn resize(&mut self);
fn render(&self); fn render(&self);
fn update(&mut self, window: &winit::window::Window, event: winit::event::WindowEvent) { fn window_event(&mut self, window: &winit::window::Window, event: winit::event::WindowEvent) {
let time=integer::Time::from_nanos(self.start_time.elapsed().as_nanos() as i64);
match event { match event {
winit::event::WindowEvent::DroppedFile(path)=>{ winit::event::WindowEvent::DroppedFile(path)=>{
std::thread::spawn(move ||{ std::thread::spawn(move ||{
@ -102,8 +104,6 @@ impl WindowState{
} }
fn device_event(&mut self, window: &winit::window::Window, event: winit::event::DeviceEvent) { 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 { match event {
winit::event::DeviceEvent::MouseMotion { winit::event::DeviceEvent::MouseMotion {
delta,//these (f64,f64) are integers on my machine delta,//these (f64,f64) are integers on my machine
@ -149,4 +149,7 @@ impl WindowState{
} }
builder.build(event_loop) builder.build(event_loop)
} }
pub fn into_thread(window:winit::window::Window){
//
}
} }