13#include <unordered_set>
21int gLastVisibleTileCount = 0;
22int gLastCustomVisualCount = 0;
23int gLastAtlasCount = 0;
24int gLastVisitedChunkCount = 0;
25int gLastVisitedCellCount = 0;
26double gAnimationTimeMs = 0.0;
35ViewCam fromEntity(graphics::Camera2D *ent) {
47Color solidForGid(uint32_t gid,
const Color &
tint) {
48 const uint32_t
h = gid * 2654435761u;
49 const float r = ((
h >> 0) & 255u) / 255.f;
50 const float g = ((
h >> 8) & 255u) / 255.f;
51 const float b = ((
h >> 16) & 255u) / 255.f;
55const TileLayer::Tileset::Atlas *atlasForGid(
const TileLayer::Tileset &ts, uint32_t gid) {
56 const TileLayer::Tileset::Atlas *
best =
nullptr;
57 for (
const auto &atlas : ts.atlases) {
58 if (gid >= uint32_t(atlas.firstGid) && (!
best || atlas.firstGid >
best->firstGid))
best = &atlas;
63bool atlasUV(
const TileLayer::Tileset::Atlas &atlas, uint32_t gid,
float &u0,
float &v0,
float &u1,
float &v1) {
64 if (!atlas.texture || atlas.columns <= 0 || atlas.tileW <= 0 || atlas.tileH <= 0)
return false;
65 if (gid < uint32_t(atlas.firstGid))
return false;
66 const int local = int(gid) - atlas.firstGid;
67 const int col =
local % atlas.columns;
68 const int row =
local / atlas.columns;
69 const float iw = float(atlas.texture->getWidth());
70 const float ih = float(atlas.texture->getHeight());
71 if (iw <= 0.f || ih <= 0.f)
return false;
73 const float px = float(atlas.margin + col * (atlas.tileW + atlas.spacing));
74 const float py = float(atlas.margin + row * (atlas.tileH + atlas.spacing));
77 u1 = (
px + float(atlas.tileW)) / iw;
78 v1 = (
py + float(atlas.tileH)) / ih;
82const TileLayer::Tileset::Visual *visualForGid(
const TileLayer::Tileset &ts, uint32_t gid) {
83 for (
const auto &visual : ts.
visuals)
84 if (visual.gid == int(gid)) return &visual;
88bool visualUV(
const TileLayer::Tileset::Atlas &atlas,
const TileLayer::Tileset::Visual &visual,
float &u0,
float &v0,
89 float &u1,
float &v1) {
90 if (!atlas.texture || visual.width <= 0 || visual.height <= 0)
return false;
91 const float iw = float(atlas.texture->getWidth());
92 const float ih = float(atlas.texture->getHeight());
93 if (iw <= 0.f || ih <= 0.f)
return false;
94 u0 = float(visual.x) / iw;
95 v0 = float(visual.y) / ih;
96 u1 = float(visual.x + visual.width) / iw;
97 v1 = float(visual.y + visual.height) / ih;
101bool subtileUV(
const TileLayer::Tileset::Atlas &atlas,
const TileLayer::Tileset::Visual::Subtile &part,
float &u0,
102 float &v0,
float &u1,
float &v1) {
103 if (!atlas.texture || part.width <= 0 || part.height <= 0)
return false;
104 const float iw = float(atlas.texture->getWidth());
105 const float ih = float(atlas.texture->getHeight());
106 if (iw <= 0.f || ih <= 0.f)
return false;
107 u0 = float(part.x) / iw;
108 v0 = float(part.y) / ih;
109 u1 = float(part.x + part.width) / iw;
110 v1 = float(part.y + part.height) / ih;
114const TileLayer::Tileset::Visual::SubtileFrame *subtileFrame(
const TileLayer::Tileset::Visual &visual) {
115 if (visual.subtileFrames.empty())
return nullptr;
117 for (
const auto &
frame : visual.subtileFrames) total +=
std::
max(1,
frame.durationMs);
118 int cursor = total > 0 ? int(std::fmod(gAnimationTimeMs,
double(total))) : 0;
119 for (
const auto &
frame : visual.subtileFrames) {
123 return &visual.subtileFrames.back();
126int64_t fileModtime(
const std::string &
path) {
127 auto *fs = eve::ModuleManager::getInstance<eve::filesystem::Filesystem>(
"Filesystem");
128 if (!fs) fs = eve::filesystem::Filesystem::create();
130 if (!fs->getInfo(
path,
info))
return -1;
134uint32_t animatedGid(
const TileLayer::Tileset &ts, uint32_t gid) {
136 if (animation.gid !=
int(gid) || animation.frames.empty())
continue;
138 for (
const auto &
frame : animation.frames) total +=
std::
max(1,
frame.durationMs);
139 if (total <= 0)
return gid;
140 int cursor = int(std::fmod(gAnimationTimeMs,
double(total)));
141 for (
const auto &
frame : animation.frames) {
149void applyTiledTransform(graphics::DrawItem2D &item, uint32_t
raw) {
150 const bool horizontal = (
raw & 0x80000000u) != 0;
151 const bool vertical = (
raw & 0x40000000u) != 0;
154 item.flipX = horizontal;
155 item.flipY = vertical;
158 item.rotation = horizontal ? 90.f : 270.f;
159 item.flipX = horizontal == vertical;
163bool chunkVisible(
const TileLayer::Config &cfg,
const TileLayer::Tileset &ts,
const ViewCam &cam,
164 int viewWidth,
int viewHeight,
int x0,
int y0,
int x1,
int y1,
float scaleX,
166 if (!cam.valid || viewWidth <= 0 || viewHeight <= 0)
return true;
167 float minX = 1e30f, minY = 1e30f, maxX = -1e30f, maxY = -1e30f;
168 const int xs[] = {x0, std::max(x0, x1 - 1)};
169 const int ys[] = {y0, std::max(y0, y1 - 1)};
172 float wx = 0.f,
wy = 0.f;
174 minX = std::min(minX,
wx);
175 minY = std::min(minY,
wy);
176 maxX = std::max(maxX,
wx + cfg.tileW * scaleX);
177 maxY = std::max(maxY,
wy + cfg.tileH * scaleY);
180 float expandX = cfg.tileW * scaleX;
181 float expandY = cfg.tileH * scaleY;
182 for (
const auto &visual : ts.
visuals) {
183 expandX = std::max(expandX,
float(visual.width) * scaleX + std::fabs(visual.pivotX) * scaleX);
184 expandY = std::max(expandY,
float(visual.height) * scaleY + std::fabs(visual.pivotY) * scaleY);
190 const float zoom = cam.zoom > 0.f ? cam.zoom : 0.0001f;
191 const float left = cam.x - float(viewWidth) * 0.5f /
zoom;
192 const float right = cam.x + float(viewWidth) * 0.5f /
zoom;
193 const float top = cam.y - float(viewHeight) * 0.5f /
zoom;
194 const float bottom = cam.y + float(viewHeight) * 0.5f /
zoom;
195 return maxX >=
left && minX <= right && maxY >=
top && minY <=
bottom;
204 gLastVisibleTileCount = 0;
205 gLastCustomVisualCount = 0;
207 gLastVisitedChunkCount = 0;
208 gLastVisitedCellCount = 0;
209 if (ecs::current()->getManager<TileLayer>() ==
nullptr)
return;
211 std::unordered_set<graphics::Texture *> atlases;
215 for (
auto it =
view.begin(); it !=
view.end(); ++it) {
217 if (!
draw->visible || cfg->mapW <= 0 || cfg->mapH <= 0)
continue;
218 if (
tiles->gids.size() < size_t(cfg->mapW * cfg->mapH))
continue;
221 const ViewCam cam = fromEntity(
draw->camera);
222 const float scaleX =
draw->visualScaleX > 0.f ?
draw->visualScaleX : 1.f;
223 const float scaleY =
draw->visualScaleY > 0.f ?
draw->visualScaleY : 1.f;
225 const int chunkColumns = std::max(1,
tiles->chunkColumns);
226 const int chunkRows = std::max(1,
tiles->chunkRows);
227 for (
int cy = 0;
cy < chunkRows; ++
cy) {
228 for (
int cx = 0;
cx < chunkColumns; ++
cx) {
229 const int chunkIndex =
cy * chunkColumns +
cx;
230 if (!
tiles->chunks.empty() &&
231 (chunkIndex >=
int(
tiles->chunks.size()) ||
232 tiles->chunks[
size_t(chunkIndex)].nonEmpty == 0))
238 if (!chunkVisible(*cfg, *ts, cam, viewWidth, viewHeight, x0, y0, x1, y1, scaleX,
241 ++gLastVisitedChunkCount;
242 for (
int ty = y0; ty < y1; ++ty) {
243 for (
int tx = x0; tx < x1; ++tx) {
244 ++gLastVisitedCellCount;
245 const uint32_t
raw =
tiles->gids[size_t(ty * cfg->mapW + tx)];
246 const uint32_t gid = animatedGid(*ts,
tileGid(
raw));
247 if (gid == 0)
continue;
251 item.
w = cfg->tileW * scaleX;
252 item.
h = cfg->tileH * scaleY;
263 applyTiledTransform(item,
raw);
265 float u0, v0, u1, v1;
266 const auto* visual = visualForGid(*ts, gid);
267 const auto *atlas = atlasForGid(*ts, gid);
268 const auto *subtiles = visual ? subtileFrame(*visual) :
nullptr;
269 if (subtiles && atlas) {
270 for (
const auto &part : subtiles->parts) {
272 if (!subtileUV(*atlas, part, u0, v0, u1, v1))
continue;
273 float centerX = (part.offsetX + float(part.width) * 0.5f) * scaleX
274 - cfg->tileW * scaleX * 0.5f;
275 float centerY = (part.offsetY + float(part.height) * 0.5f) * scaleY
276 - cfg->tileH * scaleY * 0.5f;
277 if (
raw & 0x20000000u) std::swap(centerX, centerY);
278 if (
raw & 0x80000000u) centerX = -centerX;
279 if (
raw & 0x40000000u) centerY = -centerY;
280 quarter.
x += cfg->tileW * scaleX * 0.5f + centerX
281 - float(part.width) * scaleX * 0.5f;
282 quarter.
y += cfg->tileH * scaleY * 0.5f + centerY
283 - float(part.height) * scaleY * 0.5f;
284 quarter.
w = float(part.width) * scaleX;
285 quarter.
h = float(part.height) * scaleY;
286 quarter.
texture = atlas->texture;
287 quarter.
hasUV =
true;
294 out.push_back(quarter);
295 ++gLastCustomVisualCount;
297 ++gLastVisibleTileCount;
298 if (atlas->texture) atlases.insert(atlas->texture);
301 if (visual && atlas && visualUV(*atlas, *visual, u0, v0, u1, v1)) {
302 ++gLastCustomVisualCount;
303 item.
x -= visual->pivotX * scaleX;
304 item.
y -= visual->pivotY * scaleY;
305 item.
w = float(visual->width) * scaleX;
306 item.
h = float(visual->height) * scaleY;
307 item.
depthY += visual->sortBias;
315 }
else if (atlas && atlasUV(*atlas, gid, u0, v0, u1, v1)) {
329 ++gLastVisibleTileCount;
336 gLastAtlasCount = int(atlases.size());
341 std::vector<graphics::DrawItem2D> items;
354 if (
dt > 0.f && std::isfinite(
dt)) gAnimationTimeMs += double(
dt) * 1000.0;
358 if (ecs::current()->getManager<TileLayer>() ==
nullptr)
return 0;
361 auto view = ecs::View<TileLayer, TileLayer::Config, TileLayer::Resource>();
362 for (
auto it =
view.begin(); it !=
view.end(); ++it) {
363 auto [cfg, res] = *it;
364 if (!res->autoReload || res->path.empty() || !cfg->entity)
continue;
366 const int64_t mt = fileModtime(res->path);
367 bool changed = mt >= 0 && mt != res->modtime;
369 const int64_t dependencyModtime = fileModtime(res->dependencyPaths[
index]);
370 const int64_t known =
index < res->dependencyModtimes.size() ? res->dependencyModtimes[
index] : -1;
371 changed = dependencyModtime >= 0 && dependencyModtime != known;
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
std::vector< WfcTile > tiles
std::map< Cell, int > best
std::vector< TileLayer::Tileset::Visual > visuals
std::vector< TileLayer::Tileset::Animation > animations
int getWidth() const
Returns the width.
int getHeight() const
Returns the height.
static void drawItems(Graphics &gfx, std::vector< DrawItem2D > &items, bool present)
Sort and draw items. If present=true, calls gfx.present() at the end. Map::render uses present=false ...
ECS tile layer entity. Script mutates tile GIDs / tileset / draw; TileRenderSystem batch-draws atlas ...
static int lastVisibleTileCount()
Number of non-empty visible tiles collected by the last pass.
static void collect(std::vector< graphics::DrawItem2D > &out)
Append non-empty visible tiles into a shared 2D draw queue.
static int lastVisitedCellCount()
Last visited cell count.
static int lastAtlasCount()
Number of distinct atlas textures referenced by the last pass.
static void render(graphics::Graphics *gfx)
Collect + draw tiles only (no present). Null gfx is a no-op.
static int lastCustomVisualCount()
Number of collected tiles using irregular per-GID metadata.
static void update(float dt)
Advance TileSet animation clocks once per map update.
static int lastVisitedChunkCount()
Spatial-index work counters for deterministic performance regressions.
放置世界:格子占用(多通道)+ 地形语义 + 已放置建筑实例。 行为由 PlacementSystem 提供;本类暴露便于脚本绑定的薄封装方法。 坐标换算统一走 eve::grid(支持 rectang...
bool reloadConfigFile(TileLayer *layer, std::string *error)
Reloads config file.
uint32_t tileGid(uint32_t raw)
Strip Tiled flip / rotate flags; keep low 28 bits.
void tileToWorld(const TileLayer::Config &cfg, int tx, int ty, float &wx, float &wy)
Converts a tile index to world pixels using the layer's projection.
float tileToDepthY(const TileLayer::Config &cfg, int tx, int ty)
Painter's-algorithm depth for the tile under the layer's projection.
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
glm::vec4 Color
Render-neutral RGBA color shared by graphics-facing modules.
Shared 2D draw queue item (sprites + tiles).
float camX
Screen-space camera already resolved; if cameraEntity set, RenderSystem resolves.
static constexpr int kChunkSize