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): clip fills and preserve signed coordinate edges

yhhd27117c341b6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d4ff03fab..175cb9279 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Tilemap rectangle fills clip safely across integer coordinate bounds.** Finite layers intersect the requested rectangle before visiting cells, avoiding work outside the map. Wide endpoint arithmetic preserves unsigned extents and clips infinite writes to representable coordinates; empty intersections return immediately. Negative chunk division avoids signed overflow, preserving tile storage and chunk import at both integer edges.
+
- **Paused sprite motions wait for actual completion before exit transitions.** Animator exit-time checks require the matching SpriteAnimator completion latch and stopped playback. Pausing or seeking to a stopped frame preserves the current state; resumed one-shot playback can complete normally, while a pending replay does not count as an ended clip.
- **Measured lists locate visible items without scanning every preceding row.** Ordered cached offsets use binary searches for both viewport boundaries. Non-monotone or non-finite offsets retain the existing scan semantics. Failed measurements do not publish a completed cache, and list synchronization stays dirty after errors so a subsequent update can retry.
diff --git a/src/esengine/tilemap/ChunkBlob.hpp b/src/esengine/tilemap/ChunkBlob.hpp
index 542f87540..7b9dd7528 100644
--- a/src/esengine/tilemap/ChunkBlob.hpp
+++ b/src/esengine/tilemap/ChunkBlob.hpp
@@ -170,7 +170,7 @@ inline bool walkBlobCells(const u8* raw, usize size, const BlobHeader& header, F
/** Floor division and its remainder, so a chunk left of the origin lands where
* it belongs rather than rounding toward zero. */
-inline i32 floorDiv(i32 a, i32 b) { return (a >= 0 ? a : a - (b - 1)) / b; }
+inline i32 floorDiv(i32 a, i32 b) { return a / b - static_cast<i32>(a % b < 0); }
inline i32 floorMod(i32 a, i32 b) { const i32 m = a % b; return m < 0 ? m + b : m; }
} // namespace tilemap
diff --git a/src/esengine/tilemap/TilemapSystem.cpp b/src/esengine/tilemap/TilemapSystem.cpp
index ed2342eee..b364cfe0f 100644
--- a/src/esengine/tilemap/TilemapSystem.cpp
+++ b/src/esengine/tilemap/TilemapSystem.cpp
@@ -5,6 +5,7 @@
#include <algorithm>
#include <cmath>
#include <cstring>
+#include <limits>
namespace esengine::tilemap {
@@ -36,7 +37,7 @@ namespace {
inline i32 chunkOf(i32 a) { // floor(a / CHUNK_SIZE), negative-safe
const i32 n = static_cast<i32>(CHUNK_SIZE);
- return (a >= 0) ? (a / n) : -(((-a) + n - 1) / n);
+ return a / n - static_cast<i32>(a % n < 0);
}
// Finite layers (width>0) bound writes; infinite layers (width==0) take any coord.
@@ -178,9 +179,20 @@ void TilemapSystem::fillRect(Entity entity, i32 x, i32 y,
auto it = layers_.find(entity);
if (it == layers_.end()) return;
auto& layer = it->second;
- for (i32 ty = y; ty < y + static_cast<i32>(h); ++ty) {
- for (i32 tx = x; tx < x + static_cast<i32>(w); ++tx) {
- if (inBounds(layer, tx, ty)) writeTile(layer, tx, ty, tileId);
+ const i64 coordinateEnd = static_cast<i64>(std::numeric_limits<i32>::max()) + 1;
+ i64 startX = x, startY = y;
+ i64 endX = std::min(static_cast<i64>(x) + w, coordinateEnd);
+ i64 endY = std::min(static_cast<i64>(y) + h, coordinateEnd);
+ if (layer.width != 0 || layer.height != 0) {
+ startX = std::max(startX, i64{0});
+ startY = std::max(startY, i64{0});
+ endX = std::min(endX, static_cast<i64>(layer.width));
+ endY = std::min(endY, static_cast<i64>(layer.height));
+ }
+ if (startX >= endX || startY >= endY) return;
+ for (i64 ty = startY; ty < endY; ++ty) {
+ for (i64 tx = startX; tx < endX; ++tx) {
+ writeTile(layer, static_cast<i32>(tx), static_cast<i32>(ty), tileId);
}
}
}
diff --git a/tests/tilemap/test_tilemap.cpp b/tests/tilemap/test_tilemap.cpp
index 93f213a2c..3273fb8be 100644
--- a/tests/tilemap/test_tilemap.cpp
+++ b/tests/tilemap/test_tilemap.cpp
@@ -5,6 +5,7 @@
#include <esengine/tilemap/TileFlip.hpp>
#include <esengine/tilemap/ChunkBlob.hpp>
+#include <limits>
#include <tuple>
#include <vector>
@@ -559,9 +560,9 @@ TEST_CASE("tilemap_blob_walk") {
std::memcpy(chunk, &cx, sizeof(cx));
std::memcpy(chunk + sizeof(i32), &cy, sizeof(cy));
const u16 corner = 3;
- const u16 far = static_cast<u16>(4 | TILE_FLIP_D);
+ const u16 farTile = static_cast<u16>(4 | TILE_FLIP_D);
std::memcpy(chunk + sizeof(i32) * 2, &corner, sizeof(corner)); // local (0,0)
- std::memcpy(chunk + sizeof(i32) * 2 + (31 * side + 31) * sizeof(u16), &far, sizeof(far));
+ std::memcpy(chunk + sizeof(i32) * 2 + (31 * side + 31) * sizeof(u16), &farTile, sizeof(farTile));
BlobHeader header{};
REQUIRE(parseBlobHeader(raw.data(), raw.size(), header));
@@ -577,7 +578,7 @@ TEST_CASE("tilemap_blob_walk") {
CHECK(std::get<2>(seen[0]) == corner);
CHECK(std::get<0>(seen[1]) == -1);
CHECK(std::get<1>(seen[1]) == 95);
- CHECK(std::get<2>(seen[1]) == far);
+ CHECK(std::get<2>(seen[1]) == farTile);
SUBCASE("and a payload that stops short is refused rather than read past") {
std::vector<u8> cut(raw.begin(), raw.end() - 4);
@@ -594,3 +595,38 @@ TEST_CASE("tilemap_chunk_floor") {
CHECK(floorMod(-1, 16) == 15);
CHECK(floorMod(-16, 16) == 0);
}
+
+
+TEST_CASE("tilemap_fill_clips_unsigned_extents_to_finite_bounds") {
+ TilemapSystem sys;
+ sys.initLayer(E(0), 3, 3, 16, 16);
+ sys.fillRect(E(0), -1, -1, std::numeric_limits<u32>::max(), std::numeric_limits<u32>::max(), 7);
+ for (i32 y = 0; y < 3; ++y) for (i32 x = 0; x < 3; ++x) CHECK(sys.getTile(E(0), x, y) == 7);
+ CHECK(sys.getLayerData(E(0))->chunks.size() == 1);
+ const auto revision = sys.getLayerData(E(0))->edit_revision;
+ sys.fillRect(E(0), std::numeric_limits<i32>::max(), std::numeric_limits<i32>::max(), 1, 1, 9);
+ CHECK(sys.getLayerData(E(0))->edit_revision == revision);
+}
+
+
+TEST_CASE("tilemap_signed_coordinate_edges_preserve_chunk_ownership") {
+ TilemapSystem sys;sys.initInfiniteLayer(E(0), 16, 16);
+ const i32 low = std::numeric_limits<i32>::min(), high = std::numeric_limits<i32>::max();
+ const std::vector<std::pair<i32, i32>> cells{{low, low}, {low + 15, low}, {low, low + 15},
+ {high, high}, {high - 15, high}, {high, high - 15}, {-17, -1}, {-16, 0}};
+ for (usize i = 0; i < cells.size(); ++i) sys.setTile(E(0), cells[i].first, cells[i].second, static_cast<u16>(i + 1));
+ for (usize i = 0; i < cells.size(); ++i) CHECK(sys.getTile(E(0), cells[i].first, cells[i].second) == i + 1);
+ const auto* layer = sys.getLayerData(E(0));
+ CHECK(layer->chunks.at({low / static_cast<i32>(CHUNK_SIZE), low / static_cast<i32>(CHUNK_SIZE)}).tiles[0] == 1);
+ CHECK(layer->chunks.at({high / static_cast<i32>(CHUNK_SIZE), high / static_cast<i32>(CHUNK_SIZE)}).tiles[CHUNK_SIZE * CHUNK_SIZE - 1] == 4);
+ CHECK(floorDiv(low, 16) == low / 16);CHECK(floorDiv(low + 1, 16) == low / 16);
+ CHECK(floorDiv(high, 16) == high / 16);CHECK(floorMod(low, 16) == 0);CHECK(floorMod(high, 16) == 15);
+ sys.fillRect(E(0), high, high, 2, 2, 9);
+ CHECK(sys.getTile(E(0), high, high) == 9);
+ CHECK(sys.getTile(E(0), low, low) == 1);
+ CHECK(layer->chunks.at({high / 16, high / 16}).tiles[0] == 0);
+ const auto revision = layer->edit_revision;
+ sys.fillRect(E(0), 0, low, 0, std::numeric_limits<u32>::max(), 8);
+ sys.fillRect(E(0), low, 0, std::numeric_limits<u32>::max(), 0, 8);
+ CHECK(layer->edit_revision == revision);
+}
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 0a52146c9..bb082e991 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'fix(tilemap): clip fills and preserve signed coordinate edges':
+ { note: 'Tilemap rectangle fills clip safely across integer coordinate bounds.' },
'fix(animation): keep paused sprite motions out of exit transitions':
{ note: 'Paused sprite motions wait for actual completion before exit transitions.' },
'feat(viewport): add spatial plane and center handles':