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