6 by Bill Kendrick & Tobias Glaesser
7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - March 15, 2004
24 #include <sys/types.h>
33 #include "high_scores.h"
41 #include "collision.h"
43 #include "particlesystem.h"
45 /* extern variables */
47 st_level current_level;
48 int game_started = false;
50 /* Local variables: */
52 static texture_type img_waves[3], img_water, img_pole, img_poletop, img_flag[2];
53 static texture_type img_cloud[2][4];
54 static SDL_Event event;
56 static char level_subset[100];
58 static int st_gl_mode;
59 static unsigned int last_update_time;
60 static unsigned int update_time;
61 static int pause_menu_frame;
64 /* Local function prototypes: */
66 void levelintro(void);
67 void loadshared(void);
68 void unloadshared(void);
69 void drawstatus(void);
70 void drawendscreen(void);
71 void drawresultscreen(void);
79 sprintf(str, "LEVEL %d", level);
80 text_drawf(&blue_text, str, 0, 200, A_HMIDDLE, A_TOP, 1);
82 sprintf(str, "%s", current_level.name.c_str());
83 text_drawf(&gold_text, str, 0, 224, A_HMIDDLE, A_TOP, 1);
85 sprintf(str, "TUX x %d", tux.lives);
86 text_drawf(&white_text, str, 0, 256, A_HMIDDLE, A_TOP, 1);
94 void start_timers(void)
96 timer_start(&time_left,current_level.time_left*1000);
97 st_pause_ticks_init();
98 update_time = st_get_ticks();
101 void activate_bad_guys(void)
103 for (std::vector<BadGuyData>::iterator i = current_level.badguy_data.begin();
104 i != current_level.badguy_data.end();
107 add_bad_guy(i->x, i->y, i->kind);
111 void activate_particle_systems(void)
113 if(current_level.particle_system == "clouds")
115 particle_systems.push_back(new CloudParticleSystem);
117 else if(current_level.particle_system == "snow")
119 particle_systems.push_back(new SnowParticleSystem);
121 else if(current_level.particle_system != "")
123 st_abort("unknown particle system specified in level", "");
127 /* --- GAME EVENT! --- */
129 void game_event(void)
131 while (SDL_PollEvent(&event))
133 /* Check for menu-events, if the menu is shown */
138 case SDL_QUIT: /* Quit event - quit: */
141 case SDL_KEYDOWN: /* A keypress! */
142 key = event.key.keysym.sym;
144 if(tux.key_event(key,DOWN))
149 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
152 if(st_gl_mode == ST_GL_TEST)
156 Menu::set_current(game_menu);
158 st_pause_ticks_stop();
162 Menu::set_current(game_menu);
164 st_pause_ticks_start();
172 case SDL_KEYUP: /* A keyrelease! */
173 key = event.key.keysym.sym;
175 if(tux.key_event(key, UP))
186 st_pause_ticks_stop();
191 st_pause_ticks_start();
198 tux.size = !tux.size;
201 tux.base.height = 64;
204 tux.base.height = 32;
221 timer_start(&tux.invincible_timer,TUX_INVINCIBLE_TIME);
241 case SDL_JOYAXISMOTION:
242 switch(event.jaxis.axis)
245 if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
247 tux.input.left = DOWN;
248 tux.input.right = UP;
250 else if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
253 tux.input.right = DOWN;
257 tux.input.left = DOWN;
258 tux.input.right = DOWN;
262 if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
263 tux.input.down = DOWN;
264 else if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
274 case SDL_JOYBUTTONDOWN:
275 if (event.jbutton.button == JOY_A)
277 else if (event.jbutton.button == JOY_B)
278 tux.input.fire = DOWN;
280 case SDL_JOYBUTTONUP:
281 if (event.jbutton.button == JOY_A)
283 else if (event.jbutton.button == JOY_B)
296 /* --- GAME ACTION! --- */
298 int game_action(void)
302 /* (tux_dying || next_level) */
303 if (tux.dying || next_level)
305 /* Tux either died, or reached the end of a level! */
312 /* End of a level! */
315 if(st_gl_mode != ST_GL_TEST)
322 level_free(¤t_level);
334 /* No more lives!? */
338 if(st_gl_mode != ST_GL_TEST)
341 if(st_gl_mode != ST_GL_TEST)
343 if (score > hs_score)
347 level_free(¤t_level);
352 } /* if (lives < 0) */
355 /* Either way, (re-)load the (next) level... */
359 level_free(¤t_level);
361 if (st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
363 if(level_load(¤t_level, level_subset) != 0)
368 if(level_load(¤t_level,level_subset,level) != 0)
375 activate_particle_systems();
377 level_load_gfx(¤t_level);
379 level_load_song(¤t_level);
380 if(st_gl_mode != ST_GL_TEST)
384 play_current_music();
389 /* Handle bouncy distros: */
390 for (i = 0; i < bouncy_distros.size(); i++)
392 bouncy_distro_action(&bouncy_distros[i]);
396 /* Handle broken bricks: */
397 for (i = 0; i < broken_bricks.size(); i++)
399 broken_brick_action(&broken_bricks[i]);
403 /* Handle distro counting: */
405 if (counting_distros)
409 if (distro_counter <= 0)
410 counting_distros = -1;
414 /* Handle bouncy bricks: */
416 for (i = 0; i < bouncy_bricks.size(); i++)
418 bouncy_brick_action(&bouncy_bricks[i]);
422 /* Handle floating scores: */
424 for (i = 0; i < floating_scores.size(); i++)
426 floating_score_action(&floating_scores[i]);
430 /* Handle bullets: */
432 for (i = 0; i < bullets.size(); ++i)
434 bullet_action(&bullets[i]);
437 /* Handle upgrades: */
439 for (i = 0; i < upgrades.size(); i++)
441 upgrade_action(&upgrades[i]);
445 /* Handle bad guys: */
447 for (i = 0; i < bad_guys.size(); i++)
449 bad_guys[i].action();
452 /* update particle systems */
453 std::vector<ParticleSystem*>::iterator p;
454 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
456 (*p)->simulate(frame_ratio);
459 /* Handle all possible collisions. */
465 /* --- GAME DRAW! --- */
472 if (tux.dying && (global_frame_counter % 4) == 0)
473 clearscreen(255, 255, 255);
474 else if(timer_check(&super_bkgd_timer))
475 texture_draw(&img_super_bkgd, 0, 0);
478 /* Draw the real background */
479 if(current_level.bkgd_image[0] != '\0')
481 int s = (int)scroll_x / 30;
482 texture_draw_part(&img_bkgd,s,0,0,0,img_bkgd.w - s, img_bkgd.h);
483 texture_draw_part(&img_bkgd,0,0,screen->w - s ,0,s,img_bkgd.h);
487 clearscreen(current_level.bkgd_red, current_level.bkgd_green, current_level.bkgd_blue);
491 /* Draw particle systems (background) */
492 std::vector<ParticleSystem*>::iterator p;
493 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
495 (*p)->draw(scroll_x, 0, 0);
498 /* Draw background: */
500 for (y = 0; y < 15; ++y)
502 for (x = 0; x < 21; ++x)
504 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
505 current_level.bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
509 /* Draw interactive tiles: */
510 for (y = 0; y < 15; ++y)
512 for (x = 0; x < 21; ++x)
514 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
515 current_level.ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
519 /* (Bouncy bricks): */
520 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
521 bouncy_brick_draw(&bouncy_bricks[i]);
523 for (unsigned int i = 0; i < bad_guys.size(); ++i)
528 for (unsigned int i = 0; i < bullets.size(); ++i)
529 bullet_draw(&bullets[i]);
531 for (unsigned int i = 0; i < floating_scores.size(); ++i)
532 floating_score_draw(&floating_scores[i]);
534 for (unsigned int i = 0; i < upgrades.size(); ++i)
535 upgrade_draw(&upgrades[i]);
537 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
538 bouncy_distro_draw(&bouncy_distros[i]);
540 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
541 broken_brick_draw(&broken_bricks[i]);
543 /* Draw foreground: */
544 for (y = 0; y < 15; ++y)
546 for (x = 0; x < 21; ++x)
548 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
549 current_level.fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
553 /* Draw particle systems (foreground) */
554 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
556 (*p)->draw(scroll_x, 0, 1);
563 int x = screen->h / 20;
564 for(int i = 0; i < x; ++i)
566 fillrect(i % 2 ? (pause_menu_frame * i)%screen->w : -((pause_menu_frame * i)%screen->w) ,(i*20+pause_menu_frame)%screen->h,screen->w,10,20,20,20, rand() % 20 + 1);
568 fillrect(0,0,screen->w,screen->h,rand() % 50, rand() % 50, rand() % 50, 128);
569 text_drawf(&blue_text, "PAUSE - Press 'P' To Play", 0, 230, A_HMIDDLE, A_TOP, 1);
574 menu_process_current();
575 mouse_cursor->draw();
578 /* (Update it all!) */
582 /* --- GAME LOOP! --- */
584 int gameloop(const char * subset, int levelnb, int mode)
586 int fps_cnt, jump, done;
587 timer_type fps_timer, frame_timer;
588 timer_init(&fps_timer, true);
589 timer_init(&frame_timer, true);
600 strcpy(level_subset,subset);
602 if (st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
604 if (level_load(¤t_level, level_subset))
609 if(level_load(¤t_level, level_subset, level) != 0)
613 level_load_gfx(¤t_level);
615 activate_particle_systems();
616 level_load_song(¤t_level);
620 if(st_gl_mode != ST_GL_TEST)
625 if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
628 timer_init(&time_left,true);
631 if(st_gl_mode == ST_GL_LOAD_GAME)
634 /* --- MAIN GAME LOOP!!! --- */
639 global_frame_counter = 0;
641 timer_init(&fps_timer,true);
642 timer_init(&frame_timer,true);
647 clearscreen(0, 0, 0);
651 play_current_music();
654 while (SDL_PollEvent(&event))
662 /* Calculate the movement-factor */
663 frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
664 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
665 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
667 if(!timer_check(&frame_timer))
669 timer_start(&frame_timer,25);
670 ++global_frame_counter;
675 tux.input.old_fire = tux.input.fire;
681 if(current_menu == game_menu)
683 switch (game_menu->check())
686 st_pause_ticks_stop();
689 update_load_save_game_menu(save_game_menu, false);
692 update_load_save_game_menu(load_game_menu, true);
695 st_pause_ticks_stop();
700 else if(current_menu == options_menu)
702 process_options_menu();
704 else if(current_menu == save_game_menu )
706 process_save_load_game_menu(true);
708 else if(current_menu == load_game_menu )
710 process_save_load_game_menu(false);
715 /* Handle actions: */
717 if(!game_pause && !show_menu)
719 /*float z = frame_ratio;
723 if (game_action() == 0)
725 /* == 0: no more lives */
726 /* == -1: continues */
738 if(debug_mode && debug_fps)
741 /*Draw the current scene to the screen */
742 /*If the machine running the game is too slow
743 skip the drawing of the frame (so the calculations are more precise and
744 the FPS aren't affected).*/
745 /*if( ! fps_fps < 50.0 )
748 jump = true;*/ /*FIXME: Implement this tweak right.*/
751 /* Time stops in pause mode */
752 if(game_pause || show_menu )
757 /* Set the time of the last update and the time of the current update */
758 last_update_time = update_time;
759 update_time = st_get_ticks();
761 /* Pause till next frame, if the machine running the game is too fast: */
762 /* FIXME: Works great for in OpenGl mode, where the CPU doesn't have to do that much. But
763 the results in SDL mode aren't perfect (thought the 100 FPS are reached), even on an AMD2500+. */
764 if(last_update_time >= update_time - 12 && !jump)
766 /*if((update_time - last_update_time) < 10)
767 SDL_Delay((11 - (update_time - last_update_time))/2);*/
773 if (timer_check(&time_left))
775 /* are we low on time ? */
776 if ((timer_get_left(&time_left) < TIME_WARNING)
777 && (get_current_music() != HURRYUP_MUSIC)) /* play the fast music */
779 set_current_music(HURRYUP_MUSIC);
780 play_current_music();
788 /* Calculate frames per second */
792 fps_fps = (1000.0 / (float)timer_get_gone(&fps_timer)) * (float)fps_cnt;
794 if(!timer_check(&fps_timer))
796 timer_start(&fps_timer,1000);
802 while (!done && !quit);
807 level_free(¤t_level);
812 game_started = false;
818 /* Load graphics/sounds shared between all levels: */
820 void loadshared(void)
825 texture_load(&tux_right[0], datadir + "/images/shared/tux-right-0.png", USE_ALPHA);
826 texture_load(&tux_right[1], datadir + "/images/shared/tux-right-1.png", USE_ALPHA);
827 texture_load(&tux_right[2], datadir + "/images/shared/tux-right-2.png", USE_ALPHA);
829 texture_load(&tux_left[0], datadir + "/images/shared/tux-left-0.png", USE_ALPHA);
830 texture_load(&tux_left[1], datadir + "/images/shared/tux-left-1.png", USE_ALPHA);
831 texture_load(&tux_left[2], datadir + "/images/shared/tux-left-2.png", USE_ALPHA);
833 texture_load(&firetux_right[0], datadir + "/images/shared/firetux-right-0.png", USE_ALPHA);
834 texture_load(&firetux_right[1], datadir + "/images/shared/firetux-right-1.png", USE_ALPHA);
835 texture_load(&firetux_right[2], datadir + "/images/shared/firetux-right-2.png", USE_ALPHA);
837 texture_load(&firetux_left[0], datadir + "/images/shared/firetux-left-0.png", USE_ALPHA);
838 texture_load(&firetux_left[1], datadir + "/images/shared/firetux-left-1.png", USE_ALPHA);
839 texture_load(&firetux_left[2], datadir + "/images/shared/firetux-left-2.png", USE_ALPHA);
842 texture_load(&cape_right[0], datadir + "/images/shared/cape-right-0.png",
845 texture_load(&cape_right[1], datadir + "/images/shared/cape-right-1.png",
848 texture_load(&cape_left[0], datadir + "/images/shared/cape-left-0.png",
851 texture_load(&cape_left[1], datadir + "/images/shared/cape-left-1.png",
854 texture_load(&bigtux_right[0], datadir + "/images/shared/bigtux-right-0.png",
857 texture_load(&bigtux_right[1], datadir + "/images/shared/bigtux-right-1.png",
860 texture_load(&bigtux_right[2], datadir + "/images/shared/bigtux-right-2.png",
863 texture_load(&bigtux_right_jump, datadir + "/images/shared/bigtux-right-jump.png", USE_ALPHA);
865 texture_load(&bigtux_left[0], datadir + "/images/shared/bigtux-left-0.png",
868 texture_load(&bigtux_left[1], datadir + "/images/shared/bigtux-left-1.png",
871 texture_load(&bigtux_left[2], datadir + "/images/shared/bigtux-left-2.png",
874 texture_load(&bigtux_left_jump, datadir + "/images/shared/bigtux-left-jump.png", USE_ALPHA);
876 texture_load(&bigcape_right[0], datadir + "/images/shared/bigcape-right-0.png",
879 texture_load(&bigcape_right[1], datadir + "/images/shared/bigcape-right-1.png",
882 texture_load(&bigcape_left[0], datadir + "/images/shared/bigcape-left-0.png",
885 texture_load(&bigcape_left[1], datadir + "/images/shared/bigcape-left-1.png",
888 texture_load(&bigfiretux_right[0], datadir + "/images/shared/bigfiretux-right-0.png",
891 texture_load(&bigfiretux_right[1], datadir + "/images/shared/bigfiretux-right-1.png",
894 texture_load(&bigfiretux_right[2], datadir + "/images/shared/bigfiretux-right-2.png",
897 texture_load(&bigfiretux_right_jump, datadir + "/images/shared/bigfiretux-right-jump.png", USE_ALPHA);
899 texture_load(&bigfiretux_left[0], datadir + "/images/shared/bigfiretux-left-0.png",
902 texture_load(&bigfiretux_left[1], datadir + "/images/shared/bigfiretux-left-1.png",
905 texture_load(&bigfiretux_left[2], datadir + "/images/shared/bigfiretux-left-2.png",
908 texture_load(&bigfiretux_left_jump, datadir + "/images/shared/bigfiretux-left-jump.png", USE_ALPHA);
910 texture_load(&bigcape_right[0], datadir + "/images/shared/bigcape-right-0.png",
913 texture_load(&bigcape_right[1], datadir + "/images/shared/bigcape-right-1.png",
916 texture_load(&bigcape_left[0], datadir + "/images/shared/bigcape-left-0.png",
919 texture_load(&bigcape_left[1], datadir + "/images/shared/bigcape-left-1.png",
923 texture_load(&ducktux_right, datadir +
924 "/images/shared/ducktux-right.png",
927 texture_load(&ducktux_left, datadir +
928 "/images/shared/ducktux-left.png",
931 texture_load(&skidtux_right, datadir +
932 "/images/shared/skidtux-right.png",
935 texture_load(&skidtux_left, datadir +
936 "/images/shared/skidtux-left.png",
939 texture_load(&duckfiretux_right, datadir +
940 "/images/shared/duckfiretux-right.png",
943 texture_load(&duckfiretux_left, datadir +
944 "/images/shared/duckfiretux-left.png",
947 texture_load(&skidfiretux_right, datadir +
948 "/images/shared/skidfiretux-right.png",
951 texture_load(&skidfiretux_left, datadir +
952 "/images/shared/skidfiretux-left.png",
958 texture_load(&img_box_full, datadir + "/images/shared/box-full.png",
960 texture_load(&img_box_empty, datadir + "/images/shared/box-empty.png",
967 texture_load(&img_water, datadir + "/images/shared/water.png", IGNORE_ALPHA);
969 texture_load(&img_waves[0], datadir + "/images/shared/waves-0.png",
972 texture_load(&img_waves[1], datadir + "/images/shared/waves-1.png",
975 texture_load(&img_waves[2], datadir + "/images/shared/waves-2.png",
981 texture_load(&img_pole, datadir + "/images/shared/pole.png", USE_ALPHA);
982 texture_load(&img_poletop, datadir + "/images/shared/poletop.png",
988 texture_load(&img_flag[0], datadir + "/images/shared/flag-0.png",
990 texture_load(&img_flag[1], datadir + "/images/shared/flag-1.png",
996 texture_load(&img_cloud[0][0], datadir + "/images/shared/cloud-00.png",
999 texture_load(&img_cloud[0][1], datadir + "/images/shared/cloud-01.png",
1002 texture_load(&img_cloud[0][2], datadir + "/images/shared/cloud-02.png",
1005 texture_load(&img_cloud[0][3], datadir + "/images/shared/cloud-03.png",
1009 texture_load(&img_cloud[1][0], datadir + "/images/shared/cloud-10.png",
1012 texture_load(&img_cloud[1][1], datadir + "/images/shared/cloud-11.png",
1015 texture_load(&img_cloud[1][2], datadir + "/images/shared/cloud-12.png",
1018 texture_load(&img_cloud[1][3], datadir + "/images/shared/cloud-13.png",
1027 texture_load(&img_mints, datadir + "/images/shared/mints.png", USE_ALPHA);
1028 texture_load(&img_coffee, datadir + "/images/shared/coffee.png", USE_ALPHA);
1033 texture_load(&img_bullet, datadir + "/images/shared/bullet.png", USE_ALPHA);
1035 texture_load(&img_red_glow, datadir + "/images/shared/red-glow.png",
1042 texture_load(&img_distro[0], datadir + "/images/shared/distro-0.png",
1045 texture_load(&img_distro[1], datadir + "/images/shared/distro-1.png",
1048 texture_load(&img_distro[2], datadir + "/images/shared/distro-2.png",
1051 texture_load(&img_distro[3], datadir + "/images/shared/distro-3.png",
1057 texture_load(&tux_life, datadir + "/images/shared/tux-life.png",
1062 texture_load(&img_golden_herring, datadir + "/images/shared/golden-herring.png",
1066 /* Super background: */
1068 texture_load(&img_super_bkgd, datadir + "/images/shared/super-bkgd.png",
1072 /* Sound effects: */
1074 /* if (use_sound) // this will introduce SERIOUS bugs here ! because "load_sound"
1075 // initialize sounds[i] with the correct pointer's value:
1076 // NULL or something else. And it will be dangerous to
1077 // play with not-initialized pointers.
1078 // This is also true with if (use_music)
1079 Send a mail to me: neoneurone@users.sf.net, if you have another opinion. :)
1081 for (i = 0; i < NUM_SOUNDS; i++)
1082 sounds[i] = load_sound(datadir + soundfilenames[i]);
1085 herring_song = load_song(datadir + "/music/SALCON.MOD");
1089 /* Free shared data: */
1091 void unloadshared(void)
1095 for (i = 0; i < 3; i++)
1097 texture_free(&tux_right[i]);
1098 texture_free(&tux_left[i]);
1099 texture_free(&bigtux_right[i]);
1100 texture_free(&bigtux_left[i]);
1103 texture_free(&bigtux_right_jump);
1104 texture_free(&bigtux_left_jump);
1106 for (i = 0; i < 2; i++)
1108 texture_free(&cape_right[i]);
1109 texture_free(&cape_left[i]);
1110 texture_free(&bigcape_right[i]);
1111 texture_free(&bigcape_left[i]);
1114 texture_free(&ducktux_left);
1115 texture_free(&ducktux_right);
1117 texture_free(&skidtux_left);
1118 texture_free(&skidtux_right);
1122 texture_free(&img_box_full);
1123 texture_free(&img_box_empty);
1125 texture_free(&img_water);
1126 for (i = 0; i < 3; i++)
1127 texture_free(&img_waves[i]);
1129 texture_free(&img_pole);
1130 texture_free(&img_poletop);
1132 for (i = 0; i < 2; i++)
1133 texture_free(&img_flag[i]);
1135 texture_free(&img_mints);
1136 texture_free(&img_coffee);
1138 for (i = 0; i < 4; i++)
1140 texture_free(&img_distro[i]);
1141 texture_free(&img_cloud[0][i]);
1142 texture_free(&img_cloud[1][i]);
1145 texture_free(&img_golden_herring);
1147 for (i = 0; i < NUM_SOUNDS; i++)
1148 free_chunk(sounds[i]);
1150 /* free the herring song */
1151 free_music( herring_song );
1155 /* Draw a tile on the screen: */
1157 void drawshape(float x, float y, unsigned int c)
1161 Tile* ptile = TileManager::instance()->get(c);
1164 if(ptile->images.size() > 1)
1166 texture_draw(&ptile->images[( ((global_frame_counter*25) / ptile->anim_speed) % (ptile->images.size()))],x,y);
1168 else if (ptile->images.size() == 1)
1170 texture_draw(&ptile->images[0],x,y);
1174 //printf("Tile not dravable %u\n", c);
1180 if (c == 'X' || c == 'x')
1181 texture_draw(&img_brick[0], x, y);
1182 else if (c == 'Y' || c == 'y')
1183 texture_draw(&img_brick[1], x, y);
1184 else if (c == 'A' || c =='B' || c == '!')
1185 texture_draw(&img_box_full, x, y);
1187 texture_draw(&img_box_empty, x, y);
1188 else if (c >= 'C' && c <= 'F')
1189 texture_draw(&img_cloud[0][c - 'C'], x, y);
1190 else if (c >= 'c' && c <= 'f')
1191 texture_draw(&img_cloud[1][c - 'c'], x, y);
1192 else if (c >= 'G' && c <= 'J')
1193 texture_draw(&img_bkgd_tile[0][c - 'G'], x, y);
1194 else if (c >= 'g' && c <= 'j')
1195 texture_draw(&img_bkgd_tile[1][c - 'g'], x, y);
1197 texture_draw(&img_solid[0], x, y);
1199 texture_draw(&img_solid[1], x, y);
1201 texture_draw(&img_solid[2], x, y);
1203 texture_draw(&img_solid[3], x, y);
1206 z = (global_frame_counter / 2) % 6;
1209 texture_draw(&img_distro[z], x, y);
1211 texture_draw(&img_distro[2], x, y);
1213 texture_draw(&img_distro[1], x, y);
1217 z = (global_frame_counter / 3) % 3;
1219 texture_draw(&img_waves[z], x, y);
1222 texture_draw(&img_poletop, x, y);
1225 texture_draw(&img_pole, x, y);
1230 z = (global_frame_counter / 3) % 2;
1232 texture_draw(&img_flag[z], x + 16, y);
1235 texture_draw(&img_water, x, y);*/
1240 /* What shape is at some position? */
1241 unsigned int shape(float x, float y)
1250 if (yy >= 0 && yy < 15 && xx >= 0 && xx <= current_level.width)
1252 c = current_level.ia_tiles[yy][xx];
1260 Tile* gettile(float x, float y)
1262 return TileManager::instance()->get(shape(x, y));
1265 bool issolid(float x, float y)
1267 Tile* tile = TileManager::instance()->get
1271 if(tile->solid == true)
1282 /* Is it a brick? */
1284 bool isbrick(float x, float y)
1286 Tile* tile = TileManager::instance()->get
1290 if(tile->brick == true)
1304 bool isice(float x, float y)
1306 Tile* tile = TileManager::instance()->get
1310 if(tile->ice == true)
1321 /* Is it a full box? */
1323 bool isfullbox(float x, float y)
1325 Tile* tile = TileManager::instance()->get
1329 if(tile->fullbox == true)
1340 bool isdistro(float x, float y)
1342 Tile* tile = TileManager::instance()->get(shape(x,y));
1343 return tile && tile->distro;
1346 /* Break a brick: */
1347 void trybreakbrick(float x, float y, bool small)
1349 Tile* tile = gettile(x, y);
1354 /* Get a distro from it: */
1355 add_bouncy_distro(((int)(x + 1) / 32) * 32,
1356 (int)(y / 32) * 32);
1358 if (!counting_distros)
1360 counting_distros = true;
1361 distro_counter = 50;
1364 if (distro_counter <= 0)
1365 level_change(¤t_level,x, y, TM_IA, tile->next_tile);
1367 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
1368 score = score + SCORE_DISTRO;
1373 /* Get rid of it: */
1374 level_change(¤t_level,x, y, TM_IA, tile->next_tile);
1376 /* Replace it with broken bits: */
1377 add_broken_brick(((int)(x + 1) / 32) * 32,
1378 (int)(y / 32) * 32);
1380 /* Get some score: */
1381 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
1382 score = score + SCORE_BRICK;
1388 /* Bounce a brick: */
1390 void bumpbrick(float x, float y)
1392 add_bouncy_brick(((int)(x + 1) / 32) * 32,
1393 (int)(y / 32) * 32);
1395 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
1401 void tryemptybox(float x, float y, int col_side)
1403 Tile* tile = gettile(x,y);
1407 // according to the collision side, set the upgrade direction
1408 if(col_side == LEFT)
1415 case 1: //'A': /* Box with a distro! */
1416 add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
1417 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
1418 score = score + SCORE_DISTRO;
1422 case 2: // 'B': /* Add an upgrade! */
1423 if (tux.size == SMALL) /* Tux is small, add mints! */
1424 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
1425 else /* Tux is big, add coffee: */
1426 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
1427 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
1430 case 3:// '!': /* Add a golden herring */
1431 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
1437 /* Empty the box: */
1438 level_change(¤t_level,x, y, TM_IA, tile->next_tile);
1441 /* Try to grab a distro: */
1442 void trygrabdistro(float x, float y, int bounciness)
1444 Tile* tile = gettile(x, y);
1445 if (tile && tile->distro)
1447 level_change(¤t_level,x, y, TM_IA, tile->next_tile);
1448 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
1450 if (bounciness == BOUNCE)
1452 add_bouncy_distro(((int)(x + 1) / 32) * 32,
1453 (int)(y / 32) * 32);
1456 score = score + SCORE_DISTRO;
1461 /* Try to bump a bad guy from below: */
1462 void trybumpbadguy(float x, float y)
1465 for (unsigned int i = 0; i < bad_guys.size(); i++)
1467 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
1468 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
1470 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
1476 for (unsigned int i = 0; i < upgrades.size(); i++)
1478 if (upgrades[i].base.height == 32 &&
1479 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
1480 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
1482 upgrades[i].base.xm = -upgrades[i].base.xm;
1483 upgrades[i].base.ym = -8;
1484 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
1490 void drawstatus(void)
1494 sprintf(str, "%d", score);
1495 text_draw(&white_text, "SCORE", 0, 0, 1);
1496 text_draw(&gold_text, str, 96, 0, 1);
1498 if(st_gl_mode != ST_GL_TEST)
1500 sprintf(str, "%d", hs_score);
1501 text_draw(&white_text, "HIGH", 0, 20, 1);
1502 text_draw(&gold_text, str, 96, 20, 1);
1506 text_draw(&white_text,"Press ESC To Return",0,20,1);
1509 if (timer_get_left(&time_left) > TIME_WARNING || (global_frame_counter % 10) < 5)
1511 sprintf(str, "%d", timer_get_left(&time_left) / 1000 );
1512 text_draw(&white_text, "TIME", 224, 0, 1);
1513 text_draw(&gold_text, str, 304, 0, 1);
1516 sprintf(str, "%d", distros);
1517 text_draw(&white_text, "DISTROS", screen->h, 0, 1);
1518 text_draw(&gold_text, str, 608, 0, 1);
1520 text_draw(&white_text, "LIVES", screen->h, 20, 1);
1524 sprintf(str, "%2.1f", fps_fps);
1525 text_draw(&white_text, "FPS", screen->h, 40, 1);
1526 text_draw(&gold_text, str, screen->h + 60, 40, 1);
1529 for(int i=0; i < tux.lives; ++i)
1531 texture_draw(&tux_life,565+(18*i),20);
1536 void drawendscreen(void)
1540 clearscreen(0, 0, 0);
1542 text_drawf(&blue_text, "GAMEOVER", 0, 200, A_HMIDDLE, A_TOP, 1);
1544 sprintf(str, "SCORE: %d", score);
1545 text_drawf(&gold_text, str, 0, 224, A_HMIDDLE, A_TOP, 1);
1547 sprintf(str, "DISTROS: %d", distros);
1548 text_drawf(&gold_text, str, 0, 256, A_HMIDDLE, A_TOP, 1);
1554 void drawresultscreen(void)
1558 clearscreen(0, 0, 0);
1560 text_drawf(&blue_text, "Result:", 0, 200, A_HMIDDLE, A_TOP, 1);
1562 sprintf(str, "SCORE: %d", score);
1563 text_drawf(&gold_text, str, 0, 224, A_HMIDDLE, A_TOP, 1);
1565 sprintf(str, "DISTROS: %d", distros);
1566 text_drawf(&gold_text, str, 0, 256, A_HMIDDLE, A_TOP, 1);
1572 void savegame(int slot)
1574 char savefile[1024];
1578 sprintf(savefile,"%s/slot%d.save",st_save_dir,slot);
1580 fi = fopen(savefile, "wb");
1584 fprintf(stderr, "Warning: I could not open the slot file ");
1588 fputs(level_subset, fi);
1590 fwrite(&level,sizeof(int),1,fi);
1591 fwrite(&score,sizeof(int),1,fi);
1592 fwrite(&distros,sizeof(int),1,fi);
1593 fwrite(&scroll_x,sizeof(float),1,fi);
1594 fwrite(&tux,sizeof(Player),1,fi);
1595 timer_fwrite(&tux.invincible_timer,fi);
1596 timer_fwrite(&tux.skidding_timer,fi);
1597 timer_fwrite(&tux.safe_timer,fi);
1598 timer_fwrite(&tux.frame_timer,fi);
1599 timer_fwrite(&time_left,fi);
1600 ui = st_get_ticks();
1601 fwrite(&ui,sizeof(int),1,fi);
1607 void loadgame(int slot)
1609 char savefile[1024];
1614 sprintf(savefile,"%s/slot%d.save",st_save_dir,slot);
1616 fi = fopen(savefile, "rb");
1620 fprintf(stderr, "Warning: I could not open the slot file ");
1625 fgets(str, 100, fi);
1626 strcpy(level_subset, str);
1627 level_subset[strlen(level_subset)-1] = '\0';
1628 fread(&level,sizeof(int),1,fi);
1631 level_free(¤t_level);
1632 if(level_load(¤t_level,level_subset,level) != 0)
1636 activate_bad_guys();
1637 activate_particle_systems();
1639 level_load_gfx(¤t_level);
1641 level_load_song(¤t_level);
1643 update_time = st_get_ticks();
1645 fread(&score,sizeof(int),1,fi);
1646 fread(&distros,sizeof(int),1,fi);
1647 fread(&scroll_x,sizeof(float),1,fi);
1648 fread(&tux, sizeof(Player), 1, fi);
1649 timer_fread(&tux.invincible_timer,fi);
1650 timer_fread(&tux.skidding_timer,fi);
1651 timer_fread(&tux.safe_timer,fi);
1652 timer_fread(&tux.frame_timer,fi);
1653 timer_fread(&time_left,fi);
1654 fread(&ui,sizeof(int),1,fi);
1655 tux.hphysic.start_time += st_get_ticks() - ui;
1656 tux.vphysic.start_time += st_get_ticks() - ui;
1662 void slotinfo(char **pinfo, int slot)
1665 char slotfile[1024];
1669 sprintf(slotfile,"%s/slot%d.save",st_save_dir,slot);
1671 fi = fopen(slotfile, "rb");
1673 sprintf(tmp,"Slot %d - ",slot);
1681 fgets(str, 100, fi);
1682 str[strlen(str)-1] = '\0';
1684 strcat(tmp, " / Level:");
1685 fread(&slot_level,sizeof(int),1,fi);
1686 sprintf(str,"%d",slot_level);
1691 *pinfo = (char*) malloc(sizeof(char) * (strlen(tmp)+1));