298b8dd440ae3ef0a6d7644214916f329f12e972
[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 firework
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 fireworks
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 firework color
567     int green = rand() % red;
568     currentsector->add_particles(epicenter, Vector(1.4,1.4), Vector(0,0),
569                                  45, Color(red,green,0), 3, 1300);
570
571     SoundManager::get()->play_sound(IDToSound(SND_FIREWORKS));
572     random_timer.start(rand() % 400 + 600);  // next firework
573     }
574 }
575
576 void 
577 GameSession::draw()
578 {
579   currentsector->draw(*context);
580   drawstatus(*context);
581
582   if(game_pause)
583     {
584       int x = screen->h / 20;
585       for(int i = 0; i < x; ++i)
586         {
587           context->draw_filled_rect(
588               Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
589                 -((pause_menu_frame * i)%screen->w)
590                 ,(i*20+pause_menu_frame)%screen->h),
591               Vector(screen->w,10),
592               Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
593         }
594       context->draw_filled_rect(
595           Vector(0,0), Vector(screen->w, screen->h),
596           Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
597       context->draw_text_center(blue_text, _("PAUSE - Press 'P' To Play"),
598           Vector(0, 230), LAYER_FOREGROUND1+2);
599
600       char str1[60];
601       char str2[124];
602       sprintf(str1, _("Playing: "));
603       sprintf(str2, level->name.c_str());
604
605       context->draw_text(blue_text, str1,
606           Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
607           LAYER_FOREGROUND1+2);
608       context->draw_text(white_text, str2,
609           Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
610           LAYER_FOREGROUND1+2);
611     }
612
613   if(Menu::current())
614     {
615       Menu::current()->draw(*context);
616       mouse_cursor->draw(*context);
617     }
618
619   context->do_drawing();
620 }
621
622 void
623 GameSession::process_menu()
624 {
625   Menu* menu = Menu::current();
626   if(menu)
627     {
628       menu->action();
629
630       if(menu == game_menu)
631         {
632           switch (game_menu->check())
633             {
634             case MNID_CONTINUE:
635               Ticks::pause_stop();
636               break;
637             case MNID_ABORTLEVEL:
638               Ticks::pause_stop();
639               exit_status = ES_LEVEL_ABORT;
640               break;
641             }
642         }
643       else if(menu == options_menu)
644         {
645           process_options_menu();
646         }
647       else if(menu == load_game_menu )
648         {
649           process_load_game_menu();
650         }
651     }
652 }
653
654 GameSession::ExitStatus
655 GameSession::run()
656 {
657   Menu::set_current(0);
658   current_ = this;
659   
660   int fps_cnt = 0;
661
662   frame_rate.start();
663
664   // Eat unneeded events
665   SDL_Event event;
666   while (SDL_PollEvent(&event)) {}
667
668   draw();
669
670   while (exit_status == ES_NONE)
671     {
672       /* Calculate the movement-factor */
673       double frame_ratio = frame_rate.get();
674
675       if(!frame_timer.check())
676         {
677           frame_timer.start(25);
678           ++global_frame_counter;
679         }
680
681       /* Handle events: */
682       currentsector->player->input.old_fire 
683         = currentsector->player->input.fire;
684
685       process_events();
686       process_menu();
687
688       // Update the world state and all objects in the world
689       // Do that with a constante time-delta so that the game will run
690       // determistic and not different on different machines
691       if(!game_pause && !Menu::current())
692         {
693           // Update the world
694           check_end_conditions();
695           if (end_sequence == ENDSEQUENCE_RUNNING)
696              action(frame_ratio/2);
697           else if(end_sequence == NO_ENDSEQUENCE)
698              action(frame_ratio);
699         }
700       else
701         {
702           ++pause_menu_frame;
703           SDL_Delay(50);
704         }
705
706       draw();
707
708       /* Time stops in pause mode */
709       if(game_pause || Menu::current())
710         {
711           continue;
712         }
713
714       frame_rate.update();
715
716       /* Handle time: */
717       if (!time_left.check() && currentsector->player->dying == DYING_NOT
718               && !end_sequence)
719         currentsector->player->kill(Player::KILL);
720
721       /* Handle music: */
722       if(currentsector->player->invincible_timer.check() && !end_sequence)
723         {
724           currentsector->play_music(HERRING_MUSIC);
725         }
726       /* are we low on time ? */
727       else if (time_left.get_left() < TIME_WARNING && !end_sequence)
728         {
729           currentsector->play_music(HURRYUP_MUSIC);
730         }
731       /* or just normal music? */
732       else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
733         {
734           currentsector->play_music(LEVEL_MUSIC);
735         }
736
737       /* Calculate frames per second */
738       if(show_fps)
739         {
740           ++fps_cnt;
741           fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
742
743           if(!fps_timer.check())
744             {
745               fps_timer.start(1000);
746               fps_cnt = 0;
747             }
748         }
749     }
750   
751   return exit_status;
752 }
753
754 void
755 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
756 {
757   newsector = sector;
758   newspawnpoint = spawnpoint;
759 }
760
761 /* Bounce a brick: */
762 void bumpbrick(float x, float y)
763 {
764   Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
765                          (int)(y / 32) * 32));
766
767   SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
768 }
769
770 /* (Status): */
771 void
772 GameSession::drawstatus(DrawingContext& context)
773 {
774   char str[60];
775   
776   snprintf(str, 60, " %d", player_status.score);
777   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
778   context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
779
780   if(st_gl_mode == ST_GL_TEST)
781     {
782       context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
783           LAYER_FOREGROUND1);
784     }
785
786   if(!time_left.check()) {
787     context.draw_text_center(white_text, _("TIME's UP"), Vector(0, 0),
788         LAYER_FOREGROUND1);
789   } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
790     sprintf(str, " %d", time_left.get_left() / 1000 );
791     context.draw_text_center(white_text, _("TIME"),
792         Vector(0, 0), LAYER_FOREGROUND1);
793     context.draw_text_center(gold_text, str,
794         Vector(4*16, 0), LAYER_FOREGROUND1);
795   }
796
797   sprintf(str, " %d", player_status.distros);
798   context.draw_text(white_text, _("COINS"),
799       Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
800         LAYER_FOREGROUND1);
801   context.draw_text(gold_text, str,
802       Vector(screen->w - gold_text->get_text_width(" 99"), 0),LAYER_FOREGROUND1);
803
804   if (player_status.lives >= 5)
805     {
806       sprintf(str, "%dx", player_status.lives);
807       float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
808       context.draw_text(gold_text, str, Vector(x, 20), LAYER_FOREGROUND1);
809       context.draw_surface(tux_life, Vector(screen->w - 16, 20),
810           LAYER_FOREGROUND1);
811     }
812   else
813     {
814       for(int i= 0; i < player_status.lives; ++i)
815         context.draw_surface(tux_life, 
816             Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
817             LAYER_FOREGROUND1);
818     }
819
820   context.draw_text(white_text, _("LIVES"),
821       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
822       LAYER_FOREGROUND1);
823
824   if(show_fps)
825     {
826       sprintf(str, "%2.1f", fps_fps);
827       context.draw_text(white_text, "FPS", 
828           Vector(screen->w - white_text->get_text_width("FPS     "), 40),
829           LAYER_FOREGROUND1);
830       context.draw_text(gold_text, str,
831           Vector(screen->w-4*16, 40), LAYER_FOREGROUND1);
832     }
833 }
834
835 void
836 GameSession::drawresultscreen(void)
837 {
838   char str[80];
839
840   DrawingContext context;
841   currentsector->background->draw(context);  
842
843   context.draw_text_center(blue_text, _("Result:"), Vector(0, 200),
844       LAYER_FOREGROUND1);
845
846   sprintf(str, _("SCORE: %d"), player_status.score);
847   context.draw_text_center(gold_text, str, Vector(0, 224), LAYER_FOREGROUND1);
848
849   sprintf(str, _("COINS: %d"), player_status.distros);
850   context.draw_text_center(gold_text, str, Vector(0, 256), LAYER_FOREGROUND1);
851
852   context.do_drawing();
853   
854   SDL_Event event;
855   wait_for_event(event,2000,5000,true);
856 }
857
858 std::string slotinfo(int slot)
859 {
860   std::string tmp;
861   std::string slotfile;
862   std::string title;
863   std::stringstream stream;
864   stream << slot;
865   slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
866
867   lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
868   if (savegame)
869     {
870       LispReader reader(lisp_cdr(savegame));
871       reader.read_string("title", title);
872       lisp_free(savegame);
873     }
874
875   if (access(slotfile.c_str(), F_OK) == 0)
876     {
877       if (!title.empty())
878         tmp = "Slot " + stream.str() + " - " + title;
879       else
880         tmp = "Slot " + stream.str() + " - Savegame";
881     }
882   else
883     tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
884
885   return tmp;
886 }
887
888 bool process_load_game_menu()
889 {
890   int slot = load_game_menu->check();
891
892   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
893     {
894       std::stringstream stream;
895       stream << slot;
896       std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
897
898       if (access(slotfile.c_str(), F_OK) != 0)
899         {
900           draw_intro();
901         }
902
903       // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
904       fadeout(256);
905
906       DrawingContext context;
907       context.draw_text_center(white_text, "Loading...",
908                                Vector(0, screen->h/2), LAYER_FOREGROUND1);
909       context.do_drawing();
910
911       WorldMapNS::WorldMap worldmap;
912
913       // Load the game or at least set the savegame_file variable
914       worldmap.loadgame(slotfile);
915
916       worldmap.display();
917
918       Menu::set_current(main_menu);
919
920       Ticks::pause_stop();
921       return true;
922     }
923   else
924     {
925       return false;
926     }
927 }