From 68b77dfa3abfe375baf5b43823d49db5816dc523 Mon Sep 17 00:00:00 2001 From: Hwan-seok Date: Sun, 16 Apr 2023 02:43:39 +0900 Subject: [PATCH 1/4] refactor SpriteBatch flipped status --- packages/flame/lib/src/sprite_batch.dart | 38 ++++++++++++++++-------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/flame/lib/src/sprite_batch.dart b/packages/flame/lib/src/sprite_batch.dart index 8a633fed58e..d2406afebe6 100644 --- a/packages/flame/lib/src/sprite_batch.dart +++ b/packages/flame/lib/src/sprite_batch.dart @@ -7,6 +7,7 @@ import 'package:flame/src/cache/images.dart'; import 'package:flame/src/extensions/image.dart'; import 'package:flame/src/extensions/picture_extension.dart'; import 'package:flame/src/flame.dart'; +import 'package:meta/meta.dart'; extension SpriteBatchExtension on Game { /// Utility method to load and cache the image for a [SpriteBatch] based on @@ -77,6 +78,23 @@ class BatchItem { final Paint paint; } +@internal +enum FlippedAtlasStatus { + /// There is no need to generate flipped atlas yet. + none, + + /// The flipped atlas image is currently generate in progress. + generating, + + /// The flipped atlas image has been generated. + generated, + ; + + bool get isNone => this == FlippedAtlasStatus.none; + bool get isGenerating => this == FlippedAtlasStatus.generating; + bool get isGenerated => this == FlippedAtlasStatus.generated; +} + /// The SpriteBatch API allows for rendering multiple items at once. /// /// This class allows for optimization when you want to draw many parts of an @@ -128,6 +146,8 @@ class SpriteBatch { ); } + FlippedAtlasStatus _flippedAtlasStatus = FlippedAtlasStatus.none; + /// List of all the existing batch items. final _batchItems = []; @@ -181,12 +201,6 @@ class SpriteBatch { imageCache.findKeyForImage(atlas) ?? 'image[${identityHashCode(atlas)}]'; - /// Whether any [BatchItem]s needs a flippable atlas. - bool _hasFlips = false; - - /// The status of the atlas image loading operations. - bool _atlasReady = true; - /// The default color, used as a background color for a [BatchItem]. final Color defaultColor; @@ -213,14 +227,13 @@ class SpriteBatch { bool get isEmpty => _batchItems.isEmpty; Future _makeFlippedAtlas() async { - _hasFlips = true; - _atlasReady = false; + _flippedAtlasStatus = FlippedAtlasStatus.generating; final key = '$imageKey#with-flips'; atlas = await imageCache.fetchOrGenerate( key, () => _generateFlippedAtlas(atlas), ); - _atlasReady = true; + _flippedAtlasStatus = FlippedAtlasStatus.generated; } Future _generateFlippedAtlas(Image image) { @@ -262,7 +275,7 @@ class SpriteBatch { color: color ?? defaultColor, ); - if (flip && useAtlas && !_hasFlips) { + if (flip && useAtlas && _flippedAtlasStatus.isNone) { _makeFlippedAtlas(); } @@ -270,7 +283,8 @@ class SpriteBatch { _sources.add( flip ? Rect.fromLTWH( - (atlas.width * (!_atlasReady ? 2 : 1)) - source.right, + (atlas.width * (!_flippedAtlasStatus.isGenerated ? 2 : 1)) - + source.right, source.top, source.width, source.height, @@ -361,7 +375,7 @@ class SpriteBatch { paint ??= _emptyPaint; - if (useAtlas && _atlasReady) { + if (useAtlas && !_flippedAtlasStatus.isGenerating) { canvas.drawAtlas( atlas, _transforms, From a73ee3be66aec7aaa7345e2715122c9ea752d3ce Mon Sep 17 00:00:00 2001 From: Hwan-seok Date: Sun, 16 Apr 2023 03:01:25 +0900 Subject: [PATCH 2/4] Remove unnecessary boolean inversion --- packages/flame/lib/src/sprite_batch.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/flame/lib/src/sprite_batch.dart b/packages/flame/lib/src/sprite_batch.dart index d2406afebe6..07f8298097d 100644 --- a/packages/flame/lib/src/sprite_batch.dart +++ b/packages/flame/lib/src/sprite_batch.dart @@ -283,7 +283,8 @@ class SpriteBatch { _sources.add( flip ? Rect.fromLTWH( - (atlas.width * (!_flippedAtlasStatus.isGenerated ? 2 : 1)) - + // The atlas is twice as wide when the flipped atlas is generated. + (atlas.width * (_flippedAtlasStatus.isGenerated ? 1 : 2)) - source.right, source.top, source.width, From e8c858f35f23318496cb13279196adc1efdc78f8 Mon Sep 17 00:00:00 2001 From: Hwan-seok Date: Sun, 16 Apr 2023 03:01:38 +0900 Subject: [PATCH 3/4] Hide internal member --- packages/flame/lib/sprite.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/lib/sprite.dart b/packages/flame/lib/sprite.dart index 78eebff6db0..2b6b5861c2c 100644 --- a/packages/flame/lib/sprite.dart +++ b/packages/flame/lib/sprite.dart @@ -5,5 +5,5 @@ export 'src/sprite.dart'; export 'src/sprite_animation.dart'; -export 'src/sprite_batch.dart'; +export 'src/sprite_batch.dart' hide FlippedAtlasStatus; export 'src/spritesheet.dart'; From 9443bd48ede6117f7b6601d86f1b1454af155fdd Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sun, 16 Apr 2023 00:10:58 +0200 Subject: [PATCH 4/4] Apply suggestions from code review --- packages/flame/lib/src/sprite_batch.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/flame/lib/src/sprite_batch.dart b/packages/flame/lib/src/sprite_batch.dart index 07f8298097d..2e7d9ee2e75 100644 --- a/packages/flame/lib/src/sprite_batch.dart +++ b/packages/flame/lib/src/sprite_batch.dart @@ -80,10 +80,10 @@ class BatchItem { @internal enum FlippedAtlasStatus { - /// There is no need to generate flipped atlas yet. + /// There is no need to generate a flipped atlas yet. none, - /// The flipped atlas image is currently generate in progress. + /// The flipped atlas generation is currently in progress. generating, /// The flipped atlas image has been generated.