Skip to content

Commit

Permalink
feat: Adding configurable padding to Tiled atlas packing (#2868)
Browse files Browse the repository at this point in the history
Adds new configurations to Flame Tiled's atlas packing. Spacing which
can be used to tilesets are not too close to each other in the atlas,
something that can cause texture leaking causing weird rendering issues.
  • Loading branch information
erickzanardo authored Nov 24, 2023
1 parent 212cc20 commit d0c10cb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/flame_tiled/lib/src/renderable_tile_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ class RenderableTiledMap {
bool Function(Tileset)? tsxPackingFilter,
bool useAtlas = true,
Paint Function(double opacity)? layerPaintFactory,
double atlasPackingSpacingX = 0,
double atlasPackingSpacingY = 0,
}) async {
final contents =
await (bundle ?? Flame.bundle).loadString('$prefix$fileName');
Expand All @@ -232,6 +234,8 @@ class RenderableTiledMap {
tsxPackingFilter: tsxPackingFilter,
useAtlas: useAtlas,
layerPaintFactory: layerPaintFactory ?? _defaultLayerPaintFactory,
atlasPackingSpacingX: atlasPackingSpacingX,
atlasPackingSpacingY: atlasPackingSpacingY,
);
}

Expand All @@ -253,6 +257,8 @@ class RenderableTiledMap {
bool Function(Tileset)? tsxPackingFilter,
bool useAtlas = true,
Paint Function(double opacity)? layerPaintFactory,
double atlasPackingSpacingX = 0,
double atlasPackingSpacingY = 0,
}) async {
final map = await TiledMap.fromString(
contents,
Expand All @@ -270,6 +276,8 @@ class RenderableTiledMap {
tsxPackingFilter: tsxPackingFilter,
useAtlas: useAtlas,
layerPaintFactory: layerPaintFactory ?? _defaultLayerPaintFactory,
atlasPackingSpacingX: atlasPackingSpacingX,
atlasPackingSpacingY: atlasPackingSpacingY,
);
}

Expand All @@ -288,6 +296,8 @@ class RenderableTiledMap {
bool Function(Tileset)? tsxPackingFilter,
bool useAtlas = true,
Paint Function(double opacity)? layerPaintFactory,
double atlasPackingSpacingX = 0,
double atlasPackingSpacingY = 0,
}) async {
// We're not going to load animation frames that are never referenced; but
// we do supply the common cache for all layers in this map, and maintain
Expand All @@ -312,6 +322,8 @@ class RenderableTiledMap {
images: images,
tsxPackingFilter: tsxPackingFilter,
useAtlas: useAtlas,
spacingX: atlasPackingSpacingX,
spacingY: atlasPackingSpacingY,
),
ignoreFlip: ignoreFlip,
images: images,
Expand Down
13 changes: 10 additions & 3 deletions packages/flame_tiled/lib/src/tile_atlas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class TiledAtlas {
Images? images,
bool Function(Tileset)? tsxPackingFilter,
bool useAtlas = true,
double spacingX = 0,
double spacingY = 0,
}) async {
final tilesetImageList = _onlyTileImages(
map,
Expand Down Expand Up @@ -209,12 +211,17 @@ class TiledAtlas {
final tileImageSource = entry.$1;

final image = await imagesInstance.load(tileImageSource);
final rect = bin.pack(image.width.toDouble(), image.height.toDouble());
final rect = bin.pack(
image.width.toDouble() + spacingX,
image.height.toDouble() + spacingY,
);

pictureRect = pictureRect.expandToInclude(rect);

final offset =
offsetMap[tiledImage.source!] = Offset(rect.left, rect.top);
final offset = offsetMap[tiledImage.source!] = Offset(
rect.left - spacingX,
rect.top - spacingY,
);

canvas.drawImage(image, offset, emptyPaint);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/flame_tiled/lib/src/tiled_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class TiledComponent<T extends FlameGame> extends PositionComponent
bool Function(Tileset)? tsxPackingFilter,
bool useAtlas = true,
Paint Function(double opacity)? layerPaintFactory,
double atlasPackingSpacingX = 0,
double atlasPackingSpacingY = 0,
}) async {
return TiledComponent(
await RenderableTiledMap.fromFile(
Expand All @@ -131,6 +133,8 @@ class TiledComponent<T extends FlameGame> extends PositionComponent
tsxPackingFilter: tsxPackingFilter,
useAtlas: useAtlas,
layerPaintFactory: layerPaintFactory,
atlasPackingSpacingX: atlasPackingSpacingX,
atlasPackingSpacingY: atlasPackingSpacingY,
),
priority: priority,
);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions packages/flame_tiled/test/tile_atlas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,32 @@ void main() {
);
});

test(
'packs complex maps with multiple images using a custom spacing',
() async {
final component = await TiledComponent.load(
'isometric_plain.tmx',
Vector2(128, 74),
bundle: bundle,
images: Images(bundle: bundle),
atlasPackingSpacingX: 2,
atlasPackingSpacingY: 2,
);

final atlas = TiledAtlas.atlasMap.values.first;
expect(
await imageToPng(atlas.atlas!),
matchesGoldenFile('goldens/larger_atlas_with_spacing.png'),
);
expect(
renderMapToPng(component),
matchesGoldenFile(
'goldens/larger_atlas_component_with_spacing.png',
),
);
},
);

test('can ignore tilesets in the packing', () async {
await TiledComponent.load(
'isometric_plain.tmx',
Expand Down

0 comments on commit d0c10cb

Please sign in to comment.