Skip to main content

concinnity_core/resource/
runtime.rs

1//! Payloads a running world bakes for itself at start.
2//!
3//! A geometry producer the build compiled carries a [`PayloadLocator`] into a
4//! blob section. One the world mints at start has no blob behind it, so its
5//! baked bytes live here and the renderer reads them straight out.
6
7use alloc::collections::BTreeMap;
8use alloc::vec::Vec;
9
10use crate::ecs::asset_id::AssetId;
11
12/// Mesh payloads baked at world start, keyed by the asset id of the
13/// `ProceduralMesh` that owns each one.
14///
15/// Their handles sit in the [`MeshBlock::Runtime`](super::MeshBlock::Runtime)
16/// tail of the shared mesh-source space, past every handle the build assigned.
17#[derive(Debug, Clone, Default)]
18pub struct RuntimeMeshPayloads(pub BTreeMap<AssetId, Vec<u8>>);
19
20impl RuntimeMeshPayloads {
21    /// The baked payload for one asset, if the world minted its geometry.
22    pub fn get(&self, id: AssetId) -> Option<&[u8]> {
23        self.0.get(&id).map(|b| &b[..])
24    }
25
26    /// Whether nothing was baked at start.
27    pub fn is_empty(&self) -> bool {
28        self.0.is_empty()
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use alloc::vec;
36
37    #[test]
38    fn a_baked_payload_is_found_by_its_owner_id() {
39        let mut payloads = RuntimeMeshPayloads::default();
40        assert!(payloads.is_empty());
41        payloads.0.insert(AssetId(7), vec![1, 2, 3]);
42        assert_eq!(payloads.get(AssetId(7)), Some(&[1u8, 2, 3][..]));
43        assert_eq!(payloads.get(AssetId(8)), None);
44        assert!(!payloads.is_empty());
45    }
46}