-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialise.hpp
441 lines (379 loc) · 11.1 KB
/
initialise.hpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include <bits/stdc++.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <stdio.h>
#include <string>
using namespace std;
const int BOT_SCORE= 100;
const int BOSS_SCORE= 250;
const int MEDICINE_SCORE= 5;
const int VACCINE_SCORE= 10;
bool gameRunning = false;
bool gameServer = false;
bool gameClient = false;
const int SCREEN_WIDTH = 696; //696
const int SCREEN_HEIGHT = 540*1.5;
const int cellHeight =24;
const int cellWidth = 24;
enum KeyPress_start
{
KEY_MENU,
KEY_1P,
KEY_2P,
KEY_HELP,
KEY_CREDITS,
KEY_TOTAL
};
int BOT_alive=1;
int BOT2_alive =1;
int BOT3_alive=1;
int zombie_alive=1;
int docmanLives =5;
int eggsComplete = 0;
int scared = 0;
int score=0;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//Loads individual image as texture
SDL_Texture *loadTexture(std::string path);
//The window we'll be rendering to
SDL_Window *gameWindow = NULL;
//The window renderer
SDL_Renderer *gameRenderer = NULL;
//The images that correspond to a keypress
SDL_Texture *gameKeyPressTextures[KEY_TOTAL];
//Current displayed image
SDL_Texture *gameCurrentTexture = NULL;
SDL_Texture* gWallTexture = NULL;
SDL_Texture* gGrassTexture = NULL;
SDL_Texture* gDocmanTexture = NULL;
SDL_Texture* bossZombie = NULL;
SDL_Texture* pinkZombie = NULL;
SDL_Texture* blueZombie = NULL;
SDL_Texture* greenZombie = NULL;
SDL_Texture* scaredZombie = NULL;
SDL_Texture* medicineTexture = NULL;
SDL_Texture* vaccineTexture = NULL;
SDL_Texture* deadZombieTexture =NULL;
SDL_Texture* gQuitTexture = NULL;
SDL_Texture* gDocmanWon = NULL;
SDL_Texture* gZombieWon = NULL;
docman* Docman = NULL;
bot* BOT = NULL; // this will directly chase the Docman
bot* BOT2 = NULL; // this will directly chase the Docman from 180 degree behind
bot* BOT3 = NULL; // this will chase the docman from Front, 4 rows change scheme.
Mix_Chunk* effect1;
Mix_Chunk* effect2;
Mix_Chunk* effect3;
KeyPress_start check_position(int x, int y)
{
int x_start = 198;
int x_width = 498-198; // width of button is
int y_width = 345-297;
int y_single = 297;
int y_double = 366;
int y_help = 537;
int y_credits = 449;
if (x >= x_start && x <= (x_start + x_width))
{
if (y >= y_single && y <= (y_single + y_width))
{
// cout << KEY_1P;
return KEY_1P;
}
else if (y >= y_double && y <= (y_double + y_width))
{
// cout << KEY_2P;
return KEY_2P;
}
else if (y >= y_help && y <= (y_help + y_width))
{
// cout << KEY_HELP;
return KEY_HELP;
}
else if (y >= y_credits && y <= (y_credits + y_width))
{
// cout << KEY_CREDITS;
return KEY_CREDITS;
}
else
{
// cout << KEY_MENU;
return KEY_MENU;
}
}
// cout << KEY_MENU;
return KEY_MENU;
}
bool init()
{
//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
{
//Set texture filtering to linear
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!");
}
//Create window
gameWindow = SDL_CreateWindow("COVID SMASH", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gameWindow == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create renderer for window
gameRenderer = SDL_CreateRenderer(gameWindow, -1, SDL_RENDERER_ACCELERATED);
if (gameRenderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor(gameRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
success = false;
}
}
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load Start menu surface
gameKeyPressTextures[KEY_MENU] = loadTexture("Resources/start_menu.png");
if (gameKeyPressTextures[KEY_MENU] == NULL)
{
printf("Failed to load default image!\n");
success = false;
}
//Load 1 Player surface
gameKeyPressTextures[KEY_1P] = loadTexture("Resources/1_player.png");
if (gameKeyPressTextures[KEY_1P] == NULL)
{
printf("Failed to load 1 Player image!\n");
success = false;
}
//Load 2 Player surface
gameKeyPressTextures[KEY_2P] = loadTexture("Resources/2_player.png");
if (gameKeyPressTextures[KEY_2P] == NULL)
{
printf("Failed to load 2 Player image!\n");
success = false;
}
//Load Help surface
gameKeyPressTextures[KEY_HELP] = loadTexture("Resources/help.png");
if (gameKeyPressTextures[KEY_HELP] == NULL)
{
printf("Failed to load help image!\n");
success = false;
}
//Load Credits surface
gameKeyPressTextures[KEY_CREDITS] = loadTexture("Resources/credits.png");
if (gameKeyPressTextures[KEY_CREDITS] == NULL)
{
printf("Failed to load credits image!\n");
success = false;
}
gDocmanTexture = loadTexture("Resources/docman.png");
if (gDocmanTexture == NULL)
{
printf("Failed to load Docman image!\n");
success = false;
}
bossZombie = loadTexture("Resources/boss_zombie.png");
if (bossZombie == NULL)
{
printf("Failed to load Zombie image!\n");
success = false;
}
greenZombie = loadTexture("Resources/green_zombie.png");
if (greenZombie == NULL)
{
printf("Failed to load Zombie image!\n");
success = false;
}
pinkZombie = loadTexture("Resources/pink_zombie.png");
if (pinkZombie == NULL)
{
printf("Failed to load Zombie image!\n");
success = false;
}
blueZombie = loadTexture("Resources/blue_zombie.png");
if (blueZombie == NULL)
{
printf("Failed to load Zombie image!\n");
success = false;
}
scaredZombie = loadTexture("Resources/scared_zombie.png");
if (scaredZombie == NULL)
{
printf("Failed to load Zombie image!\n");
success = false;
}
gQuitTexture = loadTexture("Resources/quit_button.png");
if (gQuitTexture == NULL)
{
printf("Failed to load Quit Button!\n");
success = false;
}
gDocmanWon = loadTexture("Resources/docman_won.png");
if (gDocmanWon == NULL)
{
printf("Failed to load Docman Won image!\n");
success = false;
}
gZombieWon = loadTexture("Resources/zombie_won.png");
if (gZombieWon == NULL)
{
printf("Failed to load Zombie won image!\n");
success = false;
}
deadZombieTexture = loadTexture("Resources/dead_zombie.png");
if (deadZombieTexture == NULL)
{
printf("Failed to load Dead Zombie image!\n");
success = false;
}
gWallTexture = loadTexture("Resources/wall.png");
if (gWallTexture == NULL)
{
printf("Failed to load wall image!\n");
success = false;
}
gGrassTexture = loadTexture("Resources/grass.png");
if (gGrassTexture == NULL)
{
printf("Failed to load grass image!\n");
success = false;
}
medicineTexture = loadTexture("Resources/medicine.png");
if (medicineTexture == NULL)
{
printf("Failed to load egg image!\n");
success = false;
}
vaccineTexture = loadTexture("Resources/vaccine.png");
if (vaccineTexture == NULL)
{
printf("Failed to load vaccine image!\n");
success = false;
}
return success;
}
void close()
{
//Free loaded image
for (int i = 0; i < KEY_TOTAL; i++)
{
SDL_DestroyTexture(gameKeyPressTextures[i]);
gameKeyPressTextures[i] = NULL;
}
SDL_DestroyTexture(gameCurrentTexture);
gameCurrentTexture = NULL;
SDL_DestroyTexture( gWallTexture );
gWallTexture = NULL;
SDL_DestroyTexture( gDocmanTexture );
gDocmanTexture = NULL;
SDL_DestroyTexture( gGrassTexture );
gGrassTexture = NULL;
//Destroy window
SDL_DestroyRenderer(gameRenderer);
SDL_DestroyWindow(gameWindow);
gameWindow = NULL;
gameRenderer = NULL;
Mix_FreeChunk(effect1);
Mix_FreeChunk(effect2);
Mix_FreeChunk(effect3);
Mix_CloseAudio();
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
SDL_Texture *loadTexture(std::string path)
{
//The final texture
SDL_Texture *newTexture = NULL;
//Load image at specified path
SDL_Surface *loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
}
else
{
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(gameRenderer, loadedSurface);
if (newTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
return newTexture;
}
void createNewGame(int type){
BOT = new bot();
BOT2 = new bot();
BOT3 = new bot();
Docman = new docman(type);
BOT_alive=1;
BOT2_alive =1;
BOT3_alive=1;
zombie_alive=1;
docmanLives =5;
eggsComplete = 0;
scared = 0;
score =0;
}
void goBackToMenu(){
gameCurrentTexture = gameKeyPressTextures[KEY_MENU];
SDL_RenderClear(gameRenderer);
SDL_RenderCopy(gameRenderer, gameCurrentTexture, NULL, NULL);
SDL_RenderPresent(gameRenderer);
}
void show_docman(){
int x_last = width*cellWidth;
int y = (height+1)*cellHeight;
for (int i=1; i<docmanLives; i++){
SDL_Rect fillRect = { x_last- 3*cellWidth, y, 2*cellWidth, 2*cellHeight };
SDL_RenderCopy(gameRenderer, gDocmanTexture, NULL, &fillRect);
x_last -= 3*cellWidth;
}
}
void frender(SDL_Texture* texture, tuple<int,int,int,int,int, int, int> pos){
SDL_Rect fillRect = { get<0>(pos), get<1>(pos), Docman->cellWidth, Docman->cellHeight };
if (texture!= gDocmanTexture && get<5>(pos)==0) texture= deadZombieTexture;
else if (texture!=gDocmanTexture && scared==1) texture = scaredZombie;
if (get<2>(pos) == 180){
SDL_RenderCopyEx(gameRenderer, texture, NULL, &fillRect, 0 , NULL,SDL_FLIP_HORIZONTAL);
}
else SDL_RenderCopyEx(gameRenderer, texture, NULL, &fillRect, get<2>(pos) , NULL,SDL_FLIP_NONE );
}
void handleEvent(SDL_Event* event){
Docman->HandleEvent(event);
}