-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsceneimporter.h
198 lines (182 loc) · 6.98 KB
/
sceneimporter.h
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
#ifndef SCENEIMPORTER_H
#define SCENEIMPORTER_H
#include <SDL.h>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include "goal.h"
#include "minion.h"
#include "not_a_platform.h"
#include "patrolling_minion.h"
#include "platform.h"
#include "player.h"
#include "pupper.h"
#include "fire.h"
namespace sdlxx {
class SceneImporter {
public:
enum class Symbol : char {
Dot = '.', // Nothing
X = 'X', // Platform
R = 'R', // Also platform
G = 'G', // Player's starting point
B = 'B', // Pupper
M = 'M', // Minion
N = 'N', // Decorator platform
O = 'O', // Also decorator platform
P = 'P', // Patrolling Minion
F = 'F', // Fire
END = '=', // End Goal
};
SceneImporter(sdlxx::Sdl_renderer& ren) : renderer(ren) {}
~SceneImporter() = default;
template <typename T>
T load(std::string path)
{
T scene{renderer};
std::map<Symbol, std::vector<SDL_Rect>> objects;
// read in BMP file or some **** like that
std::ifstream file(path);
if (file.is_open()) {
std::string line;
// make an SDL_Surface outta it
// check out dem pixels
// turn dem pixels into The Scene
// for each pixel
int x = -1;
int y = -1;
while (getline(file, line)) {
++y;
x = -1;
for (char& c : line) {
++x;
// if blank:
Symbol sym = static_cast<Symbol>(c);
if (sym == Symbol::Dot) {
// skip
continue;
}
// otherwise:
// for each object of pixel's color:
if (objects.count(sym) < 1) {
objects.emplace(sym, std::vector<SDL_Rect>());
}
bool didUpdate = false;
for (int i = 0; i < objects[sym].size(); ++i) {
SDL_Rect rect = objects[sym][i];
bool withinX = (rect.x <= x) && ((rect.x + rect.w) > x);
bool withinY = (rect.y <= y) && ((rect.y + rect.h) > y);
// if pixel within already-recorded bounds
if (withinX && withinY) {
// skip
didUpdate = true;
break;
}
bool adjacentX =
(rect.y == y) && ((rect.x + rect.w) == x);
bool adjacentY =
(rect.x == x) && ((rect.y + rect.h) == y);
// if pixel adjacent to object's recorded bounds:
// update object's recorded bounds to include pixel
if (adjacentX) {
++objects[sym][i].w;
didUpdate = true;
break;
}
if (adjacentY) {
++objects[sym][i].h;
didUpdate = true;
break;
}
}
if (!didUpdate) {
// create new object of pixel's color, with bounds
// including the pixel
SDL_Rect newRect;
newRect.x = x;
newRect.y = y;
newRect.w = 1;
newRect.h = 1;
objects[sym].push_back(newRect);
}
}
}
file.close();
bool addPlayer = false;
SDL_Rect playerRect;
bool addPupper = false;
SDL_Rect pupperRect;
for (auto it = objects.begin(); it != objects.end(); ++it) {
Symbol sym = it->first;
std::vector<SDL_Rect> vec = it->second;
for (SDL_Rect rect : vec) {
printf("%cRect: x%d y%d w%d h%d\n", char(sym), rect.x,
rect.y, rect.w, rect.h);
rect.x *= 10;
rect.y *= 10;
rect.w *= 10;
rect.h *= 10;
switch (sym) {
case Symbol::G:
addPlayer = true;
playerRect = rect;
break;
case Symbol::R:
scene.addObject(std::make_unique<Platform>(
rect.x, rect.y, rect.w, rect.h));
break;
case Symbol::X:
scene.addObject(std::make_unique<Platform>(
rect.x, rect.y, rect.w, rect.h));
break;
case Symbol::B:
addPupper = true;
pupperRect = rect;
break;
case Symbol::M:
scene.addObject(std::make_unique<Minion>(
renderer, rect.x, rect.y));
break;
case Symbol::N:
scene.addObject(std::make_unique<Not_A_Platform>(
rect.x, rect.y, rect.w, rect.h));
break;
case Symbol::O:
scene.addObject(std::make_unique<Not_A_Platform>(
rect.x, rect.y, rect.w, rect.h));
break;
case Symbol::F:
scene.addObject(std::make_unique<Fire>(
renderer, rect.x, rect.y));
break;
case Symbol::P:
scene.addObject(std::make_unique<Patrolling_Minion>(
renderer, rect.x, rect.y));
break;
case Symbol::END:
scene.addObject(std::make_unique<Goal>(
renderer, rect.x, rect.y));
break;
default:
break;
}
}
}
if (addPlayer) {
scene.addObject(std::make_unique<Player>(
renderer, playerRect.x, playerRect.y));
}
if (addPupper) {
scene.addObject(std::make_unique<Pupper>(
renderer, pupperRect.x, pupperRect.y));
}
}
return scene;
}
private:
Sdl_renderer& renderer;
};
} // namespace sdlxx
#endif // !SCENEIMPORTER_H