3#ifdef ES_ENABLE_TILEMAP
32void tilemap_initLayer(
u32 entity,
u32 width,
u32 height,
33 f32 tileWidth,
f32 tileHeight) {
36 getTilemapSystem().initLayer(e, width, height, tileWidth, tileHeight);
39void tilemap_initInfinite(
u32 entity,
f32 tileWidth,
f32 tileHeight) {
45 if (
auto* layer = getTilemapSystem().getLayerDataMut(e)) {
46 layer->tile_width = tileWidth;
47 layer->tile_height = tileHeight;
48 layer->infinite =
true;
51 getTilemapSystem().initInfiniteLayer(e, tileWidth, tileHeight);
54void tilemap_destroyLayer(
u32 entity) {
57 getTilemapSystem().destroyLayer(e);
60void tilemap_setTile(
u32 entity,
i32 x,
i32 y,
u32 tileId) {
62 if (e ==
INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return;
63 getTilemapSystem().setTile(e, x, y,
static_cast<u16>(tileId));
68 if (e ==
INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return 0;
69 return getTilemapSystem().getTile(e, x, y);
72void tilemap_fillRect(
u32 entity,
i32 x,
i32 y,
75 if (e ==
INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return;
76 getTilemapSystem().fillRect(e, x, y, w, h,
static_cast<u16>(tileId));
79void tilemap_setTiles(
u32 entity, uintptr_t tilesPtr,
u32 count) {
81 if (e ==
INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return;
84 getTilemapSystem().setTiles(e, tiles, count);
90void tilemap_setTilesets(
u32 entity, uintptr_t dataPtr,
u32 count) {
93 constexpr u32 SLOT_STRIDE = 6;
95 if (e ==
INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return;
99 if (count && !d)
return;
100 std::vector<tilemap::TilesetSlot> slots;
101 slots.reserve(count);
102 for (
u32 i = 0; i < count; ++i) {
104 slot.
first_id =
static_cast<u16>(d[i * SLOT_STRIDE + 0]);
105 slot.texture_handle = d[i * SLOT_STRIDE + 1];
106 slot.columns = d[i * SLOT_STRIDE + 2];
107 slot.margin = d[i * SLOT_STRIDE + 3];
108 slot.spacing = d[i * SLOT_STRIDE + 4];
109 slot.extrude = d[i * SLOT_STRIDE + 5];
110 slots.push_back(slot);
112 getTilemapSystem().setTilesets(e, std::move(slots));
115bool tilemap_hasLayer(
u32 entity) {
119u32 tilemap_getEditRevision(
u32 entity) {
120 const auto* layer = getTilemapSystem().getLayerData(
Entity::fromRaw(entity));
121 return layer ? layer->edit_revision : 0;
124u32 tilemap_getGeneration(
u32 entity) {
125 const auto* layer = getTilemapSystem().getLayerData(
Entity::fromRaw(entity));
126 return layer ? layer->generation : 0;
129u32 tilemap_readChunkIndex(
u32 entity, uintptr_t rowsPtr,
u32 capacity) {
130 const auto* layer = getTilemapSystem().getLayerData(
Entity::fromRaw(entity));
131 const u32 count = layer ?
static_cast<u32>(layer->chunks.size()) : 0;
132 if (count == 0 || capacity < count)
return count;
135 for (
const auto& [coord, chunk] : layer->chunks) {
136 std::memcpy(out, &coord.x,
sizeof(
i32));
137 std::memcpy(out +
sizeof(
u32), &coord.y,
sizeof(
i32));
138 std::memcpy(out + 2 *
sizeof(
u32), &chunk.revision,
sizeof(
u32));
139 out += 3 *
sizeof(
u32);
144bool tilemap_readChunkTiles(
u32 entity,
i32 chunkX,
i32 chunkY, uintptr_t tilesPtr,
u32 capacity) {
146 if (capacity < cells)
return false;
147 const auto* layer = getTilemapSystem().getLayerData(
Entity::fromRaw(entity));
148 if (!layer)
return false;
149 const auto found = layer->chunks.find({chunkX, chunkY});
150 if (found == layer->chunks.end())
return false;
152 if (!out)
return false;
153 std::memcpy(out, found->second.tiles, cells *
sizeof(
u16));
157void tilemap_setRenderProps(
u32 entity,
u32 textureHandle,
u32 tilesetColumns,
160 f32 parallaxX,
f32 parallaxY) {
162 textureHandle, tilesetColumns, uvTileW, uvTileH,
163 sortLayer, depth, parallaxX, parallaxY);
167 getTilemapSystem().setTint(
Entity::fromRaw(entity), r, g, b, a, opacity);
170void tilemap_setVisible(
u32 entity,
bool visible) {
174void tilemap_setOriginEntity(
u32 layerKey,
u32 originEntity) {
186constexpr const char* kB64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
188void base64Encode(
const u8* data,
usize len, std::string& out) {
189 out.reserve(out.size() + ((len + 2) / 3) * 4);
191 while (i + 3 <= len) {
192 const u32 v = (
u32(data[i]) << 16) | (
u32(data[i + 1]) << 8) |
u32(data[i + 2]);
193 out.push_back(kB64[(v >> 18) & 0x3F]);
194 out.push_back(kB64[(v >> 12) & 0x3F]);
195 out.push_back(kB64[(v >> 6) & 0x3F]);
196 out.push_back(kB64[v & 0x3F]);
200 const u32 v = (
u32(data[i]) << 16) | (i + 1 < len ? (
u32(data[i + 1]) << 8) : 0);
201 out.push_back(kB64[(v >> 18) & 0x3F]);
202 out.push_back(kB64[(v >> 12) & 0x3F]);
204 out.push_back(kB64[(v >> 6) & 0x3F]);
212i32 b64Value(
char c) {
213 if (c >=
'A' && c <=
'Z')
return c -
'A';
214 if (c >=
'a' && c <=
'z')
return c -
'a' + 26;
215 if (c >=
'0' && c <=
'9')
return c -
'0' + 52;
216 if (c ==
'-' || c ==
'+')
return 62;
217 if (c ==
'_' || c ==
'/')
return 63;
221bool base64Decode(
const std::string& in, std::vector<u8>& out) {
223 out.reserve(in.size() * 3 / 4);
227 if (c ==
'=' || c ==
'\n' || c ==
'\r' || c ==
' ')
continue;
228 const i32 v = b64Value(c);
229 if (v < 0)
return false;
230 buf = (buf << 6) | v;
234 out.push_back(
static_cast<u8>((buf >> bits) & 0xFF));
251std::string tilemap_exportChunks(
u32 entity) {
253 const auto* layer = getTilemapSystem().getLayerData(e);
254 if (!layer)
return {};
256 std::vector<std::pair<tilemap::ChunkCoord, const tilemap::ChunkData*>> nonEmpty;
257 nonEmpty.reserve(layer->chunks.size());
258 for (
const auto& [coord, chunk] : layer->chunks) {
259 bool anyTile =
false;
263 if (anyTile) nonEmpty.emplace_back(coord, &chunk);
266 const u32 count =
static_cast<u32>(nonEmpty.size());
267 const usize perChunk =
sizeof(
i32) * 2
272 auto append = [&](
const void* p,
usize n) {
273 const u8* b =
static_cast<const u8*
>(p);
274 raw.insert(raw.end(), b, b + n);
277 for (
const auto& [coord, chunk] : nonEmpty) {
278 append(&coord.x,
sizeof(coord.x));
279 append(&coord.y,
sizeof(coord.y));
280 append(chunk->tiles,
sizeof(chunk->tiles));
284 base64Encode(raw.data(), raw.size(), out);
288bool tilemap_importChunks(
u32 entity,
const std::string& encoded) {
293 if (!getTilemapSystem().hasLayer(e)) {
294 getTilemapSystem().initInfiniteLayer(e, 32.0f, 32.0f);
296 auto* layer = getTilemapSystem().getLayerDataMut(e);
297 if (!layer)
return false;
300 if (!base64Decode(encoded, raw))
return false;
315 if (!layer->chunks.empty()) ++layer->edit_revision;
316 layer->chunks.clear();
322 if (!tilemap::recodeCell(word, header.wrote, running, cell)) { lost = true; return; }
325 if (first || coord.x != at.x || coord.y != at.y) {
326 auto& chunk = layer->chunks[coord];
327 chunk.revision = ++layer->edit_revision;
335 if (!walked || lost) {
336 layer->chunks.clear();
342void tilemap_initInfiniteLayer(u32 entity, f32 tileWidth, f32 tileHeight) {
343 auto e = Entity::fromRaw(entity);
344 if (e == INVALID_ENTITY)
return;
345 getTilemapSystem().initInfiniteLayer(e, tileWidth, tileHeight);
348void tilemap_setChunkTiles(u32 entity, i32 chunkX, i32 chunkY,
349 uintptr_t tilesPtr, u32 width, u32 height) {
350 auto e = Entity::fromRaw(entity);
351 if (e == INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return;
352 const auto* tiles = boundarySpan<u16>(tilesPtr,
static_cast<u64>(width) * height,
"tilemap_setChunkTiles");
354 getTilemapSystem().setChunkTiles(e, chunkX, chunkY, tiles, width, height);
357void tilemap_removeChunk(u32 entity, i32 chunkX, i32 chunkY) {
358 auto e = Entity::fromRaw(entity);
359 if (e == INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return;
360 getTilemapSystem().removeChunk(e, chunkX, chunkY);
363void tilemap_setTileAnimation(u32 entity, u32 tileId,
364 uintptr_t framesPtr, u32 frameCount) {
365 auto e = Entity::fromRaw(entity);
366 if (e == INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return;
367 const auto* data = boundarySpan<u32>(framesPtr,
static_cast<u64>(frameCount) * 2,
"tilemap_setTileAnimation");
369 std::vector<tilemap::AnimFrame> frames(frameCount);
370 for (u32 i = 0; i < frameCount; i++) {
371 frames[i].tile_id =
static_cast<u16>(data[i * 2]);
372 frames[i].duration_ms =
static_cast<u16>(data[i * 2 + 1]);
374 getTilemapSystem().setTileAnimation(e,
static_cast<u16>(tileId),
375 frames.data(), frameCount);
378void tilemap_clearTileAnimations(u32 entity) {
379 auto e = Entity::fromRaw(entity);
380 if (e == INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return;
381 getTilemapSystem().clearTileAnimations(e);
384void tilemap_advanceAnimations(u32 entity, f32 dtMs) {
385 auto e = Entity::fromRaw(entity);
386 if (e == INVALID_ENTITY)
return;
387 getTilemapSystem().advanceAnimations(e, dtMs);
390void tilemap_setTileProperty(u32 entity, u32 tileId,
391 const std::string& key,
const std::string& value) {
392 auto e = Entity::fromRaw(entity);
393 if (e == INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return;
394 getTilemapSystem().setTileProperty(e,
static_cast<u16>(tileId), key, value);
397std::string tilemap_getTileProperty(u32 entity, i32 x, i32 y,
398 const std::string& key) {
399 auto e = Entity::fromRaw(entity);
400 if (e == INVALID_ENTITY || !getTilemapSystem().hasLayer(e))
return "";
401 u16 raw = getTilemapSystem().getTile(e, x, y);
402 u16 tileId = raw & tilemap::TILE_ID_MASK;
403 if (tileId == tilemap::EMPTY_TILE)
return "";
404 return getTilemapSystem().getTileProperty(e, tileId, key);
407void tilemap_flipTile(u32 entity, i32 x, i32 y,
408 bool flipH,
bool flipV,
bool flipD) {
409 auto e = Entity::fromRaw(entity);
410 if (e == INVALID_ENTITY)
return;
411 getTilemapSystem().flipTile(e, x, y, flipH, flipV, flipD);
414void tilemap_rotateTile(u32 entity, i32 x, i32 y, i32 degrees) {
415 auto e = Entity::fromRaw(entity);
416 if (e == INVALID_ENTITY)
return;
417 getTilemapSystem().rotateTile(e, x, y, degrees);
420void tilemap_setGridType(u32 entity, u32 type) {
421 auto e = Entity::fromRaw(entity);
422 if (e == INVALID_ENTITY)
return;
423 getTilemapSystem().setGridType(e,
static_cast<tilemap::GridType
>(type));
426void tilemap_setHexParams(u32 entity, f32 sideLength, u32 staggerAxisX, u32 staggerIndexEven) {
427 auto e = Entity::fromRaw(entity);
428 if (e == INVALID_ENTITY)
return;
429 getTilemapSystem().setHexParams(e, sideLength, staggerAxisX != 0, staggerIndexEven != 0);
432static f32 s_coordBuffer[2] = {};
434uintptr_t tilemap_tileToWorld(u32 entity, i32 tx, i32 ty,
435 f32 originX, f32 originY) {
436 auto e = Entity::fromRaw(entity);
437 getTilemapSystem().tileToWorld(e, tx, ty, originX, originY,
438 s_coordBuffer[0], s_coordBuffer[1]);
439 return reinterpret_cast<uintptr_t
>(s_coordBuffer);
442uintptr_t tilemap_worldToTile(u32 entity, f32 wx, f32 wy,
443 f32 originX, f32 originY) {
444 auto e = Entity::fromRaw(entity);
446 getTilemapSystem().worldToTile(e, wx, wy, originX, originY, tx, ty);
447 s_coordBuffer[0] =
static_cast<f32>(tx);
448 s_coordBuffer[1] =
static_cast<f32>(ty);
449 return reinterpret_cast<uintptr_t
>(s_coordBuffer);
Always-on validation for JS-supplied pointer+length pairs at the WASM boundary.
Custom geometry for user-defined mesh rendering.
Post-processing effects pipeline.
Rendering context containing all renderer state.
Frame resources and the shared scene rendering contract.
Central resource management system.
The tilemap entry points the SDK drives layers through.
i32 floorMod(i32 a, i32 b)
bool parseBlobHeader(const u8 *raw, usize size, BlobHeader &out)
static constexpr u32 CHUNK_SIZE
void appendBlobHeader(std::vector< u8 > &out, u32 chunkCount)
constexpr usize BLOB_HEADER_V2_BYTES
static constexpr u16 EMPTY_TILE
CellEncoding runningEncoding()
bool walkBlobCells(const u8 *raw, usize size, const BlobHeader &header, Fn &&emit)
const T * boundarySpan(uintptr_t ptr, u64 count, const char *what)
Validates a JS-supplied pointer to count elements of T and returns it typed.
std::int32_t i32
Signed 32-bit integer.
std::uint64_t u64
Unsigned 64-bit integer.
std::size_t usize
Size type for indexing and sizes.
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
T * boundarySpanMut(uintptr_t ptr, u64 count, const char *what)
Mutable variant of boundarySpan, for entry points that WRITE into a JS buffer.
std::uint32_t u32
Unsigned 32-bit integer.
std::uint8_t u8
Unsigned 8-bit integer.
std::uint16_t u16
Unsigned 16-bit integer.
static EstellaContext & ctx()
EstellaContext & activeCtx()
The engine context every binding routes through — single source of truth.
float f32
32-bit floating point
static constexpr Entity fromRaw(u32 v)
Reconstructs an Entity from a raw packed value.