-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.cpp
More file actions
209 lines (175 loc) · 5.81 KB
/
gui.cpp
File metadata and controls
209 lines (175 loc) · 5.81 KB
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
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <iostream>
#include <string>
#include "gui.h"
#include "astar.h"
SDLGui::SDLGui() {
win = NULL;
renderer = NULL;
// Array size in .h
for (auto& texture : textures) {
if (texture) {
texture = NULL;
}
}
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
map[i][j] = 0;
}
}
beginPoint = { -1,-1 };
endPoint = { -1,-1 };
snprintf(textMessage, sizeof(textMessage), "Select route begin point");
}
SDLGui::~SDLGui() {
for (auto& texture : textures) {
if (texture) {
SDL_DestroyTexture(texture);
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
printf("SDL destructed\n");
}
int SDLGui::initSDL() {
// Initialize SDL.
if ((SDL_Init(SDL_INIT_VIDEO) < 0)) {
printf("Could not initialize SDL: %s.\n", SDL_GetError());
return(-1);
}
printf("SDL initialized.\n");
if (TTF_Init() == -1) {
printf("SDL_ttf could not initialize! TTF_Error: %s\n", TTF_GetError());
return(-1);
}
// create the window and renderer
win = SDL_CreateWindow("SDL2 + Astar pathfinding CPP demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT + 50, 0);
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
printf("Window and renderer created\n");
// Load the textures
textures[0] = IMG_LoadTexture(renderer, "tile0.png");
textures[1] = IMG_LoadTexture(renderer, "tile1.png");
for (auto& texture : textures) {
if (texture == nullptr) {
printf("IMG_LoadTexture Error: %s \n", SDL_GetError());
return(-1);
}
}
printf("Textures loaded\n");
// Load font
font = TTF_OpenFont("DejaVuSansMono.ttf", 18);
if (font == nullptr) {
printf("Failed to load font! SDL_ttf Error: %s\n", TTF_GetError());
return(-1);
}
printf("Font loaded\n");
return(1);
}
void SDLGui::loadMap(int newmap[ROW][COL]) {
// Assign values using nested loops
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
map[i][j] = newmap[i][j];
}
}
}
// Print map for debug
void SDLGui::printMap() {
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
std::cout << map[i][j] << " ";
}
std::cout << std::endl;
}
}
void SDLGui::render() {
if (lastUpdateTime + 30 < SDL_GetTicks()) {
lastUpdateTime = SDL_GetTicks();
SDL_RenderClear(renderer);
// Render map
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
// Create an SDL_Rect for the current tile
SDL_Rect destRect = { j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE };
// Get the appropriate texture for the tile
SDL_Texture* texture = textures[map[i][j]];
// Render the tile
SDL_RenderCopy(renderer, texture, nullptr, &destRect);
}
}
// Render text message
SDL_Surface* textSurface = TTF_RenderText_Solid(font, textMessage, textColor);
if (textSurface == nullptr) {
std::cerr << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << std::endl;
}
// Create texture from surface pixels
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
if (textTexture == nullptr) {
printf("Unable to create texture from rendered text! SDL Error: %s \n", SDL_GetError());
}
else {
SDL_Rect renderQuad = { 10, HEIGHT + 12, textSurface->w, textSurface->h };
SDL_RenderCopy(renderer, textTexture, nullptr, &renderQuad);
}
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
// Render flags at begin and end points
// Render pathline if available
drawPath(pathdata.path);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// Render the prepared view
SDL_RenderPresent(renderer);
}
}
void SDLGui::drawPath(std::stack<Pair> path) {
SDL_SetRenderDrawColor(renderer, 255, 128, 128, 255);
std::pair<int, int> oldP = { -1,-1 };
while (!path.empty()) {
std::pair<int, int> p = path.top();
path.pop();
// printf("-> (%d,%d) ", p.second, p.first);
if (oldP.first != -1) {
// Drawing 3 lines so it can be thicker -- apparently doesn't support line thickness. This is ugly though.
SDL_RenderDrawLine(renderer, oldP.second * TILE_SIZE + (TILE_SIZE/2), oldP.first * TILE_SIZE + (TILE_SIZE / 2), p.second * TILE_SIZE + (TILE_SIZE / 2), p.first * TILE_SIZE + (TILE_SIZE / 2));
SDL_RenderDrawLine(renderer, oldP.second * TILE_SIZE + (1 + TILE_SIZE / 2), oldP.first * TILE_SIZE + (TILE_SIZE / 2), p.second * TILE_SIZE + (1 + TILE_SIZE / 2), p.first * TILE_SIZE + (TILE_SIZE / 2));
SDL_RenderDrawLine(renderer, oldP.second * TILE_SIZE + (TILE_SIZE / 2), oldP.first * TILE_SIZE + (-1 + TILE_SIZE / 2), p.second * TILE_SIZE + (TILE_SIZE / 2), p.first * TILE_SIZE + (-1 + TILE_SIZE / 2));
}
oldP = p;
}
}
int SDLGui::handleInput() {
// event handling
SDL_Event e;
if (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT)
return (SDL_QUIT);
else if (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_ESCAPE)
return (SDL_QUIT);
// else if (e.type == SDL_MOUSEMOTION || e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP) {
else if (e.type == SDL_MOUSEBUTTONDOWN) {
int x, y;
SDL_GetMouseState(&x, &y);
// Choose beginning point
if (beginPoint.first == -1 and beginPoint.second == -1) {
beginPoint.first = y / TILE_SIZE; beginPoint.second = x / TILE_SIZE;
snprintf(textMessage, sizeof(textMessage), "Select route end point");
}
// Choose end point (and show result)
else if (endPoint.first == -1 and endPoint.second == -1) {
endPoint.first = y / TILE_SIZE; endPoint.second = x / TILE_SIZE;
pathdata = aStarSearch(map, beginPoint, endPoint);
snprintf(textMessage, sizeof(textMessage), pathdata.message.c_str());
}
// Reset and ready for beginning point
else {
beginPoint = { -1,-1 }; endPoint = { -1,-1 };
std::stack<Pair> emptyPath;
pathdata.path = emptyPath;
snprintf(textMessage, sizeof(textMessage), "Select route beginning point");
}
}
}
}