Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(tilemap): an atlas margin wider than its image counts no cells

esenginec01aaef6ab1c
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
index 34fe85c8b..78c678deb 100644
--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
@@ -237,12 +237,8 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
resolved[i].uvStepY = (layer.tile_height + spacing) / th;
resolved[i].insetU = 0.5f / tw;
resolved[i].insetV = 0.5f / th;
- // n cells span margin + n*tileSize + (n-1)*spacing, so the last one
- // that fits is floor((size - margin + spacing) / step).
- resolved[i].atlasCols = resolved[i].uvStepX > 0.0f
- ? static_cast<u32>((tw - margin + spacing) / (layer.tile_width + spacing)) : 0;
- resolved[i].atlasRows = resolved[i].uvStepY > 0.0f
- ? static_cast<u32>((th - margin + spacing) / (layer.tile_height + spacing)) : 0;
+ resolved[i].atlasCols = tilemap::atlasCells(tw, margin, layer.tile_width, spacing);
+ resolved[i].atlasRows = tilemap::atlasCells(th, margin, layer.tile_height, spacing);
anySlotValid = true;
}
if (!anySlotValid) continue;
diff --git a/src/esengine/tilemap/TilesetTable.hpp b/src/esengine/tilemap/TilesetTable.hpp
index dc1b72f99..9021264b9 100644
--- a/src/esengine/tilemap/TilesetTable.hpp
+++ b/src/esengine/tilemap/TilesetTable.hpp
@@ -30,6 +30,19 @@ struct TilesetSlot {
* Returns -1 if none (id below the first slot, or empty table). @p slots must be
* sorted ascending by first_id (TilemapSystem::setTilesets guarantees this).
*/
+/**
+ * Cells of @p tileSize along an atlas axis of @p size: n span margin +
+ * n*tileSize + (n-1)*spacing. Past them a tile samples some other tile, so the
+ * renderer skips it; the palette counts the same way (desktop tileMath.ts) and
+ * the two must agree. Zero when the margin leaves none — that float would trap.
+ */
+inline uint32_t atlasCells(float size, float margin, float tileSize, float spacing) {
+ float step = tileSize + spacing;
+ if (step <= 0.0f) return 0;
+ float n = (size - margin + spacing) / step;
+ return n > 0.0f ? static_cast<uint32_t>(n) : 0;
+}
+
inline int resolveTilesetSlot(const std::vector<TilesetSlot>& slots, uint16_t id) {
int found = -1;
for (std::size_t i = 0; i < slots.size(); ++i) {
diff --git a/tests/tilemap/test_tilemap.cpp b/tests/tilemap/test_tilemap.cpp
index ae1ea318c..a479e3d95 100644
--- a/tests/tilemap/test_tilemap.cpp
+++ b/tests/tilemap/test_tilemap.cpp
@@ -398,3 +398,21 @@ TEST_CASE("tilemap_animation_revision_separation") {
sys.clearTileAnimations(E(0));
CHECK(sys.getLayerData(E(0))->anim_table_revision == 2);
}
+
+TEST_CASE("tileset_atlas_cells") {
+ using esengine::tilemap::atlasCells;
+
+ // A gapless atlas: exactly the cells that fit, and a partial one does not count.
+ CHECK(atlasCells(64.0f, 0.0f, 32.0f, 0.0f) == 2);
+ CHECK(atlasCells(80.0f, 0.0f, 32.0f, 0.0f) == 2);
+
+ // Tiled's margin sits before the first cell, spacing only between cells — so
+ // three 32px tiles with 2px gaps and a 4px border need 4 + 96 + 4 = 104.
+ CHECK(atlasCells(104.0f, 4.0f, 32.0f, 2.0f) == 3);
+ CHECK(atlasCells(103.0f, 4.0f, 32.0f, 2.0f) == 2);
+
+ // Degenerate inputs answer "no cells" rather than converting an out-of-range
+ // float: a margin wider than the image, and a tile with no size.
+ CHECK(atlasCells(16.0f, 4096.0f, 32.0f, 0.0f) == 0);
+ CHECK(atlasCells(64.0f, 0.0f, 0.0f, 0.0f) == 0);
+}