spottedcat 0.6.4

Rusty SpottedCat simple game engine
Documentation
use crate::Image;
use std::collections::HashMap;

/// Key for caching individual glyphs in the atlas
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct GlyphCacheKey {
    pub font_id: u32,
    pub font_size_bits: u32,
    pub glyph_id: u32,
}

/// Cached glyph data including atlas image handle and positioning offset
#[derive(Clone, Debug)]
pub(crate) struct GlyphEntry {
    pub image: Image,
    pub offset: [f32; 2],
    pub advance: f32, // Horizontal advance width
}

/// Manages glyph rendering and caching to texture atlas
pub(crate) struct GlyphCache {
    cache: HashMap<GlyphCacheKey, GlyphEntry>,
}

impl GlyphCache {
    pub(crate) fn new() -> Self {
        Self {
            cache: HashMap::new(),
        }
    }

    pub(crate) fn get(&self, key: &GlyphCacheKey) -> Option<&GlyphEntry> {
        self.cache.get(key)
    }

    pub(crate) fn insert(&mut self, key: GlyphCacheKey, entry: GlyphEntry) {
        self.cache.insert(key, entry);
    }

    pub(crate) fn clear(&mut self) {
        self.cache.clear();
    }
}

impl Default for GlyphCache {
    fn default() -> Self {
        Self::new()
    }
}