Added hidden texture support in packer

master
Mark 2024-01-21 11:55:58 -08:00
parent fa719d46b9
commit 267ec5a40a
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
2 changed files with 50 additions and 18 deletions

View File

@ -4,6 +4,7 @@ use image::{imageops, GenericImageView, ImageBuffer, Rgba, RgbaImage};
use std::{ use std::{
fs::File, fs::File,
io::{Read, Write}, io::{Read, Write},
num::NonZeroU32,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -205,19 +206,19 @@ impl AtlasSet {
) )
})?; })?;
self.index self.index.push(
.path_map p,
.insert(p.to_path_buf(), self.index.index.len() as u32); SpriteAtlasImage {
true_size: image_dim,
self.index.index.push(SpriteAtlasImage { // Add one to account for hidden texture
true_size: image_dim, idx: NonZeroU32::new(self.index.len() as u32 + 1).unwrap(),
idx: self.index.index.len() as u32, atlas: atlas_idx as u32,
atlas: atlas_idx as u32, x: (x + self.image_margin) as f32 / self.texture_width as f32,
x: (x + self.image_margin) as f32 / self.texture_width as f32, y: (y + self.image_margin) as f32 / self.texture_height as f32,
y: (y + self.image_margin) as f32 / self.texture_height as f32, w: image_dim.0 as f32 / self.texture_width as f32,
w: image_dim.0 as f32 / self.texture_width as f32, h: image_dim.1 as f32 / self.texture_height as f32,
h: image_dim.1 as f32 / self.texture_height as f32, },
}); );
return Ok(atlas_idx); return Ok(atlas_idx);
} }

View File

@ -2,7 +2,11 @@
//! This crate creates texture atlases from an asset tree. //! This crate creates texture atlases from an asset tree.
use std::{collections::HashMap, path::PathBuf}; use std::{
collections::HashMap,
num::NonZeroU32,
path::{Path, PathBuf},
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -13,8 +17,9 @@ pub struct SpriteAtlasImage {
/// This is an index in SpriteAtlas.atlas_list /// This is an index in SpriteAtlas.atlas_list
pub atlas: u32, pub atlas: u32,
/// A globally unique, consecutively numbered index for this sprite /// A globally unique, consecutively numbered index for this sprite.
pub idx: u32, /// This is nonzero because index zero is reserved for the "hidden" texture.
pub idx: NonZeroU32,
/// The size of this image, in pixels /// The size of this image, in pixels
pub true_size: (u32, u32), pub true_size: (u32, u32),
@ -41,10 +46,10 @@ pub struct SpriteAtlasImage {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SpriteAtlas { pub struct SpriteAtlas {
/// The images in this atlas /// The images in this atlas
pub index: Vec<SpriteAtlasImage>, pub(crate) index: Vec<SpriteAtlasImage>,
/// Map paths to image indices /// Map paths to image indices
pub path_map: HashMap<PathBuf, u32>, path_map: HashMap<PathBuf, NonZeroU32>,
/// The file names of the atlas textures we've generated /// The file names of the atlas textures we've generated
pub atlas_list: Vec<String>, pub atlas_list: Vec<String>,
@ -59,4 +64,30 @@ impl SpriteAtlas {
atlas_list: Vec::new(), atlas_list: Vec::new(),
} }
} }
/// Get a SpriteAtlasImage by index
pub fn get_by_idx(&self, idx: NonZeroU32) -> &SpriteAtlasImage {
&self.index[idx.get() as usize - 1]
}
/// Get an image index from its path
/// returns None if this path isn't in this index
pub fn get_idx_by_path(&self, path: &Path) -> Option<NonZeroU32> {
self.path_map.get(path).map(|x| *x)
}
/// Get the number of images in this atlas
pub fn len(&self) -> u32 {
self.index.len() as u32
}
/// Add an image with the given path to this index
pub fn push(&mut self, p: &Path, i: SpriteAtlasImage) {
self.path_map.insert(
p.to_path_buf(),
NonZeroU32::new(self.index.len() as u32 + 1).unwrap(),
);
self.index.push(i);
}
} }