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

feat: Use cached image when creating single source TiledAtlas if available #2348

Merged
merged 8 commits into from
Feb 22, 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
10 changes: 6 additions & 4 deletions packages/flame_tiled/lib/src/tile_atlas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class TiledAtlas {

@visibleForTesting
static String atlasKey(Iterable<TiledImage> images) {
if (images.length == 1) {
return images.first.source!;
}

final files = ([...images.map((e) => e.source)]..sort()).join(',');
return 'atlas{$files}';
}
Expand Down Expand Up @@ -94,13 +98,11 @@ class TiledAtlas {
if (imageList.length == 1) {
// The map contains one image, so its either an atlas already, or a
// really boring map.
final tiledImage = imageList.first;
final image =
(await Flame.images.load(tiledImage.source!, key: key)).clone();
spydon marked this conversation as resolved.
Show resolved Hide resolved
final image = (await Flame.images.load(key)).clone();

return atlasMap[key] ??= TiledAtlas._(
atlas: image,
offsets: {tiledImage.source!: Offset.zero},
offsets: {key: Offset.zero},
key: key,
);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/flame_tiled/test/tile_atlas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ void main() {
expect(atlas.atlas, isNotNull);
expect(atlas.atlas!.width, 128);
expect(atlas.atlas!.height, 74);
expect(atlas.key, 'images/green.png');

expect(Flame.images.containsKey('images/green.png'), isTrue);

expect(
await imageToPng(atlas.atlas!),
Expand Down