Skip to content

Commit

Permalink
Merge TextureAtlas::from_grid_with_padding into TextureAtlas::from_gr…
Browse files Browse the repository at this point in the history
…id through option arguments (bevyengine#6057)

This is an adoption of bevyengine#3775
This merges `TextureAtlas` `from_grid_with_padding` into `from_grid` , adding optional padding and optional offset.
Since the orignal PR, the offset had already been added to from_grid_with_padding through bevyengine#4836 

## Changelog

- Added `padding` and `offset` arguments to  `TextureAtlas::from_grid`
- Removed `TextureAtlas::from_grid_with_padding`

## Migration Guide

`TextureAtlas::from_grid_with_padding` was merged into `from_grid` which takes two additional parameters for padding and an offset.
```
// 0.8
TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1);
// 0.9
TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None)

// 0.8
TextureAtlas::from_grid_with_padding(texture_handle, Vec2::new(24.0, 24.0), 7, 1, Vec2::new(4.0, 4.0));
// 0.9
TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, Some(Vec2::new(4.0, 4.0)), None)
```

Co-authored-by: olefish <88390729+oledfish@users.noreply.github.com>
  • Loading branch information
2 people authored and james7132 committed Oct 28, 2022
1 parent a3c4121 commit a97d307
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
20 changes: 5 additions & 15 deletions crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,21 @@ impl TextureAtlas {
}
}

/// Generate a `TextureAtlas` by splitting a texture into a grid where each
/// `tile_size` by `tile_size` grid-cell is one of the textures in the
/// atlas. Resulting `TextureAtlas` is indexed left to right, top to bottom.
pub fn from_grid(
texture: Handle<Image>,
tile_size: Vec2,
columns: usize,
rows: usize,
) -> TextureAtlas {
Self::from_grid_with_padding(texture, tile_size, columns, rows, Vec2::ZERO, Vec2::ZERO)
}

/// Generate a `TextureAtlas` by splitting a texture into a grid where each
/// `tile_size` by `tile_size` grid-cell is one of the textures in the
/// atlas. Grid cells are separated by some `padding`, and the grid starts
/// at `offset` pixels from the top left corner. Resulting `TextureAtlas` is
/// indexed left to right, top to bottom.
pub fn from_grid_with_padding(
pub fn from_grid(
texture: Handle<Image>,
tile_size: Vec2,
columns: usize,
rows: usize,
padding: Vec2,
offset: Vec2,
padding: Option<Vec2>,
offset: Option<Vec2>,
) -> TextureAtlas {
let padding = padding.unwrap_or_default();
let offset = offset.unwrap_or_default();
let mut sprites = Vec::new();
let mut current_padding = Vec2::ZERO;

Expand Down
3 changes: 2 additions & 1 deletion examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ fn setup(
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1);
let texture_atlas =
TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
commands.spawn(Camera2dBundle::default());
commands.spawn((
Expand Down
3 changes: 2 additions & 1 deletion examples/stress_tests/many_animated_sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ fn setup(
let half_y = (map_size.y / 2.0) as i32;

let texture_handle = assets.load("textures/rpg/chars/gabe/gabe-idle-run.png");
let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1);
let texture_atlas =
TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None);
let texture_atlas_handle = texture_atlases.add(texture_atlas);

// Spawns the camera
Expand Down

0 comments on commit a97d307

Please sign in to comment.