| 47 | } |
| 48 | |
| 49 | static bool addTiles( |
| 50 | std::list<TileNode::TileQuad>& tileQuads, |
| 51 | const tmx::Map& map, |
| 52 | const std::vector<tmx::TileLayer::Tile>& tileIDs, |
| 53 | const tmx::Vector2u& mapSize, |
| 54 | const tmx::Vector2i& offset, |
| 55 | int totalRows, |
| 56 | const uint32_t vertColor, |
| 57 | std::vector<TileNode::AnimatedTile>& animatedTiles, |
| 58 | std::vector<TileNode::Animation>& animations) { |
| 59 | const auto& tileSets = map.getTilesets(); |
| 60 | const auto mapTileSize = map.getTileSize(); |
| 61 | for (auto i = 0u; i < tileSets.size(); ++i) { |
| 62 | const auto& ts = tileSets[i]; |
| 63 | auto texture = SharedTextureCache.load(ts.getImagePath()); |
| 64 | if (!texture) { |
| 65 | return false; |
| 66 | } |
| 67 | const auto paddingU = 0.5f / texture->getWidth(); |
| 68 | const auto paddingV = 0.5f / texture->getHeight(); |
| 69 | |
| 70 | TileNode::TileQuad* tileQuad = &tileQuads.emplace_back(); |
| 71 | tileQuad->texture = texture; |
| 72 | |
| 73 | for (auto y = 0u; y < mapSize.y; ++y) { |
| 74 | for (auto x = 0u; x < mapSize.x; ++x) { |
| 75 | const auto idx = y * mapSize.x + x; |
| 76 | if (idx >= tileIDs.size()) break; |
| 77 | const auto& tile = tileIDs[idx]; |
| 78 | const auto* tileInfo = ts.getTile(tile.ID); |
| 79 | if (!tileInfo) { |
| 80 | continue; |
| 81 | } |
| 82 | if (!tileInfo->imagePath.empty()) { |
| 83 | Warn("TMX file with embedded image is not supported."); |
| 84 | return false; |
| 85 | } |
| 86 | float u1 = s_cast<float>(tileInfo->imagePosition.x) / texture->getWidth(); |
| 87 | float v1 = s_cast<float>(tileInfo->imagePosition.y) / texture->getHeight(); |
| 88 | float u2 = s_cast<float>(tileInfo->imagePosition.x + tileInfo->imageSize.x) / texture->getWidth(); |
| 89 | float v2 = s_cast<float>(tileInfo->imagePosition.y + tileInfo->imageSize.y) / texture->getHeight(); |
| 90 | |
| 91 | const float tilePosX = s_cast<float>(x) * mapTileSize.x + offset.x; |
| 92 | const float tilePosY = (totalRows - 1 - s_cast<float>(y)) * mapTileSize.y - offset.y; |
| 93 | |
| 94 | const size_t MaxQuadsToSplit = 50; |
| 95 | if (tileQuad->quads.size() >= MaxQuadsToSplit) { |
| 96 | tileQuad = &tileQuads.emplace_back(); |
| 97 | tileQuad->texture = texture; |
| 98 | } |
| 99 | |
| 100 | tileQuad->quads.push_back( |
| 101 | {.rb = {0, 0, 0, 1, u2 - paddingU, v2 - paddingV, vertColor}, |
| 102 | .lb = {0, 0, 0, 1, u1 + paddingU, v2 - paddingV, vertColor}, |
| 103 | .lt = {0, 0, 0, 1, u1 + paddingU, v1 + paddingV, vertColor}, |
| 104 | .rt = {0, 0, 0, 1, u2 - paddingU, v1 + paddingV, vertColor}}); |
| 105 | |
| 106 | if (!tileInfo->animation.frames.empty()) { |
no test coverage detected