Annotation of froggix/froggix.c, revision 1.1.1.1
1.1 nick 1: /*
2: * Froggix
3: *
4: * Nicholas DeClario 2009
5: * <nick@declario.com>
6: *
7: * This program is distributed under the GNU Public License
8: * <Insert GNU license blurb here>
9: */
10:
11:
12: /*
13: * Our pretty standard includes
14: */
15: #include <stdio.h>
16: #include <stdlib.h>
17: #include <unistd.h>
18: #include <time.h>
19:
20: #include <SDL.h>
21: #include <SDL_mixer.h>
22: #include <SDL_image.h>
23: #include <SDL_ttf.h>
24:
25: /*
26: * Set some basic definitions
27: */
28: #define VERSION "$Id$"
29: #define TITLE "Froggix"
30: #define SCREEN_WIDTH 640
31: #define SCREEN_HEIGHT 480
32: #define FALSE 0
33: #define TRUE 1
34: #define LIVES 3
35: #define COLORKEY 255, 0, 255
36: #define BGCOLOR 0, 0, 0
37: #define FROGGER_START_X 290
38: #define FROGGER_START_Y 425
39: #define UP 1
40: #define DOWN 2
41: #define LEFT 3
42: #define RIGHT 4
43: #define X 0
44: #define Y 1
45: #define FRAME 24
46: #define HFRAME 12
47: #define HOP_DISTANCE 30
48: #define HOP_SPEED 3
49: #define ROW_BASE 425
50: #define LEFT_SIDE 115
51: #define RIGHT_SIDE 525
52: #define SPLASH 1 /* Death types */
53: #define SPLAT 2
54:
55: /* Point table */
56: #define SCORE_HOP 10
57: #define SCORE_GOAL 50
58: #define SCORE_LEVEL 1000
59: #define SCORE_FLY 150
60: #define SCORE_PINK 200
61: #define SCORE_SECONDS 10
62: #define HIGH_SCORE 4630
63: #define SCORE_FREE_FROG 200
64:
65: /* The green game timer */
66: #define MAX_TIMER 350
67: #define TIMER_SIZE 150
68: #define TIMER_COLOR 32, 211, 0
69: #define TIMER_LOW_COLOR 255, 0, 0
70:
71: /* baddies */
72: #define VEHICLE 0
73: #define LOG 1
74: #define TURTLE 2
75: #define GATOR 3
76: #define SNAKE 4
77: #define BEAVER 5
78:
79: /* Goal areas */
80: #define MAX_GOALS 5
81:
82: /* logs */
83: #define SHORT_LOG 4
84: #define MEDIUM_LOG 6
85: #define LONG_LOG 9
86: #define MAX_WOOD 7
87:
88: /* Turtles */
89: #define DIVE_START_TIME 50
90: #define DIVE_PHASE_TIME 20
91: #define MAX_TURTLES 9
92: #define TURTLE_ANIM_TIME 5
93:
94: /* Vehicles */
95: #define MAX_VEHICLES 40
96:
97:
98: /*
99: * Froggers dstruct
100: */
101: typedef struct {
102: int placement[2];
103: int oldPlacement[2];
104: int direction;
105: int location;
106: int hopCount;
107: int currentRow;
108: int alive;
109: int riding;
110: int ridingIdx;
111: int ridingType;
112: int frogger; /* Are we frogger or bonus frog */
113: int deathType;
114: int deathCount;
115:
116: SDL_Rect src;
117: SDL_Rect dst;
118:
119: Mix_Chunk *s_hop;
120: Mix_Chunk *s_squash;
121: Mix_Chunk *s_splash;
122: Mix_Chunk *s_extra;
123: } froggerObj;
124:
125: /*
126: * Goals
127: */
128: typedef struct {
129: int x, y, w, h;
130: int occupied;
131: int fly;
132: int gator;
133: } goalObj;
134:
135: /*
136: * Vehicles
137: */
138: typedef struct {
139: int placement[2];
140: int oldPlacement[2];
141: int direction; // LEFT or RIGHT
142: int row; // row
143: int speed; // How fast are we traveling
144: int level; // Must be >= this level to display
145:
146: SDL_Rect src;
147: } vehicleObj;
148:
149: /*
150: * It's Log!
151: */
152: typedef struct {
153: int placement[2];
154: int oldPlacement[2];
155: int row; /* Current row we are in */
156: int type; /* SHORT, MEDIUM, or LONG */
157: int speed; /* What speed does the log move at */
158: int hasPink; // Is bonus frog riding
159:
160: SDL_Rect src;
161: } logObj;
162:
163: /*
164: * Turtles
165: */
166: typedef struct {
167: int placement[2];
168: int oldPlacement[2];
169: int row; /* Which row are the turtles in? */
170: int count; /* How many turtles in this group? */
171: int speed; /* How fast are they swimming */
172: int canDive; /* Can this group dive */
173: int diveStep; /* If they can dive, what diving step are then in */
174: int diveTime; /* Current dive time */
175: int animStep; /* Current animation frame */
176: int animDelay; /* The number of ticks to wait between frames */
177:
178: SDL_Rect src;
179: } turtleObj;
180:
181: int keyEvents( SDL_Event event );
182: int mySDLInit( void );
183: void beginGame( void );
184: int loadMedia( void );
185: int heartbeat( void );
186: int updateGameState( void );
187: void configGameScreen( void );
188: void drawGameScreen( void );
189: void drawBackground( void );
190: int getRowPixel ( int row );
191: int collisionRow ( void );
192: int freeFrog( int score );
193: int collideFrogger ( int x, int y, int h, int w );
194: void checkFroggerBorder( void );
195: void levelUp( void );
196: int checkGoals( void );
197: void froggerReset( void );
198: logObj setWood( int type, int speed, int row, int startX );
199: turtleObj setTurtle( int dive, int diveTimer, int speed, int row, int startX, int count );
200: vehicleObj setVehicle( int row, int startX, int speed, int level );
201: goalObj setGoal( int x, int y, int w, int h );
202: void moveFrogger( void );
203: void ridingFrogger( );
204: void drawTitleScreen( void );
205: void drawPauseScreen( void );
206: void drawGameOver( void );
207: int drawDeathSequence( int deathType );
208: int checkTimer( void );
209: void drawScore( int high, int score );
210: void drawNumbers( int num, int x, int y );
211: void drawGoals( void );
212: void drawTimer( int length );
213: void drawLives( int lives );
214: void drawLevel( int level );
215: void drawWood( void );
216: void drawTurtles( void );
217: void drawVehicles( void );
218: void drawImage(SDL_Surface *srcimg, int sx, int sy, int sw, int sh, SDL_Surface *dstimg, int dx, int dy, int alpha );
219: void playSound( Mix_Chunk *sound );
220: void setFullScreenMode( void );
221:
222: int level = 0;
223: int playing = 0;
224: int lives = 0;
225: int players = 0;
226: int score = 0;
227: int givenFreeFrog = 0;
228: int hScore = HIGH_SCORE;
229: int redraw_all = 0;
230: int fullscreen = 0;
231: int drawBG = 0;
232: int goDelay;
233: float timeLeft;
234: froggerObj frogger;
235: logObj wood[MAX_WOOD];
236: turtleObj turtle[MAX_TURTLES];
237: vehicleObj vehicle[MAX_VEHICLES];
238: goalObj goals[MAX_GOALS];
239:
240: Mix_Chunk *s_freeFrog;
241: SDL_Surface *gfx;
242: SDL_Surface *background; // This is the frogger back drop
243: SDL_Rect backgroundRect;
244: SDL_Surface *titleSurface; // Title 'Froggix' image
245: SDL_Surface *screen; //This pointer will reference the backbuffer
246: SDL_Rect leftBorderRect;
247: SDL_Rect rightBorderRect;
248: TTF_Font *font;
249:
250: int debugBorder = 0;
251:
252: /*
253: * int mySDLInit(void);
254: *
255: * This starts the basic SDL initialization for everything we'll need
256: *
257: */
258: int mySDLInit( void ) {
259: int result = 1;
260:
261: if( SDL_Init( SDL_INIT_VIDEO ) != 0 ) {
262: fprintf( stderr, "Warning: Unable to initialize video: %s\n", SDL_GetError( ) );
263: result--;
264: }
265:
266: if( TTF_Init( ) == -1 ) {
267: fprintf( stderr, "Warning: Unable to initialize font engine: %s\n", TTF_GetError( ) );
268: result--;
269: }
270:
271: if( SDL_Init( SDL_INIT_AUDIO ) != 0 ) {
272: fprintf( stderr, "Warning: Unable to initialize audio: %s\n", SDL_GetError( ) );
273: result--;
274: }
275:
276: if( Mix_OpenAudio( 11025, AUDIO_S16, 2, 512 ) < 0 ) {
277: fprintf( stderr, "Warning: Audio set failed: %s\n", SDL_GetError( ) );
278: result--;
279: }
280:
281: screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 16, SDL_HWSURFACE );
282:
283: if ( screen == NULL ) {
284: fprintf( stderr, "Error: Unable to set video mode: %s\n", SDL_GetError( ) );
285: result--;
286: }
287:
288: SDL_WM_SetCaption( TITLE, NULL );
289:
290: return result;
291: }
292:
293: /*
294: * void beginGame( void );
295: *
296: * Main game routine
297: */
298: void beginGame( void ) {
299: float next_heartbeat = 0;
300: SDL_Event event;
301: int done = 0;
302:
303: printf ( "D: Starting main game loop\n" );
304:
305: if ( loadMedia( ) <= 0 ) {
306: fprintf( stderr, "Error: Failed to load graphics and audio!\n" );
307: return;
308: }
309:
310: drawBackground( );
311:
312: while( ! done ) {
313: while( SDL_PollEvent( &event ) ) {
314: done = keyEvents( event );
315: }
316:
317: /* Check the heartbeat to see if we're ready */
318: if ( SDL_GetTicks( ) >= next_heartbeat ) {
319: next_heartbeat = SDL_GetTicks( ) + heartbeat( );
320: }
321: SDL_Delay( 30 );
322: }
323:
324: SDL_FreeSurface( gfx );
325: }
326:
327: int loadMedia( void ) {
328: int result = 1;
329:
330: /*
331: * Load frogger's textures and sounds
332: */
333: gfx = IMG_Load( "images/frogger.png" );
334:
335: if ( gfx == NULL ) {
336: fprintf( stderr, "Error: 'images/frogger.bmp' could not be open: %s\n", SDL_GetError( ) );
337: result--;
338: }
339:
340: if ( SDL_SetColorKey( gfx, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( gfx->format, COLORKEY ) ) == -1 )
341: fprintf( stderr, "Warning: colorkey will not be used, reason: %s\n", SDL_GetError( ) );
342:
343: background = IMG_Load( "images/gameboard.png" );
344:
345: if ( gfx == NULL ) {
346: fprintf( stderr, "Error: 'images/gameboard.png' could not be open: %s\n", SDL_GetError( ) );
347: result--;
348: }
349:
350: titleSurface = IMG_Load( "images/froggix-title.png" );
351: if ( titleSurface == NULL ) {
352: fprintf( stderr, "Error: 'images/froggix-title.png' could not be open: %s\n", SDL_GetError( ) );
353: result--;
354: }
355:
356: font = TTF_OpenFont( "fonts/CourierNew-Bold.ttf", 22 );
357: if ( font == NULL ) {
358: printf( "TTF_OpenFont: %s\n", TTF_GetError( ) );
359: result--;
360: }
361:
362: frogger.s_hop = Mix_LoadWAV( "sounds/froggix-hop.wav" );
363:
364: if ( frogger.s_hop == NULL )
365: fprintf( stderr, "Warning: dp_frogger_hop.wav could not be opened: %s\n", SDL_GetError( ) );
366:
367: frogger.s_squash = Mix_LoadWAV( "sounds/dp_frogger_squash.wav" );
368: if ( frogger.s_squash == NULL )
369: fprintf( stderr, "Warning: dp_frogger_plunk could not be opened %s\n", SDL_GetError( ));
370:
371: frogger.s_splash = Mix_LoadWAV( "sounds/dp_frogger_plunk.wav" );
372: if ( frogger.s_splash == NULL )
373: fprintf( stderr, "Warning: dp_frogger_splash could not be opened %s\n", SDL_GetError( ));
374:
375: s_freeFrog = Mix_LoadWAV( "sounds/dp_frogger_extra.wav" );
376: if ( s_freeFrog == NULL )
377: fprintf( stderr, "Warning: dp_frogger_extra could not be opened %s\n", SDL_GetError( ));
378:
379: return result;
380: }
381:
382: /*
383: * void keyEvents( void );
384: *
385: * Process the incoming keyboard and mouse events
386: *
387: */
388: int keyEvents( SDL_Event event ) {
389: int done = 0;
390:
391: /* Always check for shutdown */
392: switch( event.type ) {
393: case SDL_QUIT:
394: done = 1;
395: break;
396: case SDL_KEYDOWN:
397: /* printf( "Found key: %i\n", event.key.keysym.sym );*/
398: switch( event.key.keysym.sym ) {
399: case SDLK_ESCAPE:
400: done = 1;
401: break;
402: case 102:
403: setFullScreenMode( );
404: break;
405: default:
406: break;
407: }
408: break;
409: default:
410: break;
411: }
412: /* We are playing the game */
413: if ( level ) {
414: /* Main game playing input */
415: if ( playing ) {
416: if ( event.type == SDL_KEYDOWN && frogger.alive ) {
417: switch( event.key.keysym.sym ) {
418: case SDLK_UP:
419: if ( ! frogger.direction ) {
420: frogger.hopCount = 0;
421: frogger.direction = UP;
422: frogger.currentRow++;
423: playSound( frogger.s_hop );
424: }
425: break;
426: case SDLK_DOWN:
427: if ( ! frogger.direction ) {
428: frogger.hopCount = 0;
429: frogger.direction = DOWN;
430: frogger.currentRow--;
431: playSound( frogger.s_hop );
432: }
433: break;
434: case SDLK_LEFT:
435: if ( ! frogger.direction ) {
436: frogger.hopCount = 0;
437: frogger.direction = LEFT;
438: playSound( frogger.s_hop );
439: }
440: break;
441: case SDLK_RIGHT:
442: if ( ! frogger.direction ) {
443: frogger.hopCount = 0;
444: frogger.direction = RIGHT;
445: playSound( frogger.s_hop );
446: }
447: break;
448: case 108:
449: levelUp( );
450: fprintf( stderr, "Increase level to %i.\n", level );
451: break;
452: default:
453: break;
454: }
455: printf( "x,y,d => %i,%i,%i,%i\n", frogger.placement[X],
456: frogger.placement[Y],
457: frogger.direction,
458: frogger.currentRow );
459: }
460: /* Game over man, game over! */
461: if ( ! lives ) {
462:
463: }
464: }
465: /* we're at the pause screen */
466: else {
467:
468: }
469: }
470: /* Main intro screen input */
471: else {
472: if ( event.type == SDL_KEYUP ) {
473: switch( event.key.keysym.sym ) {
474: case SDLK_ESCAPE:
475: done = 1;
476: break;
477: case SDLK_1:
478: printf( "D: Starting single player game\n" );
479: level = 1;
480: lives = LIVES;
481: playing = TRUE;
482: score = 0;
483: players = 1;
484: redraw_all = 1;
485: break;
486: default:
487: break;
488: }
489: }
490: }
491:
492: return done;
493: }
494:
495: int updateGameState( void ) {
496: /*
497: printf( "D: Updating game state\n" );
498: */
499: if ( ! drawBG ) configGameScreen( );
500:
501: if ( lives <= 0 ) {
502: goDelay++;
503: drawGameOver( );
504: /* Display game over screen for 50 ticks before returning
505: * to the main screen */
506: if ( goDelay > 7 ) {
507: playing = 0;
508: lives = 0;
509: level = 0;
510: }
511: return 500;
512: }
513:
514: drawGameScreen( );
515:
516: return 50;
517: }
518:
519: logObj setWood( int type, int speed, int row, int startX ) {
520: logObj tempWood;
521: int imgPixelSrc = 0;
522:
523: switch( type ) {
524: case LONG_LOG:
525: imgPixelSrc = 0;
526: break;
527: case MEDIUM_LOG:
528: imgPixelSrc = FRAME * LONG_LOG;
529: break;
530: case SHORT_LOG:
531: imgPixelSrc = FRAME * ( LONG_LOG + MEDIUM_LOG );
532: break;
533: }
534:
535: tempWood.row = row;
536: tempWood.type = type;
537: tempWood.speed = speed;
538: tempWood.hasPink = 0;
539: tempWood.placement[X] = LEFT_SIDE + startX;
540: tempWood.placement[Y] = getRowPixel( row );
541: tempWood.oldPlacement[X] = LEFT_SIDE + startX;
542: tempWood.oldPlacement[Y] = getRowPixel( row );
543: tempWood.src.y = FRAME;
544: tempWood.src.x = imgPixelSrc;
545: tempWood.src.w = FRAME * tempWood.type;
546: tempWood.src.h = FRAME;
547:
548: return tempWood;
549: }
550:
551: turtleObj setTurtle( int dive, int diveTimer, int speed, int row, int startX, int count ) {
552: turtleObj tt;
553:
554: tt.row = row;
555: tt.canDive = dive;
556: tt.diveStep = 0;
557: tt.diveTime = diveTimer;
558: tt.animStep = 0;
559: tt.animDelay = 0;
560: tt.speed = speed;
561: tt.count = count;
562: tt.placement[X] = LEFT_SIDE + startX;
563: tt.placement[Y] = getRowPixel( row );
564: tt.oldPlacement[X] = tt.placement[X];
565: tt.oldPlacement[Y] = tt.placement[Y];
566: tt.src.y = FRAME * 2;
567: tt.src.x = 0;
568: tt.src.w = FRAME;
569: tt.src.h = FRAME;
570:
571: return tt;
572: }
573:
574:
575: vehicleObj setVehicle( int row, int startX, int speed, int level ) {
576: vehicleObj v;
577:
578: v.direction = ( row % 2 ) ? LEFT : RIGHT; /* Odd rows travel left, evens go right */
579: v.row = row;
580: v.speed = speed;
581: v.level = level;
582: v.placement[X] = LEFT_SIDE + startX;
583: v.placement[Y] = getRowPixel( row );
584: v.oldPlacement[X] = v.placement[X];
585: v.oldPlacement[Y] = v.placement[Y];
586: v.src.y = FRAME * 2;
587: v.src.x = FRAME * ( 4 + row );
588: v.src.w = ( row == 5 ) ? FRAME * 2 : FRAME; /* Are we a truck? */
589: v.src.h = FRAME;
590:
591: return v;
592: }
593:
594: goalObj setGoals( int x, int y, int w, int h ) {
595: goalObj g;
596:
597: g.x = x;
598: g.y = y;
599: g.w = w;
600: g.h = h;
601: g.occupied = 0;
602: g.fly = 0;
603: g.gator = 0;
604:
605: return g;
606: }
607:
608: void configGameScreen( void ) {
609: drawBG = 1;
610:
611: /*
612: * Draw background map
613: */
614: //drawBackground( );
615: drawImage( background, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, screen, 0, 0, 255 );
616:
617: /* Cars drive on rows 1 - 5
618: * Logs are on rows 8, 9 and 11, 8 = short, 9 long, 11 medium
619: * Turtles are on rows 7, 10
620: * Frogger starts on Row 0,
621: * Sidewalk is row 6
622: * and the goal is row 12
623: */
624:
625: /* I MUST figure out a better way to handle the logs, turtles and cars */
626:
627: /* Set up the LONG logs on row 9 first */
628: wood[0] = setWood( LONG_LOG, 3, 9, 0 );
629: wood[1] = setWood( LONG_LOG, 3, 9, 305 );
630: wood[2] = setWood( SHORT_LOG, 2, 8, 25 );
631: wood[3] = setWood( SHORT_LOG, 2, 8, 160 );
632: wood[4] = setWood( SHORT_LOG, 2, 8, 380 );
633: wood[5] = setWood( MEDIUM_LOG, 4, 11, 140 );
634: wood[6] = setWood( MEDIUM_LOG, 4, 11, 440 );
635:
636: drawWood( );
637:
638: /* Configure our turtles */
639: turtle[0] = setTurtle( FALSE, 0, 1, 7, 0, 3 );
640: turtle[1] = setTurtle( TRUE, 0, 1, 7, 125, 3 );
641: turtle[2] = setTurtle( FALSE, 0, 1, 7, 250, 3 );
642: turtle[3] = setTurtle( TRUE, 30, 1, 7, 375, 3 );
643: turtle[4] = setTurtle( FALSE, 0, 2, 10, 100, 2 );
644: turtle[5] = setTurtle( TRUE, 50, 2, 10, 200, 2 );
645: turtle[6] = setTurtle( FALSE, 0, 2, 10, 300, 2 );
646: turtle[7] = setTurtle( TRUE, 10, 2, 10, 400, 2 );
647: turtle[8] = setTurtle( FALSE, 0, 2, 10, 500, 2 );
648:
649: drawTurtles( );
650:
651: /* Configure vehicles */
652: /* row, X, speed */
653: /* Row 1 -- yellow car */
654: vehicle[0] = setVehicle( 1, 0, 1, 1 );
655: vehicle[1] = setVehicle( 1, 100, 1, 3 );
656: vehicle[2] = setVehicle( 1, 200, 1, 1 );
657: vehicle[3] = setVehicle( 1, 300, 1, 1 );
658: // /* Row 2 -- tractor */
659: vehicle[4] = setVehicle( 2, 0, 3, 1 );
660: vehicle[5] = setVehicle( 2, 100, 3, 2 );
661: vehicle[6] = setVehicle( 2, 200, 3, 1 );
662: vehicle[7] = setVehicle( 2, 300, 3, 3 );
663: // /* Row 3 -- pink car */
664: vehicle[8] = setVehicle( 3, 75, 2,1 );
665: vehicle[9] = setVehicle( 3, 150, 2, 3 );
666: vehicle[10] = setVehicle( 3, 225, 2, 1 );
667: vehicle[11] = setVehicle( 3, 375, 2, 2 );
668: // /* Row 4 -- white car */
669: vehicle[12] = setVehicle( 4, 75, 5, 1 );
670: vehicle[13] = setVehicle( 4, 150, 5, 3 );
671: vehicle[14] = setVehicle( 4, 225, 5, 2 );
672: vehicle[15] = setVehicle( 4, 375, 5, 3 );
673: // /* Row 5 -- Trucks */
674: vehicle[16] = setVehicle( 5, 30, 3, 1 );
675: vehicle[17] = setVehicle( 5, 150, 3, 1 );
676: vehicle[18] = setVehicle( 5, 250, 3, 1 );
677: vehicle[19] = setVehicle( 5, 350, 3, 3 );
678:
679: drawVehicles( );
680:
681: /* Configure the goals for frogger */
682: goals[0] = setGoals( LEFT_SIDE + 3, 55, 43, 35 );
683: goals[1] = setGoals( LEFT_SIDE + 91, 55, 43, 35 );
684: goals[2] = setGoals( LEFT_SIDE + 179, 55, 43, 35 );
685: goals[3] = setGoals( LEFT_SIDE + 267, 55, 43, 35 );
686: goals[4] = setGoals( LEFT_SIDE + 355, 55, 43, 35 );
687:
688: /*
689: * Configure the left and right side black borders to conceal logs,
690: * turtles, cars, etc. that go past their boundries
691: */
692: leftBorderRect.x = 0;
693: leftBorderRect.y = 0;
694: leftBorderRect.w = LEFT_SIDE;
695: leftBorderRect.h = SCREEN_HEIGHT;
696:
697: rightBorderRect.x = RIGHT_SIDE;
698: rightBorderRect.y = 0;
699: rightBorderRect.w = SCREEN_WIDTH - RIGHT_SIDE;
700: rightBorderRect.h = SCREEN_HEIGHT;
701:
702:
703: /*
704: * Draw frogger in starting position
705: */
706: froggerReset( );
707: }
708:
709: void drawGameScreen( void ) {
710: /*
711: * Update frogger
712: */
713: if ( frogger.direction ) moveFrogger( );
714: if ( frogger.riding ) ridingFrogger( );
715:
716: /* Check for collisions with frogger */
717: if ( frogger.alive ) {
718: if ( frogger.currentRow > 6 && frogger.currentRow < 12 ) {
719: if ( ( ! collisionRow( ) ) || ( frogger.riding == FALSE ) ) {
720: playSound( frogger.s_splash );
721: frogger.deathType = SPLASH;
722: fprintf( stderr, "D: Frog in water!!\n" );
723: frogger.alive = FALSE;
724: }
725: }
726: else if ( frogger.currentRow == 12 ) {
727: if ( collisionRow( ) ) {
728: playSound( frogger.s_squash );
729: frogger.deathType = SPLAT;
730: fprintf( stderr, "D: Frog in thorn bushes!\n" );
731: frogger.alive = FALSE;
732: }
733: }
734: else if( collisionRow( ) ) {
735: playSound( frogger.s_squash );
736: frogger.deathType = SPLAT;
737: fprintf( stderr, "D: Frog Squashed!\n" );
738: frogger.alive = FALSE;
739: }
740: }
741:
742: /*
743: * Update and draw everthing else
744: */
745: drawImage( background, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, screen, 0, 0, 255 );
746: drawScore( 0, score );
747: drawScore( 1, hScore );
748: drawGoals( );
749: drawLives( lives );
750: drawLevel( level );
751: drawWood( );
752: drawTurtles( );
753: drawVehicles( );
754:
755: if ( frogger.alive == FALSE ) {
756: if ( ! drawDeathSequence( frogger.deathType ) ) {
757: lives--;
758: if ( lives < 0 ) { drawGameOver( ); }
759: else { froggerReset( ); }
760: }
761: }
762:
763: if ( frogger.alive ) {
764: frogger.alive = checkTimer( );
765: drawImage( gfx, frogger.src.x, frogger.src.y, frogger.src.w,
766: frogger.src.h, screen, frogger.dst.x, frogger.dst.y, 255 );
767: }
768: if ( ! debugBorder ) {
769: SDL_FillRect( screen, &leftBorderRect, SDL_MapRGB( screen->format, BGCOLOR ) );
770: SDL_FillRect( screen, &rightBorderRect, SDL_MapRGB( screen->format, BGCOLOR ) );
771: }
772:
773: SDL_Flip( screen );
774: }
775:
776: void drawBackground( void ) {
777: /*
778: * Draw background map
779: */
780: backgroundRect.x = 0;
781: backgroundRect.y = 0;
782: SDL_BlitSurface( background, NULL, screen, &backgroundRect );
783: SDL_UpdateRect( screen, 0, 0, 0, 0 );
784: }
785:
786: /*
787: * This calculates the pixel top of the requested row
788: */
789: int getRowPixel ( int row ) {
790: return ROW_BASE - ( row * HOP_DISTANCE );
791: }
792:
793: /*
794: * This does collision detection based on the row frogger
795: * is in to help reduce overhead
796: */
797: int collisionRow ( void ) {
798: int i;
799:
800: if ( frogger.currentRow <= 0 ) return 0;
801:
802: /* Check collision with cars */
803: if ( frogger.currentRow < 6 ) {
804: for( i = 0; i < MAX_VEHICLES; i++ ) {
805: if ( level < vehicle[i].level ) return 0;
806: int length = ( vehicle[i].row == 5 ) ? FRAME * 2 : FRAME; /* Trucks */
807: if ( collideFrogger( vehicle[i].placement[X],
808: vehicle[i].placement[Y], FRAME,
809: length ) ) {
810: return 1;
811: }
812: }
813: return 0;
814: }
815: /* check for collisions with turtles, logs, etc.. */
816: else if ( frogger.currentRow > 6 && frogger.currentRow < 12 ) {
817: /* here a collision is good, else death */
818: for( i = 0; i < MAX_TURTLES; i++ ) {
819: if ( collideFrogger( turtle[i].placement[X],
820: turtle[i].placement[Y], FRAME,
821: FRAME * turtle[i].count ) ) {
822: frogger.riding = ( turtle[i].diveStep == 3 ) ? FALSE : LEFT;
823: frogger.ridingIdx = i;
824: frogger.ridingType = TURTLE;
825: return 1;
826: }
827: }
828: for( i = 0; i < MAX_WOOD; i++ ) {
829: if ( collideFrogger( wood[i].placement[X],
830: wood[i].placement[Y], FRAME,
831: FRAME * wood[i].type ) ) {
832: frogger.riding = RIGHT;
833: frogger.ridingIdx = i;
834: frogger.ridingType = LOG;
835: return 1;
836: }
837: }
838: }
839: /* We're on the path, if the snake is active, check that */
840: else if ( frogger.currentRow == 6 ) {
841: frogger.riding = FALSE; /*in case we hopped off a turtle */
842: }
843: /* This leaves the goal area only */
844: else {
845: for ( i = 0; i < MAX_GOALS; i++ ) {
846: if ( collideFrogger( goals[i].x, goals[i].y, goals[i].w, goals[i].h ) ) {
847: if ( goals[i].occupied ) return 1;
848: goals[i].occupied++;
849: /* playSound( s_goal ); */
850: score += SCORE_GOAL;
851: score += ( ( int ) ( timeLeft / 10 ) ) * 10;
852: lives += freeFrog( score );
853: froggerReset( );
854: if ( checkGoals( ) ) levelUp( );
855: return 0;
856: }
857: }
858: return 1;
859: }
860:
861: return 0;
862: }
863:
864: /* If the player gets enough points, award them a free frog */
865: int freeFrog ( int score ) {
866: if ( givenFreeFrog ) return 0;
867: if ( score >= SCORE_FREE_FROG ) {
868: givenFreeFrog++;
869: playSound( s_freeFrog );
870: return 1;
871: }
872: return 0;
873: }
874:
875: /* Check what frogger is colliding with */
876: int collideFrogger ( int x, int y, int h, int w ) {
877: h++; w++;
878:
879: if ( ( frogger.placement[Y] >= ( y + h ) ) ||
880: ( frogger.placement[X] >= ( x + w ) ) ||
881: ( y >= ( frogger.placement[Y] + FRAME ) ) ||
882: ( x >= ( frogger.placement[X] + FRAME ) ) ) {
883: return( 0 );
884: }
885: return( 1 );
886: }
887:
888: /* Check left and right borders */
889: void checkFroggerBorder( void ) {
890: if ( frogger.placement[Y] - 5 >= getRowPixel( 0 ) ) {
891: frogger.placement[Y] = frogger.oldPlacement[Y];
892: frogger.currentRow = 0;
893: }
894:
895: if ( ( frogger.placement[X] <= LEFT_SIDE ) ||
896: ( frogger.placement[X] + frogger.src.w >= RIGHT_SIDE ) ) {
897: if ( ( frogger.currentRow == 0 ) ||
898: ( frogger.currentRow == 6 ) ) {
899: frogger.placement[X] = frogger.oldPlacement[X];
900: }
901: else {
902: frogger.alive = FALSE;
903: }
904: }
905: }
906:
907: void levelUp ( void ) {
908: int i;
909:
910: fprintf ( stderr, "Level %i beat! ", level );
911:
912: level++;
913: score += SCORE_LEVEL;
914: lives += freeFrog( score );
915: froggerReset( );
916: /* Play sounds */
917:
918: /* Empty goals */
919: for ( i = 0; i < MAX_GOALS; i++ ) goals[i].occupied = 0;
920:
921: /* Speed things up */
922: vehicle[0].speed = level;
923: vehicle[1].speed = level;
924: vehicle[2].speed = level;
925: vehicle[3].speed = level;
926:
927: fprintf (stderr, "Starting level %i!\n", level );
928: }
929:
930: int checkGoals ( void ) {
931: int savedFrogs = 0;
932: int i;
933:
934: for ( i = 0; i < MAX_GOALS; i++ ) {
935: if ( goals[i].occupied ) savedFrogs++;
936: }
937:
938: drawGoals( );
939:
940: return ( savedFrogs >= 5 ) ? 1 : 0;
941: }
942:
943: void froggerReset ( void ) {
944: timeLeft = MAX_TIMER;
945:
946: frogger.placement[X] = FROGGER_START_X;
947: frogger.placement[Y] = FROGGER_START_Y;
948: frogger.oldPlacement[X] = FROGGER_START_X;
949: frogger.oldPlacement[Y] = FROGGER_START_Y;
950: frogger.hopCount = 0;
951: frogger.direction = 0;
952: frogger.currentRow = 0;
953: frogger.alive = TRUE;
954: frogger.riding = FALSE;
955: frogger.deathType = 0; /* Death type SPLAT or SPLASH */
956: frogger.deathCount = 0; /* death animation timer */
957:
958: frogger.src.y = 0;
959: frogger.src.x = 0;
960: frogger.src.w = FRAME;
961: frogger.src.h = FRAME;
962: frogger.dst.y = frogger.placement[Y];
963: frogger.dst.x = frogger.placement[X];
964:
965: drawImage( gfx, frogger.src.x, frogger.src.y, frogger.src.w,
966: frogger.src.h, screen, frogger.dst.x, frogger.dst.y, 255 );
967: }
968:
969: /*
970: * This actually moves frogger... I need to come up with a better
971: * algorithm for calculating the distance and time
972: */
973: void moveFrogger( void ) {
974: int currentFrame = 0;
975: int x = 0;
976: int y = 0;
977: int h = FRAME;
978: int w = FRAME;
979: int frameLow = HOP_SPEED / 3;
980: int frameHigh = frameLow * 2;
981:
982: /* Determine which frame of frogger to display */
983: if ( ( frogger.hopCount >= frameLow ) && ( frogger.hopCount <= frameHigh ) )
984: currentFrame = FRAME;
985:
986: frogger.oldPlacement[Y] = frogger.placement[Y];
987: frogger.oldPlacement[X] = frogger.placement[X];
988:
989: switch( frogger.direction ) {
990: case UP:
991: x = currentFrame;
992: frogger.placement[Y] -= ( HOP_DISTANCE / HOP_SPEED );
993: break;
994: case DOWN:
995: x = currentFrame + ( 4 * FRAME );
996: frogger.placement[Y] += ( HOP_DISTANCE / HOP_SPEED );
997: break;
998: case LEFT:
999: x = currentFrame + ( 6 * FRAME );
1000: frogger.placement[X] -= ( HOP_DISTANCE / HOP_SPEED );
1001: break;
1002: case RIGHT:
1003: x = currentFrame + ( 2 * FRAME );
1004: frogger.placement[X] += ( HOP_DISTANCE / HOP_SPEED );
1005: break;
1006: }
1007:
1008: checkFroggerBorder( );
1009:
1010: /* select the frame to display */
1011: frogger.src.y = y;
1012: frogger.src.x = x;
1013: frogger.src.w = w;
1014: frogger.src.h = h;
1015:
1016: /* Set the old place to be erased */
1017: frogger.dst.y = frogger.oldPlacement[Y];
1018: frogger.dst.x = frogger.oldPlacement[X];
1019:
1020: SDL_FillRect( screen, NULL, SDL_MapRGB( screen->format, BGCOLOR ) );
1021:
1022: /* Place the new position */
1023: frogger.dst.y = frogger.placement[Y];
1024: frogger.dst.x = frogger.placement[X];
1025:
1026: frogger.hopCount++;
1027:
1028: if ( frogger.hopCount >= HOP_SPEED ) {
1029: frogger.hopCount = 0;
1030: frogger.direction = FALSE;
1031: score += SCORE_HOP;
1032: lives += freeFrog( score );
1033: }
1034: }
1035:
1036: void ridingFrogger( void ) {
1037: int speed = 0;
1038:
1039: if ( frogger.hopCount > 0 ) return;
1040:
1041: switch( frogger.ridingType ) {
1042: case LOG:
1043: speed = wood[frogger.ridingIdx].speed;
1044: break;
1045: case TURTLE:
1046: speed = turtle[frogger.ridingIdx].speed + 2;
1047: break;
1048: }
1049:
1050: switch( frogger.riding ) {
1051: case LEFT:
1052: frogger.oldPlacement[X] = frogger.placement[X];
1053: frogger.placement[X] -= speed;
1054: frogger.dst.x = frogger.placement[X];
1055: break;
1056: case RIGHT:
1057: frogger.oldPlacement[X] = frogger.placement[X];
1058: frogger.placement[X] += speed;
1059: frogger.dst.x = frogger.placement[X];
1060: break;
1061: }
1062:
1063: checkFroggerBorder( );
1064: }
1065:
1066: void drawTitleScreen( void ) {
1067: SDL_Surface *introText;
1068: SDL_Color fontColor = { 123, 158, 53, 255 };
1069: int center = ( SCREEN_WIDTH / 2 ) - ( titleSurface->w / 2 );
1070: int i;
1071: char *txt[] = { "Press 1 for single player game",
1072: "Press 2 for two player games",
1073: "Press F for full screen mode",
1074: "Press ESC to quit" };
1075:
1076: // drawBackground( );
1077:
1078: drawImage( titleSurface, 0, 0, titleSurface->w,
1079: titleSurface->h, screen, center, 100, 255 );
1080:
1081: for( i = 0; i <= 3; i++ ) {
1082: introText = TTF_RenderText_Solid( font, txt[i], fontColor );
1083: drawImage( introText, 0, 0, introText->w, introText->h, screen,
1084: 140, 300 + ( i * introText->h ), 255 );
1085: }
1086:
1087: SDL_Flip( screen );
1088: }
1089:
1090: void drawPauseScreen( void ) {
1091: printf( "D: Draw Pause Screen\n" );
1092:
1093: }
1094:
1095: void drawGameOver( void ) {
1096: printf( "D: Game Over\n" );
1097: }
1098:
1099: void playSound( Mix_Chunk *sound ) {
1100: Mix_PlayChannel( -1, sound, 0 );
1101: }
1102:
1103: void setFullScreenMode( void ) {
1104: /* Lets give fullscreen mode a try */
1105: if ( ! fullscreen ) {
1106: screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 16, SDL_HWSURFACE | SDL_FULLSCREEN );
1107: fullscreen = TRUE;
1108: }
1109: /* Switch back to window mode */
1110: else {
1111: screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 16, SDL_HWSURFACE );
1112: fullscreen = FALSE;
1113: }
1114:
1115: printf( "D: Fullscreen : %i\n", fullscreen );
1116: }
1117:
1118: int drawDeathSequence( int deathType ) {
1119: int animDelay = 55;
1120: int animOffset = ( deathType == SPLAT ) ? 8 : 11;
1121: int dFrame = 0;
1122:
1123: if ( frogger.deathCount < 7 ) dFrame = 0;
1124: if ( frogger.deathCount >= 7 ) dFrame = 1;
1125: if ( frogger.deathCount >= 14) dFrame = 2;
1126: if ( frogger.deathCount > 20 ) animOffset = 12;
1127:
1128: frogger.deathCount++;
1129: if ( frogger.deathCount >= animDelay )
1130: return 0; /* we're done with death */
1131:
1132: drawImage( gfx, FRAME * ( animOffset + dFrame ), 0, FRAME, FRAME,
1133: screen, frogger.placement[X], frogger.placement[Y], 255 );
1134:
1135: return 1;
1136: }
1137:
1138: /* draw green timer and return 0 if out of time */
1139: int checkTimer( void ) {
1140: float lvl = level;
1141: int step = ( int ) ( lvl / 2 );
1142: if ( step < 1 ) step = 1;
1143: if ( step > 3 ) step = 3;
1144: timeLeft -= step;
1145:
1146: drawTimer( (int) ( ( timeLeft / MAX_TIMER ) * TIMER_SIZE ) );
1147:
1148: if ( timeLeft <= 0 ) return 0;
1149: return 1;
1150: }
1151:
1152: void drawScore( int high, int score ) {
1153: int x = 169;
1154: int y = 14;
1155:
1156: if ( score > hScore ) hScore = score;
1157: if ( high ) x = 260;
1158:
1159: drawNumbers( score, x, y );
1160: }
1161:
1162: void drawNumbers( int num, int x, int y ) {
1163: char numStr[6] = "00000";
1164: int i;
1165:
1166: /* Assume anything less than 50 pixels location is a score and
1167: * pad with '0'.
1168: */
1169: if ( y <= 15 ) sprintf( numStr, "%05i", num );
1170: else sprintf( numStr, "%i", num );
1171:
1172: for ( i = 0; i <= 4; i++ ) {
1173: char c = numStr[i];
1174: int n = atoi( &c );
1175:
1176: drawImage( gfx, n * HFRAME, FRAME * 3 , HFRAME, FRAME,
1177: screen, x + ( i * HFRAME ) + 2, y, 255 );
1178: }
1179: }
1180:
1181: /* Normally this functions manages and draws the flys, gators and saved frogs
1182: * but if drawDebugRect below is turned on it will draw the rectangle
1183: * used for collision detectiong for debuggin purposes in timer green
1184: */
1185: void drawGoals( void ) {
1186: int drawDebugRect = 0;
1187: int i;
1188:
1189: for ( i = 0; i < MAX_GOALS; i++ ) {
1190: if ( drawDebugRect ) {
1191: SDL_Rect d;
1192: d.x = goals[i].x;
1193: d.y = goals[i].y;
1194: d.w = goals[i].w;
1195: d.h = goals[i].h;
1196:
1197: SDL_FillRect( screen, &d, SDL_MapRGB( screen->format, TIMER_COLOR ) );
1198: }
1199:
1200: if ( goals[i].occupied )
1201: drawImage( gfx, FRAME * 15, 0, FRAME, FRAME,
1202: screen, goals[i].x + 13, goals[i].y + 5, 255 );
1203: }
1204: }
1205:
1206: void drawTimer( int length ) {
1207: SDL_Rect timerRect;
1208:
1209: timerRect.x = RIGHT_SIDE - 60 - length;
1210: timerRect.y = 465;
1211: timerRect.w = length;
1212: timerRect.h = 15;
1213:
1214: SDL_FillRect( screen, &timerRect, SDL_MapRGB( screen->format, TIMER_COLOR ) );
1215: }
1216:
1217: void drawLives( int lives ) {
1218: int i;
1219: int lifeFroggerSize = 16;
1220:
1221: for( i = 0; i <= lives - 2; i++ ) {
1222: drawImage( gfx, FRAME * 11, FRAME * 2, lifeFroggerSize, FRAME,
1223: screen, LEFT_SIDE + ( lifeFroggerSize * i ), 450, 255 );
1224: }
1225: }
1226:
1227: void drawLevel( int level ) {
1228: int i;
1229: int levelImageSize = 12;
1230:
1231: for( i = 0; i <= level - 1; i++ ) {
1232: drawImage( gfx, FRAME * 12, FRAME * 2, levelImageSize, FRAME,
1233: screen, RIGHT_SIDE - levelImageSize - ( levelImageSize * i ), 450, 255 );
1234: }
1235: }
1236:
1237: void drawWood ( void ) {
1238: int i;
1239:
1240: for ( i = 0; i < MAX_WOOD; i++ ) {
1241: if ( wood[i].placement[X] > ( RIGHT_SIDE + 5 ) )
1242: wood[i].placement[X] = LEFT_SIDE - wood[i].src.w - 5;
1243: wood[i].placement[X] += wood[i].speed;
1244: drawImage( gfx, wood[i].src.x, wood[i].src.y,
1245: wood[i].src.w, wood[i].src.h,
1246: screen, wood[i].placement[X],
1247: wood[i].placement[Y], 255 );
1248: }
1249: }
1250:
1251: void drawTurtles ( void ) {
1252: int i = 0;
1253: int n = 0;
1254: int animFrame = 0;
1255:
1256: for ( i = 0; i < MAX_TURTLES; i++ ) {
1257: /* This managed the turtles basic 3 frames of animation */
1258: animFrame = turtle[i].animStep;
1259: if ( turtle[i].animDelay >= TURTLE_ANIM_TIME ) {
1260: turtle[i].animDelay = 0;
1261: turtle[i].animStep++;
1262: if ( turtle[i].animStep > 2 ) turtle[i].animStep = 0;
1263: }
1264: else {
1265: turtle[i].animDelay++;
1266: }
1267:
1268: /* If a set of turtles have dive capability, this enables that */
1269: if ( turtle[i].canDive ) {
1270: turtle[i].diveTime++;
1271: /* Check if turtle is diving */
1272: if ( turtle[i].diveStep > 0 ) {
1273: switch( turtle[i].diveStep ) {
1274: case 1:
1275: animFrame = 3;
1276: break;
1277: case 2:
1278: animFrame = 4;
1279: break;
1280: case 4:
1281: animFrame = 4;
1282: break;
1283: case 5:
1284: animFrame = 3;
1285: break;
1286: case 6:
1287: turtle[i].diveStep = 0;
1288: turtle[i].diveTime = 0;
1289: break;
1290: default:
1291: animFrame = 4;
1292: break;
1293: }
1294:
1295: if ( turtle[i].diveTime > DIVE_START_TIME + ( DIVE_PHASE_TIME * turtle[i].diveStep ) )
1296: turtle[i].diveStep++;
1297:
1298: }
1299: else {
1300: if ( turtle[i].diveTime > DIVE_START_TIME )
1301: turtle[i].diveStep++;
1302: }
1303: }
1304:
1305: /* Display out turtles */
1306: for ( n = 0; n <= ( turtle[i].count - 1 ); n++ ) {
1307: turtle[i].placement[X] -= turtle[i].speed;
1308: if ( turtle[i].placement[X] <= LEFT_SIDE - ( turtle[i].count * FRAME ) + 10 )
1309: turtle[i].placement[X] = RIGHT_SIDE + 10;
1310: if ( turtle[i].diveStep != 4 )
1311: drawImage( gfx, turtle[i].src.x + ( FRAME * animFrame ),
1312: turtle[i].src.y, turtle[i].src.w, turtle[i].src.h,
1313: screen, turtle[i].placement[X] + ( ( FRAME + 3 ) * n ),
1314: turtle[i].placement[Y], 255 );
1315: }
1316: }
1317: }
1318:
1319: void drawVehicles ( void ) {
1320: int i;
1321:
1322: for ( i = 0; i < MAX_VEHICLES; i++ ) {
1323: if ( vehicle[i].direction == RIGHT ) {
1324: if ( vehicle[i].placement[X] > ( RIGHT_SIDE + 5 ) )
1325: vehicle[i].placement[X] = LEFT_SIDE - vehicle[i].src.w - 5;
1326: vehicle[i].placement[X] += vehicle[i].speed;
1327: }
1328: else {
1329: if ( vehicle[i].placement[X] < ( LEFT_SIDE - 5 ) )
1330: vehicle[i].placement[X] = RIGHT_SIDE + vehicle[i].src.w + 5;
1331: vehicle[i].placement[X] -= vehicle[i].speed;
1332: }
1333:
1334: if ( level >= vehicle[i].level )
1335: drawImage( gfx, vehicle[i].src.x, vehicle[i].src.y,
1336: vehicle[i].src.w, vehicle[i].src.h,
1337: screen, vehicle[i].placement[X],
1338: vehicle[i].placement[Y], 255 );
1339: }
1340: }
1341:
1342: void drawImage( SDL_Surface *srcimg, int sx, int sy, int sw, int sh,
1343: SDL_Surface *dstimg, int dx, int dy, int alpha ) {
1344: if ((!srcimg) || (alpha == 0)) return;
1345: SDL_Rect src, dst;
1346:
1347: src.x = sx; src.y = sy; src.w = sw; src.h = sh;
1348: dst.x = dx; dst.y = dy; dst.w = src.w; dst.h = src.h;
1349:
1350: if (alpha != 255) SDL_SetAlpha(srcimg, SDL_SRCALPHA, alpha);
1351: SDL_BlitSurface(srcimg, &src, dstimg, &dst);
1352: }
1353:
1354: int heartbeat ( void ) {
1355: int ticks;
1356: if ( level ) {
1357: if ( playing ) {
1358: ticks = updateGameState( );
1359: if ( ticks <= 0 ) ticks = 50;
1360: return ticks;
1361: }
1362: else {
1363: drawPauseScreen( );
1364: return 500;
1365: }
1366: }
1367: else {
1368: drawTitleScreen( );
1369: return 500;
1370: }
1371:
1372: return 50;
1373: }
1374:
1375: /*
1376: * Main program starts here. We'll init the video and audio
1377: * and then begin our main program loop here
1378: *
1379: */
1380: int main ( int argc, char **argv ) {
1381: if ( mySDLInit( ) <= 0 ) {
1382: fprintf( stderr, "Failure to start froggix\n" );
1383: return 255;
1384: }
1385:
1386: beginGame( );
1387:
1388: SDL_Quit( );
1389:
1390: return 0;
1391: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>