Skip to content

Commit

Permalink
Add Spritesheet::atlas_layout()
Browse files Browse the repository at this point in the history
  • Loading branch information
merwaaan committed Aug 28, 2024
1 parent 5797c49 commit 1b6bfe0
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 77 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## 0.4.0 - 2024-??-??
## 0.4.0 - 2024-08-28

This update simplifies the high-level API of the library.

Expand All @@ -14,6 +14,7 @@ To create a variant of a clip, just clone and reconfigure it before registering
### Added

- Add with_xxx() methods to Clip and Animation to make it easier to set their parameters
- Add Spritesheet::atlas_layout() to create a TextureAtlasLayout from a spritesheet

### Changed

Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,7 @@ fn setup(

let texture = assets.load("character.png");

let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(96, 96),
8,
8,
None,
None,
));
let layout = atlas_layouts.add(spritesheet.atlas_layout(96, 96));

commands.spawn((
SpriteBundle {
Expand Down
8 changes: 1 addition & 7 deletions benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ fn basic(c: &mut Criterion) {
.get_resource_mut::<Assets<TextureAtlasLayout>>()
.unwrap();

let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(96, 96),
8,
8,
None,
None,
));
let layout = atlas_layouts.add(Spritesheet::new(8, 8).atlas_layout(96, 96));

let mut library = app
.world_mut()
Expand Down
8 changes: 1 addition & 7 deletions examples/3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ fn setup(

let texture = assets.load("character.png");

let atlas_layout_handle = atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(96, 96),
8,
8,
None,
None,
));
let atlas_layout_handle = atlas_layouts.add(spritesheet.atlas_layout(96, 96));

// Spawn 3D sprites

Expand Down
8 changes: 1 addition & 7 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ fn spawn_sprite(

let texture = assets.load("character.png");

let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(96, 96),
8,
8,
None,
None,
));
let layout = atlas_layouts.add(Spritesheet::new(8, 8).atlas_layout(96, 96));

// Spawn a sprite with Bevy's built-in SpriteBundle

Expand Down
8 changes: 1 addition & 7 deletions examples/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,7 @@ fn setup(

let texture = assets.load("character.png");

let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(96, 96),
8,
8,
None,
None,
));
let layout = atlas_layouts.add(spritesheet.atlas_layout(96, 96));

commands.spawn((
SpriteBundle {
Expand Down
8 changes: 1 addition & 7 deletions examples/composition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ fn setup(

let texture = assets.load("character.png");

let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(96, 96),
8,
8,
None,
None,
));
let layout = atlas_layouts.add(spritesheet.atlas_layout(96, 96));

commands.spawn((
SpriteBundle {
Expand Down
8 changes: 1 addition & 7 deletions examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ fn setup(

let texture = assets.load("character.png");

let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(96, 96),
8,
8,
None,
None,
));
let layout = atlas_layouts.add(spritesheet.atlas_layout(96, 96));

commands.spawn((
SpriteBundle {
Expand Down
18 changes: 6 additions & 12 deletions examples/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ fn setup(
) {
commands.spawn(Camera2dBundle::default());

// Load assets for the sprite

let texture = assets.load("ball.png");

let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(100, 20),
1,
30,
None,
None,
));

// Create a clip

let spritesheet = Spritesheet::new(1, 30);
Expand All @@ -46,6 +34,12 @@ fn setup(

let clip_id = library.register_clip(clip);

// Load assets for the sprites

let texture = assets.load("ball.png");

let layout = atlas_layouts.add(spritesheet.atlas_layout(96, 96));

// Create an animated sprite for each parameter set

let mut parameters = vec![
Expand Down
8 changes: 1 addition & 7 deletions examples/stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@ fn setup(

let texture = assets.load("character.png");

let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
UVec2::new(96, 96),
8,
8,
None,
None,
));
let layout = atlas_layouts.add(spritesheet.atlas_layout(96, 96));

for _ in 0..100_000 {
let animation = animation_ids.choose(&mut rng).unwrap();
Expand Down
8 changes: 1 addition & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@
//!
//! let texture = assets.load("character.png");
//!
//! let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
//! UVec2::new(96, 96),
//! 8,
//! 8,
//! None,
//! None,
//! ));
//! let layout = atlas_layouts.add(spritesheet.atlas_layout(96, 96));
//!
//! commands.spawn((
//! SpriteBundle {
Expand Down
51 changes: 50 additions & 1 deletion src/spritesheet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::log::warn;
use bevy::{log::warn, math::UVec2, sprite::TextureAtlasLayout};
use std::ops::RangeBounds;

use crate::CRATE_NAME;
Expand Down Expand Up @@ -448,4 +448,53 @@ impl Spritesheet {

frames
}

/// Creates a [TextureAtlasLayout] from the spritesheet.
///
/// # Arguments
///
/// * `frame_width` - the width of a single frame
/// * `frame_height` - the height of a single frame
///
/// # Example
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_spritesheet_animation::prelude::*;
/// fn setup(
/// mut commands: Commands,
/// mut library: ResMut<AnimationLibrary>,
/// mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
/// assets: Res<AssetServer>,
/// # animation_id: AnimationId
/// ) {
/// let spritesheet = Spritesheet::new(8, 8);
///
/// // ... omitted: create an animation ...
///
/// let layout = atlas_layouts.add(spritesheet.atlas_layout(100, 200));
///
/// commands.spawn((
/// SpriteBundle {
/// texture: assets.load("character.png"),
/// ..default()
/// },
/// TextureAtlas {
/// layout,
/// ..default()
/// },
/// // Add a SpritesheetAnimation component that references our animation
/// SpritesheetAnimation::from_id(animation_id),
/// ));
/// }
/// ```
pub fn atlas_layout(&self, frame_width: u32, frame_height: u32) -> TextureAtlasLayout {
TextureAtlasLayout::from_grid(
UVec2::new(frame_width, frame_height),
self.columns as u32,
self.rows as u32,
None,
None,
)
}
}
25 changes: 25 additions & 0 deletions tests/spritesheet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::prelude::*;
use bevy_spritesheet_animation::prelude::*;

#[test]
Expand Down Expand Up @@ -127,3 +128,27 @@ fn vertical_strip() {
assert_eq!(sheet.vertical_strip(1, 1, 6), vec![5, 9, 2, 6, 10, 3]);
assert_eq!(sheet.vertical_strip(3, 0, 1000), vec![3, 7, 11]);
}

#[test]
fn atlas_layout() {
let sheet1 = Spritesheet::new(2, 3);
let layout1 = sheet1.atlas_layout(100, 200);

assert_eq!(layout1.size, UVec2::new(200, 600));
assert_eq!(layout1.textures.len(), 6);

assert_eq!(layout1.textures.get(0), Some(&URect::new(0, 0, 100, 200)));
assert_eq!(layout1.textures.get(1), Some(&URect::new(100, 0, 200, 200)));

assert_eq!(layout1.textures.get(2), Some(&URect::new(0, 200, 100, 400)));
assert_eq!(
layout1.textures.get(3),
Some(&URect::new(100, 200, 200, 400))
);

assert_eq!(layout1.textures.get(4), Some(&URect::new(0, 400, 100, 600)));
assert_eq!(
layout1.textures.get(5),
Some(&URect::new(100, 400, 200, 600))
);
}

0 comments on commit 1b6bfe0

Please sign in to comment.