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