/
App.cpp
283 lines (249 loc) · 6.65 KB
/
App.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
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
#include "App.h"
App::App() : map(100, 100)
{
bgIMG = NULL;
tileIMG = NULL;
cellSize = SCREEN_WIDTH / mapLength;
}
void App::start()
{
if (init())
{
run();
}
close();
}
bool App::init()
{
createTableMap();
return initSDL() && loadMedia();
}
bool App::initSDL()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
//SDL_ShowCursor(SDL_DISABLE); //Disable cursor
//Create window
mainWin.gWindow = SDL_CreateWindow("Pathfinder", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (mainWin.gWindow == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Get window surface
mainWin.gScreenSurface = SDL_GetWindowSurface(mainWin.gWindow);
}
}
return success;
}
bool App::loadMedia()
{
bgIMG = IMG_Load("resources/gfx/mainBG.png");
tileIMG = IMG_Load("resources/gfx/emtyTile.png");
return bgIMG != NULL && tileIMG != NULL;
}
void App::run()
{
//seekerBot.Path = seekerBot.pathfindingComponent.findPath(map.mapNodeGraph, map.XYToGraphNodeMap[coords(0,0)], map.XYToGraphNodeMap[coords(10,10)], Pathfinder::manhattanDistanceHeuristic);
seekerBot.Path = map.findPath(map.mapNodeGraph, map.XYToGraphNodeMap[coords(0, 0)], map.XYToGraphNodeMap[coords(10, 10)], Map::manhattanDistanceHeuristic);
quit = false;
while (!quit)
{
handleEvents();
updateDisplay();
}
}
void App::close()
{
//Deallocate surface
SDL_FreeSurface(bgIMG);
bgIMG = NULL;
SDL_FreeSurface(tileIMG);
tileIMG = NULL;
//Destroy window
SDL_DestroyWindow(mainWin.gWindow);
mainWin.gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
void App::handleEvents()
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
else if (e.button.type == SDL_MOUSEBUTTONDOWN)
{
if (e.button.button == SDL_BUTTON_LEFT)
tryb = dodaj;
if (e.button.button == SDL_BUTTON_RIGHT)
tryb = usun;
App::mouseClick(e.button.x, e.button.y);
}
//User presses a key
/*
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_UP:
map.moveCursor(0, -1);
break;
case SDLK_DOWN:
map.moveCursor(0, 1);
break;
case SDLK_LEFT:
map.moveCursor(-1, 0);
break;
case SDLK_RIGHT:
map.moveCursor(1, 0);
break;
case SDLK_z:
//make tile at the cursor position unpassable
break;
case SDLK_x:
//make tile at the cursor position passable
break;
case SDLK_c:
//place goal for the bots to reach at the cursor position
break;
default:
break;
}
}*/
}
}
void App::updateDisplay()
{
//Apply the image
SDL_BlitSurface(bgIMG, NULL, mainWin.gScreenSurface, NULL);
drawMap();
//Update the surface
SDL_UpdateWindowSurface(mainWin.gWindow);
}
void App::createTableMap()
{
for (int i = 0; i<mapLength; i++)
{
for (int j = 0; j<mapLength; j++)
{
tableMap[i][j].x = i*cellSize;
tableMap[i][j].y = j*cellSize;
tableMap[i][j].color = 0x78AB46;
}
}
}
void App::drawMap()
{
int bpp = mainWin.gScreenSurface->format->BytesPerPixel;
MapVertex v;
int x, y, i, j, x1, y1;
Uint8 *p;
// kolorowanie kratek
for (x = 0; x < SCREEN_HEIGHT; x += cellSize)
{
for (y = 0; y < SCREEN_HEIGHT; y += cellSize)
{
for (i = x + 1; i < x + cellSize - 1; i++)
{
for (j = y + 1; j < y + cellSize - 1; j++)
{
p = (Uint8 *)mainWin.gScreenSurface->pixels + j * mainWin.gScreenSurface->pitch + i * bpp;
*(Uint32 *)p = tableMap[x / cellSize][y / cellSize].color;
}
}
}
}
// kolorowanie œcie¿ki
if (!seekerBot.Path.empty())
{
for (std::list<MapVertex>::iterator iter = seekerBot.Path.begin(); iter != seekerBot.Path.end(); ++iter)
{
coords nodeCoords = map.mapNodeGraph[*iter].getXY();
x1 = nodeCoords.first * cellSize;
y1 = nodeCoords.second * cellSize;
for (i = x1 + 1; i < x1 + cellSize - 1; i++)
{
for (j = y1 + 1; j < y1 + cellSize - 1; j++)
{
p = (Uint8 *)mainWin.gScreenSurface->pixels + j * mainWin.gScreenSurface->pitch + i * bpp;
*(Uint32 *)p = 0xFFFFFF;
}
}
}
}
}
void App::mouseClick(int x, int y)
{
if (x < SCREEN_WIDTH)
{
x /= cellSize;
y /= cellSize;
if (tryb == dodaj)
{
if (tableMap[x][y].color != 0x78AB46)
{
tableMap[x][y].color = 0x78AB46;
//tableMap[(x/cellSize)+1][(y/cellSize)].color = 0x78AB46;
//tableMap[(x/cellSize)+1][(y/cellSize)+1].color = 0x78AB46;
//tableMap[(x/cellSize)][(y/cellSize)+1].color = 0x78AB46;
// dodanie wierzcho³ka
MapNode *node = map.nodeTab[x][y];
MapVertex v = add_vertex(*node, map.mapNodeGraph);
map.XYToGraphNodeMap[coords(x, y)] = v;
//EDGES
//up
if (map.XYToGraphNodeMap.find(coords(x, y - 1)) != map.XYToGraphNodeMap.end()) {
add_edge(v, map.XYToGraphNodeMap[coords(x, y - 1)], 1, map.mapNodeGraph);
add_edge(map.XYToGraphNodeMap[coords(x, y - 1)], v, 1, map.mapNodeGraph);
}
//down
if (map.XYToGraphNodeMap.find(coords(x, y + 1)) != map.XYToGraphNodeMap.end()) {
add_edge(v, map.XYToGraphNodeMap[coords(x, y + 1)], 1, map.mapNodeGraph);
add_edge(map.XYToGraphNodeMap[coords(x, y + 1)], v, 1, map.mapNodeGraph);
}
//left
if (map.XYToGraphNodeMap.find(coords(x + 1, y)) != map.XYToGraphNodeMap.end()) {
add_edge(v, map.XYToGraphNodeMap[coords(x + 1, y)], 1, map.mapNodeGraph);
add_edge(map.XYToGraphNodeMap[coords(x + 1, y)], v, 1, map.mapNodeGraph);
}
//right
if (map.XYToGraphNodeMap.find(coords(x -1, y)) != map.XYToGraphNodeMap.end()) {
add_edge(v, map.XYToGraphNodeMap[coords(x - 1, y)], 1, map.mapNodeGraph);
add_edge(map.XYToGraphNodeMap[coords(x - 1, y)], v, 1, map.mapNodeGraph);
}
}
}
else
{
if (tableMap[x][y].color != 0x000000)
{
tableMap[x][y].color = 0x000000;
//tableMap[(x/cellSize)+1][(y/cellSize)+1].color = 0x000000;
//tableMap[(x/cellSize)+1][(y/cellSize)].color = 0x000000;
//tableMap[(x/cellSize)][(y/cellSize)+1].color = 0x000000;
clear_vertex(map.XYToGraphNodeMap[coords(x, y)], map.mapNodeGraph);
remove_vertex(map.XYToGraphNodeMap[coords(x, y)], map.mapNodeGraph);
map.XYToGraphNodeMap.erase(coords(x, y));
map.refreshMapping(map.XYToGraphNodeMap, map.mapNodeGraph);
}
}
//seekerBot.Path = seekerBot.pathfindingComponent.updatePath(map.mapNodeGraph, seekerBot.Path, Pathfinder::manhattanDistanceHeuristic);
seekerBot.Path = map.findPath(map.mapNodeGraph, map.XYToGraphNodeMap[coords(0, 0)], map.XYToGraphNodeMap[coords(10, 10)], Map::manhattanDistanceHeuristic);
//map.pathToCoords(seekerBot.Path, map.mapNodeGraph);
}
}