-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilemapbuilder.cpp
127 lines (106 loc) · 3.68 KB
/
tilemapbuilder.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
116
117
118
119
120
121
122
123
124
125
126
127
#include "tilemapbuilder.h"
#include <fstream>
#include <algorithm>
#include <iostream>
std::vector<std::vector<int>> TilemapBuilder::ReadCSV(const std::string& filename)
{
std::ifstream file(filename);
int columnCount = 0;
int rowCount = 0;
std::string line;
char delimiter = ',';
// Get the max number of columns and rows.
while (std::getline(file, line, '\n'))
{
columnCount = std::max(columnCount, (int) (std::count(line.begin(), line.end(), delimiter) + 1));
rowCount++;
}
// Initialize all values in the 2D vector to -1
std::vector<std::vector<int>> vec(rowCount, std::vector<int>(columnCount, -1));
// return to beginning of file to populate 2D vector
file.clear();
file.seekg(0);
columnCount = 0;
rowCount = 0;
std::string token;
std::string::iterator stringIterator;
int subStart;
int subEnd;
while (std::getline(file, line, '\n'))
{
subStart = 0;
subEnd = 0;
stringIterator = line.begin();
while (stringIterator != line.end())
{
if (*stringIterator == ',')
{
// If the cell has a value, put it in the 2D vector. Otherwise leave as -1.
if (subEnd > 0)
{
token = line.substr(subStart, subEnd);
vec[rowCount][columnCount] = std::stoi(token);
}
subStart += subEnd + 1;
subEnd = 0;
stringIterator++;
columnCount++;
}
else
{
stringIterator++;
subEnd++;
}
if (stringIterator == line.end())
{
if (subEnd > 0)
{
token = line.substr(subStart, subEnd);
vec[rowCount][columnCount] = std::stoi(token);
}
}
}
columnCount = 0;
rowCount++;
}
return vec;
}
Magick::Image TilemapBuilder::createTiledImage(std::vector<std::vector<int>> csvVec, std::string tilemapFile, int tileWidth)
{
Magick::Image tilemap(tilemapFile);
int numTileColumns = tilemap.size().width() / tileWidth;
if (numTileColumns < 1)
throw std::range_error("Tile width is too large.");
std::vector<Magick::Image> rowImages;
for (int i = 0; i < csvVec.size(); ++i)
{
std::vector<Magick::Image> tiledImages;
for (int j = 0; j < csvVec[i].size(); ++j)
{
int tileIndex = csvVec[i][j];
if (tileIndex == -1)
{
Magick::Image transparentSquare(Magick::Geometry(tileWidth, tileWidth), Magick::Color(0, 0, 0, 0));
tiledImages.push_back(transparentSquare);
}
else
{
int xOffset = tileIndex % numTileColumns;
int yOffset = tileIndex / numTileColumns;
int xCropPos = tileWidth * xOffset;
int yCropPos = tileWidth * yOffset;
Magick::Image tile = tilemap;
if (xCropPos > tile.size().width() || yCropPos > tile.size().height())
throw std::range_error("CSV file contains out-of-range tile.");
tile.crop(Magick::Geometry(tileWidth, tileWidth, xCropPos, yCropPos));
tiledImages.push_back(tile);
}
}
Magick::Image appendedRow;
Magick::appendImages(&appendedRow, tiledImages.begin(), tiledImages.end());
rowImages.push_back(appendedRow);
}
Magick::Image appendedFinal;
Magick::appendImages(&appendedFinal, rowImages.begin(), rowImages.end(), true);
return appendedFinal;
}