-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.cpp
More file actions
286 lines (239 loc) · 8.42 KB
/
level.cpp
File metadata and controls
286 lines (239 loc) · 8.42 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "level.h"
#include <iostream>
#include "tinyxml.h"
int Object::GetPropertyInt(std::string name)
{
return atoi(properties[name].c_str());
}
float Object::GetPropertyFloat(std::string name)
{
return strtod(properties[name].c_str(), NULL);
}
std::string Object::GetPropertyString(std::string name)
{
return properties[name];
}
bool Level::LoadFromFile(std::string filename)
{
TiXmlDocument levelFile(filename.c_str());
// Çàãðóæàåì XML-êàðòó
if(!levelFile.LoadFile())
{
std::cout << "Loading level \"" << filename << "\" failed." << std::endl;
return false;
}
// Ðàáîòàåì ñ êîíòåéíåðîì map
TiXmlElement *map;
map = levelFile.FirstChildElement("map");
// Ïðèìåð êàðòû: <map version="1.0" orientation="orthogonal"
// width="10" height="10" tilewidth="34" tileheight="34">
width = atoi(map->Attribute("width"));
height = atoi(map->Attribute("height"));
tileWidth = atoi(map->Attribute("tilewidth"));
tileHeight = atoi(map->Attribute("tileheight"));
// Áåðåì îïèñàíèå òàéëñåòà è èäåíòèôèêàòîð ïåðâîãî òàéëà
TiXmlElement *tilesetElement;
tilesetElement = map->FirstChildElement("tileset");
firstTileID = atoi(tilesetElement->Attribute("firstgid"));
// source - ïóòü äî êàðòèíêè â êîíòåéíåðå image
TiXmlElement *image;
image = tilesetElement->FirstChildElement("image");
this->imagepath = image->Attribute("source");
// Ïîëó÷àåì êîëè÷åñòâî ñòîëáöîâ è ñòðîê òàéëñåòà
image_width = atoi(image->Attribute("width"));
image_height = atoi(image->Attribute("height"));
int columns = image_width / tileWidth;
int rows = image_height / tileHeight;
// Âåêòîð èç ïðÿìîóãîëüíèêîâ èçîáðàæåíèé (TextureRect)
std::vector<SDL_Rect> subRects;
for(int y = 0; y < rows; y++)
for(int x = 0; x < columns; x++)
{
SDL_Rect rect;
rect.y = y * tileHeight;
rect.h = tileHeight;
rect.x = x * tileWidth;
rect.w = tileWidth;
subRects.push_back(rect);
}
// Ðàáîòà ñî ñëîÿìè
TiXmlElement *layerElement;
layerElement = map->FirstChildElement("layer");
while(layerElement)
{
Layer layer;
// Åñëè ïðèñóòñòâóåò opacity, òî çàäàåì ïðîçðà÷íîñòü ñëîÿ, èíà÷å îí ïîëíîñòüþ íåïðîçðà÷åí
if (layerElement->Attribute("opacity") != NULL)
{
float opacity = strtod(layerElement->Attribute("opacity"), NULL);
layer.opacity = int(255 * opacity);
}
else
{
layer.opacity = 255;
}
// Êîíòåéíåð <data>
TiXmlElement *layerDataElement;
layerDataElement = layerElement->FirstChildElement("data");
if(layerDataElement == NULL)
{
std::cout << "Bad map. No layer information found." << std::endl;
}
// Êîíòåéíåð <tile> - îïèñàíèå òàéëîâ êàæäîãî ñëîÿ
TiXmlElement *tileElement;
tileElement = layerDataElement->FirstChildElement("tile");
if(tileElement == NULL)
{
std::cout << "Bad map. No tile information found." << std::endl;
return false;
}
int x = 0;
int y = 0;
while(tileElement)
{
int tileGID = atoi(tileElement->Attribute("gid"));
int subRectToUse = tileGID - firstTileID;
// Óñòàíàâëèâàåì TextureRect êàæäîãî òàéëà
if (subRectToUse >= 0)
{
Sprite sprite;
sprite.setSurfaceSrcRect(subRects[subRectToUse], image_width, image_height);
SDL_Rect tmp_DstRect;
tmp_DstRect.x = x*tileWidth;
tmp_DstRect.y = y*tileWidth;
tmp_DstRect.h = tileHeight;
tmp_DstRect.w = tileWidth;
sprite.setSurfaceDstRect(tmp_DstRect);
sprite.setOpacuty(layer.opacity);
layer.tiles.push_back(sprite);
}
tileElement = tileElement->NextSiblingElement("tile");
x++;
if (x >= width)
{
x = 0;
y++;
if(y >= height)
y = 0;
}
}
layers.push_back(layer);
layerElement = layerElement->NextSiblingElement("layer");
}
// Ðàáîòà ñ îáúåêòàìè
TiXmlElement *objectGroupElement;
// Åñëè åñòü ñëîè îáúåêòîâ
if (map->FirstChildElement("objectgroup") != NULL)
{
objectGroupElement = map->FirstChildElement("objectgroup");
while (objectGroupElement)
{
// Êîíòåéíåð <object>
TiXmlElement *objectElement;
objectElement = objectGroupElement->FirstChildElement("object");
while(objectElement)
{
// Ïîëó÷àåì âñå äàííûå - òèï, èìÿ, ïîçèöèÿ, etc
std::string objectType;
if (objectElement->Attribute("type") != NULL)
{
objectType = objectElement->Attribute("type");
}
std::string objectName;
if (objectElement->Attribute("name") != NULL)
{
objectName = objectElement->Attribute("name");
}
int x = atoi(objectElement->Attribute("x"));
int y = atoi(objectElement->Attribute("y"));
int width, height;
Sprite sprite;
SDL_Rect zeroRect;
memset(&zeroRect, 0, sizeof(zeroRect));
sprite.setSurfaceSrcRect(zeroRect, image_width, image_height);
sprite.setPosition(x, y);
zeroRect.h = tileHeight; zeroRect.w = tileWidth;
sprite.setSurfaceDstRect(zeroRect);
if (objectElement->Attribute("width") != NULL)
{
width = atoi(objectElement->Attribute("width"));
height = atoi(objectElement->Attribute("height"));
}
else
{
width = subRects[atoi(objectElement->Attribute("gid")) - firstTileID].w;
height = subRects[atoi(objectElement->Attribute("gid")) - firstTileID].h;
sprite.setSurfaceSrcRect(subRects[atoi(objectElement->Attribute("gid")) - firstTileID], image_width, image_height);
}
// Ýêçåìïëÿð îáúåêòà
Object object;
object.name = objectName;
object.type = objectType;
object.sprite = sprite;
SDL_Rect objectRect;
objectRect.y = y;
objectRect.x = x;
objectRect.h = height;
objectRect.w = width;
object.rect = objectRect;
// "Ïåðåìåííûå" îáúåêòà
TiXmlElement *properties;
properties = objectElement->FirstChildElement("properties");
if (properties != NULL)
{
TiXmlElement *prop;
prop = properties->FirstChildElement("property");
if (prop != NULL)
{
while(prop)
{
std::string propertyName = prop->Attribute("name");
std::string propertyValue = prop->Attribute("value");
object.properties[propertyName] = propertyValue;
prop = prop->NextSiblingElement("property");
}
}
}
// Ïèõàåì îáúåêò â âåêòîð
objects.push_back(object);
objectElement = objectElement->NextSiblingElement("object");
}
objectGroupElement = objectGroupElement->NextSiblingElement("objectgroup");
}
}
else
{
std::cout << "No object layers found..." << std::endl;
}
return true;
}
Object Level::GetFirstObject(std::string name)
{
// Òîëüêî ïåðâûé îáúåêò ñ çàäàííûì èìåíåì
for (unsigned i = 0; i < objects.size(); i++)
if (objects[i].name == name)
return objects[i];
}
std::vector<Object> Level::GetObjects(std::string name)
{
// Âñå îáúåêòû ñ çàäàííûì èìåíåì
std::vector<Object> vec;
for(unsigned i = 0; i < objects.size(); i++)
if(objects[i].name == name)
vec.push_back(objects[i]);
return vec;
}
SDL_Rect Level::GetTileSize() const {
SDL_Rect res;
res.x = tileWidth;
res.y = tileHeight;
return res;
}
SDL_Rect Level::GetMapSize() const {
SDL_Rect res;
res.x = width;
res.y = height;
return res;
}
Level::~Level() {
}