forked from Persnickity/PianoPlane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPianoPlane.c
422 lines (354 loc) · 8.56 KB
/
PianoPlane.c
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
#include <SDL.h>
#include <stdlib.h>
#include <SDL_mixer.h>
#define MOVE_SPEED 200.0f
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define ARRAY_SIZE 100
#define BOX_WIDTH 30
#define BOX_HEIGHT 30
#define MUSBUFFER 1024
Sint16 stream[2][MUSBUFFER*2*2];
int len=MUSBUFFER*2*2, which=0;
// Struct to contain the Game's Rectangle Objects
typedef struct
{
int exist;
float x, y;
int red, green, blue;
SDL_Rect rect;
}gameRect;
// Constructor for gameRect
gameRect createGameRect(float x, float y, int width, int height, int red, int green, int blue)
{
gameRect object;
object.exist = 1;
object.x = x;
object.y = y;
object.rect.x = (int)x;
object.rect.y = (int)y;
object.rect.w = width;
object.rect.h = height;
object.red = red;
object.green = green;
object.blue = blue;
return object;
}
// Collision Detection
int collisionDetection(gameRect player, gameRect* array)
{
int c;
// Initialize player left right top bottom
int pl = (int)player.x,
pr = (int)player.x + player.rect.w,
pt = (int)player.y,
pb = (int)player.y + player.rect.h;
// Declare bar left right top bottom
int bl,br,bt,bb;
for( c = 0; c < ARRAY_SIZE; c++ )
{
if( array[c].exist == 1 )
{
// Set bar left right top bottom
bl = (int)array[c].x;
br = (int)array[c].x + array[c].rect.w;
bt = (int)array[c].y;
bb = (int)array[c].y + array[c].rect.h;
// Check for collision
if( ( (pl < bl) && (pr > bl) ) || ( (pl < br) && (pr > br) ) )
{
if( ( (pt < bb) && (pb > bb) ) || ( (pt < bt) && (pb > bt) ) || ((pt > bt) && (pb < bb) ) )
{
// Return 1 for collision found
return 1;
}
}
}
}
// No collision detected, return 0
return 0;
}
// Draw Game Rectangle
int drawGameRect(SDL_Surface* display, gameRect object)
{
// Update Player.rect
object.rect.x = (int)object.x;
object.rect.y = (int)object.y;
// Draw Player
if (SDL_FillRect(display,
&(object.rect),
SDL_MapRGB(display->format, object.red, object.green, object.blue))
!= 0)
{
fprintf(stderr, "SDL_FillRect() Failed: %s\n", SDL_GetError());
return 0;
}
return 1;
}
// Draw array of bars
int drawGameRectArray(SDL_Surface* display, gameRect *array)
{
int i;
// For every Game Rectangle object that exists
for( i = 0; i < ARRAY_SIZE; i++ )
{
if(array[i].exist == 1)
{
// Call draw Game Rectangle
if(drawGameRect(display, array[i]) == 0)
return 0;
}
}
return 1;
}
// Create new bar
int createNewBar(gameRect *array, int y, int value )
{
int i, j = 0;
// If bar is too low push it back up
if( y > 400 )
y = 370;
// If bar is too high, push it back down
if( y < 150 )
y = 180;
// Normalize difference value
if( value > 20 )
value = 20;
if( value < -20 )
value = -20;
// For all game Rectangle Objects that exist
for( i = 0; i < ARRAY_SIZE; i++ )
{
if(array[i].exist == 0)
{
// Create bottom bar
if( j == 0)
{
array[i] = createGameRect( 630.0f, (float)y + value, 15, SCREEN_HEIGHT - (y + value), rand() % 255, rand() % 255, rand() % 255);
j++;
// Create top bar
} else {
array[i] = createGameRect( 630.0f, 0.0f, 15, y + value - 125, rand() % 255, rand() % 255, rand() % 255);
return y + value;
}
}
}
return 0;
}
// Update Array of bars
int updateArrayOfBars(gameRect *array, float ftime)
{
int i;
// For every game Rectangle Object that exists
for( i = 0; i < ARRAY_SIZE; i++ )
{
if(array[i].exist == 1)
{
// Move left at half the player speed
array[i].x -= MOVE_SPEED * ftime / 2;
// If game Rectangle Object has gone off screen
// Set existence to 0
if ( (array[i].x + array[i].rect.w) < 0 )
{
array[i].exist = 0;
}
}
}
return 1;
}
// Init Array
void initArrayOfBars(gameRect *array)
{
int i;
// Set every object in array's existence to 0
for( i = 0; i < ARRAY_SIZE; i++ )
{
array[i].exist = 0;
}
}
// Postmix updates audio stream buffer
void postmix(void *udata, Uint8 *_stream, int _len)
{
//len = audio buffer length
len=_len;
//update audio stream buffer
memcpy(stream[(which+1)%2],_stream,len);
//which = which stream i.e. stereo's two streams
which = (which + 1) % 2;
}
// getHeight returns music data
// returns average of all positive integers in song buffer
int getheight()
{
Sint16 *buf;
buf = stream[which];
int height = 0;// "height of bars"
int c = 0;//count
int x;
for( x = 0; x < MUSBUFFER; x++ )
{
if ( buf[x] > 0 )
{
height += buf[x];
c++;
}
}
if( (height == 0) || (c == 0) )
{
//enter here if buffer not filling correctly
return 1;
}
return ( height / c );//average sound wave peaks per buffer instance
}
// Play Music file
void startMusic(char* song)
{
// aud = audio
int aud_rate;
Uint16 aud_format;
int aud_channels;
int aud_volume = MIX_MAX_VOLUME;
Mix_Music *music = NULL;
// Initialize
// Hard coded to stereo settings
aud_rate = 22050;
aud_format = AUDIO_S16;
aud_channels = 2;
// Initialize SDL library
if ( SDL_Init(SDL_INIT_AUDIO < 0 ))
{
fprintf(stderr, "Couldn't initialize the SDL, EC: %s\n",SDL_GetError());
}
// Initialize audio device
if (Mix_OpenAudio(aud_rate, aud_format, aud_channels, MUSBUFFER) < 0)
{
fprintf(stderr, "Couldn't open audio, EC: %s\n", SDL_GetError());
}
// set up the post mix processor
Mix_SetPostMix(postmix,song);
// Set volume for music
Mix_VolumeMusic(aud_volume);
// Load music
music = Mix_LoadMUS(song);
// Play
Mix_FadeInMusic(music, 1, 1000);
}
// Main
int main(int argc, char* argv[])
{
// Initialize the SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
fprintf(stderr, "SDL_Init() Failed: %s\n", SDL_GetError());
exit(1);
}
// Set the video mode
SDL_Surface* display;
display = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (display == NULL)
{
fprintf(stderr, "SDL_SetVideoMode() Failed: %s\n", SDL_GetError());
exit(1);
}
// Set the title bar
SDL_WM_SetCaption("Piano Plane", "Piano Plane");
// Gain access to keystate array
Uint8 *keys = SDL_GetKeyState(NULL);
// Timing variables
Uint32 old_time, current_time;
float ftime;
// Need to initialize this here for event loop to work
current_time = SDL_GetTicks();
SDL_Event event;
// Initialize Player
gameRect player = createGameRect(35.0f, 35.0f, BOX_WIDTH, BOX_HEIGHT, 0, 0, 255);
// Initialize rects
gameRect rects[ARRAY_SIZE];
initArrayOfBars(rects);
// Counter to wait for bars to advance before creating new bar
float counter = 0;
// Previously created bars y location. Start location at 240
int yPrev = 240;
// Check command line for correct input
if ( (argc < 2) || (argc >= 3) )
{
printf("Please provide one music file\n\nAvaiable music types: .mp3, .ogg, .min\n");
}
// Start The Song
startMusic(argv[1]);
SDL_Delay(2000);
// Game loop
while(1)
{
// Update the timing information
old_time = current_time;
current_time = SDL_GetTicks();
ftime = (current_time - old_time) / 1000.0f;
// Check for messages
if (SDL_PollEvent(&event))
{
// Check for the quit message
if (event.type == SDL_QUIT)
{
// Quit the program
break;
}
}
// Is the song over?
if( !(Mix_PlayingMusic()) )
{
// Quit the program
break;
}
// Create new bar
if(counter > 20.0f)
{
if( ( yPrev = createNewBar(rects, yPrev, ( ( ( getheight() / -300 ) + 12) * 3) ) ) == 0)
{
fprintf(stderr, "Create New Bars Error\n");
break;
}
counter = 0;
}
// Handle input
if ( keys[SDLK_LEFT] && (player.x > 5.0f) )
player.x -= MOVE_SPEED * ftime;
if (keys[SDLK_RIGHT] && (player.x + player.rect.w < SCREEN_WIDTH - 5.0f) )
player.x += MOVE_SPEED * ftime;
if (keys[SDLK_DOWN] && (player.y + player.rect.h < SCREEN_HEIGHT - 5.0f) )
player.y += MOVE_SPEED * ftime;
if (keys[SDLK_UP] && (player.y > 5.0f) )
player.y -= MOVE_SPEED * ftime;
// Gravity Impact
if ( player.y + player.rect.h < SCREEN_HEIGHT - 5.0f )
player.y += MOVE_SPEED * ftime / 3;
// Update Array
if ( updateArrayOfBars(rects, ftime) == 0 )
{
fprintf(stderr, "Update Array of Bars Failed \n");
break;
}
// Collision Detection
if (collisionDetection(player, rects) == 1) break;
// Clear the screen
if (SDL_FillRect(display,
NULL,
SDL_MapRGB( display->format, 0,0,0))
!= 0)
{
fprintf(stderr, "SDL_FillRect() Failed: %s\n", SDL_GetError());
break;
}
// Draw Player
if(drawGameRect(display, player) == 0) break;
// Draw Array
if(drawGameRectArray(display, rects) == 0) break;
// Update Display
SDL_Flip(display);
// Increment counter
counter+= MOVE_SPEED * ftime / 2;
}
// Tell the SDL to clean up and shut down
SDL_Quit();
return 0;
}