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
fix(tilemap): render diagonal tile flip (flipD) — fixes rotated Tiled tiles
yhh5abe189f0db1
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cppindex f3d9b100b..0acb619c8 100644--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp@@ -4,6 +4,7 @@#include "../BatchBuilder.hpp"#include "../../tilemap/TilemapSystem.hpp"+#include "../../tilemap/TileFlip.hpp"#include "../../ecs/components/Transform.hpp"#include "../../ecs/components/TilemapLayer.hpp"#include "../RenderFrame.hpp"@@ -54,6 +55,7 @@ void TilemapRenderPlugin::rebuildChunk(bool flipH = (rawTile & tilemap::TILE_FLIP_H) != 0;bool flipV = (rawTile & tilemap::TILE_FLIP_V) != 0;+ bool flipD = (rawTile & tilemap::TILE_FLIP_D) != 0;u32 tileIndex = tileId - 1;u32 tileCol = tileIndex % tilesetColumns;@@ -72,19 +74,31 @@ void TilemapRenderPlugin::rebuildChunk(worldY = originY - static_cast<f32>(ty) * layer.tile_height - hh;}- f32 u0 = static_cast<f32>(tileCol) * uvTileW;- f32 v0 = 1.0f - static_cast<f32>(tileRow + 1) * uvTileH;- f32 su = uvTileW;- f32 sv = uvTileH;-- if (flipH) { u0 += uvTileW; su = -su; }- if (flipV) { v0 += uvTileH; sv = -sv; }+ // Base tile UV rect (GL v increases upward, so vBottom < vTop).+ f32 uMin = static_cast<f32>(tileCol) * uvTileW;+ f32 uMax = uMin + uvTileW;+ f32 vBottom = 1.0f - static_cast<f32>(tileRow + 1) * uvTileH;+ f32 vTop = vBottom + uvTileH;++ // Map a corner's normalized (s,t) in {0,1}^2 to its texture UV, applying+ // the tile flip flags. Tiled order — diagonal (transpose) first, then H,+ // then V — so the standard 90°/270° combos (H|D, V|D) come out as true+ // rotations. H/V-only reduce to the previous behavior exactly.+ auto cornerUV = [&](f32 s, f32 t) -> glm::vec2 {+ tilemap::applyTileFlip(s, t, flipH, flipV, flipD);+ return glm::vec2{ uMin + s * (uMax - uMin), vBottom + t * (vTop - vBottom) };+ };++ glm::vec2 bl = cornerUV(0.0f, 0.0f);+ glm::vec2 br = cornerUV(1.0f, 0.0f);+ glm::vec2 tr = cornerUV(1.0f, 1.0f);+ glm::vec2 tl = cornerUV(0.0f, 1.0f);u32 baseVertex = static_cast<u32>(cache.vertices.size());- cache.vertices.push_back({ {worldX - hw, worldY - hh}, packedColor, {u0, v0} });- cache.vertices.push_back({ {worldX + hw, worldY - hh}, packedColor, {u0 + su, v0} });- cache.vertices.push_back({ {worldX + hw, worldY + hh}, packedColor, {u0 + su, v0 + sv} });- cache.vertices.push_back({ {worldX - hw, worldY + hh}, packedColor, {u0, v0 + sv} });+ cache.vertices.push_back({ {worldX - hw, worldY - hh}, packedColor, {bl.x, bl.y} });+ cache.vertices.push_back({ {worldX + hw, worldY - hh}, packedColor, {br.x, br.y} });+ cache.vertices.push_back({ {worldX + hw, worldY + hh}, packedColor, {tr.x, tr.y} });+ cache.vertices.push_back({ {worldX - hw, worldY + hh}, packedColor, {tl.x, tl.y} });for (u32 i = 0; i < 6; ++i) {cache.indices.push_back(baseVertex + BATCH_QUAD_INDICES[i]);diff --git a/src/esengine/tilemap/TileFlip.hpp b/src/esengine/tilemap/TileFlip.hppnew file mode 100644index 000000000..8013a1c9a--- /dev/null+++ b/src/esengine/tilemap/TileFlip.hpp@@ -0,0 +1,31 @@+// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file TileFlip.hpp+ * @brief Tiled tile-flip → UV transform (single source, dependency-free).+ */+#pragma once++namespace esengine {+namespace tilemap {++/**+ * Apply Tiled's tile-flip flags to a screen corner's normalized coordinate+ * (s,t) in [0,1]², yielding the texture-space (s,t) to sample.+ *+ * Order matters: diagonal (transpose) first, then horizontal, then vertical.+ * That is the order Tiled encodes, so the standard rotation combos render as+ * true rotations:+ * 90° CW = flipH | flipD+ * 180° = flipH | flipV+ * 270° CW = flipV | flipD+ * With flipD unset this reduces to plain H/V mirroring.+ */+inline void applyTileFlip(float& s, float& t, bool flipH, bool flipV, bool flipD) {+ if (flipD) { float tmp = s; s = t; t = tmp; }+ if (flipH) s = 1.0f - s;+ if (flipV) t = 1.0f - t;+}++} // namespace tilemap+} // namespace esenginediff --git a/tests/tilemap/test_tilemap.cpp b/tests/tilemap/test_tilemap.cppindex e743fb47e..e41238a4c 100644--- a/tests/tilemap/test_tilemap.cpp+++ b/tests/tilemap/test_tilemap.cpp@@ -2,6 +2,7 @@#include <doctest.h>#include <esengine/tilemap/TilemapSystem.hpp>+#include <esengine/tilemap/TileFlip.hpp>using namespace esengine;using namespace esengine::tilemap;@@ -192,3 +193,54 @@ TEST_CASE("tilemap_compute_visible_range") {CHECK_EQ(r.max_y, 3);}}++TEST_CASE("tilemap_tile_flip_uv") {+ // Screen corners as normalized (s,t): BL(0,0) BR(1,0) TR(1,1) TL(0,1).+ // applyTileFlip maps them to the texture coord to sample. Verify each of the+ // 8 Tiled orientations — diagonal (transpose) applied before H and V.+ auto uv = [](float s, float t, bool h, bool v, bool d) {+ applyTileFlip(s, t, h, v, d);+ return std::pair<float, float>{s, t};+ };+ using P = std::pair<float, float>;++ SUBCASE("identity") {+ CHECK(uv(0,0, false,false,false) == P{0,0});+ CHECK(uv(1,0, false,false,false) == P{1,0});+ CHECK(uv(1,1, false,false,false) == P{1,1});+ CHECK(uv(0,1, false,false,false) == P{0,1});+ }+ SUBCASE("flipH mirrors horizontally") {+ CHECK(uv(0,0, true,false,false) == P{1,0});+ CHECK(uv(1,0, true,false,false) == P{0,0});+ CHECK(uv(1,1, true,false,false) == P{0,1});+ CHECK(uv(0,1, true,false,false) == P{1,1});+ }+ SUBCASE("flipV mirrors vertically") {+ CHECK(uv(0,0, false,true,false) == P{0,1});+ CHECK(uv(1,1, false,true,false) == P{1,0});+ }+ SUBCASE("flipD transposes (BR<->TL texels)") {+ CHECK(uv(0,0, false,false,true) == P{0,0});+ CHECK(uv(1,0, false,false,true) == P{0,1});+ CHECK(uv(1,1, false,false,true) == P{1,1});+ CHECK(uv(0,1, false,false,true) == P{1,0});+ }+ SUBCASE("90 CW = flipH|flipD") {+ // top texture edge (texTL/texTR) must land on the screen's right edge.+ CHECK(uv(0,0, true,false,true) == P{1,0}); // BL -> texBR+ CHECK(uv(1,0, true,false,true) == P{1,1}); // BR -> texTR+ CHECK(uv(1,1, true,false,true) == P{0,1}); // TR -> texTL+ CHECK(uv(0,1, true,false,true) == P{0,0}); // TL -> texBL+ }+ SUBCASE("270 CW = flipV|flipD") {+ CHECK(uv(0,0, false,true,true) == P{0,1}); // BL -> texTL+ CHECK(uv(1,0, false,true,true) == P{0,0}); // BR -> texBL+ CHECK(uv(1,1, false,true,true) == P{1,0}); // TR -> texBR+ CHECK(uv(0,1, false,true,true) == P{1,1}); // TL -> texTR+ }+ SUBCASE("180 = flipH|flipV (point reflection)") {+ CHECK(uv(0,0, true,true,false) == P{1,1});+ CHECK(uv(1,1, true,true,false) == P{0,0});+ }+}