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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
* @file TilesetTable.hpp
* @brief Per-layer tileset table + GID→tileset resolution (dependency-free).
*
* A layer can reference several tilesets (as Tiled maps do). Tile ids are global
* and contiguous across tilesets (matching Tiled GIDs): each tileset starts at a
* `first_id`, so an id resolves to (tileset, local index) via the largest
* `first_id <= id`. A single-tileset layer is just one slot with first_id == 1.
*/
#pragma once
#include <cstdint>
#include <vector>
namespace esengine {
namespace tilemap {
struct TilesetSlot {
uint16_t first_id = 1; // global tile-id at which this tileset begins (Tiled firstgid)
uint32_t texture_handle = 0; // resource::TextureHandle id
uint32_t columns = 1; // tiles per row in the tileset texture
uint32_t margin = 0; // atlas border in px before the first tile (Tiled margin)
uint32_t spacing = 0; // gap in px between adjacent tiles (Tiled spacing)
// Duplicated-edge border each cell carries, in px. Non-zero lets the renderer
// send the exact cell rect; 0 = a packed atlas, where it must inset half a
// texel and stretch the tile instead.
uint32_t extrude = 0;
bool operator==(const TilesetSlot&) const = default;
};
/**
* Cells of @p tileSize along an atlas axis of @p size: n span 2*margin +
* n*tileSize + (n-1)*spacing (Tiled's margin borders both sides). Past them a
* tile samples some other tile, so it is skipped. Must agree with the SDK's
* `atlasCells`; zero when the margin leaves none, a float that 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 - 2.0f * margin + spacing) / step;
return n > 0.0f ? static_cast<uint32_t>(n) : 0;
}
/**
* Index of the slot owning @p id — the one with the largest first_id <= id.
* 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).
*/
inline int resolveTilesetSlot(const std::vector<TilesetSlot>& slots, uint16_t id) {
int found = -1;
for (std::size_t i = 0; i < slots.size(); ++i) {
if (id >= slots[i].first_id) {
found = static_cast<int>(i);
} else {
break;
}
}
return found;
}
} // namespace tilemap
} // namespace esengine