Skip to content

Commit

Permalink
Added gutters to sprite sheet
Browse files Browse the repository at this point in the history
Closes #74
  • Loading branch information
ecton committed Apr 11, 2024
1 parent 0978438 commit f63027b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions examples/sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -55,6 +56,7 @@ impl WindowBehavior for Sprites {
let sheet = SpriteSheet::new(
texture,
SPRITE_SIZE,
Size::ZERO,
vec![
StickGuy::Idle1,
StickGuy::Idle2,
Expand Down
29 changes: 23 additions & 6 deletions src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,26 @@ impl<T> SpriteSheet<T>
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<ShareableTexture>, tile_size: Size<UPx>, tiles: Vec<T>) -> Self {
pub fn new(
texture: impl Into<ShareableTexture>,
tile_size: Size<UPx>,
gutter_size: Size<UPx>,
tiles: Vec<T>,
) -> 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,
)),
}
}

Expand Down Expand Up @@ -723,17 +734,23 @@ where
}

impl<T: Debug + Eq + Hash> SpriteSheetData<T> {
fn from_tiles(tiles: Vec<T>, tile_size: Size<UPx>, dimensions: Size<UPx>) -> Self {
fn from_tiles(
tiles: Vec<T>,
tile_size: Size<UPx>,
gutters: Size<UPx>,
dimensions: Size<UPx>,
) -> 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::<u32>());
let y = index / dimensions.width;
let x = index - y * dimensions.width;
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,
),
);
Expand Down

0 comments on commit f63027b

Please sign in to comment.