aboutsummaryrefslogtreecommitdiff
path: root/resource-manager/map_asset.cpp
blob: ab19e595a7fa3a360b265afc12d1d481e7ef37be (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
#include "map_asset.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>


Map::Map(const std::string& path){
	if(m_Map.load(path)){
		std::cout << "Map loaded correctly " << std::endl;
	}
}

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

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


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

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

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

	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 Map::SetMapLayers(){
	const auto& mapLayers = m_Map.getLayers();

	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_Map, i, this->m_MapTextures);
		}
	}
}