diff options
Diffstat (limited to 'src/map.cpp')
| -rw-r--r-- | src/map.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
| diff --git a/src/map.cpp b/src/map.cpp new file mode 100644 index 0000000..a60cbba --- /dev/null +++ b/src/map.cpp | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | #include "map.h" | ||
| 2 | #include <tmxlite/Map.hpp> | ||
| 3 | #include <tmxlite/Layer.hpp> | ||
| 4 | #include <tmxlite/TileLayer.hpp> | ||
| 5 | #include <tmxlite/Tileset.hpp> | ||
| 6 | #include "renderer.h" | ||
| 7 | |||
| 8 | Map::Map(std::string_view filename, Renderer& renderer) { | ||
| 9 | tmx::Map mapfile; | ||
| 10 | if (!mapfile.load(filename.data())) { | ||
| 11 | throw std::invalid_argument("Could not find map file: " + std::string(filename)); | ||
| 12 | } | ||
| 13 | |||
| 14 | const tmx::Vector2u& mapSize = mapfile.getTileCount(); | ||
| 15 | mapSize_.x() = mapSize.x; | ||
| 16 | mapSize_.y() = mapSize.y; | ||
| 17 | |||
| 18 | const tmx::Vector2u& tileSize = mapfile.getTileSize(); | ||
| 19 | tileSize_.x() = tileSize.x; | ||
| 20 | tileSize_.y() = tileSize.y; | ||
| 21 | |||
| 22 | int firstGID = 0; | ||
| 23 | // There should only be one tileset. | ||
| 24 | for (const tmx::Tileset& tileset : mapfile.getTilesets()) { | ||
| 25 | firstGID = tileset.getFirstGID(); | ||
| 26 | tilesetTextureId_ = renderer.loadImageFromFile(tileset.getImagePath()); | ||
| 27 | tilesetColumns_ = tileset.getColumnCount(); | ||
| 28 | } | ||
| 29 | |||
| 30 | for (const auto& layer : mapfile.getLayers()) { | ||
| 31 | if (layer->getType() == tmx::Layer::Type::Tile) { | ||
| 32 | const auto& tileLayer = layer->getLayerAs<tmx::TileLayer>(); | ||
| 33 | |||
| 34 | std::vector<Tile> tilesToStore; | ||
| 35 | |||
| 36 | for (const auto& maptile : tileLayer.getTiles()) { | ||
| 37 | tilesToStore.push_back(Tile { | ||
| 38 | .id = maptile.ID - firstGID, | ||
| 39 | .flipHorizontal = (maptile.flipFlags & tmx::TileLayer::Horizontal) != 0, | ||
| 40 | .flipVertical = (maptile.flipFlags & tmx::TileLayer::Vertical) != 0 }); | ||
| 41 | } | ||
| 42 | |||
| 43 | layers_.push_back(std::move(tilesToStore)); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
