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