diff options
Diffstat (limited to 'engine/src/rendering/object.rs')
| -rw-r--r-- | engine/src/rendering/object.rs | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/engine/src/rendering/object.rs b/engine/src/rendering/object.rs new file mode 100644 index 0000000..61e7ed5 --- /dev/null +++ b/engine/src/rendering/object.rs @@ -0,0 +1,137 @@ +use std::collections::HashMap; +use std::fmt::Display; +use std::sync::atomic::{AtomicU64, Ordering}; + +use crate::asset::Id as AssetId; +use crate::ecs::Sole; + +pub type RawValue = u32; + +/// Rendering object ID. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Id +{ + Asset(AssetId), + Sequential(SequentialId), +} + +impl Id +{ + pub fn new_sequential() -> Self + { + static NEXT_SEQUENTIAL_ID: AtomicU64 = AtomicU64::new(0); + + Self::Sequential(SequentialId( + NEXT_SEQUENTIAL_ID.fetch_add(1, Ordering::Relaxed), + )) + } + + pub fn into_asset_id(self) -> Option<AssetId> + { + match self { + Self::Asset(asset_id) => Some(asset_id), + Self::Sequential(_) => None, + } + } +} + +impl From<AssetId> for Id +{ + fn from(asset_id: AssetId) -> Self + { + Self::Asset(asset_id) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct SequentialId(u64); + +/// Rendering object store. +#[derive(Debug, Default, Sole)] +pub struct Store +{ + objects: HashMap<Id, Option<Object>>, +} + +impl Store +{ + pub fn get_obj(&self, id: &Id) -> Option<&Object> + { + self.objects.get(id).and_then(|obj| obj.as_ref()) + } + + pub fn contains_maybe_pending_with_id(&self, id: &Id) -> bool + { + self.objects.contains_key(id) + } + + pub fn contains_non_pending_with_id(&self, id: &Id) -> bool + { + self.objects.get(id).and_then(|obj| obj.as_ref()).is_some() + } + + pub fn insert(&mut self, id: Id, object: Object) + { + self.objects.insert(id, Some(object)); + } + + pub fn insert_pending(&mut self, id: Id) + { + self.objects.insert(id, None); + } + + pub fn remove(&mut self, id: &Id) -> Option<Option<Object>> + { + self.objects.remove(id) + } +} + +/// Rendering object. +#[derive(Debug, Clone)] +pub struct Object +{ + raw: RawValue, + kind: Kind, +} + +impl Object +{ + pub fn from_raw(raw: RawValue, kind: Kind) -> Self + { + Self { raw, kind } + } + + pub fn as_raw(&self) -> RawValue + { + self.raw + } + + pub fn kind(&self) -> Kind + { + self.kind + } +} + +/// Rendering object kind. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] +pub enum Kind +{ + Texture, + ShaderProgram, + Mesh, + Framebuffer, +} + +impl Display for Kind +{ + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + formatter.write_str(match self { + Self::Texture => "texture", + Self::ShaderProgram => "shader program", + Self::Mesh => "mesh", + Self::Framebuffer => "framebuffer", + }) + } +} |
