-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
217 lines (195 loc) · 8.81 KB
/
main.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
//======================================================================================
//Source code for main function = main control loop for programme
//
//(c) Patrick Dickinson, University of Lincoln, School of Computer Science, 2020
//======================================================================================
#include "game.hpp"
#include "level.hpp"
#include "dynamic.hpp"
#include "bots.hpp"
//======================================================================================
//Globals
//======================================================================================
SDL_Window* gMainWindow = NULL;
SDL_Renderer* gMainRenderer = NULL;
SDL_Surface* tileSurface = NULL;
SDL_Surface* tileBlockedSurface = NULL;
SDL_Surface* targetSurface = NULL;
SDL_Surface* botSurface = NULL;
SDL_Surface* tileClosedSurface = NULL;
SDL_Surface* tileRouteSurface = NULL;
SDL_Texture* tileTexture = NULL;
SDL_Texture* tileBlockedTexture = NULL;
SDL_Texture* targetTexture = NULL;
SDL_Texture* botTexture = NULL;
SDL_Texture* tileClosedTexture = NULL;
SDL_Texture* tileRouteTexture = NULL;
bool gQuit;
// initialising our functions' selection to 0, it'll be changed depending on the user choice
int select_function = 0;
//======================================================================================
//Main function
//======================================================================================
int main(int argc, char* argv[])
{
gQuit = false;
//======================================================================================
//Initialise SDL
//======================================================================================
SDL_Init(SDL_INIT_EVERYTHING);
gMainWindow = SDL_CreateWindow
("Pathfinder", // window's title
30, 50, // coordinates on the screen, in pixels, of the window's upper left corner
640, 640, // window's length and height in pixels
SDL_WINDOW_OPENGL);
gMainRenderer = SDL_CreateRenderer(gMainWindow, -1, SDL_RENDERER_ACCELERATED);
//======================================================================================
//Load graphics for tiles, bot and target
//======================================================================================
tileSurface = SDL_LoadBMP("tile.bmp");
tileBlockedSurface = SDL_LoadBMP("tile-blocked.bmp");
targetSurface = SDL_LoadBMP("target.bmp");
botSurface = SDL_LoadBMP("bot.bmp");
tileClosedSurface = SDL_LoadBMP("tile-closed.bmp");
tileRouteSurface = SDL_LoadBMP("tile-route.bmp");
tileTexture = SDL_CreateTextureFromSurface(gMainRenderer, tileSurface);
tileBlockedTexture = SDL_CreateTextureFromSurface(gMainRenderer, tileBlockedSurface);
targetTexture = SDL_CreateTextureFromSurface(gMainRenderer, targetSurface);
botTexture = SDL_CreateTextureFromSurface(gMainRenderer, botSurface);
tileClosedTexture = SDL_CreateTextureFromSurface(gMainRenderer, tileClosedSurface);
tileRouteTexture = SDL_CreateTextureFromSurface(gMainRenderer, tileRouteSurface);
//======================================================================================
//Load the map and set target position
//======================================================================================
gLevel.Load("maps/3.txt");
gTarget.SetCurrent(30, 20, gLevel);
//======================================================================================
//Locals variables fro key presses and frame timer
//======================================================================================
SDL_Event event;
const Uint8* keystate;
int timerMS = SDL_GetTicks();
int deltaTimeMS = 0;
//======================================================================================
// Create Bot
//======================================================================================
// the following bot is using the heuristic functions
cBotBase* pBot = new cAStarBot();
pBot->SetCurrent(10, 20, gLevel);
//======================================================================================
//Main loop
//======================================================================================
while (!gQuit)
{
//======================================================================================
//Poll events for quit
//======================================================================================
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
gQuit = true;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
gQuit = true;
break;
}
break;
}
}
//======================================================================================
//Keyboard input for target control
//======================================================================================
keystate = SDL_GetKeyboardState(NULL);
int offsetX = 0;
int offsetY = 0;
if (keystate[SDL_SCANCODE_UP]) offsetY -= 1;
if (keystate[SDL_SCANCODE_DOWN]) offsetY += 1;
if (keystate[SDL_SCANCODE_LEFT]) offsetX -= 1;
if (keystate[SDL_SCANCODE_RIGHT]) offsetX += 1;
{
// Displaying Manhattan distance by clicking on A
static bool p_down = false;
if (keystate[SDL_SCANCODE_A])
{
if (!p_down)
{
gAStar.Build(*pBot);
select_function = 1;
p_down = true;
}
}
else { p_down = false; }
}
{
// Displaying Euclidean distance by clicking on B
static bool a_down = false;
if (keystate[SDL_SCANCODE_B])
{
if (!a_down)
{
gAStar2.Build(*pBot);
select_function = 2;
a_down = true;
}
}
else { a_down = false; }
}
{
// Displaying Diagonal distance by clicking on C
static bool b_down = false;
if (keystate[SDL_SCANCODE_C])
{
if (!b_down)
{
gAStar3.Build(*pBot);
select_function = 3;
b_down = true;
}
}
else { b_down = false; }
}
if ((offsetX != 0) || (offsetY != 0))
{
gTarget.SetNext((gTarget.PositionX() + offsetX), (gTarget.PositionY() + offsetY), gLevel);
}
//======================================================================================
//Start render for this frame
//======================================================================================
SDL_SetRenderDrawColor(gMainRenderer, 200, 200, 255, 255);
SDL_RenderClear(gMainRenderer);
//======================================================================================
//Compute time in miliseconds of last update cycle
//======================================================================================
int newFrameTimeMS = SDL_GetTicks();
deltaTimeMS = newFrameTimeMS - timerMS;
if (deltaTimeMS < 0) deltaTimeMS = 0;
timerMS = newFrameTimeMS;
//======================================================================================
//Update moving objects
//======================================================================================
gTarget.Update(deltaTimeMS);
pBot->Update(deltaTimeMS);
//======================================================================================
//Draw the level grid, then target, then bot
//======================================================================================
gLevel.Draw(select_function); // the grid will be drawn and show the heuristics functions depending on the user choice
gTarget.Draw(targetTexture);
pBot->Draw(botTexture);
//======================================================================================
//Finalise render for this frame
//======================================================================================
SDL_RenderPresent(gMainRenderer);
}
//======================================================================================
//Clean up
//======================================================================================
SDL_DestroyWindow(gMainWindow);
SDL_Quit();
return 0;
}