aboutsummaryrefslogtreecommitdiff
path: root/mwe/resource-manager/tiledMap.cpp
blob: 836929cb95d84d868cde50db6063ce8ff9f12ae1 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "tiledMap.h"
#include "TextureMap.h"
#include "map_layer.h"
#include <SDL_render.h>
#include <cassert>
#include <iostream>
#include <ostream>
#include <string>
#include <tmxlite/Layer.hpp>
#include <tmxlite/Map.hpp>

TiledMap::TiledMap(const std::string & content) {
	if (m_TmxMap.loadFromString(content, "../../asset/tiled/")) {
		std::cout << "Map loaded correctly " << std::endl;
	}
}

TiledMap::~TiledMap() {
	for (const auto & r : m_MapTextures) {
		delete r;
	}
	m_MapTextures.clear();

	for (const auto & m : m_RenderLayers) {
		delete m;
	}
	m_RenderLayers.clear();
}

void TiledMap::SetRenderer(SDL_Renderer & renderer) {
	this->m_Renderer = &renderer;
	this->SetMapTextures();
	this->SetMapLayers();
}

void TiledMap::draw() const {
	for (const auto & l : m_RenderLayers) {
		l->draw(m_Renderer);
	}
}

void TiledMap::SetMapTextures() {
	const auto & tileSets = m_TmxMap.getTilesets();
	assert(~tileSets.empty());

	std::cout << "Processing SetMapTextures " << std::endl;

	for (const auto & ts : tileSets) {
		m_MapTextures.emplace_back(new TextureMap);
		if (!m_MapTextures.back()->loadFromFile(ts.getImagePath(),
												m_Renderer)) {
			std::cerr << "Failed opening " << ts.getImagePath() << "\n";
		}
	}
}

void TiledMap::SetMapLayers() {
	const auto & mapLayers = m_TmxMap.getLayers();

	std::cout << "Processing SetMapLayers " << std::endl;
	for (auto i = 0u; i < mapLayers.size(); ++i) {
		if (mapLayers[i]->getType() == tmx::Layer::Type::Tile) {
			m_RenderLayers.emplace_back(new MapLayer);
			m_RenderLayers.back()->create(this->m_TmxMap, i,
										  this->m_MapTextures);
		}
	}
}