-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevels.cpp
196 lines (147 loc) · 6.53 KB
/
levels.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
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
#include "levels.h"
sf::Image Levels::loadImage() {
sf::Image image;
if (!image.loadFromFile("img/test.png")) {
std::cout << "Failed to load image \n";
}
return image;
}
sf::Vector2f Levels::getImageDimensions(sf::Image image) {
sf::Vector2f imageDimension;
imageDimension.x = image.getSize().x;
//std::cout << imageDimension.x << "\n";
imageDimension.y = image.getSize().y;
//std::cout << imageDimension.y << "\n";
return imageDimension;
}
std::vector<std::vector<int>> Levels::image2Grid() {
sf::Image image = loadImage();
sf::Vector2f imageDimension = getImageDimensions(image);
float cellWidth = imageDimension.x / static_cast<float>(gridWidth);
float cellHeight = imageDimension.y / static_cast<float>(gridHeight);
std::vector<std::vector<int>> resultGrid(gridHeight, std::vector<int>(gridWidth));
for (int row = 0; row < gridHeight; ++row) {
for (int col = 0; col < gridWidth; ++col) {
sf::Color cellColor; // Store the RGB color code for non-transparent cells
// Calculate the pixel range for the current cell
int startX = col * cellWidth;
int startY = row * cellHeight;
int endX = static_cast<int>((col + 1) * cellWidth);
int endY = static_cast<int>((row + 1) * cellHeight);
bool hasColoredPixel = false;
// Check each pixel within the current cell range
for (int y = startY; y < endY; ++y) {
for (int x = startX; x < endX; ++x) {
sf::Color pixelColor = image.getPixel(x, y);
// Check if the pixel color is not transparent (alpha > 0)
if (pixelColor.a > 0) {
hasColoredPixel = true;
cellColor = sf::Color(pixelColor.r, pixelColor.g, pixelColor.b);
break;
}
}
if (hasColoredPixel) {
break;
}
}
// Store either the RGB code or 0 for transparent cells in the result grid
if (hasColoredPixel) {
resultGrid[row][col] = cellColor.toInteger(); // Store RGB code
} else {
resultGrid[row][col] = 0; // Transparent cell
}
}
}
return resultGrid;
}
std::vector<std::vector<sf::Color>> Levels::grid2shapes() {
sf::Image image = loadImage();
sf::Vector2f imageDimension = getImageDimensions(image);
float cellWidth = imageDimension.x / static_cast<float>(gridWidth);
float cellHeight = imageDimension.y / static_cast<float>(gridHeight);
std::vector<std::vector<sf::Color>> resultGrid(gridHeight, std::vector<sf::Color>(gridWidth));
for (int row = 0; row < gridHeight; ++row) {
for (int col = 0; col < gridWidth; ++col) {
sf::Color cellColor; // Store the RGB color code for non-transparent cells
// Calculate the pixel range for the current cell
int startX = col * cellWidth;
int startY = row * cellHeight;
int endX = static_cast<int>((col + 1) * cellWidth);
int endY = static_cast<int>((row + 1) * cellHeight);
bool hasColoredPixel = false;
// Check each pixel within the current cell range
for (int y = startY; y < endY; ++y) {
for (int x = startX; x < endX; ++x) {
sf::Color pixelColor = image.getPixel(x, y);
// Check if the pixel color is not transparent (alpha > 0)
if (pixelColor.a == 255) {
hasColoredPixel = true;
cellColor = pixelColor;
break;
}
}
if (hasColoredPixel) {
break;
}
}
// Store either the RGB code or transparent value in the result grid
if (hasColoredPixel) {
resultGrid[row][col] = cellColor; // Store RGB color
} else {
resultGrid[row][col] = sf::Color::Transparent; // Transparent cell
}
}
}
return resultGrid;
}
std::vector<sf::RectangleShape> Levels::createLevel() {
std::vector<std::vector<sf::Color>> resultGrid = grid2shapes();
std::vector<sf::RectangleShape> level;
for (int row = 0; row < gridHeight; ++row) {
for (int col = 0; col < gridWidth; ++col) {
if (resultGrid[row][col] != sf::Color::Transparent) {
// Find the adjacent cells of the same color horizontally
int startCol = col;
int endCol = col;
while (endCol < gridWidth && resultGrid[row][endCol] == resultGrid[row][col]) {
++endCol;
}
// Find the adjacent cells of the same color vertically
int startRow = row;
int endRow = row;
while (endRow < gridHeight && resultGrid[endRow][col] == resultGrid[row][col]) {
++endRow;
}
// Create a rectangle shape for the adjacent cells
sf::RectangleShape cellShape(sf::Vector2f((endCol - startCol) * cellSize, (endRow - startRow) * cellSize));
cellShape.setFillColor(resultGrid[row][col]);
cellShape.setOutlineColor(sf::Color::White);
cellShape.setOutlineThickness(2.0);
cellShape.setPosition(startCol * cellSize, startRow * cellSize);
level.push_back(cellShape);
// Mark the processed cells as transparent
for (int i = startRow; i < endRow; ++i) {
for (int j = startCol; j < endCol; ++j) {
resultGrid[i][j] = sf::Color::Transparent;
}
}
}
}
}
return level;
}
std::vector<sf::RectangleShape> Levels::createLevelShapes() {
std::vector<std::vector<sf::Color>> resultGrid = grid2shapes();
std::vector<sf::RectangleShape> level;
for (int row = 0; row < gridHeight; ++row) {
for (int col = 0; col < gridWidth; ++col) {
if (resultGrid[row][col] != sf::Color::Transparent) {
sf::RectangleShape cellShape(sf::Vector2f(cellSize, cellSize));
cellShape.setFillColor(resultGrid[row][col]);
cellShape.setPosition(col * cellSize, row * cellSize);
level.push_back(cellShape);
}
}
}
return level;
}