combs_mesh/engine/
sprites.rs1use crate::blocks::SpriteAtlas;
5use crate::error::{MeshError, Result};
6
7pub fn frame_rect(atlas: &SpriteAtlas, frame_index: u32) -> Result<(u32, u32, u32, u32)> {
10 atlas.validate()?;
11 if frame_index >= atlas.frame_count {
12 return Err(MeshError::InvalidBlock(format!(
13 "frame index {frame_index} out of range ({} frames)",
14 atlas.frame_count
15 )));
16 }
17 let cols = atlas.width / atlas.frame_width;
18 let x = (frame_index % cols) * atlas.frame_width;
19 let y = (frame_index / cols) * atlas.frame_height;
20 Ok((x, y, atlas.frame_width, atlas.frame_height))
21}
22
23pub fn extract_frame(atlas: &SpriteAtlas, frame_index: u32) -> Result<Vec<u8>> {
26 let (x, y, w, h) = frame_rect(atlas, frame_index)?;
27 let stride = atlas.width as usize * 4;
28 let row_bytes = w as usize * 4;
29 let mut out = Vec::with_capacity(row_bytes * h as usize);
30 for row in 0..h as usize {
31 let start = (y as usize + row) * stride + x as usize * 4;
32 out.extend_from_slice(&atlas.rgba[start..start + row_bytes]);
33 }
34 Ok(out)
35}