From c4ad4fbdf90b15a582265d2395f46fcb1bf3523a Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Mon, 23 Oct 2023 17:27:18 -0700
Subject: [PATCH] move run stuff into run module

---
 src/graphics_context.rs | 50 ++---------------------------------------
 src/run.rs              | 45 +++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 48 deletions(-)

diff --git a/src/graphics_context.rs b/src/graphics_context.rs
index 872a6c9..f42d32c 100644
--- a/src/graphics_context.rs
+++ b/src/graphics_context.rs
@@ -1,4 +1,5 @@
 use crate::instruction::TimedInstruction;
+use crate::run::RunInstruction;
 
 fn optional_features() -> wgpu::Features {
 	wgpu::Features::empty()
@@ -207,53 +208,6 @@ 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 run:crate::run::RunState)->crate::worker::QNWorker<TimedInstruction<RunInstruction>>{
-		crate::worker::QNWorker::new(move |ins:TimedInstruction<RunInstruction>|{
-			match ins.instruction{
-				RunInstruction::WindowEvent(window_event)=>{
-					run.window_event(window_event);
-				},
-				RunInstruction::DeviceEvent(device_event)=>{
-					run.device_event(device_event);
-				},
-				RunInstruction::Resize(size)=>{
-					self.config.width=size.width.max(1);
-					self.config.height=size.height.max(1);
-					run.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()
-					});
-
-					run.graphics.render(&view,&self.device,&self.queue);
-
-					frame.present();
-				}
-			}
-		})
-	}
-}
-
 struct GraphicsContextSetup{
 	window:winit::window::Window,
 	event_loop:winit::event_loop::EventLoop<()>,
@@ -276,7 +230,7 @@ impl GraphicsContextSetup{
 		//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(run);
+		let run_thread=run.into_worker(graphics_context);
 
 		println!("Entering render loop...");
 		let root_time=std::time::Instant::now();
diff --git a/src/run.rs b/src/run.rs
index 061ad34..dc3e2c9 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -2,6 +2,13 @@ use crate::physics::PhysicsInstruction;
 use crate::render_thread::InputInstruction;
 use crate::instruction::{TimedInstruction, InstructionConsumer};
 
+pub enum RunInstruction{
+	Resize(winit::dpi::PhysicalSize<u32>),
+	WindowEvent(winit::event::WindowEvent),
+	DeviceEvent(winit::event::DeviceEvent),
+	Render,
+}
+
 pub struct RunState{
 	manual_mouse_lock:bool,
 	mouse:std::sync::Arc<std::sync::Mutex<physics::MouseState>>,
@@ -184,4 +191,42 @@ impl RunState {
 			_=>(),
 		}
 	}
+
+	pub fn into_worker(self,mut graphics_context:crate::graphics_context::GraphicsContext)->crate::worker::QNWorker<TimedInstruction<RunInstruction>>{
+		crate::worker::QNWorker::new(move |ins:TimedInstruction<RunInstruction>|{
+			match ins.instruction{
+				RunInstruction::WindowEvent(window_event)=>{
+					self.window_event(window_event);
+				},
+				RunInstruction::DeviceEvent(device_event)=>{
+					self.device_event(device_event);
+				},
+				RunInstruction::Resize(size)=>{
+					graphics_context.config.width=size.width.max(1);
+					graphics_context.config.height=size.height.max(1);
+					self.graphics.resize(&graphics_context.device,&graphics_context.config);
+					graphics_context.surface.configure(&graphics_context.device,&graphics_context.config);
+				}
+				RunInstruction::Render=>{
+					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()
+					});
+
+					self.graphics.render(&view,&graphics_context.device,&graphics_context.queue);
+
+					frame.present();
+				}
+			}
+		})
+	}
 }
\ No newline at end of file