-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMXParser.cpp
115 lines (96 loc) · 4.76 KB
/
TMXParser.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "TMXParser.h"
#include <cstdlib>
#include <iostream>
#include <sstream>
namespace TMX {
Parser::Parser( const char* filename )
{
load( filename );
}
Parser::Parser()
{
}
Parser::~Parser()
{
}
bool Parser::load( const char* filename )
{
std::string version = VERSION;
rapidxml::xml_node<>* root_node;
rapidxml::xml_document<> doc;
rapidxml::file<> file( filename );
doc.parse<0>( file.data() );
//get root nodes
root_node = doc.first_node( "map" );
//load map element
if( root_node->first_attribute( "version" )->value() != version ) {
std::cout << "ERROR: Map version mismatch. Required version: " << VERSION << "." << std::endl;
std::cout << "LO QUE SE HA ENCONTRAO: " << root_node->first_attribute( "version" )->value() << "." << std::endl;
return false;
}
mapInfo.version = root_node->first_attribute( "version" )->value();
std::cout << "Version: " << mapInfo.version << std::endl;
mapInfo.orientation = root_node->first_attribute( "orientation" )->value();
std::cout << "Orientation: " << mapInfo.orientation << std::endl;
mapInfo.width = std::atoi( root_node->first_attribute( "width" )->value() );
std::cout << "Width: " << mapInfo.width << std::endl;
mapInfo.height = std::atoi( root_node->first_attribute( "height" )->value() );
std::cout << "Height: " << mapInfo.height << std::endl;
mapInfo.tileWidth = std::atoi( root_node->first_attribute( "tilewidth" )->value() );
std::cout << "Tile Width: " << mapInfo.tileWidth << std::endl;
mapInfo.tileHeight = std::atoi( root_node->first_attribute( "tileheight" )->value() );
std::cout << "Tile Height: " << mapInfo.tileHeight << std::endl;
if( root_node->first_attribute( "backgroundcolor" )->value() != 0 )
{
mapInfo.backgroundColor = root_node->first_attribute( "backgroundcolor" )->value();
std::cout << "Background Color: " << mapInfo.backgroundColor << std::endl;
}
for (rapidxml::xml_node<>* tileset_node = root_node->first_node("tileset"); tileset_node; tileset_node = tileset_node->next_sibling("tileset") ) /// para cada uno de los tileset en el archivo
{
Tileset tmpTileset;
tmpTileset.firstGID = std::atoi(tileset_node->first_attribute("firstgid")->value());
tmpTileset.source = tileset_node->first_node("image")->first_attribute("source")->value();
tmpTileset.columns = std::atoi(tileset_node->first_attribute("columns")->value());
tmpTileset.tileHeight = std::atoi(tileset_node->first_attribute("tileheight")->value());
tmpTileset.tileWidth = std::atoi(tileset_node->first_attribute("tilewidth")->value());
tmpTileset.spacing = std::atoi(tileset_node->first_attribute("spacing")->value());
tmpTileset.tileCount = std::atoi(tileset_node->first_attribute("tilecount")->value());
std::cout << "Texture source(s) path: " << tmpTileset.source << std::endl;
tilesetList.push_back(tmpTileset);
}
for (rapidxml::xml_node<>* layer_node = root_node->first_node("layer"); layer_node; layer_node = layer_node->next_sibling("layer"))
{
TileLayer layer;
layer.name = layer_node->first_attribute("name")->value();
layer.id = std::atoi(layer_node->first_attribute("id")->value());
layer.width = std::atoi(layer_node->first_attribute("width")->value());
layer.height = std::atoi(layer_node->first_attribute("height")->value());
std::cout << "Layer " << layer.id << " correctly loaded. Size: " << layer.width << "x" << layer.height << std::endl;
ObjectGroup tmpObjectGroup;
std::vector<Object> objectRow;
int flag = 0;
for (rapidxml::xml_node<>* tile_node = layer_node->first_node("data")->first_node("tile"); tile_node; tile_node = tile_node->next_sibling("tile"))
{
if(flag < layer.width)
{
Object tmpObject;
tmpObject.gid = std::atoi(tile_node->first_attribute("gid")->value());
objectRow.push_back(tmpObject);
flag++;
//std::cout << "Cargando tile con gid: " << tmpObject.gid << std::endl;
}
else
{
tmpObjectGroup.objects.push_back(objectRow); /// la almacenamos en el coonjunto de objetos
objectRow.clear(); /// vaciamos la variable auxiliar
flag = 0;
}
}
// una vez tenemos todos los objetos de una capa cargador: los pasamos a la variable
objectGroup[layer.name] = tmpObjectGroup;
std::cout << "Tiles from layer " << layer.name << " initialized" << std::endl;
}
std::cout << "Test of access on a position of object matrix: " << objectGroup["Capa1"].objects[4][4].gid << std::endl;
return true;
}
}