From f0dc1cca3c5e0a75f8fca765043cc4d15d5c5609 Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Wed, 7 Apr 2021 14:13:01 +0000 Subject: [PATCH] engine: add optional labels to resource manager --- engine/src/main.rs | 8 ++++---- engine/src/render/mod.rs | 4 ++-- engine/src/render/resource.rs | 30 ++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/engine/src/main.rs b/engine/src/main.rs index a52e2b8..ae8bada 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -103,13 +103,13 @@ impl Main { 20, 22, 21, 22, 20, 23, ]); - renderer.add_resource(Mesh::new(vertices, indices)) + renderer.add_resource(Mesh::new(vertices, indices), Some("cube")) }; let material = renderer.add_resource(PBRMaterialBuilder { diffuse: Texture::from_image(String::from("//assets/test-128px.png")), roughness: Texture::from_image(String::from("//assets/test-128px-roughness.png")), - }.build()); + }.build(), Some("test-128px")); for x in -20..20 { for y in -20..20 { @@ -122,7 +122,7 @@ impl Main { } } - let light = renderer.add_resource(Light::omni_test()); + let light = renderer.add_resource(Light::omni_test(), Some("omni")); // The Sun (Sol) is 1AU from the Earth. We ignore the diameter of the Sun and the Earth, as // these are negligible at this scale. @@ -135,7 +135,7 @@ impl Main { let sun_lumen: f32 = sun_luminous_emittance * (4.0 * 3.14159 * sun_distance * sun_distance); let sun_color = color::XYZ::new(sun_lumen/3.0, sun_lumen/3.0, sun_lumen/3.0); - let sun = renderer.add_resource(Light::omni_with_color(sun_color)); + let sun = renderer.add_resource(Light::omni_with_color(sun_color), Some("sun")); // In our scene, the sun at a 30 degree zenith. let sun_angle: f32 = (3.14159 * 2.0) / (360.0 / 30.0); diff --git a/engine/src/render/mod.rs b/engine/src/render/mod.rs index 4be825f..7cda0b4 100644 --- a/engine/src/render/mod.rs +++ b/engine/src/render/mod.rs @@ -303,7 +303,7 @@ impl Renderer { return (close, events); } - pub fn add_resource(&mut self, r: T) -> ResourceID { - self.rm.add(r) + pub fn add_resource(&mut self, r: T, label: Option) -> ResourceID { + self.rm.add(r, label) } } diff --git a/engine/src/render/resource.rs b/engine/src/render/resource.rs index f97e0c8..09dc92f 100644 --- a/engine/src/render/resource.rs +++ b/engine/src/render/resource.rs @@ -9,6 +9,8 @@ pub struct Manager { meshes: Map, materials: Map, lights: Map, + + label_to_numeric: BTreeMap, counter: u64, } @@ -19,6 +21,7 @@ impl Manager { materials: BTreeMap::new(), lights: BTreeMap::new(), + label_to_numeric: BTreeMap::new(), counter: 0, } } @@ -27,15 +30,30 @@ impl Manager { T::map(&self) } - pub fn add(&mut self, r: T) -> ResourceID { + pub fn add(&mut self, r: T, label: Option) -> ResourceID { + let numeric = self.counter; + if let Some(label) = label { + self.label_to_numeric.insert(label.to_string(), numeric); + } let id = ResourceID { - numerical: self.counter, + numeric, phantom: std::marker::PhantomData, }; + self.counter += 1; T::map_mut(self).insert(id, r); id } + + pub fn by_label(&self, label: S) -> Option<&T> { + let label = label.to_string(); + let numeric = self.label_to_numeric.get(&label)?.clone(); + let rid = ResourceID { + numeric, + phantom: std::marker::PhantomData, + }; + T::map(self).get(&rid) + } } pub trait Resource: Sized { @@ -58,14 +76,14 @@ impl Resource for Material { #[derive(Debug)] pub struct ResourceID { - numerical: u64, + numeric: u64, phantom: std::marker::PhantomData, } impl Clone for ResourceID { fn clone(&self) -> ResourceID { ResourceID { - numerical: self.numerical.clone(), + numeric: self.numeric.clone(), phantom: std::marker::PhantomData, } } @@ -81,7 +99,7 @@ impl ResourceID { impl Ord for ResourceID { fn cmp(&self, other: &Self) -> Ordering { - self.numerical.cmp(&other.numerical) + self.numeric.cmp(&other.numeric) } } @@ -93,7 +111,7 @@ impl PartialOrd for ResourceID { impl PartialEq for ResourceID { fn eq(&self, other: &Self) -> bool { - self.numerical == other.numerical + self.numeric == other.numeric } }