Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,22 @@ impl TextureAtlas {
}
}

/// Generate a `TextureAtlas` by splitting a texture into a grid where each
/// cell of the grid of `tile_size` is one of the textures in the atlas
/// Generate a `TextureAtlas` by splitting a texture into a grid.
///
/// Each cell in the grid of `tile_size` corresponds to one texture in the atlas.
///
/// * Cells can have horizontal and/or vertical separation specified by `padding`
/// * The grid can start at a point `offset` relative to the top-left corner of the texture
pub fn from_grid(
texture: Handle<Image>,
tile_size: Vec2,
columns: usize,
rows: usize,
padding: Option<Vec2>,
offset: Option<Vec2>,
) -> TextureAtlas {
Self::from_grid_with_padding(texture, tile_size, columns, rows, Vec2::new(0f32, 0f32))
}

/// Generate a `TextureAtlas` by splitting a texture into a grid where each
/// cell of the grid of `tile_size` is one of the textures in the atlas and is separated by
/// some `padding` in the texture
pub fn from_grid_with_padding(
texture: Handle<Image>,
tile_size: Vec2,
columns: usize,
rows: usize,
padding: Vec2,
) -> TextureAtlas {
let padding = padding.unwrap_or_default();
let offset = offset.unwrap_or_default();
let mut sprites = Vec::new();
let mut x_padding = 0.0;
let mut y_padding = 0.0;
Expand All @@ -101,8 +96,8 @@ impl TextureAtlas {
}

let rect_min = Vec2::new(
(tile_size.x + x_padding) * x as f32,
(tile_size.y + y_padding) * y as f32,
offset.x + (tile_size.x + x_padding) * x as f32,
offset.y + (tile_size.y + y_padding) * y as f32,
);

sprites.push(Rect {
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 @@ -28,7 +28,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_bundle(OrthographicCameraBundle::new_2d());
commands
Expand Down