use crate::compat::HashMap;
use crate::core::Size;
#[derive(Debug, Clone, Copy)]
pub struct AtlasRect {
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone)]
pub struct AtlasEntry {
pub rect: AtlasRect,
pub texture_id: u64,
}
pub struct TextureAtlas {
max_size: Size,
entries: HashMap<u64, AtlasEntry>,
next_id: u64,
cursor_x: u32,
cursor_y: u32,
row_height: u32,
}
impl TextureAtlas {
pub fn new(max_size: Size) -> Self {
Self {
max_size,
entries: HashMap::new(),
next_id: 1,
cursor_x: 0,
cursor_y: 0,
row_height: 0,
}
}
pub fn allocate(&mut self, width: u32, height: u32) -> Option<(u64, AtlasRect)> {
if width > self.max_size.width || height > self.max_size.height {
return None;
}
let wrap = self.cursor_x.checked_add(width).map(|next| next > self.max_size.width)?;
let x = if wrap { 0 } else { self.cursor_x };
let y = if wrap { self.cursor_y.checked_add(self.row_height)? } else { self.cursor_y };
if y.checked_add(height)? > self.max_size.height {
return None; }
let id = self.next_id;
self.next_id = self.next_id.saturating_add(1);
let rect = AtlasRect { x, y, width, height };
self.entries.insert(id, AtlasEntry { rect, texture_id: id });
self.cursor_x = x.checked_add(width)?;
self.cursor_y = y;
self.row_height = if wrap { height } else { self.row_height.max(height) };
Some((id, rect))
}
pub fn get(&self, id: u64) -> Option<&AtlasEntry> {
self.entries.get(&id)
}
pub fn remove(&mut self, id: u64) {
self.entries.remove(&id);
}
pub fn clear(&mut self) {
self.entries.clear();
self.cursor_x = 0;
self.cursor_y = 0;
self.row_height = 0;
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn utilization(&self) -> f32 {
if self.max_size.width == 0 || self.max_size.height == 0 {
return 0.0;
}
let total_pixels = self.max_size.width as u64 * self.max_size.height as u64;
let used_pixels: u64 =
self.entries.values().map(|e| e.rect.width as u64 * e.rect.height as u64).sum();
used_pixels as f32 / total_pixels as f32
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entries_pack_left_to_right_and_wrap_to_a_new_row() {
let mut atlas = TextureAtlas::new(Size::new(100, 100));
let (_, a) = atlas.allocate(60, 20).expect("first entry fits");
assert_eq!((a.x, a.y), (0, 0));
let (_, b) = atlas.allocate(60, 20).expect("second entry wraps to a new row");
assert_eq!((b.x, b.y), (0, 20), "it starts at column 0 on the row below");
let (_, c) = atlas.allocate(30, 20).expect("a narrow entry follows on the current row");
assert_eq!((c.x, c.y), (60, 20));
}
#[test]
fn an_entry_larger_than_the_atlas_is_refused() {
let mut atlas = TextureAtlas::new(Size::new(64, 64));
assert!(atlas.allocate(128, 16).is_none(), "wider than the atlas");
assert!(atlas.allocate(16, 128).is_none(), "taller than the atlas");
assert!(atlas.allocate(64, 64).is_some(), "an exactly-atlas-sized entry still fits");
}
#[test]
fn a_huge_request_is_refused_without_panicking() {
let mut atlas = TextureAtlas::new(Size::new(64, 64));
assert!(atlas.allocate(u32::MAX, 1).is_none());
assert!(atlas.allocate(1, u32::MAX).is_none());
assert!(atlas.allocate(u32::MAX, u32::MAX).is_none());
assert!(atlas.allocate(8, 8).is_some());
}
#[test]
fn a_failed_allocation_does_not_corrupt_packer_state() {
let mut atlas = TextureAtlas::new(Size::new(10, 10));
assert!(atlas.allocate(6, 6).is_some());
assert!(atlas.allocate(5, 5).is_none());
let (_, rect) = atlas.allocate(4, 6).expect("packer state must remain intact");
assert_eq!((rect.x, rect.y), (6, 0));
}
#[test]
fn utilization_tracks_the_used_area() {
let mut atlas = TextureAtlas::new(Size::new(100, 100));
assert_eq!(atlas.utilization(), 0.0);
let (_, rect) = atlas.allocate(50, 50).expect("fits");
assert_eq!(rect.width, 50);
assert!((atlas.utilization() - 0.25).abs() < 1e-6);
}
}