7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - January 1st, 2004
23 #include <sys/types.h>
32 #include "high_scores.h"
40 #include "collision.h"
42 /* extern variables */
44 extern char* soundfilenames[NUM_SOUNDS];
46 /* Local variables: */
48 texture_type img_waves[3], img_water, img_pole, img_poletop, img_flag[2];
49 texture_type img_cloud[2][4];
52 char level_subset[100];
56 /* Local function prototypes: */
58 void levelintro(void);
60 void loadlevelsong(void);
61 void unloadlevelsong(void);
62 void loadshared(void);
63 void unloadshared(void);
64 void drawstatus(void);
65 void drawendscreen(void);
66 void drawresultscreen(void);
74 sprintf(str, "LEVEL %d", level);
75 drawcenteredtext(str, 200, letters_red, NO_UPDATE, 1);
77 sprintf(str, "%s", current_level.name);
78 drawcenteredtext(str, 224, letters_gold, NO_UPDATE, 1);
80 sprintf(str, "TUX x %d", tux.lives);
81 drawcenteredtext(str, 256, letters_blue, NO_UPDATE, 1);
89 void start_timers(void)
91 timer_start(&time_left,current_level.time_left*1000);
94 void activate_bad_guys(void)
98 /* Activate bad guys: */
100 for (y = 0; y < 15; y++)
102 for (x = 0; x < current_level.width; x++)
104 if (current_level.tiles[y][x] >= '0' && current_level.tiles[y][x] <= '9')
106 add_bad_guy(x * 32, y * 32, current_level.tiles[y][x] - '0');
107 current_level.tiles[y][x] = '.';
114 /* --- GAME EVENT! --- */
116 void game_event(void)
118 while (SDL_PollEvent(&event))
122 case SDL_QUIT: /* Quit event - quit: */
125 case SDL_KEYDOWN: /* A keypress! */
126 key = event.key.keysym.sym;
128 /* Check for menu-events, if the menu is shown */
134 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
144 tux.input.right = DOWN;
147 tux.input.left = DOWN;
153 tux.input.down = DOWN;
156 tux.input.fire = DOWN;
162 case SDL_KEYUP: /* A keyrelease! */
163 key = event.key.keysym.sym;
168 tux.input.right = UP;
192 if(debug_mode == YES)
193 tux.size = !tux.size;
196 if(debug_mode == YES)
200 if(debug_mode == YES)
204 if(debug_mode == YES)
208 if(debug_mode == YES)
209 timer_start(&tux.invincible_timer,TUX_INVINCIBLE_TIME);
216 case SDL_JOYAXISMOTION:
217 switch(event.jaxis.axis)
220 if (event.jaxis.value < -256)
221 tux.input.left = DOWN;
225 if (event.jaxis.value > 256)
226 tux.input.right = DOWN;
228 tux.input.right = UP;
231 if (event.jaxis.value > 256)
232 tux.input.down = DOWN;
236 /* Handle joystick for the menu */
239 if(tux.input.down == DOWN)
240 menuaction = MN_DOWN;
249 case SDL_JOYBUTTONDOWN:
250 if (event.jbutton.button == JOY_A)
252 else if (event.jbutton.button == JOY_B)
253 tux.input.fire = DOWN;
255 case SDL_JOYBUTTONUP:
256 if (event.jbutton.button == JOY_A)
258 else if (event.jbutton.button == JOY_B)
274 /* --- GAME ACTION! --- */
276 int game_action(void)
280 /* (tux_dying || next_level) */
281 if (tux.dying || next_level)
283 /* Tux either died, or reached the end of a level! */
292 /* End of a level! */
302 /* No more lives!? */
308 if (score > highscore)
314 } /* if (lives < 0) */
317 /* Either way, (re-)load the (next) level... */
319 player_level_begin(&tux);
321 loadlevel(¤t_level,"default",level);
324 loadlevelgfx(¤t_level);
333 /* Handle bouncy distros: */
335 for (i = 0; i < NUM_BOUNCY_DISTROS; i++)
337 bouncy_distro_action(&bouncy_distros[i]);
341 /* Handle broken bricks: */
343 for (i = 0; i < NUM_BROKEN_BRICKS; i++)
345 broken_brick_action(&broken_bricks[i]);
349 /* Handle distro counting: */
351 if (counting_distros == YES)
355 if (distro_counter <= 0)
356 counting_distros = -1;
360 /* Handle bouncy bricks: */
362 for (i = 0; i < NUM_BOUNCY_BRICKS; i++)
364 bouncy_brick_action(&bouncy_bricks[i]);
368 /* Handle floating scores: */
370 for (i = 0; i < NUM_FLOATING_SCORES; i++)
372 floating_score_action(&floating_scores[i]);
376 /* Handle bullets: */
378 for (i = 0; i < NUM_BULLETS; ++i)
380 bullet_action(&bullets[i]);
384 /* Handle background timer: */
390 /* Handle upgrades: */
392 for (i = 0; i < NUM_UPGRADES; i++)
394 upgrade_action(&upgrades[i]);
395 } /* for (i = 0; i < NUM_UPGRADES; i++) */
398 /* Handle bad guys: */
400 for (i = 0; i < NUM_BAD_GUYS; i++)
402 badguy_action(&bad_guys[i]);
405 /* Handle all possible collisions. */
411 /* --- GAME DRAW! --- */
419 if (tux.dying && (frame % 4) == 0)
420 clearscreen(255, 255, 255);
423 if (super_bkgd_time == 0)
424 clearscreen(current_level.bkgd_red, current_level.bkgd_green, current_level.bkgd_blue);
426 texture_draw(&img_super_bkgd, 0, 0, NO_UPDATE);
429 /* Draw background: */
431 for (y = 0; y < 15; y++)
433 for (x = 0; x < 21; x++)
435 drawshape(x * 32 - ((int)scroll_x % 32), y * 32,
436 current_level.tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
441 /* (Bouncy bricks): */
443 for (i = 0; i < NUM_BOUNCY_BRICKS; i++)
445 bouncy_brick_draw(&bouncy_bricks[i]);
451 for (i = 0; i < NUM_BAD_GUYS; i++)
453 badguy_draw(&bad_guys[i]);
462 for (i = 0; i < NUM_BULLETS; i++)
464 bullet_draw(&bullets[i]);
467 /* (Floating scores): */
469 for (i = 0; i < NUM_FLOATING_SCORES; i++)
471 floating_score_draw(&floating_scores[i]);
477 for (i = 0; i < NUM_UPGRADES; i++)
479 upgrade_draw(&upgrades[i]);
483 /* (Bouncy distros): */
485 for (i = 0; i < NUM_BOUNCY_DISTROS; i++)
487 bouncy_distro_draw(&bouncy_distros[i]);
491 /* (Broken bricks): */
493 for (i = 0; i < NUM_BROKEN_BRICKS; i++)
495 broken_brick_draw(&broken_bricks[i]);
502 drawcenteredtext("PAUSE",230,letters_red, NO_UPDATE, 1);
507 /* (Update it all!) */
514 /* --- GAME LOOP! --- */
519 Uint32 last_time, now_time;
523 clearscreen(0, 0, 0);
530 menumenu = MENU_GAME;
535 loadlevel(¤t_level,"default",level);
536 loadlevelgfx(¤t_level);
539 highscore = load_hs();
546 /* --- MAIN GAME LOOP!!! --- */
556 last_time = SDL_GetTicks();
562 tux.input.old_fire = tux.input.fire;
567 /* Handle actions: */
569 if(!game_pause && !show_menu)
571 if (game_action() == 0)
573 /* == 0: no more lives */
574 /* == -1: continues */
581 /*Draw the current scene to the screen */
584 /* Time stops in pause mode */
585 if(game_pause || show_menu )
590 /* Pause til next frame: */
592 now_time = SDL_GetTicks();
593 /*if (now_time < last_time + FPS)
594 SDL_Delay(last_time + FPS - now_time);*/
600 if (timer_check(&time_left))
602 /* are we low on time ? */
603 if ((timer_get_left(&time_left) < TIME_WARNING)
604 && (current_music != HURRYUP_MUSIC))
606 current_music = HURRYUP_MUSIC;
607 /* stop the others music, prepare to play the fast music */
616 player_kill(&tux,KILL);
619 /* Keep playing the correct music: */
621 if (!playing_music())
623 play_current_music();
628 while (!done && !quit);
641 /* Initialize the game stuff: */
650 /* Free music data for this level: */
652 void unloadlevelsong(void)
654 free_music(level_song);
655 free_music(level_song_fast);
660 void loadlevelsong(void)
664 char * song_subtitle;
666 song_path = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) +
667 strlen(current_level.song_title) + 8));
668 sprintf(song_path, "%s/music/%s", DATA_PREFIX, current_level.song_title);
669 level_song = load_song(song_path);
673 song_path = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) +
674 strlen(current_level.song_title) + 8 + 5));
675 song_subtitle = strdup(current_level.song_title);
676 strcpy(strstr(song_subtitle, "."), "\0");
677 sprintf(song_path, "%s/music/%s-fast%s", DATA_PREFIX, song_subtitle, strstr(current_level.song_title, "."));
678 level_song_fast = load_song(song_path);
683 /* Load graphics/sounds shared between all levels: */
685 void loadshared(void)
688 char * herring_song_path; /* for loading herring song*/
692 texture_load(&tux_right[0],DATA_PREFIX "/images/shared/tux-right-0.png", USE_ALPHA);
693 texture_load(&tux_right[1],DATA_PREFIX "/images/shared/tux-right-1.png", USE_ALPHA);
694 texture_load(&tux_right[2],DATA_PREFIX "/images/shared/tux-right-2.png", USE_ALPHA);
696 texture_load(&tux_left[0],DATA_PREFIX "/images/shared/tux-left-0.png", USE_ALPHA);
697 texture_load(&tux_left[1],DATA_PREFIX "/images/shared/tux-left-1.png", USE_ALPHA);
698 texture_load(&tux_left[2],DATA_PREFIX "/images/shared/tux-left-2.png", USE_ALPHA);
700 texture_load(&firetux_right[0],DATA_PREFIX "/images/shared/firetux-right-0.png", USE_ALPHA);
701 texture_load(&firetux_right[1],DATA_PREFIX "/images/shared/firetux-right-1.png", USE_ALPHA);
702 texture_load(&firetux_right[2],DATA_PREFIX "/images/shared/firetux-right-2.png", USE_ALPHA);
704 texture_load(&firetux_left[0],DATA_PREFIX "/images/shared/firetux-left-0.png", USE_ALPHA);
705 texture_load(&firetux_left[1],DATA_PREFIX "/images/shared/firetux-left-1.png", USE_ALPHA);
706 texture_load(&firetux_left[2],DATA_PREFIX "/images/shared/firetux-left-2.png", USE_ALPHA);
709 texture_load(&cape_right[0] ,DATA_PREFIX "/images/shared/cape-right-0.png",
712 texture_load(&cape_right[1] ,DATA_PREFIX "/images/shared/cape-right-1.png",
715 texture_load(&cape_left[0] ,DATA_PREFIX "/images/shared/cape-left-0.png",
718 texture_load(&cape_left[1] ,DATA_PREFIX "/images/shared/cape-left-1.png",
721 texture_load(&bigtux_right[0] ,DATA_PREFIX "/images/shared/bigtux-right-0.png",
724 texture_load(&bigtux_right[1] ,DATA_PREFIX "/images/shared/bigtux-right-1.png",
727 texture_load(&bigtux_right[2] ,DATA_PREFIX "/images/shared/bigtux-right-2.png",
730 texture_load(&bigtux_right_jump ,DATA_PREFIX "/images/shared/bigtux-right-jump.png", USE_ALPHA);
732 texture_load(&bigtux_left[0] ,DATA_PREFIX "/images/shared/bigtux-left-0.png",
735 texture_load(&bigtux_left[1] ,DATA_PREFIX "/images/shared/bigtux-left-1.png",
738 texture_load(&bigtux_left[2] ,DATA_PREFIX "/images/shared/bigtux-left-2.png",
741 texture_load(&bigtux_left_jump ,DATA_PREFIX "/images/shared/bigtux-left-jump.png", USE_ALPHA);
743 texture_load(&bigcape_right[0] ,DATA_PREFIX "/images/shared/bigcape-right-0.png",
746 texture_load(&bigcape_right[1] ,DATA_PREFIX "/images/shared/bigcape-right-1.png",
749 texture_load(&bigcape_left[0] ,DATA_PREFIX "/images/shared/bigcape-left-0.png",
752 texture_load(&bigcape_left[1] ,DATA_PREFIX "/images/shared/bigcape-left-1.png",
755 texture_load(&bigfiretux_right[0] ,DATA_PREFIX "/images/shared/bigfiretux-right-0.png",
758 texture_load(&bigfiretux_right[1] ,DATA_PREFIX "/images/shared/bigfiretux-right-1.png",
761 texture_load(&bigfiretux_right[2] ,DATA_PREFIX "/images/shared/bigfiretux-right-2.png",
764 texture_load(&bigfiretux_right_jump ,DATA_PREFIX "/images/shared/bigfiretux-right-jump.png", USE_ALPHA);
766 texture_load(&bigfiretux_left[0] ,DATA_PREFIX "/images/shared/bigfiretux-left-0.png",
769 texture_load(&bigfiretux_left[1] ,DATA_PREFIX "/images/shared/bigfiretux-left-1.png",
772 texture_load(&bigfiretux_left[2] ,DATA_PREFIX "/images/shared/bigfiretux-left-2.png",
775 texture_load(&bigfiretux_left_jump ,DATA_PREFIX "/images/shared/bigfiretux-left-jump.png", USE_ALPHA);
777 texture_load(&bigcape_right[0] ,DATA_PREFIX "/images/shared/bigcape-right-0.png",
780 texture_load(&bigcape_right[1] ,DATA_PREFIX "/images/shared/bigcape-right-1.png",
783 texture_load(&bigcape_left[0] ,DATA_PREFIX "/images/shared/bigcape-left-0.png",
786 texture_load(&bigcape_left[1] ,DATA_PREFIX "/images/shared/bigcape-left-1.png",
790 texture_load(&ducktux_right ,DATA_PREFIX
791 "/images/shared/ducktux-right.png",
794 texture_load(&ducktux_left ,DATA_PREFIX
795 "/images/shared/ducktux-left.png",
798 texture_load(&skidtux_right ,DATA_PREFIX
799 "/images/shared/skidtux-right.png",
802 texture_load(&skidtux_left ,DATA_PREFIX
803 "/images/shared/skidtux-left.png",
806 texture_load(&duckfiretux_right ,DATA_PREFIX
807 "/images/shared/duckfiretux-right.png",
810 texture_load(&duckfiretux_left ,DATA_PREFIX
811 "/images/shared/duckfiretux-left.png",
814 texture_load(&skidfiretux_right ,DATA_PREFIX
815 "/images/shared/skidfiretux-right.png",
818 texture_load(&skidfiretux_left ,DATA_PREFIX
819 "/images/shared/skidfiretux-left.png",
825 texture_load(&img_box_full ,DATA_PREFIX "/images/shared/box-full.png",
827 texture_load(&img_box_empty ,DATA_PREFIX "/images/shared/box-empty.png",
834 texture_load(&img_water ,DATA_PREFIX "/images/shared/water.png", IGNORE_ALPHA);
836 texture_load(&img_waves[0] ,DATA_PREFIX "/images/shared/waves-0.png",
839 texture_load(&img_waves[1] ,DATA_PREFIX "/images/shared/waves-1.png",
842 texture_load(&img_waves[2] ,DATA_PREFIX "/images/shared/waves-2.png",
848 texture_load(&img_pole ,DATA_PREFIX "/images/shared/pole.png", USE_ALPHA);
849 texture_load(&img_poletop ,DATA_PREFIX "/images/shared/poletop.png",
855 texture_load(&img_flag[0] ,DATA_PREFIX "/images/shared/flag-0.png",
857 texture_load(&img_flag[1] ,DATA_PREFIX "/images/shared/flag-1.png",
863 texture_load(&img_cloud[0][0] ,DATA_PREFIX "/images/shared/cloud-00.png",
866 texture_load(&img_cloud[0][1] ,DATA_PREFIX "/images/shared/cloud-01.png",
869 texture_load(&img_cloud[0][2] ,DATA_PREFIX "/images/shared/cloud-02.png",
872 texture_load(&img_cloud[0][3] ,DATA_PREFIX "/images/shared/cloud-03.png",
876 texture_load(&img_cloud[1][0] ,DATA_PREFIX "/images/shared/cloud-10.png",
879 texture_load(&img_cloud[1][1] ,DATA_PREFIX "/images/shared/cloud-11.png",
882 texture_load(&img_cloud[1][2] ,DATA_PREFIX "/images/shared/cloud-12.png",
885 texture_load(&img_cloud[1][3] ,DATA_PREFIX "/images/shared/cloud-13.png",
893 texture_load(&img_bsod_left[0] ,DATA_PREFIX
894 "/images/shared/bsod-left-0.png",
897 texture_load(&img_bsod_left[1] ,DATA_PREFIX
898 "/images/shared/bsod-left-1.png",
901 texture_load(&img_bsod_left[2] ,DATA_PREFIX
902 "/images/shared/bsod-left-2.png",
905 texture_load(&img_bsod_left[3] ,DATA_PREFIX
906 "/images/shared/bsod-left-3.png",
909 texture_load(&img_bsod_right[0] ,DATA_PREFIX
910 "/images/shared/bsod-right-0.png",
913 texture_load(&img_bsod_right[1] ,DATA_PREFIX
914 "/images/shared/bsod-right-1.png",
917 texture_load(&img_bsod_right[2] ,DATA_PREFIX
918 "/images/shared/bsod-right-2.png",
921 texture_load(&img_bsod_right[3] ,DATA_PREFIX
922 "/images/shared/bsod-right-3.png",
925 texture_load(&img_bsod_squished_left ,DATA_PREFIX
926 "/images/shared/bsod-squished-left.png",
929 texture_load(&img_bsod_squished_right ,DATA_PREFIX
930 "/images/shared/bsod-squished-right.png",
933 texture_load(&img_bsod_falling_left ,DATA_PREFIX
934 "/images/shared/bsod-falling-left.png",
937 texture_load(&img_bsod_falling_right ,DATA_PREFIX
938 "/images/shared/bsod-falling-right.png",
944 texture_load(&img_laptop_left[0] ,DATA_PREFIX
945 "/images/shared/laptop-left-0.png",
948 texture_load(&img_laptop_left[1] ,DATA_PREFIX
949 "/images/shared/laptop-left-1.png",
952 texture_load(&img_laptop_left[2] ,DATA_PREFIX
953 "/images/shared/laptop-left-2.png",
956 texture_load(&img_laptop_right[0] ,DATA_PREFIX
957 "/images/shared/laptop-right-0.png",
960 texture_load(&img_laptop_right[1] ,DATA_PREFIX
961 "/images/shared/laptop-right-1.png",
964 texture_load(&img_laptop_right[2] ,DATA_PREFIX
965 "/images/shared/laptop-right-2.png",
968 texture_load(&img_laptop_flat_left ,DATA_PREFIX
969 "/images/shared/laptop-flat-left.png",
972 texture_load(&img_laptop_flat_right ,DATA_PREFIX
973 "/images/shared/laptop-flat-right.png",
976 texture_load(&img_laptop_falling_left ,DATA_PREFIX
977 "/images/shared/laptop-falling-left.png",
980 texture_load(&img_laptop_falling_right ,DATA_PREFIX
981 "/images/shared/laptop-falling-right.png",
987 texture_load(&img_money_left[0] ,DATA_PREFIX
988 "/images/shared/bag-left-0.png",
991 texture_load(&img_money_left[1] ,DATA_PREFIX
992 "/images/shared/bag-left-1.png",
995 texture_load(&img_money_right[0] ,DATA_PREFIX
996 "/images/shared/bag-right-0.png",
999 texture_load(&img_money_right[1] ,DATA_PREFIX
1000 "/images/shared/bag-right-1.png",
1007 texture_load(&img_mints ,DATA_PREFIX "/images/shared/mints.png", USE_ALPHA);
1008 texture_load(&img_coffee ,DATA_PREFIX "/images/shared/coffee.png", USE_ALPHA);
1013 texture_load(&img_bullet ,DATA_PREFIX "/images/shared/bullet.png", USE_ALPHA);
1015 texture_load(&img_red_glow ,DATA_PREFIX "/images/shared/red-glow.png",
1022 texture_load(&img_distro[0] ,DATA_PREFIX "/images/shared/distro-0.png",
1025 texture_load(&img_distro[1] ,DATA_PREFIX "/images/shared/distro-1.png",
1028 texture_load(&img_distro[2] ,DATA_PREFIX "/images/shared/distro-2.png",
1031 texture_load(&img_distro[3] ,DATA_PREFIX "/images/shared/distro-3.png",
1037 texture_load(&tux_life ,DATA_PREFIX "/images/shared/tux-life.png",
1042 texture_load(&img_golden_herring, DATA_PREFIX "/images/shared/golden-herring.png",
1046 /* Super background: */
1048 texture_load(&img_super_bkgd ,DATA_PREFIX "/images/shared/super-bkgd.png",
1052 /* Sound effects: */
1054 /* if (use_sound) // this will introduce SERIOUS bugs here ! because "load_sound"
1055 // initialize sounds[i] with the correct pointer's value:
1056 // NULL or something else. And it will be dangerous to
1057 // play with not-initialized pointers.
1058 // This is also true with if (use_music)
1059 Send a mail to me: neoneurone@users.sf.net, if you have another opinion. :)
1061 for (i = 0; i < NUM_SOUNDS; i++)
1062 sounds[i] = load_sound(soundfilenames[i]);
1065 herring_song_path = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) +
1066 strlen("SALCON.MOD") + 8)); /* FIXME: We need a real herring_song! Thats a fake.:) */
1068 sprintf(herring_song_path, "%s/music/%s", DATA_PREFIX, "SALCON.MOD");
1070 herring_song = load_song(herring_song_path);
1072 free(herring_song_path);
1077 /* Free shared data: */
1079 void unloadshared(void)
1083 for (i = 0; i < 3; i++)
1085 texture_free(&tux_right[i]);
1086 texture_free(&tux_left[i]);
1087 texture_free(&bigtux_right[i]);
1088 texture_free(&bigtux_left[i]);
1091 texture_free(&bigtux_right_jump);
1092 texture_free(&bigtux_left_jump);
1094 for (i = 0; i < 2; i++)
1096 texture_free(&cape_right[i]);
1097 texture_free(&cape_left[i]);
1098 texture_free(&bigcape_right[i]);
1099 texture_free(&bigcape_left[i]);
1102 texture_free(&ducktux_left);
1103 texture_free(&ducktux_right);
1105 texture_free(&skidtux_left);
1106 texture_free(&skidtux_right);
1108 for (i = 0; i < 4; i++)
1110 texture_free(&img_bsod_left[i]);
1111 texture_free(&img_bsod_right[i]);
1114 texture_free(&img_bsod_squished_left);
1115 texture_free(&img_bsod_squished_right);
1117 texture_free(&img_bsod_falling_left);
1118 texture_free(&img_bsod_falling_right);
1120 for (i = 0; i < 3; i++)
1122 texture_free(&img_laptop_left[i]);
1123 texture_free(&img_laptop_right[i]);
1126 texture_free(&img_laptop_flat_left);
1127 texture_free(&img_laptop_flat_right);
1129 texture_free(&img_laptop_falling_left);
1130 texture_free(&img_laptop_falling_right);
1132 for (i = 0; i < 2; i++)
1134 texture_free(&img_money_left[i]);
1135 texture_free(&img_money_right[i]);
1138 texture_free(&img_box_full);
1139 texture_free(&img_box_empty);
1141 texture_free(&img_water);
1142 for (i = 0; i < 3; i++)
1143 texture_free(&img_waves[i]);
1145 texture_free(&img_pole);
1146 texture_free(&img_poletop);
1148 for (i = 0; i < 2; i++)
1149 texture_free(&img_flag[i]);
1151 texture_free(&img_mints);
1152 texture_free(&img_coffee);
1154 for (i = 0; i < 4; i++)
1156 texture_free(&img_distro[i]);
1157 texture_free(&img_cloud[0][i]);
1158 texture_free(&img_cloud[1][i]);
1161 texture_free(&img_golden_herring);
1163 for (i = 0; i < NUM_SOUNDS; i++)
1164 free_chunk(sounds[i]);
1166 /* free the herring song */
1167 free_music( herring_song );
1171 /* Draw a tile on the screen: */
1173 void drawshape(float x, float y, unsigned char c)
1177 if (c == 'X' || c == 'x')
1178 texture_draw(&img_brick[0], x, y, NO_UPDATE);
1179 else if (c == 'Y' || c == 'y')
1180 texture_draw(&img_brick[1], x, y, NO_UPDATE);
1181 else if (c == 'A' || c =='B' || c == '!')
1182 texture_draw(&img_box_full, x, y, NO_UPDATE);
1184 texture_draw(&img_box_empty, x, y, NO_UPDATE);
1185 else if (c >= 'C' && c <= 'F')
1186 texture_draw(&img_cloud[0][c - 'C'], x, y, NO_UPDATE);
1187 else if (c >= 'c' && c <= 'f')
1188 texture_draw(&img_cloud[1][c - 'c'], x, y, NO_UPDATE);
1189 else if (c >= 'G' && c <= 'J')
1190 texture_draw(&img_bkgd[0][c - 'G'], x, y, NO_UPDATE);
1191 else if (c >= 'g' && c <= 'j')
1192 texture_draw(&img_bkgd[1][c - 'g'], x, y, NO_UPDATE);
1194 texture_draw(&img_solid[0], x, y, NO_UPDATE);
1196 texture_draw(&img_solid[1], x, y, NO_UPDATE);
1198 texture_draw(&img_solid[2], x, y, NO_UPDATE);
1200 texture_draw(&img_solid[3], x, y, NO_UPDATE);
1204 z = (frame / 2) % 6;
1207 texture_draw(&img_distro[z], x, y, NO_UPDATE);
1209 texture_draw(&img_distro[2], x, y, NO_UPDATE);
1211 texture_draw(&img_distro[1], x, y, NO_UPDATE);
1215 z = (frame / 3) % 3;
1217 texture_draw(&img_waves[z], x, y, NO_UPDATE);
1220 texture_draw(&img_poletop, x, y, NO_UPDATE);
1223 texture_draw(&img_pole, x, y, NO_UPDATE);
1225 /* Mark this as the end position of the level! */
1231 z = (frame / 3) % 2;
1233 texture_draw(&img_flag[z], x + 16, y, NO_UPDATE);
1236 texture_draw(&img_water, x, y, NO_UPDATE);
1240 /* What shape is at some position? */
1242 unsigned char shape(float x, float y)
1251 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= current_level.width)
1253 c = current_level.tiles[yy][xx];
1263 int issolid(float x, float y)
1265 if (isbrick(x, y) ||
1266 isbrick(x + 31, y) ||
1269 (shape(x, y) == '[' ||
1270 shape(x + 31, y) == '[') ||
1271 (shape(x, y) == '=' ||
1272 shape(x + 31, y) == '=') ||
1273 (shape(x, y) == ']' ||
1274 shape(x + 31, y) == ']') ||
1275 (shape(x, y) == 'A' ||
1276 shape(x + 31, y) == 'A') ||
1277 (shape(x, y) == 'B' ||
1278 shape(x + 31, y) == 'B') ||
1279 (shape(x, y) == '!' ||
1280 shape(x + 31, y) == '!') ||
1281 (shape(x, y) == 'a' ||
1282 shape(x + 31, y) == 'a'))
1291 /* Is it a brick? */
1293 int isbrick(float x, float y)
1295 if (shape(x, y) == 'X' ||
1296 shape(x, y) == 'x' ||
1297 shape(x, y) == 'Y' ||
1309 int isice(float x, float y)
1311 if (shape(x, y) == '#')
1320 /* Is it a full box? */
1322 int isfullbox(float x, float y)
1324 if (shape(x, y) == 'A' ||
1325 shape(x, y) == 'B' ||
1334 /* Break a brick: */
1336 void trybreakbrick(float x, float y)
1340 if (shape(x, y) == 'x' || shape(x, y) == 'y')
1342 /* Get a distro from it: */
1344 add_bouncy_distro(((x + 1) / 32) * 32,
1345 (int)(y / 32) * 32);
1347 if (counting_distros == NO)
1349 counting_distros = YES;
1350 distro_counter = 50;
1353 if (distro_counter <= 0)
1354 level_change(¤t_level,x, y, 'a');
1356 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
1357 score = score + SCORE_DISTRO;
1362 /* Get rid of it: */
1364 level_change(¤t_level,x, y,'.');
1368 /* Replace it with broken bits: */
1370 add_broken_brick(((x + 1) / 32) * 32,
1371 (int)(y / 32) * 32);
1374 /* Get some score: */
1376 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
1377 score = score + SCORE_BRICK;
1382 /* Bounce a brick: */
1384 void bumpbrick(float x, float y)
1386 add_bouncy_brick(((int)(x + 1) / 32) * 32,
1387 (int)(y / 32) * 32);
1389 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
1395 void tryemptybox(float x, float y)
1397 if (isfullbox(x, y))
1399 if (shape(x, y) == 'A')
1401 /* Box with a distro! */
1403 add_bouncy_distro(((x + 1) / 32) * 32,
1404 (int)(y / 32) * 32 - 32);
1406 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
1407 score = score + SCORE_DISTRO;
1410 else if (shape(x, y) == 'B')
1412 /* Add an upgrade! */
1414 if (tux.size == SMALL)
1416 /* Tux is small, add mints! */
1418 add_upgrade(((x + 1) / 32) * 32,
1419 (int)(y / 32) * 32 - 32,
1424 /* Tux is big, add coffee: */
1426 add_upgrade(((x + 1) / 32) * 32,
1427 (int)(y / 32) * 32 - 32,
1431 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
1433 else if (shape(x, y) == '!')
1435 /* Add a golden herring */
1437 add_upgrade(((x + 1) / 32) * 32,
1438 (int)(y / 32) * 32 - 32,
1442 /* Empty the box: */
1444 level_change(¤t_level,x, y, 'a');
1449 /* Try to grab a distro: */
1451 void trygrabdistro(float x, float y, int bounciness)
1453 if (shape(x, y) == '$')
1455 level_change(¤t_level,x, y, '.');
1456 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
1458 if (bounciness == BOUNCE)
1460 add_bouncy_distro(((x + 1) / 32) * 32,
1461 (int)(y / 32) * 32);
1464 score = score + SCORE_DISTRO;
1469 /* Try to bump a bad guy from below: */
1471 void trybumpbadguy(float x, float y)
1478 for (i = 0; i < NUM_BAD_GUYS; i++)
1480 if (bad_guys[i].alive &&
1481 bad_guys[i].x >= x - 32 && bad_guys[i].x <= x + 32 &&
1482 bad_guys[i].y >= y - 16 && bad_guys[i].y <= y + 16)
1484 if (bad_guys[i].kind == BAD_BSOD ||
1485 bad_guys[i].kind == BAD_LAPTOP)
1487 bad_guys[i].dying = FALLING;
1488 bad_guys[i].ym = -8;
1489 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
1497 for (i = 0; i < NUM_UPGRADES; i++)
1499 if (upgrades[i].alive && upgrades[i].height == 32 &&
1500 upgrades[i].x >= x - 32 && upgrades[i].x <= x + 32 &&
1501 upgrades[i].y >= y - 16 && upgrades[i].y <= y + 16)
1503 upgrades[i].xm = -upgrades[i].xm;
1504 upgrades[i].ym = -8;
1505 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
1511 /* Add an upgrade: */
1513 void add_upgrade(float x, float y, int kind)
1519 for (i = 0; i < NUM_UPGRADES && found == -1; i++)
1521 if (!upgrades[i].alive)
1527 upgrades[found].alive = YES;
1528 upgrades[found].kind = kind;
1529 upgrades[found].x = x;
1530 upgrades[found].y = y;
1531 upgrades[found].xm = 2;
1532 upgrades[found].ym = -2;
1533 upgrades[found].height = 0;
1539 void add_bullet(float x, float y, float xm, int dir)
1543 printf("X: %f Y: %f -- YOOYOYOYO\n",x,y);
1547 for (i = 0; i < NUM_BULLETS && found == -1; i++)
1549 if (!bullets[i].alive)
1555 bullets[found].alive = YES;
1559 bullets[found].x = x + 32;
1560 bullets[found].xm = BULLET_XM + xm;
1564 bullets[found].x = x;
1565 bullets[found].xm = -BULLET_XM + xm;
1568 bullets[found].y = y;
1569 bullets[found].ym = BULLET_STARTING_YM;
1571 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
1577 void drawstatus(void)
1581 sprintf(str, "%d", score);
1582 drawtext("SCORE", 0, 0, letters_blue, NO_UPDATE, 1);
1583 drawtext(str, 96, 0, letters_gold, NO_UPDATE, 1);
1585 sprintf(str, "%d", highscore);
1586 drawtext("HIGH", 0, 20, letters_blue, NO_UPDATE, 1);
1587 drawtext(str, 96, 20, letters_gold, NO_UPDATE, 1);
1589 if (timer_get_left(&time_left) > TIME_WARNING || (frame % 10) < 5)
1591 sprintf(str, "%d", timer_get_left(&time_left) / 1000 );
1592 drawtext("TIME", 224, 0, letters_blue, NO_UPDATE, 1);
1593 drawtext(str, 304, 0, letters_gold, NO_UPDATE, 1);
1596 sprintf(str, "%d", distros);
1597 drawtext("DISTROS", screen->h, 0, letters_blue, NO_UPDATE, 1);
1598 drawtext(str, 608, 0, letters_gold, NO_UPDATE, 1);
1600 drawtext("LIVES", screen->h, 20, letters_blue, NO_UPDATE, 1);
1602 for(i=0; i < tux.lives; ++i)
1604 texture_draw(&tux_life,565+(18*i),20,NO_UPDATE);
1609 void drawendscreen(void)
1613 clearscreen(0, 0, 0);
1615 drawcenteredtext("GAMEOVER", 200, letters_red, NO_UPDATE, 1);
1617 sprintf(str, "SCORE: %d", score);
1618 drawcenteredtext(str, 224, letters_gold, NO_UPDATE, 1);
1620 sprintf(str, "DISTROS: %d", distros);
1621 drawcenteredtext(str, 256, letters_blue, NO_UPDATE, 1);
1627 void drawresultscreen(void)
1631 clearscreen(0, 0, 0);
1633 drawcenteredtext("Result:", 200, letters_red, NO_UPDATE, 1);
1635 sprintf(str, "SCORE: %d", score);
1636 drawcenteredtext(str, 224, letters_gold, NO_UPDATE, 1);
1638 sprintf(str, "DISTROS: %d", distros);
1639 drawcenteredtext(str, 256, letters_blue, NO_UPDATE, 1);
1648 time_t current_time = time(NULL);
1649 struct tm* time_struct;
1652 time_struct = localtime(¤t_time);
1653 sprintf(savefile,"%s/%d-%d-%d-%d.save",st_save_dir,time_struct->tm_year+1900,time_struct->tm_mon,time_struct->tm_mday,time_struct->tm_hour);
1654 printf("%s",savefile);
1657 fi = fopen(savefile, "wb");
1661 fprintf(stderr, "Warning: I could not open the high score file ");
1666 fwrite(&level,4,1,fi);
1667 fwrite(&score,4,1,fi);
1668 fwrite(&distros,4,1,fi);
1669 fwrite(&tux.x,4,1,fi);
1670 fwrite(&tux.y,4,1,fi);
1671 fwrite(&scroll_x,4,1,fi);
1672 fwrite(¤t_level.time_left,4,1,fi);
1678 void loadgame(char* filename)
1682 time_t current_time = time(NULL);
1683 struct tm* time_struct;
1685 time_struct = localtime(¤t_time);
1686 sprintf(savefile,"%s/%d-%d-%d-%d.save",st_save_dir,time_struct->tm_year+1900,time_struct->tm_mon,time_struct->tm_mday,time_struct->tm_hour);
1687 printf("%s",savefile);
1690 fi = fopen(savefile, "rb");
1694 fprintf(stderr, "Warning: I could not open the high score file ");
1699 player_level_begin(&tux);
1701 loadlevel(¤t_level,"default",level);
1702 activate_bad_guys();
1704 loadlevelgfx(¤t_level);
1710 fread(&level,4,1,fi);
1711 fread(&score,4,1,fi);
1712 fread(&distros,4,1,fi);
1713 fread(&tux.x,4,1,fi);
1714 fread(&tux.y,4,1,fi);
1715 fread(&scroll_x,4,1,fi);
1716 fread(¤t_level.time_left,4,1,fi);