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