From 899f4e66f776a2657c11153a477e0840a8aa3112 Mon Sep 17 00:00:00 2001 From: DevKage <33748002+ufrshubham@users.noreply.github.com> Date: Sat, 16 Dec 2023 13:31:04 +0530 Subject: [PATCH 1/3] Add margin and spacing to spritesheet --- packages/flame/lib/src/sprite_sheet.dart | 32 ++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/flame/lib/src/sprite_sheet.dart b/packages/flame/lib/src/sprite_sheet.dart index c1f2d1200ee..4990487f92d 100644 --- a/packages/flame/lib/src/sprite_sheet.dart +++ b/packages/flame/lib/src/sprite_sheet.dart @@ -27,30 +27,46 @@ class SpriteSheet { /// size. If it's an animation sheet, this would be the frame size. final Vector2 srcSize; + /// The empty space around the edges of the image. + final double margin; + + /// This empty space in between adjacent tiles within the image. + final double spacing; + + final int _rows; + final int _columns; final Map _spriteCache = {}; /// Creates a sprite sheet given the image and the tile size. SpriteSheet({ required this.image, required this.srcSize, - }); + this.margin = 0, + this.spacing = 0, + }) : _columns = + (image.width - 2 * margin + spacing) ~/ (srcSize.x + spacing), + _rows = (image.height - 2 * margin + spacing) ~/ (srcSize.y + spacing); SpriteSheet.fromColumnsAndRows({ required this.image, required int columns, required int rows, - }) : srcSize = Vector2( - image.width / columns, - image.height / rows, + this.spacing = 0, + this.margin = 0, + }) : _columns = columns, + _rows = rows, + srcSize = Vector2( + (image.width - 2 * margin - (columns - 1) * spacing) / columns, + (image.height - 2 * margin - (rows - 1) * spacing) / rows, ); /// Compute the number of columns the image has /// by using the image width and tile size. - int get columns => image.width ~/ srcSize.x; + int get columns => _columns; /// Compute the number of rows the image has /// by using the image height and tile size. - int get rows => image.height ~/ srcSize.y; + int get rows => _rows; /// Gets the sprite in the position (row, column) on the sprite sheet grid. /// @@ -101,7 +117,9 @@ class SpriteSheet { final j = spriteId ~/ columns; return Sprite( image, - srcPosition: Vector2Extension.fromInts(i, j)..multiply(srcSize), + srcPosition: Vector2Extension.fromInts(i, j) + ..multiply(srcSize) + ..translate(margin + i * spacing, margin + j * spacing), srcSize: srcSize, ); } From 1aa1ade1cdeb7d97228a08e5568b846406ebd500 Mon Sep 17 00:00:00 2001 From: DevKage <33748002+ufrshubham@users.noreply.github.com> Date: Sat, 16 Dec 2023 13:31:16 +0530 Subject: [PATCH 2/3] Add tests --- packages/flame/test/spritesheet_test.dart | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/flame/test/spritesheet_test.dart b/packages/flame/test/spritesheet_test.dart index 79ff8b3f2f6..05666c871c4 100644 --- a/packages/flame/test/spritesheet_test.dart +++ b/packages/flame/test/spritesheet_test.dart @@ -23,6 +23,31 @@ void main() { expect(spriteSheet.columns, 100); }); + test('calculates columns and rows with margin and spacing', () { + final spriteSheet = SpriteSheet( + image: image, + srcSize: Vector2(1, 2), + margin: 3, + spacing: 2, + ); + + expect(spriteSheet.rows, 24); + expect(spriteSheet.columns, 32); + }); + + test('calculates srcSize with margin and spacing', () { + final spriteSheet = SpriteSheet.fromColumnsAndRows( + image: image, + columns: 32, + rows: 24, + margin: 3, + spacing: 2, + ); + + expect(spriteSheet.srcSize.x, 1); + expect(spriteSheet.srcSize.y, 2); + }); + test('assign the correct time in sprite', () { final spriteSheet = SpriteSheet( image: image, From b14fbe5bd88a3437ecf9d773146b110f0a2b8620 Mon Sep 17 00:00:00 2001 From: DevKage Date: Sun, 17 Dec 2023 18:32:15 +0530 Subject: [PATCH 3/3] Replace row, columns getters with final members --- packages/flame/lib/src/sprite_sheet.dart | 31 ++++++++++-------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/packages/flame/lib/src/sprite_sheet.dart b/packages/flame/lib/src/sprite_sheet.dart index 4990487f92d..67dd00c9383 100644 --- a/packages/flame/lib/src/sprite_sheet.dart +++ b/packages/flame/lib/src/sprite_sheet.dart @@ -33,8 +33,14 @@ class SpriteSheet { /// This empty space in between adjacent tiles within the image. final double spacing; - final int _rows; - final int _columns; + /// The number of rows in the image based on the image height and the tile + /// size. + final int rows; + + /// The number of columns in the image based on the image width and the tile + /// size. + final int columns; + final Map _spriteCache = {}; /// Creates a sprite sheet given the image and the tile size. @@ -43,31 +49,20 @@ class SpriteSheet { required this.srcSize, this.margin = 0, this.spacing = 0, - }) : _columns = - (image.width - 2 * margin + spacing) ~/ (srcSize.x + spacing), - _rows = (image.height - 2 * margin + spacing) ~/ (srcSize.y + spacing); + }) : columns = (image.width - 2 * margin + spacing) ~/ (srcSize.x + spacing), + rows = (image.height - 2 * margin + spacing) ~/ (srcSize.y + spacing); SpriteSheet.fromColumnsAndRows({ required this.image, - required int columns, - required int rows, + required this.columns, + required this.rows, this.spacing = 0, this.margin = 0, - }) : _columns = columns, - _rows = rows, - srcSize = Vector2( + }) : srcSize = Vector2( (image.width - 2 * margin - (columns - 1) * spacing) / columns, (image.height - 2 * margin - (rows - 1) * spacing) / rows, ); - /// Compute the number of columns the image has - /// by using the image width and tile size. - int get columns => _columns; - - /// Compute the number of rows the image has - /// by using the image height and tile size. - int get rows => _rows; - /// Gets the sprite in the position (row, column) on the sprite sheet grid. /// /// This is lazily computed and cached for your convenience.