Skip to content

Commit

Permalink
Merge branch 'main' into luan.benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
luanpotter authored Jan 6, 2025
2 parents 95c56bb + 09fc0f2 commit 1af44e0
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/flame_texturepacker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ dev_dependencies:
flame_lint: ^1.2.1
flutter_test:
sdk: flutter
mocktail: ^1.0.4
69 changes: 69 additions & 0 deletions packages/flame_texturepacker/test/flame_texturepacker_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'dart:io';

import 'package:flame/cache.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flame_texturepacker/flame_texturepacker.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class _MockAssetBundle extends Mock implements AssetBundle {}

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('TexturepackerLoader', () {
const atlasPath =
'test/assets/newFormat/multiplePages/MultiplePageAtlasMap.atlas';
const atlasImage1 =
'test/assets/newFormat/multiplePages/MultiplePageAtlasMap.png';

test('load atlas from storage', () async {
final flameGame = FlameGame();
final atlas = await flameGame.atlasFromStorage(atlasPath);

expect(atlas, isNotNull);
expect(atlas.sprites.length, equals(12));

final firstSprite = atlas.findSpriteByName('robot_walk');
expect(firstSprite, isNotNull);
expect(firstSprite!.srcSize, isNotNull);
expect(firstSprite.srcPosition, isNotNull);
});

test('load atlas from assets', () async {
final bundle = _MockAssetBundle();
when(() => bundle.loadString(any()))
.thenAnswer((_) async => File(atlasPath).readAsString());
when(() => bundle.load(any())).thenAnswer(
(_) async => ByteData.sublistView(
File(atlasImage1).readAsBytesSync(),
),
);

final flameGame = FlameGame();
Flame.assets = AssetsCache(bundle: bundle, prefix: '');
Flame.images = Images(bundle: bundle, prefix: '');

final atlas = await flameGame.atlasFromAssets(atlasPath);

expect(atlas, isNotNull);
expect(atlas.sprites.length, equals(12));

final firstSprite = atlas.findSpriteByName('robot_walk');
expect(firstSprite, isNotNull);
expect(firstSprite!.srcSize.x, greaterThan(0));
expect(firstSprite.srcSize.y, greaterThan(0));
expect(firstSprite.srcPosition, isNotNull);
});

test('throws exception for invalid atlas path', () async {
final flameGame = FlameGame();
expect(
() => flameGame.atlasFromStorage('invalid_path.atlas'),
throwsException,
);
});
});
}
107 changes: 106 additions & 1 deletion packages/flame_texturepacker/test/legacy_format_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import 'dart:math' as math;
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame_texturepacker/flame_texturepacker.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class _MockCanvas extends Mock implements Canvas {}

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -59,6 +64,7 @@ void main() {
expect(jumpSprite.srcSize, Vector2(192, 256));
expect(jumpSprite.originalSize, Vector2(192, 256));
expect(jumpSprite.offset, Vector2(0, 0));
expect(jumpSprite.angle, math.pi / 2);

final walkSprite = atlas.findSpriteByName('robot_walk')!;
expect(walkSprite.rotate, false);
Expand All @@ -75,6 +81,7 @@ void main() {
expect(walkSprite.srcSize, Vector2(192, 256));
expect(walkSprite.originalSize, Vector2(192, 256));
expect(walkSprite.offset, Vector2(0, 0));
expect(walkSprite.angle, 0);
});

test('Trimmed sprite data is loaded correctly', () async {
Expand All @@ -98,12 +105,14 @@ void main() {
expect(idleSprite.srcSize, Vector2(192, 256));
expect(idleSprite.originalSize, Vector2(192, 256));
expect(idleSprite.offset, Vector2(31, 0));
expect(idleSprite.angle, 0);

final idleSpritePackedSize = idleSprite.clone(useOriginalSize: false);
expect(idleSpritePackedSize.src, const Rect.fromLTWH(0, 310, 130, 182));
expect(idleSpritePackedSize.srcSize, Vector2(130, 182));
expect(idleSpritePackedSize.originalSize, Vector2(130, 182));
expect(idleSpritePackedSize.offset, Vector2(0, 0));
expect(idleSpritePackedSize.angle, 0);

final walkSprite = atlas.findSpriteByName('robot_walk')!;
expect(walkSprite.rotate, true);
Expand All @@ -120,12 +129,14 @@ void main() {
expect(walkSprite.srcSize, Vector2(192, 256));
expect(walkSprite.originalSize, Vector2(192, 256));
expect(walkSprite.offset, Vector2(14, 2));
expect(walkSprite.angle, math.pi / 2);

final walkSpritePackedSize = walkSprite.clone(useOriginalSize: false);
expect(walkSpritePackedSize.src, const Rect.fromLTWH(0, 0, 183, 150));
expect(walkSpritePackedSize.srcSize, Vector2(150, 183));
expect(walkSpritePackedSize.originalSize, Vector2(150, 183));
expect(walkSpritePackedSize.offset, Vector2(0, 0));
expect(walkSpritePackedSize.angle, math.pi / 2);
});
});
group('Single page atlas', () {
Expand Down Expand Up @@ -159,6 +170,15 @@ void main() {
expect(jumpSprite, isNotNull);
});

test('findSpriteByNameIndex will return 1 sprite', () async {
final atlas = await TexturePackerAtlas.load(
atlasPath,
fromStorage: true,
);
final jumpSprite = atlas.findSpriteByNameIndex('robot_jump', -1);
expect(jumpSprite, isNotNull);
});

test('Sprite data is loaded correctly', () async {
final atlas = await TexturePackerAtlas.load(
atlasPath,
Expand All @@ -180,6 +200,7 @@ void main() {
expect(jumpSprite.srcSize, Vector2(192, 256));
expect(jumpSprite.originalSize, Vector2(192, 256));
expect(jumpSprite.offset, Vector2(0, 0));
expect(jumpSprite.angle, 0);

final walkSprite = atlas.findSpriteByName('robot_walk')!;
expect(walkSprite.rotate, false);
Expand All @@ -196,6 +217,7 @@ void main() {
expect(walkSprite.srcSize, Vector2(192, 256));
expect(walkSprite.originalSize, Vector2(192, 256));
expect(walkSprite.offset, Vector2(0, 0));
expect(walkSprite.angle, 0);
});

test('Trimmed sprite data is loaded correctly', () async {
Expand All @@ -219,12 +241,14 @@ void main() {
expect(idleSprite.srcSize, Vector2(192, 256));
expect(idleSprite.originalSize, Vector2(192, 256));
expect(idleSprite.offset, Vector2(31, 0));
expect(idleSprite.angle, 0);

final idleSpritePackedSize = idleSprite.clone(useOriginalSize: false);
expect(idleSpritePackedSize.src, const Rect.fromLTWH(0, 160, 130, 182));
expect(idleSpritePackedSize.srcSize, Vector2(130, 182));
expect(idleSpritePackedSize.originalSize, Vector2(130, 182));
expect(idleSpritePackedSize.offset, Vector2(0, 0));
expect(idleSpritePackedSize.angle, 0);

final walkSprite = atlas.findSpriteByName('robot_walk')!;
expect(walkSprite.rotate, true);
Expand All @@ -241,12 +265,93 @@ void main() {
expect(walkSprite.srcSize, Vector2(192, 256));
expect(walkSprite.originalSize, Vector2(192, 256));
expect(walkSprite.offset, Vector2(14, 2));
expect(walkSprite.angle, math.pi / 2);

final walkSpritePackedSize = walkSprite.clone(useOriginalSize: false);
expect(walkSpritePackedSize.src, const Rect.fromLTWH(191, 367, 183, 150));
expect(walkSpritePackedSize.srcSize, Vector2(150, 183));
expect(walkSpritePackedSize.originalSize, Vector2(150, 183));
expect(walkSpritePackedSize.offset, Vector2(0, 0));
expect(walkSpritePackedSize.angle, math.pi / 2);
});

test('Sprite renders correctly when not rotated', () async {
final atlas = await TexturePackerAtlas.load(
atlasPath,
fromStorage: true,
);

final sprite = atlas.findSpriteByName('robot_jump')!;
final canvas = _MockCanvas();

sprite.render(
canvas,
position: Vector2(100, 100),
size: Vector2(384, 512),
);

expect(sprite.rotate, false);
expect(sprite.offset, Vector2.zero());
expect(sprite.originalSize, Vector2(192, 256));
expect(sprite.srcSize, Vector2(192, 256));
});

test('Sprite renders correctly when rotated', () async {
final atlas = await TexturePackerAtlas.load(
atlasTrimmedPath,
fromStorage: true,
);

final sprite = atlas.findSpriteByName('robot_walk')!;
final canvas = _MockCanvas();

sprite.render(
canvas,
position: Vector2(100, 100),
size: Vector2(384, 512),
);

expect(sprite.rotate, true);
expect(sprite.offset, Vector2(14, 2));
expect(sprite.originalSize, Vector2(192, 256));
expect(sprite.srcSize, Vector2(192, 256));
expect(sprite.angle, math.pi / 2);
});

test('Sprite renders with correct anchor point', () async {
final atlas = await TexturePackerAtlas.load(
atlasPath,
fromStorage: true,
);

final sprite = atlas.findSpriteByName('robot_jump')!;
final canvas = _MockCanvas();

sprite.render(
canvas,
position: Vector2(100, 100),
size: Vector2(192, 256),
anchor: Anchor.center,
);

expect(sprite.originalSize, Vector2(192, 256));
});

test('Sprite renders correctly with default position', () async {
final atlas = await TexturePackerAtlas.load(
atlasPath,
fromStorage: true,
);

final sprite = atlas.findSpriteByName('robot_jump')!;
final canvas = _MockCanvas();

sprite.render(
canvas,
size: Vector2(192, 256),
);

expect(sprite.originalSize, Vector2(192, 256));
});
});
}
Loading

0 comments on commit 1af44e0

Please sign in to comment.