diff --git a/CHANGELOG.md b/CHANGELOG.md index c60b75b40..a5d0f2fa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `WindowBehavior::power_preference()` - `WindowBehavior::limits()` - `WindowBehavior::multisample_count()` +- `SpriteSheet::new()` now takes an additional parameter: `gutter_size`. Passing + `Size::ZERO` will cause the returned sprite sheet to be the same as before + this change. + + This new parameter allows using sprite sheets that have been exported with + consistent spacing between each sprite. ### Changed diff --git a/examples/sprites.rs b/examples/sprites.rs index 6c84c17a7..857ada6e3 100644 --- a/examples/sprites.rs +++ b/examples/sprites.rs @@ -3,6 +3,7 @@ use std::time::Duration; use appit::winit::error::EventLoopError; use appit::winit::keyboard::{Key, NamedKey}; use figures::units::{Px, UPx}; +use figures::Zero; use kludgine::app::WindowBehavior; use kludgine::figures::Size; use kludgine::sprite::{ @@ -55,6 +56,7 @@ impl WindowBehavior for Sprites { let sheet = SpriteSheet::new( texture, SPRITE_SIZE, + Size::ZERO, vec![ StickGuy::Idle1, StickGuy::Idle2, diff --git a/src/sprite.rs b/src/sprite.rs index aa42f9617..dcc9c0b7b 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -660,15 +660,26 @@ impl SpriteSheet where T: Debug + Eq + Hash, { - /// Creates a new sprite sheet, diving `texture` into a grid of `tile_size`. - /// The order of `tiles` will be read left-to-right, top-to-bottom. + /// Creates a new sprite sheet, diving `texture` into a grid of `tile_size` + /// with `gutter_size` spacing between each row and column. The order of + /// `tiles` will be read left-to-right, top-to-bottom. #[must_use] - pub fn new(texture: impl Into, tile_size: Size, tiles: Vec) -> Self { + pub fn new( + texture: impl Into, + tile_size: Size, + gutter_size: Size, + tiles: Vec, + ) -> Self { let texture = texture.into(); let dimensions = texture.size() / tile_size; Self { texture, - data: Arc::new(SpriteSheetData::from_tiles(tiles, tile_size, dimensions)), + data: Arc::new(SpriteSheetData::from_tiles( + tiles, + tile_size, + gutter_size, + dimensions, + )), } } @@ -723,9 +734,15 @@ where } impl SpriteSheetData { - fn from_tiles(tiles: Vec, tile_size: Size, dimensions: Size) -> Self { + fn from_tiles( + tiles: Vec, + tile_size: Size, + gutters: Size, + dimensions: Size, + ) -> Self { let mut sprites = HashMap::new(); + let full_size = tile_size + gutters; for (index, tile) in tiles.into_iter().enumerate() { let index = UPx::new(index.cast::()); let y = index / dimensions.width; @@ -733,7 +750,7 @@ impl SpriteSheetData { sprites.insert( tile, Rect::new( - Point::new(x * tile_size.width, y * tile_size.height), + Point::new(x * full_size.width, y * full_size.height), tile_size, ), );