Show a message when more than one bad guys is killed in a row.
[supertux.git] / src / gameloop.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; either version 2
11 //  of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 // 
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22 #include <iostream>
23 #include <sstream>
24 #include <cassert>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cmath>
28 #include <cstring>
29 #include <cerrno>
30 #include <unistd.h>
31 #include <ctime>
32
33 #include "SDL.h"
34
35 #ifndef WIN32
36 #include <sys/types.h>
37 #include <ctype.h>
38 #endif
39
40 #include "defines.h"
41 #include "app/globals.h"
42 #include "gameloop.h"
43 #include "video/screen.h"
44 #include "app/setup.h"
45 #include "high_scores.h"
46 #include "gui/menu.h"
47 #include "badguy.h"
48 #include "sector.h"
49 #include "special.h"
50 #include "player.h"
51 #include "level.h"
52 #include "scene.h"
53 #include "collision.h"
54 #include "tile.h"
55 #include "particlesystem.h"
56 #include "resources.h"
57 #include "background.h"
58 #include "tilemap.h"
59 #include "app/gettext.h"
60 #include "worldmap.h"
61 #include "intro.h"
62 #include "misc.h"
63 #include "camera.h"
64 #include "statistics.h"
65
66 GameSession* GameSession::current_ = 0;
67
68 bool compare_last(std::string& haystack, std::string needle)
69 {
70 int haystack_size = haystack.size();
71 int needle_size = needle.size();
72
73 if(haystack_size < needle_size)
74   return false;
75
76 if(haystack.compare(haystack_size-needle_size, needle_size, needle) == 0)
77   return true;
78 return false;
79 }
80
81 GameSession::GameSession(const std::string& levelname_, int mode, bool flip_level_, Statistics* statistics)
82   : level(0), currentsector(0), st_gl_mode(mode),
83     end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_),
84     best_level_statistics(statistics)
85 {
86   current_ = this;
87   
88   global_frame_counter = 0;
89   game_pause = false;
90   fps_fps = 0;
91
92   fps_timer.init(true);
93   frame_timer.init(true);
94   random_timer.init(true);
95   frame_rate.set_fps(100);
96
97   context = new DrawingContext();
98
99   if(flip_levels_mode)
100     flip_level = true;
101
102   last_swap_point = Vector(-1, -1);
103   last_swap_stats.reset();
104
105   restart_level();
106 }
107
108 void
109 GameSession::restart_level()
110 {
111   game_pause   = false;
112   exit_status  = ES_NONE;
113   end_sequence = NO_ENDSEQUENCE;
114
115   fps_timer.init(true);
116   frame_timer.init(true);
117   random_timer.init(true);
118
119   last_keys.clear();
120
121   Vector tux_pos = Vector(-1,-1);
122   if (currentsector)
123     { // Tux has lost a life, so we try to respawn him at the nearest reset point
124       tux_pos = currentsector->player->base;
125     }
126   
127   delete level;
128   currentsector = 0;
129
130   level = new Level;
131   level->load(levelname);
132   if(flip_level)
133     level->do_vertical_flip();
134
135   global_stats.reset();
136   global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
137   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
138   global_stats.set_total_points(TIME_NEEDED_STAT, level->time_left);
139
140   currentsector = level->get_sector("main");
141   if(!currentsector)
142     Termination::abort("Level has no main sector.", "");
143   currentsector->activate("main");
144
145   // Set Tux to the nearest reset point
146   if(tux_pos.x != -1)
147     {
148     tux_pos = currentsector->get_best_spawn_point(tux_pos);
149
150     if(last_swap_point.x > tux_pos.x)
151       tux_pos = last_swap_point;
152     else  // new swap point
153       {
154       last_swap_point = tux_pos;
155
156       last_swap_stats += global_stats;
157       }
158
159     currentsector->player->base.x = tux_pos.x;
160     currentsector->player->base.y = tux_pos.y;
161
162     // has to reset camera on swapping
163     currentsector->camera->reset(Vector(currentsector->player->base.x,
164                                         currentsector->player->base.y));
165     }
166
167   if (st_gl_mode != ST_GL_DEMO_GAME)
168     {
169       if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
170         levelintro();
171     }
172
173   time_left.init(true);
174   start_timers();
175   currentsector->play_music(LEVEL_MUSIC);
176 }
177
178 GameSession::~GameSession()
179 {
180   delete level;
181   delete context;
182 }
183
184 void
185 GameSession::levelintro(void)
186 {
187   SoundManager::get()->halt_music();
188   
189   char str[60];
190
191   DrawingContext context;
192   currentsector->background->draw(context);
193
194   context.draw_text(gold_text, level->get_name(), Vector(screen->w/2, 160),
195       CENTER_ALLIGN, LAYER_FOREGROUND1);
196
197   sprintf(str, "TUX x %d", player_status.lives);
198   context.draw_text(white_text, str, Vector(screen->w/2, 210),
199       CENTER_ALLIGN, LAYER_FOREGROUND1);
200
201   if(level->get_author().size())
202     context.draw_text(white_small_text,
203       std::string(_("by ")) + level->get_author(), 
204       Vector(screen->w/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
205
206
207   if(flip_level)
208     context.draw_text(white_text,
209       _("Level Vertically Flipped!"),
210       Vector(screen->w/2, 310), CENTER_ALLIGN, LAYER_FOREGROUND1);
211
212   if(best_level_statistics != NULL)
213     best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
214
215   context.do_drawing();
216
217   SDL_Event event;
218   wait_for_event(event,1000,3000,true);
219 }
220
221 /* Reset Timers */
222 void
223 GameSession::start_timers()
224 {
225   time_left.start(level->time_left*1000);
226   Ticks::pause_init();
227   frame_rate.start();
228 }
229
230 void
231 GameSession::on_escape_press()
232 {
233   if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
234     return;   // don't let the player open the menu, when he is dying
235   
236   if(game_pause)
237     return;
238
239   if(st_gl_mode == ST_GL_TEST)
240     {
241       exit_status = ES_LEVEL_ABORT;
242     }
243   else if (!Menu::current())
244     {
245       /* Tell Tux that the keys are all down, otherwise
246         it could have nasty bugs, like going allways to the right
247         or whatever that key does */
248       Player& tux = *(currentsector->player);
249       tux.key_event((SDLKey)keymap.jump, UP);
250       tux.key_event((SDLKey)keymap.duck, UP);
251       tux.key_event((SDLKey)keymap.left, UP);
252       tux.key_event((SDLKey)keymap.right, UP);
253       tux.key_event((SDLKey)keymap.fire, UP);
254
255       Menu::set_current(game_menu);
256       Ticks::pause_start();
257     }
258 }
259
260 void
261 GameSession::process_events()
262 {
263   if (end_sequence != NO_ENDSEQUENCE)
264     {
265       Player& tux = *currentsector->player;
266          
267       tux.input.fire  = UP;
268       tux.input.left  = UP;
269       tux.input.right = DOWN;
270       tux.input.down  = UP; 
271
272       if (int(last_x_pos) == int(tux.base.x))
273         tux.input.up    = DOWN; 
274       else
275         tux.input.up    = UP; 
276
277       last_x_pos = tux.base.x;
278
279       SDL_Event event;
280       while (SDL_PollEvent(&event))
281         {
282           /* Check for menu-events, if the menu is shown */
283           if (Menu::current())
284             {
285               Menu::current()->event(event);
286               if(!Menu::current())
287               Ticks::pause_stop();
288             }
289
290           switch(event.type)
291             {
292             case SDL_QUIT:        /* Quit event - quit: */
293               Termination::abort("Received window close", "");
294               break;
295               
296             case SDL_KEYDOWN:     /* A keypress! */
297               {
298                 SDLKey key = event.key.keysym.sym;
299            
300                 switch(key)
301                   {
302                   case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
303                     on_escape_press();
304                     break;
305                   default:
306                     break;
307                   }
308               }
309           
310             case SDL_JOYBUTTONDOWN:
311               if (event.jbutton.button == joystick_keymap.start_button)
312                 on_escape_press();
313               break;
314             }
315         }
316     }
317   else // normal mode
318     {
319       if(!Menu::current() && !game_pause)
320         Ticks::pause_stop();
321
322       SDL_Event event;
323       while (SDL_PollEvent(&event))
324         {
325           /* Check for menu-events, if the menu is shown */
326           if (Menu::current())
327             {
328               Menu::current()->event(event);
329               if(!Menu::current())
330                 Ticks::pause_stop();
331             }
332           else
333             {
334               Player& tux = *currentsector->player;
335   
336               switch(event.type)
337                 {
338                 case SDL_QUIT:        /* Quit event - quit: */
339                   Termination::abort("Received window close", "");
340                   break;
341
342                 case SDL_KEYDOWN:     /* A keypress! */
343                   {
344                     SDLKey key = event.key.keysym.sym;
345             
346                     if(tux.key_event(key,DOWN))
347                       break;
348
349                     switch(key)
350                       {
351                       case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
352                         on_escape_press();
353                         break;
354                       default:
355                         break;
356                       }
357                   }
358                   break;
359                 case SDL_KEYUP:      /* A keyrelease! */
360                   {
361                     SDLKey key = event.key.keysym.sym;
362
363                     if(tux.key_event(key, UP))
364                       break;
365
366                     switch(key)
367                       {
368                       case SDLK_a:
369                         if(debug_mode)
370                         {
371                           char buf[160];
372                           snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
373                               tux.base.x, tux.base.y);
374                           context->draw_text(white_text, buf,
375                               Vector(0, screen->h - white_text->get_height()),
376                               LEFT_ALLIGN, LAYER_FOREGROUND1);
377                           context->do_drawing();
378                           SDL_Delay(1000);
379                         }
380                         break;
381                       case SDLK_p:
382                         if(!Menu::current())
383                           {
384                           // "lifeup" cheat activates pause cause of the 'p'
385                           // so work around to ignore it
386                             if(compare_last(last_keys, "lifeu"))
387                               break;
388
389                             if(game_pause)
390                               {
391                                 game_pause = false;
392                                 Ticks::pause_stop();
393                               }
394                             else
395                               {
396                                 game_pause = true;
397                                 Ticks::pause_start();
398                               }
399                           }
400                         break;
401                       default:
402                         break;
403                       }
404                   }
405
406                         /* Check if chacrater is ASCII */
407                         char ch[2];
408                         if((event.key.keysym.unicode & 0xFF80) == 0)
409                           {
410                           ch[0] = event.key.keysym.unicode & 0x7F;
411                           ch[1] = '\0';
412                           }
413                         last_keys.append(ch);  // add to cheat keys
414
415                         // Cheating words (the goal of this is really for debugging,
416                         // but could be used for some cheating, nothing wrong with that)
417                         if(compare_last(last_keys, "grow"))
418                           {
419                           tux.grow(false);
420                           last_keys.clear();
421                           }
422                         if(compare_last(last_keys, "fire"))
423                           {
424                           tux.grow(false);
425                           tux.got_power = tux.FIRE_POWER;
426                           last_keys.clear();
427                           }
428                         if(compare_last(last_keys, "ice"))
429                           {
430                           tux.grow(false);
431                           tux.got_power = tux.ICE_POWER;
432                           last_keys.clear();
433                           }
434                         if(compare_last(last_keys, "lifeup"))
435                           {
436                           player_status.lives++;
437                           last_keys.clear();
438                           }
439                         if(compare_last(last_keys, "lifedown"))
440                           {
441                           player_status.lives--;
442                           last_keys.clear();
443                           }
444                         if(compare_last(last_keys, "invincible"))
445                           {    // be invincle for the rest of the level
446                           tux.invincible_timer.start(time_left.get_left());
447                           last_keys.clear();
448                           }
449                         if(compare_last(last_keys, "shrink"))
450                           {    // remove powerups
451                           tux.kill(tux.SHRINK);
452                           last_keys.clear();
453                           }
454                         if(compare_last(last_keys, "kill"))
455                           {    // kill Tux, but without losing a life
456                           player_status.lives++;
457                           tux.kill(tux.KILL);
458                           last_keys.clear();
459                           }
460                         if(compare_last(last_keys, "hover"))
461                           {    // toggle hover ability on/off
462                           tux.enable_hover = !tux.enable_hover;
463                           last_keys.clear();
464                           }
465                         if(compare_last(last_keys, "gotoend"))
466                           {    // goes to the end of the level
467                           tux.base.x = (currentsector->solids->get_width()*32) - (screen->w*2);
468                           tux.base.y = 0;
469                           currentsector->camera->reset(Vector(tux.base.x, tux.base.y));
470                           last_keys.clear();
471                           }
472                   break;
473
474                 case SDL_JOYAXISMOTION:
475                   if (event.jaxis.axis == joystick_keymap.x_axis)
476                     {
477                       if (event.jaxis.value < -joystick_keymap.dead_zone)
478                         {
479                           tux.input.left  = DOWN;
480                           tux.input.right = UP;
481                         }
482                       else if (event.jaxis.value > joystick_keymap.dead_zone)
483                         {
484                           tux.input.left  = UP;
485                           tux.input.right = DOWN;
486                         }
487                       else
488                         {
489                           tux.input.left  = DOWN;
490                           tux.input.right = DOWN;
491                         }
492                     }
493                   else if (event.jaxis.axis == joystick_keymap.y_axis)
494                     {
495                       if (event.jaxis.value > joystick_keymap.dead_zone)
496                         tux.input.down = DOWN;
497                       else if (event.jaxis.value < -joystick_keymap.dead_zone)
498                         tux.input.down = UP;
499                       else
500                         tux.input.down = UP;
501                     }
502                   break;
503
504                 case SDL_JOYHATMOTION:
505                   if(event.jhat.value & SDL_HAT_UP) {
506                     tux.input.up = DOWN;
507                     tux.input.down = UP;
508                   } else if(event.jhat.value & SDL_HAT_DOWN) {
509                     tux.input.up = UP;
510                     tux.input.down = DOWN;
511                   } else if(event.jhat.value & SDL_HAT_LEFT) {
512                     tux.input.left = DOWN;
513                     tux.input.right = UP;
514                   } else if(event.jhat.value & SDL_HAT_RIGHT) {
515                     tux.input.left = UP;
516                     tux.input.right = DOWN;
517                   } else if(event.jhat.value == SDL_HAT_CENTERED) {
518                     tux.input.left = UP;
519                     tux.input.right = UP;
520                     tux.input.up = UP;
521                     tux.input.down = UP;
522                   }
523                   break;
524             
525                 case SDL_JOYBUTTONDOWN:
526                   if (event.jbutton.button == joystick_keymap.a_button)
527                     tux.input.up = DOWN;
528                   else if (event.jbutton.button == joystick_keymap.b_button)
529                     tux.input.fire = DOWN;
530                   else if (event.jbutton.button == joystick_keymap.start_button)
531                     on_escape_press();
532                   break;
533                 case SDL_JOYBUTTONUP:
534                   if (event.jbutton.button == joystick_keymap.a_button)
535                     tux.input.up = UP;
536                   else if (event.jbutton.button == joystick_keymap.b_button)
537                     tux.input.fire = UP;
538                   break;
539
540                 default:
541                   break;
542                 }  /* switch */
543             }
544         } /* while */
545     }
546 }
547
548 void
549 GameSession::check_end_conditions()
550 {
551   Player* tux = currentsector->player;
552
553   /* End of level? */
554   Tile* endtile = collision_goal(tux->base);
555
556   if(end_sequence && !endsequence_timer.check())
557     {
558       exit_status = ES_LEVEL_FINISHED;
559       global_stats += last_swap_stats;  // add swap points stats
560       return;
561     }
562   else if(end_sequence == ENDSEQUENCE_RUNNING && endtile && endtile->data >= 1)
563     {
564       end_sequence = ENDSEQUENCE_WAITING;
565     }
566   else if(!end_sequence && endtile && endtile->data == 0)
567     {
568       end_sequence = ENDSEQUENCE_RUNNING;
569       endsequence_timer.start(7000); // 5 seconds until we finish the map
570       last_x_pos = -1;
571       SoundManager::get()->play_music(level_end_song, 0);
572       tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
573
574       // add left time to stats
575       global_stats.set_points(TIME_NEEDED_STAT, time_left.get_gone() / 1000);
576
577       random_timer.start(200);  // start 1st firework
578     }
579   else if (!end_sequence && tux->is_dead())
580     {
581       player_status.bonus = PlayerStatus::NO_BONUS;
582
583       if (player_status.lives < 0)
584         { // No more lives!?
585           exit_status = ES_GAME_OVER;
586         }
587       else
588         { // Still has lives, so reset Tux to the levelstart
589           restart_level();
590         }
591
592       return;
593     }
594 }
595
596 void
597 GameSession::action(double frame_ratio)
598 {
599   if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
600     {
601       // Update Tux and the World
602       currentsector->action(frame_ratio);
603     }
604
605   // respawning in new sector?
606   if(newsector != "" && newspawnpoint != "") {
607     Sector* sector = level->get_sector(newsector);
608     currentsector = sector;
609     currentsector->activate(newspawnpoint);
610     currentsector->play_music(LEVEL_MUSIC);
611     newsector = newspawnpoint = "";
612   }
613
614   // on end sequence make a few fireworks
615   if(end_sequence == ENDSEQUENCE_RUNNING && !random_timer.check() &&
616      currentsector->end_sequence_animation() == FIREWORKS_ENDSEQ_ANIM)
617     {
618     Vector epicenter = currentsector->camera->get_translation();
619     epicenter.x += screen->w * ((float)rand() / RAND_MAX);
620     epicenter.y += (screen->h/2) * ((float)rand() / RAND_MAX);
621
622     int red = rand() % 255;  // calculate firework color
623     int green = rand() % red;
624     currentsector->add_particles(epicenter, Vector(1.4,1.4), Vector(0,0),
625                                  45, Color(red,green,0), 3, 1300);
626
627     SoundManager::get()->play_sound(IDToSound(SND_FIREWORKS));
628     random_timer.start(rand() % 400 + 600);  // next firework
629     }
630 }
631
632 void 
633 GameSession::draw()
634 {
635   currentsector->draw(*context);
636   drawstatus(*context);
637
638   if(game_pause)
639     {
640       int x = screen->h / 20;
641       for(int i = 0; i < x; ++i)
642         {
643           context->draw_filled_rect(
644               Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
645                 -((pause_menu_frame * i)%screen->w)
646                 ,(i*20+pause_menu_frame)%screen->h),
647               Vector(screen->w,10),
648               Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
649         }
650       context->draw_filled_rect(
651           Vector(0,0), Vector(screen->w, screen->h),
652           Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
653       context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
654           Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
655
656       char str1[60];
657       char str2[124];
658       sprintf(str1, _("Playing: "));
659       sprintf(str2, level->name.c_str());
660
661       context->draw_text(blue_text, str1,
662           Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
663           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
664       context->draw_text(white_text, str2,
665           Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
666           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
667     }
668
669   if(Menu::current())
670     {
671       Menu::current()->draw(*context);
672       mouse_cursor->draw(*context);
673     }
674
675   context->do_drawing();
676 }
677
678 void
679 GameSession::process_menu()
680 {
681   Menu* menu = Menu::current();
682   if(menu)
683     {
684       menu->action();
685
686       if(menu == game_menu)
687         {
688           switch (game_menu->check())
689             {
690             case MNID_CONTINUE:
691               Ticks::pause_stop();
692               break;
693             case MNID_ABORTLEVEL:
694               Ticks::pause_stop();
695               exit_status = ES_LEVEL_ABORT;
696               break;
697             }
698         }
699       else if(menu == options_menu)
700         {
701           process_options_menu();
702         }
703       else if(menu == load_game_menu )
704         {
705           process_load_game_menu();
706         }
707     }
708 }
709
710 GameSession::ExitStatus
711 GameSession::run()
712 {
713   Menu::set_current(0);
714   current_ = this;
715   
716   int fps_cnt = 0;
717
718   frame_rate.start();
719
720   // Eat unneeded events
721   SDL_Event event;
722   while (SDL_PollEvent(&event)) {}
723
724   draw();
725
726   while (exit_status == ES_NONE)
727     {
728       /* Calculate the movement-factor */
729       double frame_ratio = frame_rate.get();
730
731       if(!frame_timer.check())
732         {
733           frame_timer.start(25);
734           ++global_frame_counter;
735         }
736
737       /* Handle events: */
738       currentsector->player->input.old_fire 
739         = currentsector->player->input.fire;
740
741       process_events();
742       process_menu();
743
744       // Update the world state and all objects in the world
745       // Do that with a constante time-delta so that the game will run
746       // determistic and not different on different machines
747       if(!game_pause && !Menu::current())
748         {
749           // Update the world
750           check_end_conditions();
751           if (end_sequence == ENDSEQUENCE_RUNNING)
752              action(frame_ratio/2);
753           else if(end_sequence == NO_ENDSEQUENCE)
754              action(frame_ratio);
755         }
756       else
757         {
758           ++pause_menu_frame;
759           SDL_Delay(50);
760         }
761
762       draw();
763
764       /* Time stops in pause mode */
765       if(game_pause || Menu::current())
766         {
767           continue;
768         }
769
770       frame_rate.update();
771
772       /* Handle time: */
773       if (!time_left.check() && currentsector->player->dying == DYING_NOT
774               && !end_sequence)
775         currentsector->player->kill(Player::KILL);
776
777       /* Handle music: */
778       if(currentsector->player->invincible_timer.check() && !end_sequence)
779         {
780           currentsector->play_music(HERRING_MUSIC);
781         }
782       /* are we low on time ? */
783       else if (time_left.get_left() < TIME_WARNING && !end_sequence)
784         {
785           currentsector->play_music(HURRYUP_MUSIC);
786         }
787       /* or just normal music? */
788       else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
789         {
790           currentsector->play_music(LEVEL_MUSIC);
791         }
792
793       /* Calculate frames per second */
794       if(show_fps)
795         {
796           ++fps_cnt;
797           fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
798
799           if(!fps_timer.check())
800             {
801               fps_timer.start(1000);
802               fps_cnt = 0;
803             }
804         }
805     }
806   
807   return exit_status;
808 }
809
810 void
811 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
812 {
813   newsector = sector;
814   newspawnpoint = spawnpoint;
815 }
816
817 /* Bounce a brick: */
818 void bumpbrick(float x, float y)
819 {
820   Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
821                          (int)(y / 32) * 32));
822
823   SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
824 }
825
826 /* (Status): */
827 void
828 GameSession::drawstatus(DrawingContext& context)
829 {
830   char str[60];
831   
832   snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
833   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
834   context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
835
836   if(st_gl_mode == ST_GL_TEST)
837     {
838       context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
839           LEFT_ALLIGN, LAYER_FOREGROUND1);
840     }
841
842   if(!time_left.check()) {
843     context.draw_text(white_text, _("TIME's UP"), Vector(screen->w/2, 0),
844         CENTER_ALLIGN, LAYER_FOREGROUND1);
845   } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
846     sprintf(str, " %d", time_left.get_left() / 1000 );
847     context.draw_text(white_text, _("TIME"),
848         Vector(screen->w/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
849     context.draw_text(gold_text, str,
850         Vector(screen->w/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
851   }
852
853   sprintf(str, " %d", player_status.distros);
854   context.draw_text(white_text, _("COINS"),
855       Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
856         LEFT_ALLIGN, LAYER_FOREGROUND1);
857   context.draw_text(gold_text, str,
858       Vector(screen->w - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
859
860   if (player_status.lives >= 5)
861     {
862       sprintf(str, "%dx", player_status.lives);
863       float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
864       context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
865       context.draw_surface(tux_life, Vector(screen->w - 16, 20),
866           LAYER_FOREGROUND1);
867     }
868   else
869     {
870       for(int i= 0; i < player_status.lives; ++i)
871         context.draw_surface(tux_life, 
872             Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
873             LAYER_FOREGROUND1);
874     }
875
876   context.draw_text(white_text, _("LIVES"),
877       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
878       LEFT_ALLIGN, LAYER_FOREGROUND1);
879
880   if(show_fps)
881     {
882       sprintf(str, "%2.1f", fps_fps);
883       context.draw_text(white_text, "FPS", 
884           Vector(screen->w - white_text->get_text_width("FPS     "), 40),
885           LEFT_ALLIGN, LAYER_FOREGROUND1);
886       context.draw_text(gold_text, str,
887           Vector(screen->w-4*16, 40), LEFT_ALLIGN, LAYER_FOREGROUND1);
888     }
889 }
890
891 void
892 GameSession::drawresultscreen(void)
893 {
894   char str[80];
895
896   DrawingContext context;
897   currentsector->background->draw(context);  
898
899   context.draw_text(blue_text, _("Result:"), Vector(screen->w/2, 200),
900       CENTER_ALLIGN, LAYER_FOREGROUND1);
901
902   sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
903   context.draw_text(gold_text, str, Vector(screen->w/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
904
905   sprintf(str, _("COINS: %d"), player_status.distros);
906   context.draw_text(gold_text, str, Vector(screen->w/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
907
908   context.do_drawing();
909   
910   SDL_Event event;
911   wait_for_event(event,2000,5000,true);
912 }
913
914 std::string slotinfo(int slot)
915 {
916   std::string tmp;
917   std::string slotfile;
918   std::string title;
919   std::stringstream stream;
920   stream << slot;
921   slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
922
923   lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
924   if (savegame)
925     {
926       LispReader reader(lisp_cdr(savegame));
927       reader.read_string("title", title);
928       lisp_free(savegame);
929     }
930
931   if (access(slotfile.c_str(), F_OK) == 0)
932     {
933       if (!title.empty())
934         tmp = "Slot " + stream.str() + " - " + title;
935       else
936         tmp = "Slot " + stream.str() + " - Savegame";
937     }
938   else
939     tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
940
941   return tmp;
942 }
943
944 bool process_load_game_menu()
945 {
946   int slot = load_game_menu->check();
947
948   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
949     {
950       std::stringstream stream;
951       stream << slot;
952       std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
953
954       if (access(slotfile.c_str(), F_OK) != 0)
955         {
956           draw_intro();
957         }
958
959       // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
960       fadeout(256);
961
962       DrawingContext context;
963       context.draw_text(white_text, "Loading...",
964                         Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
965       context.do_drawing();
966
967       WorldMapNS::WorldMap worldmap;
968
969       // Load the game or at least set the savegame_file variable
970       worldmap.loadgame(slotfile);
971
972       worldmap.display();
973
974       Menu::set_current(main_menu);
975
976       Ticks::pause_stop();
977       return true;
978     }
979   else
980     {
981       return false;
982     }
983 }