Skip to content

Commit

Permalink
More resilient version
Browse files Browse the repository at this point in the history
  • Loading branch information
luanpotter committed Oct 13, 2024
1 parent 6892ce7 commit e9006b9
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 10 deletions.
5 changes: 5 additions & 0 deletions examples/lib/stories/sprites/sprite_batch_load_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class SpriteBatchLoadExample extends FlameGame {

class MySpriteBatchComponent extends SpriteBatchComponent
with HasGameReference<SpriteBatchLoadExample> {
MySpriteBatchComponent()
: super(
blendMode: BlendMode.srcOver,
);

@override
Future<void> onLoad() async {
final spriteBatch = await game.loadSpriteBatch('boom.png');
Expand Down
12 changes: 9 additions & 3 deletions packages/flame/lib/src/sprite_batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,19 @@ class SpriteBatch {

final renderPaint = paint ?? _emptyPaint;

final hasNoColors = _colors.every((c) => c == _defaultColor);
final actualBlendMode = blendMode ?? defaultBlendMode;
if (!hasNoColors && actualBlendMode == null) {
throw 'When setting any colors, a blend mode must be provided.';
}

if (useAtlas && !_flippedAtlasStatus.isGenerating) {
canvas.drawAtlas(
atlas,
_transforms,
_sources,
_colors.every((c) => c == _defaultColor) ? null : _colors,
blendMode ?? defaultBlendMode,
hasNoColors ? null : _colors,
actualBlendMode,
cullRect,
renderPaint,
);
Expand All @@ -438,5 +444,5 @@ class SpriteBatch {
}
}

static const _defaultColor = Color(0xFF000000);
static const _defaultColor = Color(0x00000000);
}
Binary file modified packages/flame/test/_goldens/sprite_batch_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/flame/test/_goldens/sprite_batch_test_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/flame/test/_goldens/sprite_batch_test_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 40 additions & 6 deletions packages/flame/test/sprite_batch_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,26 @@ void main() {
);
});

const margin = 2.0;
const tileSize = 6.0;

testGolden(
'can render a batch',
'can render a batch with blend mode',
(game) async {
final spriteSheet = await loadImage('boom.png');
final spriteSheet = await loadImage('alphabet.png');
final spriteBatch = SpriteBatch(spriteSheet);

const source = Rect.fromLTWH(128 * 4.0, 128 * 4.0, 64, 128);
const source = Rect.fromLTWH(3 * tileSize, 0, tileSize, tileSize);

spriteBatch.add(
source: source,
color: Colors.redAccent,
offset: Vector2.all(margin),
);

spriteBatch.add(
source: source,
offset: Vector2(100, 0),
offset: Vector2(2 * margin + tileSize, margin),
);

game.add(
Expand All @@ -86,8 +90,38 @@ void main() {
),
);
},
size: Vector2(300, 200),
goldenFile: '_goldens/sprite_batch_test.png',
size: Vector2(3 * margin + 2 * tileSize, 2 * margin + tileSize),
backgroundColor: const Color(0xFFFFFFFF),
goldenFile: '_goldens/sprite_batch_test_1.png',
);

testGolden(
'can render a batch without blend mode',
(game) async {
final spriteSheet = await loadImage('alphabet.png');
final spriteBatch = SpriteBatch(spriteSheet);

const source = Rect.fromLTWH(3 * tileSize, 0, tileSize, tileSize);

spriteBatch.add(
source: source,
offset: Vector2.all(margin),
);

spriteBatch.add(
source: source,
offset: Vector2(2 * margin + tileSize, margin),
);

game.add(
SpriteBatchComponent(
spriteBatch: spriteBatch,
),
);
},
size: Vector2(3 * margin + 2 * tileSize, 2 * margin + tileSize),
backgroundColor: const Color(0xFFFFFFFF),
goldenFile: '_goldens/sprite_batch_test_2.png',
);
});
}
15 changes: 14 additions & 1 deletion packages/flame_test/lib/src/test_golden.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ void testGolden(
PrepareGameFunction testBody, {
required String goldenFile,
Vector2? size,
Color? backgroundColor,
FlameGame? game,
bool skip = false,
}) {
testWidgets(
testName,
(tester) async {
final gameInstance = game ?? FlameGame();
final gameInstance = game ??
(backgroundColor != null
? GameWithBackgroundColor(backgroundColor)
: FlameGame());
const myKey = ValueKey('game-instance');

await tester.runAsync(() async {
Expand Down Expand Up @@ -73,3 +77,12 @@ void testGolden(
}

typedef PrepareGameFunction = Future<void> Function(FlameGame game);

class GameWithBackgroundColor extends FlameGame {
final Color _backgroundColor;

GameWithBackgroundColor(this._backgroundColor);

@override
Color backgroundColor() => _backgroundColor;
}

0 comments on commit e9006b9

Please sign in to comment.