-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreen.cpp
316 lines (291 loc) · 7.88 KB
/
screen.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
#include "screen.h"
#include "map.h"
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <iostream>
#include <SDL_ttf.h>
//Screen dimension constants
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
int start_count = 0;
//The music that will be played
Mix_Music *gMusic = NULL;
//The sound effects that will be used
Mix_Chunk *gcoin = NULL;
Mix_Chunk *gbump = NULL;
Mix_Chunk *gdead = NULL;
Mix_Chunk *gplus = NULL;
//Scene textures
LTexture gDotTexture1;
LTexture gDotTexture2;
LTexture gBGTexture;
LTexture gTextTexture1;
LTexture gTextTexture2;
LTexture gTextTexture3;
LTexture gTextTexture4;
LTexture gTimeTextTexture;
LTexture gTimeTextTexture2;
LTexture gTimeTextTexture3;
LTexture gPausePromptTexture;
LTexture gStartPromptTexture;
LTexture gMoneyTexture;
LTexture gBuildingTexture1;
LTexture gBuildingTexture2;
LTexture gPeopleTexture1;
LTexture gPeopleTexture2;
LTexture gPeopleTexture3;
LTexture gPeopleTexture12;
LTexture gPeopleTexture22;
LTexture gPeopleTexture32;
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
gWindow = SDL_CreateWindow( "SDL Tutorial", 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 vsynced renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
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;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
//Initialize SDL_mixer
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 loadMedia()
{
//Loading success flag
bool success = true;
//Load dot texture
if( !gDotTexture1.loadFromFile( "pic/trump_D.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gDotTexture2.loadFromFile( "pic/trump_U.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gMoneyTexture.loadFromFile( "pic/coin_2.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gBuildingTexture1.loadFromFile( "pic/Empire_State_Building.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gBuildingTexture2.loadFromFile( "pic/NewYorkCity_StatueOfLiberty.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gPeopleTexture1.loadFromFile( "pic/white_D.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gPeopleTexture2.loadFromFile( "pic/mexican_D.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gPeopleTexture3.loadFromFile( "pic/african_D.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gPeopleTexture12.loadFromFile( "pic/white_U.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gPeopleTexture22.loadFromFile( "pic/mexican_U.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
if( !gPeopleTexture32.loadFromFile( "pic/african_U.png" ) )
{
printf( "Failed to load dot texture!\n" );
success = false;
}
//Load background texture
if( !gBGTexture.loadFromFile( "pic/map2.png" ) )
{
printf( "Failed to load background texture!\n" );
success = false;
}
//Load pause state
if( !gPausePromptTexture.loadFromFile( "pic/pause.png" ) )
{
printf( "Failed to load background texture!\n" );
success = false;
}
//Load music
gMusic = Mix_LoadMUS( "pic/Jay_Jay.wav" );
if( gMusic == NULL )
{
printf( "Failed to load beat music! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
//Load sound effects
gcoin = Mix_LoadWAV( "pic/coin.wav" );
if( gcoin == NULL )
{
printf( "Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
gbump = Mix_LoadWAV( "pic/wall.mp4.wav" );
if( gbump == NULL )
{
printf( "Failed to load high sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
gdead = Mix_LoadWAV( "pic/dead.wav" );
if( gdead == NULL )
{
printf( "Failed to load medium sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
gplus = Mix_LoadWAV( "pic/plus.wav" );
if( gplus == NULL )
{
printf( "Failed to load low sound effect! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
//Open the font
gFont = TTF_OpenFont( "pic/AGENCYB.ttf", 150 );
//TTF_SetFontStyle(gfont, TTF_STYLE_BOLD);
if( gFont == NULL )
{
printf( "Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
else
{
//Render text
SDL_Color textColor = { 49, 49, 49 };
if( !gTextTexture1.loadFromRenderedText( "3", textColor ) ){
printf( "Failed to render text texture!\n" );
success = false;
}
if( !gTextTexture2.loadFromRenderedText( "2", textColor ) ){
printf( "Failed to render text texture!\n" );
success = false;
}
if( !gTextTexture3.loadFromRenderedText( "1", textColor ) ){
printf( "Failed to render text texture!\n" );
success = false;
}
if( !gTextTexture4.loadFromRenderedText( "YOU LOSE", textColor ) ){
printf( "Failed to render text texture!\n" );
success = false;
}
//Load stop prompt texture
if( !gStartPromptTexture.loadFromRenderedText( "Press S to Start or Stop the Timer", textColor ) )
{
printf( "Unable to render start/stop prompt texture!\n" );
success = false;
}
}
return success;
}
void close()
{
//Free loaded images
gDotTexture1.free();
gDotTexture2.free();
gBGTexture.free();
gTextTexture1.free();
gTextTexture2.free();
gTextTexture3.free();
gTextTexture4.free();
gTimeTextTexture.free();
gTimeTextTexture2.free();
gTimeTextTexture3.free();
gStartPromptTexture.free();
gPausePromptTexture.free();
gMoneyTexture.free();
gBuildingTexture1.free();
gBuildingTexture2.free();
gPeopleTexture1.free();
gPeopleTexture2.free();
gPeopleTexture3.free();
//Free the sound effects
Mix_FreeChunk( gcoin );
Mix_FreeChunk( gbump );
Mix_FreeChunk( gdead );
Mix_FreeChunk( gplus );
gcoin = NULL;
gbump = NULL;
gdead = NULL;
gplus = NULL;
//Free the music
Mix_FreeMusic( gMusic );
gMusic = NULL;
//Free global font
TTF_CloseFont( gFont );
gFont = NULL;
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
TTF_Quit();
IMG_Quit();
SDL_Quit();
}