Skip to content

Commit

Permalink
fix: Remove forced location of the atlas file. (#3481)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Klingsbo <[email protected]>
  • Loading branch information
exoad and spydon authored Feb 10, 2025
1 parent a0ebaef commit bac68dc
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions packages/flame_texturepacker/lib/src/texture_packer_atlas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,26 @@ class TexturePackerAtlas {
/// If [useOriginalSize] is true, the sprites loaded will be use original size
/// instead of the packed size. For animation sprites, load with origin size
/// is recommended for smooth result.
///
/// If [assetsPrefix] is not specified, the atlas file [path] will be resolved
/// relative to the `assets/images/` directory.
static Future<TexturePackerAtlas> load(
String path, {
bool fromStorage = false,
Images? images,
bool useOriginalSize = true,
String assetsPrefix = 'images/',
}) async {
final _TextureAtlasData atlasData;

if (fromStorage) {
atlasData = await _fromStorage(path, images: images);
} else {
atlasData = await _fromAssets(path, images: images);
atlasData = await _fromAssets(
path,
images: images,
assetsPrefix: assetsPrefix,
);
}

return TexturePackerAtlas(
Expand Down Expand Up @@ -73,22 +81,40 @@ class TexturePackerAtlas {

/// Loads images from the assets folder.
/// Uses the [path] to find the image directory.
Future<_TextureAtlasData> _fromAssets(String path, {Images? images}) async {
Future<_TextureAtlasData> _fromAssets(
String path, {
required String assetsPrefix,
Images? images,
}) async {
try {
return await _parse(path, fromStorage: false, images: images);
return await _parse(
path,
fromStorage: false,
images: images,
assetsPrefix: assetsPrefix,
);
} on Exception catch (e, stack) {
Error.throwWithStackTrace(
Exception('Error loading $path from assets: $e'),
Exception(
'Error loading $assetsPrefix$path from assets: $e',
),
stack,
);
}
}

/// Loads images from the device's storage.
/// Uses the [path] to find the image directory.
Future<_TextureAtlasData> _fromStorage(String path, {Images? images}) async {
Future<_TextureAtlasData> _fromStorage(
String path, {
Images? images,
}) async {
try {
return await _parse(path, fromStorage: true, images: images);
return await _parse(
path,
fromStorage: true,
images: images,
);
} on Exception catch (e, stack) {
Error.throwWithStackTrace(
Exception('Error loading $path from storage: $e'),
Expand All @@ -106,6 +132,7 @@ Future<_TextureAtlasData> _parse(
String path, {
required bool fromStorage,
Images? images,
String? assetsPrefix,
}) async {
final pages = <Page>[];
final regions = <Region>[];
Expand All @@ -114,7 +141,11 @@ Future<_TextureAtlasData> _parse(
if (fromStorage) {
fileAsString = await File(path).readAsString();
} else {
fileAsString = await Flame.assets.readFile('images/$path');
assert(
assetsPrefix != null,
'When reading from storage, the assetsPrefix needs to be provided.',
);
fileAsString = await Flame.assets.readFile('$assetsPrefix!/$path');
}

final iterator = LineSplitter.split(fileAsString).iterator;
Expand Down

0 comments on commit bac68dc

Please sign in to comment.