From 545f5b326ee12d46e6cfbfa1a99cc83de06d7caf Mon Sep 17 00:00:00 2001 From: Steins7 Date: Wed, 3 Feb 2021 09:10:53 +0100 Subject: [PATCH] Fixed close button + added controller main loop * cleaned code from previous commit --- src/controller.rs | 101 ++++++++++++++++-------------- src/lib.rs | 5 +- src/renderer/swap_system.rs | 30 +++++---- src/renderer/swap_system/frame.rs | 3 +- 4 files changed, 69 insertions(+), 70 deletions(-) diff --git a/src/controller.rs b/src/controller.rs index 595fe23..e84331b 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -55,60 +55,65 @@ where let mut input_keys: Vec = Vec::new(); - for pipeline in &mut self.pipelines { - for input in &pipeline.inputs { - match input.borrow().read(Duration::from_millis(1)) { - Ok(key) => input_keys.push(key), - Err(_err) => (), + loop { + for pipeline in &mut self.pipelines { + for input in &pipeline.inputs { + match input.borrow().read(Duration::from_millis(1)) { + Ok(key) => input_keys.push(key), + Err(_err) => (), + } } - } - for input_key in &input_keys { - match input_key { - Key::MouseMove{x,y} => { - self.mouse_pos = [ - (x/1280.0 * 2.0 - 1.0) as f32, - (y/720.0 * 2.0 - 1.0) as f32, - ]; - self.color = [ - (x/1280.0) as f32, - (y/720.0) as f32, - (((x + y)/2.0)/((1280.0 + 720.0)/2.0)) as f32, - ]; - }, - Key::Close => return, - _ => (), + for input_key in &input_keys { + match input_key { + Key::MouseMove{x,y} => { + self.mouse_pos = [ + (x/1280.0 * 2.0 - 1.0) as f32, + (y/720.0 * 2.0 - 1.0) as f32, + ]; + self.color = [ + (x/1280.0) as f32, + (y/720.0) as f32, + (((x + y)/2.0)/((1280.0 + 720.0)/2.0)) as f32, + ]; + }, + Key::Close => { + info!("Shutting down IV !"); + return; + } + _ => (), + }; + } + for subengines in &pipeline.subengines { + for subengine in subengines { + subengine.exec(SubengineCommand::Run); + } + for subengine in subengines { + subengine.wait_for_exec(Duration::from_millis(1)).unwrap(); + } + } + + let triangle = Triangle { + points: [self.mouse_pos, [-0.5, 0.5], [-0.5, -0.5]], }; - } - for subengines in &pipeline.subengines { - for subengine in subengines { - subengine.exec(SubengineCommand::Run); - } - for subengine in subengines { - subengine.wait_for_exec(Duration::from_millis(1)).unwrap(); - } - } - - let triangle = Triangle { - points: [self.mouse_pos, [-0.5, 0.5], [-0.5, -0.5]], - }; - let colors = [ - [self.color[0], self.color[1], self.color[2]], - [self.color[2], self.color[0], self.color[1]], - [self.color[1], self.color[2], self.color[0]], - ]; + let colors = [ + [self.color[0], self.color[1], self.color[2]], + [self.color[2], self.color[0], self.color[1]], + [self.color[1], self.color[2], self.color[0]], + ]; - for (renderer, output) in &mut pipeline.renderers { - // match renderer.draw_clear_frame(output, self.color) { - // Err(err) => warn!("{}", err), - // _ => (), - // } - match renderer.draw_triangle_frame(output, triangle, colors) { - Err(err) => warn!("{}", err), - _ => (), + for (renderer, output) in &mut pipeline.renderers { + // match renderer.draw_clear_frame(output, self.color) { + // Err(err) => warn!("{}", err), + // _ => (), + // } + match renderer.draw_triangle_frame(output, triangle, colors) { + Err(err) => warn!("{}", err), + _ => (), + }; }; }; - }; + } } } //These tests are disabled because of some stange issue with cargo not waiting for the drop diff --git a/src/lib.rs b/src/lib.rs index 33b0421..7b560c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,10 +90,7 @@ pub fn run() -> Result<(), &'static str> { //running controller let mut controller = Controller::new(vec![subengine_pipeline]); - loop { - //for _i in 0..40 { - controller.run(); - } + controller.run(); //let engine_pipelines = vec![ // EnginePipeline { diff --git a/src/renderer/swap_system.rs b/src/renderer/swap_system.rs index cffe1a0..4ba1b0f 100644 --- a/src/renderer/swap_system.rs +++ b/src/renderer/swap_system.rs @@ -189,11 +189,11 @@ where //println!("Frame nb : {}", self.frames.len()); //TODO frames number diminish sometimes at resize... - static mut id: i32 = 0; + //static mut id: i32 = 0; let mut frame = self.frames.pop_back().unwrap(); - unsafe { - id = (id+1)%3; - } + //unsafe { + // id = (id+1)%3; + //} trace!("Waiting for Frame..."); unsafe { @@ -214,9 +214,10 @@ where frame.command_buffer.reset(false); //TODO may be needed at some point... }; - unsafe { - trace!("Acquiring Frame {}...", id); - } + //unsafe { + // trace!("Acquiring Frame {}...", id); + //} + trace!("Acquiring Frame..."); let image = unsafe { match self.surface.acquire_image(core::u64::MAX) { Ok((image, suboptimal)) => { @@ -228,7 +229,7 @@ where }}; trace!("Creating Framebuffer..."); - let mut framebuffer = unsafe { + let framebuffer = unsafe { use gfx_hal::device::Device; use std::borrow::Borrow; @@ -239,15 +240,12 @@ where .unwrap() //TODO improve that }; - unsafe { - use gfx_hal::{ - device::Device, - command::CommandBuffer, - }; + //unsafe { + // use gfx_hal::{device::Device}; - gpu.device() - .set_framebuffer_name(&mut framebuffer, format!("frambuffer {}", id).as_str()); - } + // gpu.device() + // .set_framebuffer_name(&mut framebuffer, format!("frambuffer {}", id).as_str()); + //} frame.link_swapchain_image(gpu, image, framebuffer); diff --git a/src/renderer/swap_system/frame.rs b/src/renderer/swap_system/frame.rs index 1d0b2af..3312a4d 100644 --- a/src/renderer/swap_system/frame.rs +++ b/src/renderer/swap_system/frame.rs @@ -98,10 +98,9 @@ where } pub fn link_swapchain_image(&mut self, - gpu: &Gpu, + _gpu: &Gpu, //TODO clear that when framebuffer issue is fixed image: >::SwapchainImage, framebuffer: B::Framebuffer) { - use gfx_hal::device::Device; self.framebuffer = Some(framebuffer); //match self.framebuffer.replace(framebuffer) {