93abe39072f4394036236813372133b023dad166
[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                           // "lifeup" activates pause cause of the 'p'
411                           // so work around to ignore it
412                             if(game_pause)
413                               {
414                                 game_pause = false;
415                                 Ticks::pause_stop();
416                               }
417                             else
418                               {
419                                 game_pause = true;
420                                 Ticks::pause_start();
421                               }
422                           }
423                         if(compare_last(last_keys, "lifedown"))
424                           {
425                           player_status.lives--;
426                           last_keys.clear();
427                           }
428                         if(compare_last(last_keys, "invincible"))
429                           {    // be invincle for the rest of the level
430                           tux.invincible_timer.start(time_left.get_left());
431                           last_keys.clear();
432                           }
433
434                   break;
435
436                 case SDL_JOYAXISMOTION:
437                   if (event.jaxis.axis == joystick_keymap.x_axis)
438                     {
439                       if (event.jaxis.value < -joystick_keymap.dead_zone)
440                         {
441                           tux.input.left  = DOWN;
442                           tux.input.right = UP;
443                         }
444                       else if (event.jaxis.value > joystick_keymap.dead_zone)
445                         {
446                           tux.input.left  = UP;
447                           tux.input.right = DOWN;
448                         }
449                       else
450                         {
451                           tux.input.left  = DOWN;
452                           tux.input.right = DOWN;
453                         }
454                     }
455                   else if (event.jaxis.axis == joystick_keymap.y_axis)
456                     {
457                       if (event.jaxis.value > joystick_keymap.dead_zone)
458                         tux.input.down = DOWN;
459                       else if (event.jaxis.value < -joystick_keymap.dead_zone)
460                         tux.input.down = UP;
461                       else
462                         tux.input.down = UP;
463                     }
464                   break;
465
466                 case SDL_JOYHATMOTION:
467                   if(event.jhat.value & SDL_HAT_UP) {
468                     tux.input.up = DOWN;
469                     tux.input.down = UP;
470                   } else if(event.jhat.value & SDL_HAT_DOWN) {
471                     tux.input.up = UP;
472                     tux.input.down = DOWN;
473                   } else if(event.jhat.value & SDL_HAT_LEFT) {
474                     tux.input.left = DOWN;
475                     tux.input.right = UP;
476                   } else if(event.jhat.value & SDL_HAT_RIGHT) {
477                     tux.input.left = UP;
478                     tux.input.right = DOWN;
479                   } else if(event.jhat.value == SDL_HAT_CENTERED) {
480                     tux.input.left = UP;
481                     tux.input.right = UP;
482                     tux.input.up = UP;
483                     tux.input.down = UP;
484                   }
485                   break;
486             
487                 case SDL_JOYBUTTONDOWN:
488                   if (event.jbutton.button == joystick_keymap.a_button)
489                     tux.input.up = DOWN;
490                   else if (event.jbutton.button == joystick_keymap.b_button)
491                     tux.input.fire = DOWN;
492                   else if (event.jbutton.button == joystick_keymap.start_button)
493                     on_escape_press();
494                   break;
495                 case SDL_JOYBUTTONUP:
496                   if (event.jbutton.button == joystick_keymap.a_button)
497                     tux.input.up = UP;
498                   else if (event.jbutton.button == joystick_keymap.b_button)
499                     tux.input.fire = UP;
500                   break;
501
502                 default:
503                   break;
504                 }  /* switch */
505             }
506         } /* while */
507     }
508 }
509
510 void
511 GameSession::check_end_conditions()
512 {
513   Player* tux = currentsector->player;
514
515   /* End of level? */
516   Tile* endtile = collision_goal(tux->base);
517
518   if(end_sequence && !endsequence_timer.check())
519     {
520       exit_status = ES_LEVEL_FINISHED;
521       return;
522     }
523   else if(end_sequence == ENDSEQUENCE_RUNNING && endtile && endtile->data >= 1)
524     {
525       end_sequence = ENDSEQUENCE_WAITING;
526     }
527   else if(!end_sequence && endtile && endtile->data == 0)
528     {
529       end_sequence = ENDSEQUENCE_RUNNING;
530       random_timer.start(200);  // start 1st firework
531       last_x_pos = -1;
532       SoundManager::get()->play_music(level_end_song, 0);
533       endsequence_timer.start(7000); // 5 seconds until we finish the map
534       tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
535     }
536   else if (!end_sequence && tux->is_dead())
537     {
538       player_status.bonus = PlayerStatus::NO_BONUS;
539
540       if (player_status.lives < 0)
541         { // No more lives!?
542           exit_status = ES_GAME_OVER;
543         }
544       else
545         { // Still has lives, so reset Tux to the levelstart
546           restart_level();
547         }
548
549       return;
550     }
551 }
552
553 void
554 GameSession::action(double frame_ratio)
555 {
556   if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
557     {
558       // Update Tux and the World
559       currentsector->action(frame_ratio);
560     }
561
562   // respawning in new sector?
563   if(newsector != "" && newspawnpoint != "") {
564     Sector* sector = level->get_sector(newsector);
565     currentsector = sector;
566     currentsector->activate(newspawnpoint);
567     currentsector->play_music(LEVEL_MUSIC);
568     newsector = newspawnpoint = "";
569   }
570
571   // on end sequence make a few fireworks
572   if(end_sequence == ENDSEQUENCE_RUNNING && !random_timer.check())
573     {
574     Vector epicenter = currentsector->camera->get_translation();
575     epicenter.x += screen->w * ((float)rand() / RAND_MAX);
576     epicenter.y += (screen->h/2) * ((float)rand() / RAND_MAX);
577
578     int red = rand() % 255;  // calculate firework color
579     int green = rand() % red;
580     currentsector->add_particles(epicenter, Vector(1.4,1.4), Vector(0,0),
581                                  45, Color(red,green,0), 3, 1300);
582
583     SoundManager::get()->play_sound(IDToSound(SND_FIREWORKS));
584     random_timer.start(rand() % 400 + 600);  // next firework
585     }
586 }
587
588 void 
589 GameSession::draw()
590 {
591   currentsector->draw(*context);
592   drawstatus(*context);
593
594   if(game_pause)
595     {
596       int x = screen->h / 20;
597       for(int i = 0; i < x; ++i)
598         {
599           context->draw_filled_rect(
600               Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
601                 -((pause_menu_frame * i)%screen->w)
602                 ,(i*20+pause_menu_frame)%screen->h),
603               Vector(screen->w,10),
604               Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
605         }
606       context->draw_filled_rect(
607           Vector(0,0), Vector(screen->w, screen->h),
608           Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
609       context->draw_text_center(blue_text, _("PAUSE - Press 'P' To Play"),
610           Vector(0, 230), LAYER_FOREGROUND1+2);
611
612       char str1[60];
613       char str2[124];
614       sprintf(str1, _("Playing: "));
615       sprintf(str2, level->name.c_str());
616
617       context->draw_text(blue_text, str1,
618           Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
619           LAYER_FOREGROUND1+2);
620       context->draw_text(white_text, str2,
621           Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
622           LAYER_FOREGROUND1+2);
623     }
624
625   if(Menu::current())
626     {
627       Menu::current()->draw(*context);
628       mouse_cursor->draw(*context);
629     }
630
631   context->do_drawing();
632 }
633
634 void
635 GameSession::process_menu()
636 {
637   Menu* menu = Menu::current();
638   if(menu)
639     {
640       menu->action();
641
642       if(menu == game_menu)
643         {
644           switch (game_menu->check())
645             {
646             case MNID_CONTINUE:
647               Ticks::pause_stop();
648               break;
649             case MNID_ABORTLEVEL:
650               Ticks::pause_stop();
651               exit_status = ES_LEVEL_ABORT;
652               break;
653             }
654         }
655       else if(menu == options_menu)
656         {
657           process_options_menu();
658         }
659       else if(menu == load_game_menu )
660         {
661           process_load_game_menu();
662         }
663     }
664 }
665
666 GameSession::ExitStatus
667 GameSession::run()
668 {
669   Menu::set_current(0);
670   current_ = this;
671   
672   int fps_cnt = 0;
673
674   frame_rate.start();
675
676   // Eat unneeded events
677   SDL_Event event;
678   while (SDL_PollEvent(&event)) {}
679
680   draw();
681
682   while (exit_status == ES_NONE)
683     {
684       /* Calculate the movement-factor */
685       double frame_ratio = frame_rate.get();
686
687       if(!frame_timer.check())
688         {
689           frame_timer.start(25);
690           ++global_frame_counter;
691         }
692
693       /* Handle events: */
694       currentsector->player->input.old_fire 
695         = currentsector->player->input.fire;
696
697       process_events();
698       process_menu();
699
700       // Update the world state and all objects in the world
701       // Do that with a constante time-delta so that the game will run
702       // determistic and not different on different machines
703       if(!game_pause && !Menu::current())
704         {
705           // Update the world
706           check_end_conditions();
707           if (end_sequence == ENDSEQUENCE_RUNNING)
708              action(frame_ratio/2);
709           else if(end_sequence == NO_ENDSEQUENCE)
710              action(frame_ratio);
711         }
712       else
713         {
714           ++pause_menu_frame;
715           SDL_Delay(50);
716         }
717
718       draw();
719
720       /* Time stops in pause mode */
721       if(game_pause || Menu::current())
722         {
723           continue;
724         }
725
726       frame_rate.update();
727
728       /* Handle time: */
729       if (!time_left.check() && currentsector->player->dying == DYING_NOT
730               && !end_sequence)
731         currentsector->player->kill(Player::KILL);
732
733       /* Handle music: */
734       if(currentsector->player->invincible_timer.check() && !end_sequence)
735         {
736           currentsector->play_music(HERRING_MUSIC);
737         }
738       /* are we low on time ? */
739       else if (time_left.get_left() < TIME_WARNING && !end_sequence)
740         {
741           currentsector->play_music(HURRYUP_MUSIC);
742         }
743       /* or just normal music? */
744       else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
745         {
746           currentsector->play_music(LEVEL_MUSIC);
747         }
748
749       /* Calculate frames per second */
750       if(show_fps)
751         {
752           ++fps_cnt;
753           fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
754
755           if(!fps_timer.check())
756             {
757               fps_timer.start(1000);
758               fps_cnt = 0;
759             }
760         }
761     }
762   
763   return exit_status;
764 }
765
766 void
767 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
768 {
769   newsector = sector;
770   newspawnpoint = spawnpoint;
771 }
772
773 /* Bounce a brick: */
774 void bumpbrick(float x, float y)
775 {
776   Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
777                          (int)(y / 32) * 32));
778
779   SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
780 }
781
782 /* (Status): */
783 void
784 GameSession::drawstatus(DrawingContext& context)
785 {
786   char str[60];
787   
788   snprintf(str, 60, " %d", player_status.score);
789   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
790   context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
791
792   if(st_gl_mode == ST_GL_TEST)
793     {
794       context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
795           LAYER_FOREGROUND1);
796     }
797
798   if(!time_left.check()) {
799     context.draw_text_center(white_text, _("TIME's UP"), Vector(0, 0),
800         LAYER_FOREGROUND1);
801   } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
802     sprintf(str, " %d", time_left.get_left() / 1000 );
803     context.draw_text_center(white_text, _("TIME"),
804         Vector(0, 0), LAYER_FOREGROUND1);
805     context.draw_text_center(gold_text, str,
806         Vector(4*16, 0), LAYER_FOREGROUND1);
807   }
808
809   sprintf(str, " %d", player_status.distros);
810   context.draw_text(white_text, _("COINS"),
811       Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
812         LAYER_FOREGROUND1);
813   context.draw_text(gold_text, str,
814       Vector(screen->w - gold_text->get_text_width(" 99"), 0),LAYER_FOREGROUND1);
815
816   if (player_status.lives >= 5)
817     {
818       sprintf(str, "%dx", player_status.lives);
819       float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
820       context.draw_text(gold_text, str, Vector(x, 20), LAYER_FOREGROUND1);
821       context.draw_surface(tux_life, Vector(screen->w - 16, 20),
822           LAYER_FOREGROUND1);
823     }
824   else
825     {
826       for(int i= 0; i < player_status.lives; ++i)
827         context.draw_surface(tux_life, 
828             Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
829             LAYER_FOREGROUND1);
830     }
831
832   context.draw_text(white_text, _("LIVES"),
833       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
834       LAYER_FOREGROUND1);
835
836   if(show_fps)
837     {
838       sprintf(str, "%2.1f", fps_fps);
839       context.draw_text(white_text, "FPS", 
840           Vector(screen->w - white_text->get_text_width("FPS     "), 40),
841           LAYER_FOREGROUND1);
842       context.draw_text(gold_text, str,
843           Vector(screen->w-4*16, 40), LAYER_FOREGROUND1);
844     }
845 }
846
847 void
848 GameSession::drawresultscreen(void)
849 {
850   char str[80];
851
852   DrawingContext context;
853   currentsector->background->draw(context);  
854
855   context.draw_text_center(blue_text, _("Result:"), Vector(0, 200),
856       LAYER_FOREGROUND1);
857
858   sprintf(str, _("SCORE: %d"), player_status.score);
859   context.draw_text_center(gold_text, str, Vector(0, 224), LAYER_FOREGROUND1);
860
861   sprintf(str, _("COINS: %d"), player_status.distros);
862   context.draw_text_center(gold_text, str, Vector(0, 256), LAYER_FOREGROUND1);
863
864   context.do_drawing();
865   
866   SDL_Event event;
867   wait_for_event(event,2000,5000,true);
868 }
869
870 std::string slotinfo(int slot)
871 {
872   std::string tmp;
873   std::string slotfile;
874   std::string title;
875   std::stringstream stream;
876   stream << slot;
877   slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
878
879   lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
880   if (savegame)
881     {
882       LispReader reader(lisp_cdr(savegame));
883       reader.read_string("title", title);
884       lisp_free(savegame);
885     }
886
887   if (access(slotfile.c_str(), F_OK) == 0)
888     {
889       if (!title.empty())
890         tmp = "Slot " + stream.str() + " - " + title;
891       else
892         tmp = "Slot " + stream.str() + " - Savegame";
893     }
894   else
895     tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
896
897   return tmp;
898 }
899
900 bool process_load_game_menu()
901 {
902   int slot = load_game_menu->check();
903
904   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
905     {
906       std::stringstream stream;
907       stream << slot;
908       std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
909
910       if (access(slotfile.c_str(), F_OK) != 0)
911         {
912           draw_intro();
913         }
914
915       // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
916       fadeout(256);
917
918       DrawingContext context;
919       context.draw_text_center(white_text, "Loading...",
920                                Vector(0, screen->h/2), LAYER_FOREGROUND1);
921       context.do_drawing();
922
923       WorldMapNS::WorldMap worldmap;
924
925       // Load the game or at least set the savegame_file variable
926       worldmap.loadgame(slotfile);
927
928       worldmap.display();
929
930       Menu::set_current(main_menu);
931
932       Ticks::pause_stop();
933       return true;
934     }
935   else
936     {
937       return false;
938     }
939 }