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

fix: Bug with anchor parameter in Sprite.render() #1508

Merged
merged 7 commits into from
Apr 7, 2022
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/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(
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
'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'),
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
);
},
);
});
}

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);
}
}