engine: add optional labels to resource manager

master
q3k 2021-04-07 14:13:01 +00:00
parent 8b49f8324e
commit f0dc1cca3c
3 changed files with 30 additions and 12 deletions

View File

@ -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);

View File

@ -303,7 +303,7 @@ impl Renderer {
return (close, events);
}
pub fn add_resource<T: Resource>(&mut self, r: T) -> ResourceID<T> {
self.rm.add(r)
pub fn add_resource<T: Resource, S: ToString>(&mut self, r: T, label: Option<S>) -> ResourceID<T> {
self.rm.add(r, label)
}
}

View File

@ -9,6 +9,8 @@ pub struct Manager {
meshes: Map<Mesh>,
materials: Map<Material>,
lights: Map<Light>,
label_to_numeric: BTreeMap<String, u64>,
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<T: Resource>(&mut self, r: T) -> ResourceID<T> {
pub fn add<T: Resource, S: ToString>(&mut self, r: T, label: Option<S>) -> ResourceID<T> {
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<T: Resource, S: ToString>(&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<T: Resource> {
numerical: u64,
numeric: u64,
phantom: std::marker::PhantomData<T>,
}
impl <T: Resource> Clone for ResourceID<T> {
fn clone(&self) -> ResourceID<T> {
ResourceID {
numerical: self.numerical.clone(),
numeric: self.numeric.clone(),
phantom: std::marker::PhantomData,
}
}
@ -81,7 +99,7 @@ impl <T: Resource> ResourceID<T> {
impl <T: Resource> Ord for ResourceID<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.numerical.cmp(&other.numerical)
self.numeric.cmp(&other.numeric)
}
}
@ -93,7 +111,7 @@ impl <T: Resource> PartialOrd for ResourceID<T> {
impl <T: Resource> PartialEq for ResourceID<T> {
fn eq(&self, other: &Self) -> bool {
self.numerical == other.numerical
self.numeric == other.numeric
}
}