diff --git a/build.rs b/build.rs index 08382a0..14f80a7 100644 --- a/build.rs +++ b/build.rs @@ -19,6 +19,15 @@ struct MapperFrame { height: u16, } +impl MapperFrame { + fn to_u16vec2_format(self) -> String { + format!( + "MapperFrame {{ pos: glam::U16Vec2::new({}, {}), size: glam::U16Vec2::new({}, {}) }}", + self.x, self.y, self.width, self.height + ) + } +} + fn main() { let path = Path::new(&env::var("OUT_DIR").unwrap()).join("atlas_data.rs"); let mut file = BufWriter::new(File::create(&path).unwrap()); @@ -37,12 +46,7 @@ fn main() { .unwrap(); for (name, frame) in atlas_mapper.frames { - writeln!( - &mut file, - " \"{}\" => MapperFrame {{ x: {}, y: {}, width: {}, height: {} }},", - name, frame.x, frame.y, frame.width, frame.height - ) - .unwrap(); + writeln!(&mut file, " \"{}\" => {},", name, frame.to_u16vec2_format()).unwrap(); } writeln!(&mut file, "}};").unwrap(); diff --git a/src/texture/sprite.rs b/src/texture/sprite.rs index afd7acd..815ba33 100644 --- a/src/texture/sprite.rs +++ b/src/texture/sprite.rs @@ -3,24 +3,21 @@ use glam::U16Vec2; use sdl2::pixels::Color; use sdl2::rect::Rect; use sdl2::render::{Canvas, RenderTarget, Texture}; -use serde::Deserialize; use std::collections::HashMap; use crate::error::TextureError; /// Atlas frame mapping data loaded from JSON metadata files. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug)] pub struct AtlasMapper { /// Mapping from sprite name to frame bounds within the atlas texture pub frames: HashMap, } -#[derive(Copy, Clone, Debug, Deserialize)] +#[derive(Copy, Clone, Debug)] pub struct MapperFrame { - pub x: u16, - pub y: u16, - pub width: u16, - pub height: u16, + pub pos: U16Vec2, + pub size: U16Vec2, } #[derive(Copy, Clone, Debug, PartialEq)] @@ -108,8 +105,8 @@ impl SpriteAtlas { /// for repeated use in animations and entity sprites. pub fn get_tile(&self, name: &str) -> Option { self.tiles.get(name).map(|frame| AtlasTile { - pos: U16Vec2::new(frame.x, frame.y), - size: U16Vec2::new(frame.width, frame.height), + pos: frame.pos, + size: frame.size, color: None, }) } diff --git a/tests/sprite.rs b/tests/sprite.rs index 74974ba..338ef52 100644 --- a/tests/sprite.rs +++ b/tests/sprite.rs @@ -13,10 +13,8 @@ fn test_sprite_atlas_basic() { frames.insert( "test".to_string(), MapperFrame { - x: 10, - y: 20, - width: 32, - height: 64, + pos: U16Vec2::new(10, 20), + size: U16Vec2::new(32, 64), }, ); @@ -38,19 +36,15 @@ fn test_sprite_atlas_multiple_tiles() { frames.insert( "tile1".to_string(), MapperFrame { - x: 0, - y: 0, - width: 32, - height: 32, + pos: U16Vec2::new(0, 0), + size: U16Vec2::new(32, 32), }, ); frames.insert( "tile2".to_string(), MapperFrame { - x: 32, - y: 0, - width: 64, - height: 64, + pos: U16Vec2::new(32, 0), + size: U16Vec2::new(64, 64), }, );