Skip to content

Commit

Permalink
fix: Bug with anchor parameter in Sprite.render() (#1508)
Browse files Browse the repository at this point in the history
  • Loading branch information
st-pasha authored Apr 7, 2022
1 parent 5b2471c commit 325df46
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/flame/lib/src/sprite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Sprite {
final drawSize = size ?? srcSize;

final delta = anchor.toVector2()..multiply(drawSize);
final drawRect = (drawPosition + delta).toPositionedRect(drawSize);
final drawRect = (drawPosition - delta).toPositionedRect(drawSize);

final drawPaint = overridePaint ?? paint;

Expand Down
Binary file added packages/flame/test/_goldens/sprite_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/_resources/flame.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions packages/flame/test/_resources/load_image.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'dart:io';
import 'dart:ui';

Future<Image> loadImage(String fileName) async {
final bytes = await File('test/_resources/$fileName').readAsBytes();
final codec = await instantiateImageCodec(bytes);
final frameInfo = await codec.getNextFrame();
return frameInfo.image;
}
50 changes: 50 additions & 0 deletions packages/flame/test/sprite_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import '_resources/load_image.dart';

void main() {
group('Sprite', () {
FlameTester(() => FlameGame()).testGameWidget(
'Render with anchor',
setUp: (game, tester) async {
game.add(MyComponent()..position = Vector2.all(25));
await game.ready();
},
verify: (game, tester) async {
await expectLater(
find.byGame<FlameGame>(),
matchesGoldenFile('_goldens/sprite_test_1.png'),
);
},
);
});
}

class MyComponent extends PositionComponent {
MyComponent() : super(size: Vector2(200, 400));
late final Sprite sprite;

@override
Future<void> onLoad() async {
sprite = Sprite(await loadImage('flame.png'));
}

@override
void render(Canvas canvas) {
canvas.drawRect(
size.toRect(),
Paint()
..color = const Color(0xffffffff)
..style = PaintingStyle.stroke
..strokeWidth = 2,
);
// Expected: sprite is rendered in the center of the rect
sprite.render(canvas, position: size / 2, anchor: Anchor.center);
}
}

0 comments on commit 325df46

Please sign in to comment.