-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
313 lines (261 loc) · 7.59 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
#include "game.hpp"
#include "JetpackJoyride.hpp"
#include<vector>
#include "fx.hpp"
float bg_speed=4;
float delay=9;
// int pause_delay;
bool isPaused=false;
bool Game::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
gWindow = SDL_CreateWindow( "Jetpack Joyride!", 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
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( 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;
}
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;
}
}
}
}
return success;
}
bool Game::loadMedia()
{
//Loading success flag
bool success = true;
// SDL_Event e;
startscreen = Mix_LoadMUS( "The_Stash.mp3" );
bgMusic = Mix_LoadMUS( "bg_music.mp3" );
jetpacksound = Mix_LoadMUS ("jetpack_jet_lp.wav");
if(bgMusic == NULL or jetpacksound==NULL){
printf("Unable to load music: %s \n", Mix_GetError());
success = false;
}
assets = loadTexture("assets.png");
gTexture=loadTexture(screen[bg_selector]);
if(assets==NULL || gTexture==NULL)
{
printf("Unable to run due to error: %s\n",SDL_GetError());
success =false;
}
return success;
}
void Game::close()
{
//Free loaded images
SDL_DestroyTexture(assets);
assets=NULL;
SDL_DestroyTexture(gTexture);
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
int bgWidth;
int bgHeight;
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( gRenderer, 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;
}
bool Game::run1( ){
SDL_QueryTexture(gTexture, NULL, NULL, &bgWidth, &bgHeight); //returns the bgWidth and bgHeight
SDL_Rect bgRect = {0, 0, bgWidth, bgHeight};
bool quit = false;
SDL_Event e;
while( !quit ){
if( Mix_PlayingMusic() == 0 )
{
Mix_PlayMusic( startscreen,-1 );
}
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
return false; //so it exits the while loop in main
}
if((bg_selector==0) && (e.type == SDL_MOUSEBUTTONDOWN)){
bg_selector+=1;
return true;
}
if((bg_selector==1) && (e.type == SDL_MOUSEBUTTONDOWN)){
int xMouse, yMouse;
SDL_GetMouseState(&xMouse,&yMouse);
if(xMouse>=542 && xMouse<=744){
if(yMouse>=266 && yMouse<=353){
bg_selector+=1;
return true;
}
}
}
SDL_RenderClear(gRenderer); //removes everything from renderer
SDL_RenderCopy(gRenderer, gTexture, NULL, &bgRect);//Draws background to renderer
SDL_RenderPresent(gRenderer); //displays the updated renderer
SDL_Delay(200); //causes sdl engine to delay for specified miliseconds
}
}
}
bool Game::run2(){
cout<<"run2 called"<<endl;
SDL_QueryTexture(gTexture, NULL, NULL, &bgWidth, &bgHeight); //returns the bgWidth and bgHeight
SDL_Rect bgRect = {0, 0, bgWidth, bgHeight}; //create an SDL_Rect object of the same size as the background
bool quit = false; //to stop the while loop
int dead_Counter=0;
SDL_Event e;
JetpackJoyride JetpackJoyride(gRenderer, assets);
// bool check=true;
FX effects;
effects.initialize();
effects.load();
JetpackJoyride.createBarry();
Mix_HaltMusic();
while( !quit )
{
if( Mix_PlayingMusic() == 0 )
{
Mix_PlayMusic( bgMusic,-1 ); //start playing the background music
}
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
return false; //so it exits the while loop in main
}
if(e.type == SDL_MOUSEBUTTONDOWN){
//this is a good location to add pigeon in linked list.
int xMouse, yMouse;
SDL_GetMouseState(&xMouse,&yMouse);
}
if(e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_SPACE && !isPaused){
JetpackJoyride.fire_jetpack(); //to make barry rise
}
if(e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_SPACE && !isPaused){
JetpackJoyride.jetpack_off(); //to make barry fall
}
if(e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_f){ //fast forward for testing
// JetpackJoyride.fire_jetpack();
delay = 1;
}
if(e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_f){ //release the fast forward
// JetpackJoyride.jetpack_off();
delay=9;
}
if(e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_p){ //pause implementation
// JetpackJoyride.fire_jetpack();
isPaused = !isPaused; //toggle the pause
}
}
if (!isPaused){
//to control speed of objects
JetpackJoyride.object_speed=bg_speed;
if (JetpackJoyride.game_end==true){
if(bg_speed>0){
bg_speed = bg_speed*0.993; //to slow down the background as in the real game
dead_Counter+=1;
}
if(dead_Counter>=250){
if(e.type == SDL_MOUSEBUTTONDOWN){
int xMouse, yMouse;
SDL_GetMouseState(&xMouse,&yMouse);
if(xMouse>=705 && xMouse<=872){
if(yMouse>=365 && yMouse<=430){ //retry button clicked
bg_speed=4;
delay = 9;
return true;
}
}
}
}
}
SDL_RenderClear(gRenderer); //removes everything from renderer
SDL_RenderCopy(gRenderer, gTexture, NULL, &bgRect);//Draws background to renderer
//***********************draw the objects here********************
JetpackJoyride.drawObjects();
//****************************************************************
SDL_RenderPresent(gRenderer); //displays the updated renderer
bgRect.x=bgRect.x-bg_speed; //moves the background
if (bgRect.x <= -bgWidth+920) {
bgRect.x = -40; //snap back the background
delay = delay-0.4; //to speed up the game
bgRect.y = bgRect.y-460; //move the SDL_Rect object down
JetpackJoyride.bselector=1; //for the redlights to appear
if (bgRect.y==-460){
JetpackJoyride.bselector=2; //no more redlights
}
if (bgRect.y==-1840){
bgRect.y=0; //go to the top
}
}
JetpackJoyride.create_at_random();
SDL_Delay(delay); //causes sdl engine to delay for specified miliseconds
}
}
}