Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Make atlas status to be more readable #2502

Merged
merged 6 commits into from
Apr 15, 2023
Merged
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
2 changes: 1 addition & 1 deletion packages/flame/lib/sprite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
39 changes: 27 additions & 12 deletions packages/flame/lib/src/sprite_batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flame/cache.dart';
import 'package:flame/extensions.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:meta/meta.dart';

extension SpriteBatchExtension on Game {
/// Utility method to load and cache the image for a [SpriteBatch] based on
Expand Down Expand Up @@ -76,6 +77,23 @@ class BatchItem {
final Paint paint;
}

@internal
enum FlippedAtlasStatus {
/// There is no need to generate a flipped atlas yet.
none,

/// The flipped atlas generation is currently 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
Expand Down Expand Up @@ -127,6 +145,8 @@ class SpriteBatch {
);
}

FlippedAtlasStatus _flippedAtlasStatus = FlippedAtlasStatus.none;

/// List of all the existing batch items.
final _batchItems = <BatchItem>[];

Expand Down Expand Up @@ -180,12 +200,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;

Expand All @@ -212,14 +226,13 @@ class SpriteBatch {
bool get isEmpty => _batchItems.isEmpty;

Future<void> _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<Image> _generateFlippedAtlas(Image image) {
Expand Down Expand Up @@ -261,15 +274,17 @@ class SpriteBatch {
color: color ?? defaultColor,
);

if (flip && useAtlas && !_hasFlips) {
if (flip && useAtlas && _flippedAtlasStatus.isNone) {
_makeFlippedAtlas();
}

_batchItems.add(batchItem);
_sources.add(
flip
? Rect.fromLTWH(
(atlas.width * (!_atlasReady ? 2 : 1)) - source.right,
// The atlas is twice as wide when the flipped atlas is generated.
(atlas.width * (_flippedAtlasStatus.isGenerated ? 1 : 2)) -
source.right,
source.top,
source.width,
source.height,
Expand Down Expand Up @@ -360,7 +375,7 @@ class SpriteBatch {

paint ??= _emptyPaint;

if (useAtlas && _atlasReady) {
if (useAtlas && !_flippedAtlasStatus.isGenerating) {
canvas.drawAtlas(
atlas,
_transforms,
Expand Down