From 2559d6b301f6268304bebaa7e5d55d9019b6edfd Mon Sep 17 00:00:00 2001 From: Elham Aryanpur Date: Wed, 13 Sep 2023 12:38:52 +0300 Subject: [PATCH] chore: added some fixes to the PR --- examples/utils/render_order.rs | 27 ++++++++++++++++++++++++--- src/objects.rs | 2 +- src/render.rs | 8 ++++---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/examples/utils/render_order.rs b/examples/utils/render_order.rs index b631b21..2ce579b 100644 --- a/examples/utils/render_order.rs +++ b/examples/utils/render_order.rs @@ -1,8 +1,17 @@ +/* +* Render Order example by https://github.com/akowi-sknobloch which shows which object is rendered on top. +* +* The license is same as the one on the root. +*/ + +// imports needed use blue_engine::{primitive_shapes::square, Engine, ObjectSettings}; fn main() { - let mut engine = Engine::new().expect("win"); + // initialize the engine + let mut engine = Engine::new().expect("couldn't initialize engine"); + // make the first layer square( "layer1", ObjectSettings::default(), @@ -11,6 +20,7 @@ fn main() { ) .expect("failed to create square"); + // make the second layer square( "layer2", ObjectSettings::default(), @@ -19,37 +29,48 @@ fn main() { ) .expect("failed to create square"); + // Get layer 1 object let layer1 = engine .objects .get_mut("layer1") .expect("failed to gete object"); + // set a color to differenciate it layer1 .set_uniform_color(1f32, 0.5, 0f32, 1f32) .expect("failed to set color"); + // move it to left a bit layer1.set_position(-0.5, 0f32, 0f32); - + // set render order to 0th layer1.set_render_order(0).unwrap(); + // Get layer 2 object let layer2 = engine .objects .get_mut("layer2") .expect("failed to gete object"); + // set a color to differenciate it layer2 .set_uniform_color(0f32, 0f32, 1f32, 1f32) .expect("failed to set color"); + // move it to right a bit layer2.set_position(0.5, 0f32, 0f32); - + // set render order to 1st layer2.set_render_order(1).unwrap(); + // get a timer for order change let start = std::time::SystemTime::now(); + // start the update loop engine .update_loop(move |_, _, object_storage, _, _, _| { + // get the target layer to change order of let target = object_storage.get_mut("layer1").unwrap(); + // on ever 2 seconds change order if start.elapsed().unwrap().as_secs() % 2 == 0 { target.set_render_order(2).unwrap(); } else { + // change back to default target.set_render_order(0).unwrap(); } }) diff --git a/src/objects.rs b/src/objects.rs index a72138b..830233d 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -310,10 +310,10 @@ impl Object { } /// Changes the render order of the Object. + /// /// Objects with higher number get rendered later and appear "on top" when occupying the same space pub fn set_render_order(&mut self, render_order: usize) -> anyhow::Result<()> { self.render_order = render_order; - self.changed = true; Ok(()) } diff --git a/src/render.rs b/src/render.rs index 038cc78..d1f84ae 100644 --- a/src/render.rs +++ b/src/render.rs @@ -272,11 +272,11 @@ impl Renderer { // sort the object list in descending render order let mut object_list: Vec<_> = objects.iter().collect(); - object_list.sort_by(|a, b| a.1.render_order.cmp(&b.1.render_order).reverse()); + object_list.sort_by(|(_, a), (_, b)| a.render_order.cmp(&b.render_order).reverse()); - for i in object_list { - if i.1.is_visible { - let i = i.1; + for (_, i) in object_list { + if i.is_visible { + let i = i; let vertex_buffer = get_pipeline_vertex_buffer(&i.pipeline.vertex_buffer, objects); let shader = get_pipeline_shader(&i.pipeline.shader, objects);