载入中...
搜索中...
未找到
TileSystem.cpp
浏览该文件的文档.
1#include "map/TileSystem.h"
2#include "common/Module.h"
3#include "common/Profile.h"
5#include "graphics/Canvas.h"
6#include "graphics/Graphics.h"
8#include "graphics/Texture.h"
9#include "map/TileConfig.h"
10#include "map/TileLayer.h"
11#include "map/TileProjection.h"
12
13#include <unordered_set>
14#include <vector>
15#include <algorithm>
16#include <cmath>
17
18namespace eve::map {
19namespace {
20
21int gLastVisibleTileCount = 0;
22int gLastCustomVisualCount = 0;
23int gLastAtlasCount = 0;
24int gLastVisitedChunkCount = 0;
25int gLastVisitedCellCount = 0;
26double gAnimationTimeMs = 0.0;
27
28struct ViewCam {
29 float x = 0.f;
30 float y = 0.f;
31 float zoom = 1.f;
32 bool valid = false;
33};
34
35ViewCam fromEntity(graphics::Camera2D *ent) {
36 ViewCam v;
37 if (!ent) return v;
38 auto d = ent->data();
39 v.valid = true;
40 v.x = d->x;
41 v.y = d->y;
42 v.zoom = d->zoom;
43 return v;
44}
45
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;
52 return Color{r * tint.r, g * tint.g, b * tint.b, tint.a};
53}
54
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;
59 }
60 return best;
61}
62
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;
72
73 const float px = float(atlas.margin + col * (atlas.tileW + atlas.spacing));
74 const float py = float(atlas.margin + row * (atlas.tileH + atlas.spacing));
75 u0 = px / iw;
76 v0 = py / ih;
77 u1 = (px + float(atlas.tileW)) / iw;
78 v1 = (py + float(atlas.tileH)) / ih;
79 return true;
80}
81
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;
85 return nullptr;
86}
87
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;
98 return true;
99}
100
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;
111 return true;
112}
113
114const TileLayer::Tileset::Visual::SubtileFrame *subtileFrame(const TileLayer::Tileset::Visual &visual) {
115 if (visual.subtileFrames.empty()) return nullptr;
116 int total = 0;
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) {
120 cursor -= std::max(1, frame.durationMs);
121 if (cursor < 0) return &frame;
122 }
123 return &visual.subtileFrames.back();
124}
125
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;
131 return info.modtime;
132}
133
134uint32_t animatedGid(const TileLayer::Tileset &ts, uint32_t gid) {
135 for (const auto &animation : ts.animations) {
136 if (animation.gid != int(gid) || animation.frames.empty()) continue;
137 int total = 0;
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) {
142 cursor -= std::max(1, frame.durationMs);
143 if (cursor < 0) return uint32_t(frame.gid);
144 }
145 }
146 return gid;
147}
148
149void applyTiledTransform(graphics::DrawItem2D &item, uint32_t raw) {
150 const bool horizontal = (raw & 0x80000000u) != 0;
151 const bool vertical = (raw & 0x40000000u) != 0;
152 const bool diagonal = (raw & 0x20000000u) != 0;
153 if (!diagonal) {
154 item.flipX = horizontal;
155 item.flipY = vertical;
156 return;
157 }
158 item.rotation = horizontal ? 90.f : 270.f;
159 item.flipX = horizontal == vertical;
160 item.flipY = false;
161}
162
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,
165 float scaleY) {
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)};
170 for (int y : ys) {
171 for (int x : xs) {
172 float wx = 0.f, wy = 0.f;
173 tileToWorld(cfg, x, y, wx, wy);
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);
178 }
179 }
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);
185 }
186 minX -= expandX;
187 minY -= expandY;
188 maxX += expandX;
189 maxY += expandY;
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;
196}
197
198} // namespace
199
200void TileRenderSystem::collect(std::vector<graphics::DrawItem2D> &out) { collect(out, 0, 0); }
201
202void TileRenderSystem::collect(std::vector<graphics::DrawItem2D> &out, int viewWidth,
203 int viewHeight) {
204 gLastVisibleTileCount = 0;
205 gLastCustomVisualCount = 0;
206 gLastAtlasCount = 0;
207 gLastVisitedChunkCount = 0;
208 gLastVisitedCellCount = 0;
209 if (ecs::current()->getManager<TileLayer>() == nullptr) return;
210
211 std::unordered_set<graphics::Texture *> atlases;
212
215 for (auto it = view.begin(); it != view.end(); ++it) {
216 auto [cfg, tiles, ts, draw] = *it;
217 if (!draw->visible || cfg->mapW <= 0 || cfg->mapH <= 0) continue;
218 if (tiles->gids.size() < size_t(cfg->mapW * cfg->mapH)) continue;
219
220 const Color &tint = draw->tint;
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;
224
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))
233 continue;
234 const int x0 = cx * TileLayer::Tiles::kChunkSize;
235 const int y0 = cy * TileLayer::Tiles::kChunkSize;
236 const int x1 = std::min(cfg->mapW, x0 + TileLayer::Tiles::kChunkSize);
237 const int y1 = std::min(cfg->mapH, y0 + TileLayer::Tiles::kChunkSize);
238 if (!chunkVisible(*cfg, *ts, cam, viewWidth, viewHeight, x0, y0, x1, y1, scaleX,
239 scaleY))
240 continue;
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;
248
250 tileToWorld(*cfg, tx, ty, item.x, item.y);
251 item.w = cfg->tileW * scaleX;
252 item.h = cfg->tileH * scaleY;
253 item.depthY = tileToDepthY(*cfg, tx, ty);
254 item.layer = draw->layer;
255 item.canvas = draw->canvas;
256 item.camera = draw->camera;
257 item.camValid = cam.valid;
258 item.camX = cam.x;
259 item.camY = cam.y;
260 item.camZoom = cam.zoom;
261 item.receiveLight = draw->receiveLight;
262 item.litPath = false;
263 applyTiledTransform(item, raw);
264
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) {
271 graphics::DrawItem2D quarter = item;
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;
288 quarter.u0 = u0;
289 quarter.v0 = v0;
290 quarter.u1 = u1;
291 quarter.v1 = v1;
292 quarter.color = tint;
293 quarter.litPath = quarter.receiveLight && quarter.texture != nullptr;
294 out.push_back(quarter);
295 ++gLastCustomVisualCount;
296 }
297 ++gLastVisibleTileCount;
298 if (atlas->texture) atlases.insert(atlas->texture);
299 continue;
300 }
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;
308 item.texture = atlas->texture;
309 item.hasUV = true;
310 item.u0 = u0;
311 item.v0 = v0;
312 item.u1 = u1;
313 item.v1 = v1;
314 item.color = tint;
315 } else if (atlas && atlasUV(*atlas, gid, u0, v0, u1, v1)) {
316 item.texture = atlas->texture;
317 item.hasUV = true;
318 item.u0 = u0;
319 item.v0 = v0;
320 item.u1 = u1;
321 item.v1 = v1;
322 item.color = tint;
323 } else {
324 item.texture = nullptr;
325 item.color = solidForGid(gid, tint);
326 }
327 item.litPath = item.receiveLight && item.texture != nullptr;
328 out.push_back(item);
329 ++gLastVisibleTileCount;
330 if (item.texture) atlases.insert(item.texture);
331 }
332 }
333 }
334 }
335 }
336 gLastAtlasCount = int(atlases.size());
337}
338
340 if (!gfx) return;
341 std::vector<graphics::DrawItem2D> items;
342 collect(items, gfx->getWidth(), gfx->getHeight());
343 graphics::RenderSystem::drawItems(*gfx, items, false);
344}
345
346int TileRenderSystem::lastVisibleTileCount() { return gLastVisibleTileCount; }
347int TileRenderSystem::lastCustomVisualCount() { return gLastCustomVisualCount; }
348int TileRenderSystem::lastAtlasCount() { return gLastAtlasCount; }
349int TileRenderSystem::lastVisitedChunkCount() { return gLastVisitedChunkCount; }
350int TileRenderSystem::lastVisitedCellCount() { return gLastVisitedCellCount; }
351
353 EV_PROFILE_MODULE("map", "TileRenderSystem::update");
354 if (dt > 0.f && std::isfinite(dt)) gAnimationTimeMs += double(dt) * 1000.0;
355}
356
358 if (ecs::current()->getManager<TileLayer>() == nullptr) return 0;
359
360 int reloaded = 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;
365
366 const int64_t mt = fileModtime(res->path);
367 bool changed = mt >= 0 && mt != res->modtime;
368 for (size_t index = 0; !changed && index < res->dependencyPaths.size(); ++index) {
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;
372 }
373 if (!changed) continue;
374 if (reloadConfigFile(cfg->entity, nullptr)) ++reloaded;
375 }
376 return reloaded;
377}
378
379} // namespace eve::map
double y
double x
std::size_t cursor
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
bool changed
double raw
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec4 tint
double r
float v
HexVec3 left
HexVec3 right
int h
std::vector< Colorf > px
std::string local
bool valid
MeleePoint3 b
Definition MeleeHit.cpp:41
MeshVfxBatchedDraw draw
ssq::Object frame
double dt
OceanSprayView view
bool diagonal
std::string path
Definition PlayHost.cpp:111
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
float d
float zoom
std::vector< WfcTile > tiles
ScriptStructure out
std::map< Cell, int > best
std::vector< TileLayer::Tileset::Visual > visuals
std::vector< TileLayer::Tileset::Animation > animations
uint32_t index
float bottom
int getWidth() const
Returns the width.
Definition Graphics.h:489
int getHeight() const
Returns the height.
Definition Graphics.h:491
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 ...
static int poll()
Polls .
ECS tile layer entity. Script mutates tile GIDs / tileset / draw; TileRenderSystem batch-draws atlas ...
Definition TileLayer.h:33
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.
SQInteger top
Definition Snapshot.cpp:115
放置世界:格子占用(多通道)+ 地形语义 + 已放置建筑实例。 行为由 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.
Definition TileLayer.h:392
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.
Definition Widget.cpp:870
glm::vec4 Color
Render-neutral RGBA color shared by graphics-facing modules.
Definition RenderTypes.h:8
Shared 2D draw queue item (sprites + tiles).
Definition DrawItem2D.h:19
float camX
Screen-space camera already resolved; if cameraEntity set, RenderSystem resolves.
Definition DrawItem2D.h:55
Config public API.
Definition TileLayer.h:50
static constexpr int kChunkSize
Definition TileLayer.h:72
glm::uvec4 info