use std::sync::Arc;
use peniko::Blob;
use peniko::ImageAlphaType;
use peniko::ImageData;
use peniko::ImageFormat;
use rustc_hash::FxHashMap;
#[derive(Clone, Debug, Default)]
pub struct Assets {
images: FxHashMap<Arc<str>, ImageData>,
}
impl Assets {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn insert_rgba8(&mut self, asset: &str, width: u32, height: u32, pixels: Vec<u8>) {
assert_eq!(
pixels.len(),
width as usize * height as usize * 4,
"RGBA8 needs four bytes per pixel"
);
let image = ImageData {
data: Blob::new(Arc::new(pixels)),
format: ImageFormat::Rgba8,
alpha_type: ImageAlphaType::Alpha,
width,
height,
};
self.images.insert(asset.into(), image);
}
#[must_use]
pub fn get(&self, asset: &str) -> Option<&ImageData> {
self.images.get(asset)
}
pub fn remove(&mut self, asset: &str) {
self.images.remove(asset);
}
}