-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
409 lines (347 loc) · 9.18 KB
/
game.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
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
#include "game.hpp"
#include "SprintCar.hpp"
#include "Drawing.hpp"
#include "button.hpp"
#include "Fire.hpp"
#include <SDL_Mixer.h>
#include <list>
#include <vector>
#include "Health.hpp"
#include "Text.hpp"
#include "Timer.hpp"
#include <string>
#include <sstream>
#include "finish.hpp"
using namespace std;
SDL_Renderer* Drawing::gRenderer = NULL;
SDL_Texture* Drawing::assets = NULL;
bool Game::init()
{
//Initialization flag
bool success = true;
if( TTF_Init() == -1 )
{
printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 )
{
printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
//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
gWindow = SDL_CreateWindow( "Sprint Car Derby", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
Drawing::gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
if( Drawing::gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( Drawing::gRenderer, 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 Game::loadMedia()
{
//Loading success flag
bool success = true;
Drawing::assets = loadTexture("./assets/spritesheet.png");
gTexture = loadTexture(img);
if(Drawing::assets==NULL || gTexture==NULL)
{
printf("Unable to run due to error: %s\n",SDL_GetError());
success =false;
}
gMusic = Mix_LoadMUS( "./sounds/tokyo.wav" );
if( gMusic == NULL )
{
printf( "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
return success;
}
void Game::close()
{
//Free loaded images
SDL_DestroyTexture(Drawing::assets);
Drawing::assets=NULL;
SDL_DestroyTexture(gTexture);
//Destroy window
SDL_DestroyRenderer( Drawing::gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
Drawing::gRenderer = NULL;
Mix_FreeMusic(gMusic);
//Quit SDL subsystems
Mix_Quit();
IMG_Quit();
SDL_Quit();
}
SDL_Texture* Game::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( Drawing::gRenderer, loadedSurface );
if( newTexture == NULL )
{
printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
}
return newTexture;
}
void Game::run()
{
bool quit = false;
SDL_Event e;
//game timer
Timer timer;
SprintCar sprintcar;
int scrollingOffset = 0;
Mouse m;
//buttons
button play;
button rules;
button exit;
button back;
button car1;
button car2;
button car3;
button replay;
// Health life;
Text text(Drawing::gRenderer, "./fonts/MATURASC.TTF",160, "Sprint Car Derby", {255, 255 ,255 ,255});
Text text1(Drawing::gRenderer, "./fonts/MATURASC.TTF",160, "Sprint Car Derby", {192, 47 ,201 ,255});
Text text2(Drawing::gRenderer, "./fonts/MATURASC.TTF",160, "Sprint Car Derby", {33, 158 ,192 ,255});
finishLine line;
while( !quit )
{
m.update();
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
if(e.type == SDL_KEYDOWN)
{
//move the car using arrow keys
sprintcar.move(e.key.keysym.sym);
}
//when buttons are clicked
if(e.type == SDL_MOUSEBUTTONDOWN)
{
//start game
if(img == "./assets/smthng.gif")
{
if(play.handleEvent(&e) ==true)
{
//go to choose screen
img ="./assets/choose.gif";
loadMedia();
}
//show rules screen
if(rules.handleEvent(&e) ==true)
{
img = "./assets/rules.png";
loadMedia();
}
}
if(img =="./assets/smthng.gif" || img =="./assets/GameWon.png" || img == "./assets/gamelost.png")
{
//exit game
if(exit.handleEvent(&e) ==true)
{
quit = true;
}
}
if(img == "./assets/rules.png")
{
//go back from rules screen to main screen
if(back.handleEvent(&e) ==true)
{
img = "./assets/smthng.gif";
loadMedia();
}
}
//choosing cars
if(img =="./assets/choose.gif")
{
//first button clicked car 1 created
if(car1.handleEvent(&e) ==true)
{
img = "./assets/road.png";
loadMedia();
sprintcar.CreateHero(1);
timer.start();
}
//second button clicked car 2 created
else if(car2.handleEvent(&e) ==true)
{
img = "./assets/road.png";
sprintcar.CreateHero(2);
loadMedia();
timer.start();
}
//third button clicked car 3 created
else if(car3.handleEvent(&e) ==true)
{
img = "./assets/road.png";
sprintcar.CreateHero(3);
loadMedia();
timer.start();
}
}
if(replay.handleEvent(&e) ==true)
{
//go to choose screen again
img ="./assets/choose.gif";
loadMedia();
}
}
}
SDL_RenderClear(Drawing::gRenderer); //removes everything from renderer
SDL_RenderCopy(Drawing::gRenderer, gTexture, NULL, NULL); //Draws background to renderer
//***********************draw the objects here********************//
if(img =="./assets/road.png")
{
//background scrolling
scrollingOffset += 30;
if( scrollingOffset > SCREEN_HEIGHT)
{
scrollingOffset = 0;
}
//calling background scroll
SDL_Rect renderQuad = { 0, scrollingOffset, SCREEN_WIDTH, SCREEN_HEIGHT };
SDL_RenderCopy( Drawing::gRenderer, gTexture, NULL, &renderQuad);
renderQuad = { 0, scrollingOffset - SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT};
SDL_RenderCopy( Drawing::gRenderer, gTexture, NULL, &renderQuad );
//display timer
timer.display();
//game win if time up
if(timer.getTicks()> 90000)
{
img = "./assets/GameWon.png"; //game won screen
loadMedia();
timer.stop();
}
//draw chosencar
else if(timer.getTicks()<=90000)
{
sprintcar.DrawObject();
}
//game is lost when lives are = 0
if(sprintcar.getLife()==0)
{
img = "./assets/gamelost.png";
loadMedia();
}
//if time not up create cars
if(timer.getTicks()<=87000 && timer.getTicks()<90000)
{
//create normal cars
sprintcar.CreateNormal();
sprintcar.DrawCars();
sprintcar.moveCars();
}
//draw finish line when timer about to end
else if(timer.getTicks() >= 87050 && timer.getTicks()<90000)
{
line.draw();
line.move();
}
}
//play music for all screen
if( Mix_PlayingMusic() == 0 )
{
//Play the music
Mix_PlayMusic( gMusic, -1 );
}
//buttons drawn for main screen
if(img =="./assets/smthng.gif")
{
text2.display(87,87,Drawing::gRenderer);
text1.display(80,80,Drawing::gRenderer);
text.display(84,84,Drawing::gRenderer);
play.draw(25,24,189,70, 180, 500, 250, 70);
rules.draw(25,111,189,71,180,580,250,70);
exit.draw(25,206,189,71, 180, 660, 250,70);
}
//buttons drawn for rules screen
else if(img == "./assets/rules.png")
{
back.draw(25,301,189,70,20,10,150,70);
}
//buttons for choose screen
else if(img == "./assets/choose.gif")
{
car1.draw(278,16,206,198, 200,300,300,300); //ferrari button
car2.draw(521,15,206,197, 600,300,300,300); //ducati button
car3.draw(415,251,206,197,1000,300,300,300); //monster truck button
}
//buttons for game won and game lost screen
else if(img == "./assets/GameWon.png")
{
replay.draw(25,389,189,70, 180, 500, 250, 70);
exit.draw(25,206,189,71,180,580,250,70);
sprintcar.DisplayScore();
}
else if(img == "./assets/gamelost.png")
{
replay.draw(25,389,189,70, 180, 500, 250, 70);
exit.draw(25,206,189,71,180,580,250,70);
sprintcar.DisplayScore();
}
//draw mouse
m.draw();
//****************************************************************
SDL_RenderPresent(Drawing::gRenderer); //displays the updated renderer
SDL_Delay(100); //causes sdl engine to delay for specified miliseconds
}
}