-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileLayer.cpp
74 lines (59 loc) · 2.14 KB
/
TileLayer.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
#include <iostream>
#include <string>
#include <sstream>
#include "TileLayer.h"
#include "Game.h"
bool TileLayer::Parse(XMLElement* root, int iLayer)
{
XMLElement* xmlElem = root->FirstChildElement("layer");
xmlElem = root->FirstChildElement("layer");
for (int i = 0; i < iLayer; ++i)
{
xmlElem = xmlElem->NextSiblingElement();
}
texture = TextureManager::Load(xmlElem->Attribute("texture"));
xmlElem->QueryIntAttribute("columns", &assetColumn);
xmlElem->QueryIntAttribute("width", &LayerWidth);
xmlElem->QueryIntAttribute("height", &LayerHeigth);
xmlElem->QueryIntAttribute("tilewidth", &tileW);
xmlElem->QueryIntAttribute("tileheight", &tileH);
if (assetColumn <= 0 || LayerWidth <= 0 || LayerHeigth <= 0 || tileW <= 0 || tileH <= 0)
throw std::string("Invalid TileLayer dimensions parameters \n");
XMLElement* xmlData = xmlElem->FirstChildElement("data");
if (!xmlData || !xmlData->GetText())
throw std::string("No data for map matrix \n");
std::string matrix(xmlData->GetText());
std::istringstream stream(matrix);
std::string value;
TileIdMap = new Uint16*[LayerHeigth];
for (int id = 0; id < LayerHeigth; ++id)
{
TileIdMap[id] = new Uint16[LayerWidth];
for (int jd = 0; jd < LayerWidth; ++jd)
{
std::getline(stream, value, ',');
std::stringstream convertor(value);
convertor >> TileIdMap[id][jd];
if (!stream.good()) break;
}
}
return true;
}
void TileLayer::Draw(const SDL_Point* CameraTranslate)
{
int row, column, Heigth = Game::ScreenHeigth() / tileH, Width = Game::ScreenWidth() / tileW;
int id = CameraTranslate->y / tileH;
Heigth = ((Heigth + id + 2) > LayerHeigth) ? (LayerHeigth) : (Heigth + id + 2);
for (id; id < Heigth; ++id)
{
int jd = CameraTranslate->x / tileW -1;
int Width_d = ((Width + jd +2) > LayerWidth) ? (LayerWidth) : (Width + jd + 2);
for (jd; jd < Width_d; ++jd)
{
if (TileIdMap[id][jd] == 0) continue;
column = TileIdMap[id][jd] % assetColumn -1;
row = TileIdMap[id][jd] / assetColumn;
TextureManager::Draw(texture, { tileW * column, tileH * row, tileW, tileH }, { offSetX + jd * tileW, offSetY + id * tileH, tileW, tileH }, SDL_FLIP_NONE, CameraTranslate);
}
}
}