3b593f955e95d12961ecde83c8e8385aea9850e7
[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 #include <config.h>
22
23 #include <iostream>
24 #include <sstream>
25 #include <cassert>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cmath>
29 #include <cstring>
30 #include <cerrno>
31 #include <unistd.h>
32 #include <ctime>
33 #include <stdexcept>
34
35 #include <SDL.h>
36
37 #ifndef WIN32
38 #include <sys/types.h>
39 #include <ctype.h>
40 #endif
41
42 #include "app/globals.h"
43 #include "gameloop.h"
44 #include "video/screen.h"
45 #include "app/setup.h"
46 #include "gui/menu.h"
47 #include "sector.h"
48 #include "level.h"
49 #include "tile.h"
50 #include "player_status.h"
51 #include "object/particlesystem.h"
52 #include "object/background.h"
53 #include "object/tilemap.h"
54 #include "object/camera.h"
55 #include "object/player.h"
56 #include "lisp/lisp.h"
57 #include "lisp/parser.h"
58 #include "resources.h"
59 #include "app/gettext.h"
60 #include "worldmap.h"
61 #include "misc.h"
62 #include "statistics.h"
63 #include "timer.h"
64 #include "object/fireworks.h"
65
66 GameSession* GameSession::current_ = 0;
67
68 bool compare_last(std::string& haystack, std::string needle)
69 {
70   int haystack_size = haystack.size();
71   int needle_size = needle.size();
72
73   if(haystack_size < needle_size)
74     return false;
75
76   if(haystack.compare(haystack_size-needle_size, needle_size, needle) == 0)
77     return true;
78   return false;
79 }
80
81 GameSession::GameSession(const std::string& levelfile_, int mode,
82     Statistics* statistics)
83   : level(0), currentsector(0), st_gl_mode(mode),
84     end_sequence(NO_ENDSEQUENCE), levelfile(levelfile_),
85     best_level_statistics(statistics)
86 {
87   current_ = this;
88   
89   game_pause = false;
90   fps_fps = 0;
91
92   context = new DrawingContext();
93
94   restart_level();
95 }
96
97 void
98 GameSession::restart_level()
99 {
100   game_pause   = false;
101   exit_status  = ES_NONE;
102   end_sequence = NO_ENDSEQUENCE;
103
104   last_keys.clear();
105
106 #if 0
107   Vector tux_pos = Vector(-1,-1);
108   if (currentsector)
109     { 
110       // Tux has lost a life, so we try to respawn him at the nearest reset point
111       tux_pos = currentsector->player->base;
112     }
113 #endif
114   
115   delete level;
116   currentsector = 0;
117
118   level = new Level;
119   level->load(levelfile);
120
121   global_stats.reset();
122   global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
123   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
124   global_stats.set_total_points(TIME_NEEDED_STAT, level->timelimit);
125
126   if(reset_sector != "") {
127     currentsector = level->get_sector(reset_sector);
128     if(!currentsector) {
129       std::stringstream msg;
130       msg << "Couldn't find sector '" << reset_sector << "' for resetting tux.";
131       throw std::runtime_error(msg.str());
132     }
133     currentsector->activate(reset_pos);
134   } else {
135     currentsector = level->get_sector("main");
136     if(!currentsector)
137       throw std::runtime_error("Couldn't find main sector");
138     currentsector->activate("main");
139   }
140   
141   if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
142     levelintro();
143
144   start_timers();
145   currentsector->play_music(LEVEL_MUSIC);
146 }
147
148 GameSession::~GameSession()
149 {
150   delete level;
151   delete context;
152 }
153
154 void
155 GameSession::levelintro()
156 {
157   SoundManager::get()->halt_music();
158   
159   char str[60];
160
161   DrawingContext context;
162   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
163       i != currentsector->gameobjects.end(); ++i) {
164     Background* background = dynamic_cast<Background*> (*i);
165     if(background) {
166       background->draw(context);
167     }
168   }
169
170 //  context.draw_text(gold_text, level->get_name(), Vector(screen->w/2, 160),
171 //      CENTER_ALLIGN, LAYER_FOREGROUND1);
172   context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
173       LAYER_FOREGROUND1);
174
175   sprintf(str, "TUX x %d", player_status.lives);
176   context.draw_text(white_text, str, Vector(screen->w/2, 210),
177       CENTER_ALLIGN, LAYER_FOREGROUND1);
178
179   if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
180     //TODO make author check case/blank-insensitive
181     context.draw_text(white_small_text,
182       std::string(_("contributed by ")) + level->get_author(), 
183       Vector(screen->w/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
184
185
186   if(best_level_statistics != NULL)
187     best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
188
189   context.do_drawing();
190
191   SDL_Event event;
192   wait_for_event(event,1000,3000,true);
193 }
194
195 /* Reset Timers */
196 void
197 GameSession::start_timers()
198 {
199   time_left.start(level->timelimit);
200   Ticks::pause_init();
201 }
202
203 void
204 GameSession::on_escape_press()
205 {
206   if(currentsector->player->is_dying() || end_sequence != NO_ENDSEQUENCE)
207     return;   // don't let the player open the menu, when he is dying
208   
209   if(game_pause)
210     return;
211
212   if(st_gl_mode == ST_GL_TEST) {
213     exit_status = ES_LEVEL_ABORT;
214   } else if (!Menu::current()) {
215     Menu::set_current(game_menu);
216     Ticks::pause_start();
217   }
218 }
219
220 void
221 GameSession::process_events()
222 {
223   if (end_sequence != NO_ENDSEQUENCE)
224     {
225       Player& tux = *currentsector->player;
226          
227       tux.input.fire  = false;
228       tux.input.left  = false;
229       tux.input.right = true;
230       tux.input.down  = false; 
231
232       if (int(last_x_pos) == int(tux.get_pos().x))
233         tux.input.up    = true; 
234       else
235         tux.input.up    = false;
236
237       last_x_pos = tux.get_pos().x;
238
239       SDL_Event event;
240       while (SDL_PollEvent(&event))
241         {
242           /* Check for menu-events, if the menu is shown */
243           if (Menu::current())
244             {
245               Menu::current()->event(event);
246               if(!Menu::current())
247               Ticks::pause_stop();
248             }
249
250           switch(event.type)
251             {
252             case SDL_QUIT:        /* Quit event - quit: */
253               Termination::abort("Received window close", "");
254               break;
255               
256             case SDL_KEYDOWN:     /* A keypress! */
257               {
258                 SDLKey key = event.key.keysym.sym;
259            
260                 switch(key)
261                   {
262                   case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
263                     on_escape_press();
264                     break;
265                   default:
266                     break;
267                   }
268               }
269           
270             case SDL_JOYBUTTONDOWN:
271               if (event.jbutton.button == joystick_keymap.start_button)
272                 on_escape_press();
273               break;
274             }
275         }
276     }
277   else // normal mode
278     {
279       if(!Menu::current() && !game_pause)
280         Ticks::pause_stop();
281
282       SDL_Event event;
283       while (SDL_PollEvent(&event))
284         {
285           /* Check for menu-events, if the menu is shown */
286           if (Menu::current())
287             {
288               Menu::current()->event(event);
289               if(!Menu::current())
290                 Ticks::pause_stop();
291             }
292           else
293             {
294               Player& tux = *currentsector->player;
295   
296               switch(event.type)
297                 {
298                 case SDL_QUIT:        /* Quit event - quit: */
299                   Termination::abort("Received window close", "");
300                   break;
301
302                 case SDL_KEYDOWN:     /* A keypress! */
303                   {
304                     SDLKey key = event.key.keysym.sym;
305             
306                     if(tux.key_event(key, true))
307                       break;
308
309                     switch(key)
310                       {
311                       case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
312                         on_escape_press();
313                         break;
314                       default:
315                         break;
316                       }
317                   }
318                   break;
319                 case SDL_KEYUP:      /* A keyrelease! */
320                   {
321                     SDLKey key = event.key.keysym.sym;
322
323                     if(tux.key_event(key, false))
324                       break;
325
326                     switch(key)
327                       {
328                       case SDLK_a:
329                         if(debug_mode)
330                         {
331                           char buf[160];
332                           snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
333                               tux.get_pos().x, tux.get_pos().y);
334                           context->draw_text(white_text, buf,
335                               Vector(0, screen->h - white_text->get_height()),
336                               LEFT_ALLIGN, LAYER_FOREGROUND1);
337                           context->do_drawing();
338                           SDL_Delay(1000);
339                         }
340                         break;
341                       case SDLK_p:
342                         if(!Menu::current())
343                           {
344                           // "lifeup" cheat activates pause cause of the 'p'
345                           // so work around to ignore it
346                             if(compare_last(last_keys, "lifeu"))
347                               break;
348
349                             if(game_pause)
350                               {
351                                 game_pause = false;
352                                 Ticks::pause_stop();
353                               }
354                             else
355                               {
356                                 game_pause = true;
357                                 Ticks::pause_start();
358                               }
359                           }
360                         break;
361                       default:
362                         break;
363                       }
364                   }
365
366                   /* Check if chacrater is ASCII */
367                   char ch[2];
368                   if((event.key.keysym.unicode & 0xFF80) == 0)
369                   {
370                       ch[0] = event.key.keysym.unicode & 0x7F;
371                       ch[1] = '\0';
372                   }
373                         last_keys.append(ch);  // add to cheat keys
374                         handle_cheats();
375                   break;
376
377                 case SDL_JOYAXISMOTION:
378                   if (event.jaxis.axis == joystick_keymap.x_axis)
379                     {
380                       if (event.jaxis.value < -joystick_keymap.dead_zone)
381                         {
382                           tux.input.left  = true;
383                           tux.input.right = false;
384                         }
385                       else if (event.jaxis.value > joystick_keymap.dead_zone)
386                         {
387                           tux.input.left  = false;
388                           tux.input.right = true;
389                         }
390                       else
391                         {
392                           tux.input.left  = false;
393                           tux.input.right = false;
394                         }
395                     }
396                   else if (event.jaxis.axis == joystick_keymap.y_axis)
397                     {
398                       if (event.jaxis.value > joystick_keymap.dead_zone)
399                         {
400                         tux.input.up = true;
401                         tux.input.down = false;
402                         }
403                       else if (event.jaxis.value < -joystick_keymap.dead_zone)
404                         {
405                         tux.input.up = false;
406                         tux.input.down = true;
407                         }
408                       else
409                         {
410                         tux.input.up = false;
411                         tux.input.down = false;
412                         }
413                     }
414                   break;
415
416                 case SDL_JOYHATMOTION:
417                   if(event.jhat.value & SDL_HAT_UP) {
418                     tux.input.up = true;
419                     tux.input.down = false;
420                   } else if(event.jhat.value & SDL_HAT_DOWN) {
421                     tux.input.up = false;
422                     tux.input.down = true;
423                   } else if(event.jhat.value & SDL_HAT_LEFT) {
424                     tux.input.left = true;
425                     tux.input.right = false;
426                   } else if(event.jhat.value & SDL_HAT_RIGHT) {
427                     tux.input.left = false;
428                     tux.input.right = true;
429                   } else if(event.jhat.value == SDL_HAT_CENTERED) {
430                     tux.input.left = false;
431                     tux.input.right = false;
432                     tux.input.up = false;
433                     tux.input.down = false;
434                   }
435                   break;
436             
437                 case SDL_JOYBUTTONDOWN:
438                   // FIXME: I assume we have to set old_jump and stuff here?!?
439                   if (event.jbutton.button == joystick_keymap.a_button)
440                     tux.input.jump = true;
441                   else if (event.jbutton.button == joystick_keymap.b_button)
442                     tux.input.fire = true;
443                   else if (event.jbutton.button == joystick_keymap.start_button)
444                     on_escape_press();
445                   break;
446                 case SDL_JOYBUTTONUP:
447                   if (event.jbutton.button == joystick_keymap.a_button)
448                     tux.input.jump = false;
449                   else if (event.jbutton.button == joystick_keymap.b_button)
450                     tux.input.fire = false;
451                   break;
452
453                 default:
454                   break;
455                 }  /* switch */
456             }
457         } /* while */
458     }
459 }
460
461 void
462 GameSession::handle_cheats()
463 {
464   Player& tux = *currentsector->player;
465   
466   // Cheating words (the goal of this is really for debugging,
467   // but could be used for some cheating, nothing wrong with that)
468   if(compare_last(last_keys, "grow")) {
469     tux.grow(false);
470     last_keys.clear();
471   }
472   if(compare_last(last_keys, "fire")) {
473     tux.grow(false);
474     tux.got_power = tux.FIRE_POWER;
475     last_keys.clear();
476   }
477   if(compare_last(last_keys, "ice")) {
478     tux.grow(false);
479     tux.got_power = tux.ICE_POWER;
480     last_keys.clear();
481   }
482   if(compare_last(last_keys, "lifeup")) {
483     player_status.lives++;
484     last_keys.clear();
485   }
486   if(compare_last(last_keys, "lifedown")) {
487     player_status.lives--;
488     last_keys.clear();
489   }
490   if(compare_last(last_keys, "grease")) {
491     tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
492     last_keys.clear();
493   }
494   if(compare_last(last_keys, "invincible")) {
495     // be invincle for the rest of the level
496     tux.invincible_timer.start(10000);
497     last_keys.clear();
498   }
499   if(compare_last(last_keys, "shrink")) {
500     // remove powerups
501     tux.kill(tux.SHRINK);
502     last_keys.clear();
503   }
504   if(compare_last(last_keys, "kill")) {
505     // kill Tux, but without losing a life
506     player_status.lives++;
507     tux.kill(tux.KILL);
508     last_keys.clear();
509   }
510   if(compare_last(last_keys, "grid")) {
511     // toggle debug grid
512     debug_grid = !debug_grid;
513     last_keys.clear();
514   }
515   if(compare_last(last_keys, "hover")) {
516     // toggle hover ability on/off
517     tux.enable_hover = !tux.enable_hover;
518     last_keys.clear();
519   }
520   if(compare_last(last_keys, "gotoend")) {
521     // goes to the end of the level
522     tux.move(Vector(
523           (currentsector->solids->get_width()*32) - (screen->w*2), 0));
524     currentsector->camera->reset(
525         Vector(tux.get_pos().x, tux.get_pos().y));
526     last_keys.clear();
527   }
528   if(compare_last(last_keys, "finish")) {
529     // finish current sector
530     exit_status = ES_LEVEL_FINISHED;
531     // don't add points to stats though...
532   }
533   // temporary to help player's choosing a flapping
534   if(compare_last(last_keys, "marek")) {
535     tux.flapping_mode = Player::MAREK_FLAP;
536     last_keys.clear();
537   }
538   if(compare_last(last_keys, "ricardo")) {
539     tux.flapping_mode = Player::RICARDO_FLAP;
540     last_keys.clear();
541   }
542   if(compare_last(last_keys, "ryan")) {
543     tux.flapping_mode = Player::RYAN_FLAP;
544     last_keys.clear();
545   }
546 }
547
548 void
549 GameSession::check_end_conditions()
550 {
551   Player* tux = currentsector->player;
552
553   /* End of level? */
554   if(end_sequence && endsequence_timer.check()) {
555     exit_status = ES_LEVEL_FINISHED;
556     return;
557   } else if (!end_sequence && tux->is_dead()) {
558     player_status.bonus = PlayerStatus::NO_BONUS;
559
560     if (player_status.lives < 0) { // No more lives!?
561       exit_status = ES_GAME_OVER;
562     } else { // Still has lives, so reset Tux to the levelstart
563       restart_level();
564     }
565     
566     return;
567   }
568 }
569
570 void
571 GameSession::action(float elapsed_time)
572 {
573   // advance timers
574   if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
575     {
576       // Update Tux and the World
577       currentsector->action(elapsed_time);
578     }
579
580   // respawning in new sector?
581   if(newsector != "" && newspawnpoint != "") {
582     Sector* sector = level->get_sector(newsector);
583     currentsector = sector;
584     currentsector->activate(newspawnpoint);
585     currentsector->play_music(LEVEL_MUSIC);
586     newsector = newspawnpoint = "";
587   }
588 }
589
590 void 
591 GameSession::draw()
592 {
593   currentsector->draw(*context);
594   drawstatus(*context);
595
596   if(game_pause)
597     {
598       int x = screen->h / 20;
599       for(int i = 0; i < x; ++i)
600         {
601           context->draw_filled_rect(
602               Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
603                 -((pause_menu_frame * i)%screen->w)
604                 ,(i*20+pause_menu_frame)%screen->h),
605               Vector(screen->w,10),
606               Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
607         }
608       context->draw_filled_rect(
609           Vector(0,0), Vector(screen->w, screen->h),
610           Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
611       context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
612           Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
613
614       char str1[60];
615       char str2[124];
616       sprintf(str1, _("Playing: "));
617       sprintf(str2, level->name.c_str());
618
619       context->draw_text(blue_text, str1,
620           Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
621           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
622       context->draw_text(white_text, str2,
623           Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
624           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
625     }
626
627   if(Menu::current())
628     {
629       Menu::current()->draw(*context);
630       mouse_cursor->draw(*context);
631     }
632
633   context->do_drawing();
634 }
635
636 void
637 GameSession::process_menu()
638 {
639   Menu* menu = Menu::current();
640   if(menu)
641     {
642       menu->action();
643
644       if(menu == game_menu)
645         {
646           switch (game_menu->check())
647             {
648             case MNID_CONTINUE:
649               Ticks::pause_stop();
650               break;
651             case MNID_ABORTLEVEL:
652               Ticks::pause_stop();
653               exit_status = ES_LEVEL_ABORT;
654               break;
655             }
656         }
657       else if(menu == options_menu)
658         {
659           process_options_menu();
660         }
661       else if(menu == load_game_menu )
662         {
663           process_load_game_menu();
664         }
665     }
666 }
667
668 GameSession::ExitStatus
669 GameSession::run()
670 {
671   Menu::set_current(0);
672   current_ = this;
673   
674   int fps_cnt = 0;
675
676   // Eat unneeded events
677   SDL_Event event;
678   while(SDL_PollEvent(&event))
679   {}
680
681   draw();
682
683   Uint32 lastticks = SDL_GetTicks();
684   fps_ticks = SDL_GetTicks();
685
686   while (exit_status == ES_NONE) {
687     Uint32 ticks = SDL_GetTicks();
688     float elapsed_time = float(ticks - lastticks) / 1000.;
689     if(!game_pause)
690       global_time += elapsed_time;
691     lastticks = ticks;
692
693     // 40fps is minimum
694     if(elapsed_time > .025)
695       elapsed_time = .025;
696     
697     /* Handle events: */
698     currentsector->player->input.old_fire = currentsector->player->input.fire;
699     currentsector->player->input.old_up = currentsector->player->input.old_up;
700
701     process_events();
702     process_menu();
703
704     // Update the world state and all objects in the world
705     // Do that with a constante time-delta so that the game will run
706     // determistic and not different on different machines
707     if(!game_pause && !Menu::current())
708     {
709       // Update the world
710       check_end_conditions();
711       if (end_sequence == ENDSEQUENCE_RUNNING)
712         action(elapsed_time/2);
713       else if(end_sequence == NO_ENDSEQUENCE)
714         action(elapsed_time);
715     }
716     else
717     {
718       ++pause_menu_frame;
719       SDL_Delay(50);
720     }
721
722     draw();
723
724     /* Time stops in pause mode */
725     if(game_pause || Menu::current())
726     {
727       continue;
728     }
729
730     //frame_rate.update();
731     
732     /* Handle time: */
733     if (time_left.check() && !end_sequence)
734       currentsector->player->kill(Player::KILL);
735     
736     /* Handle music: */
737     if (currentsector->player->invincible_timer.started() && !end_sequence)
738     {
739       currentsector->play_music(HERRING_MUSIC);
740     }
741     /* are we low on time ? */
742     else if (time_left.get_timeleft() < TIME_WARNING && !end_sequence)
743     {
744       currentsector->play_music(HURRYUP_MUSIC);
745     }
746     /* or just normal music? */
747     else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
748     {
749       currentsector->play_music(LEVEL_MUSIC);
750     }
751
752     /* Calculate frames per second */
753     if(show_fps)
754     {
755       ++fps_cnt;
756       
757       if(SDL_GetTicks() - fps_ticks >= 500)
758       {
759         fps_fps = (float) fps_cnt / .5;
760         fps_cnt = 0;
761         fps_ticks = SDL_GetTicks();
762       }
763     }
764   }
765   
766   return exit_status;
767 }
768
769 void
770 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
771 {
772   newsector = sector;
773   newspawnpoint = spawnpoint;
774 }
775
776 void
777 GameSession::set_reset_point(const std::string& sector, const Vector& pos)
778 {
779   reset_sector = sector;
780   reset_pos = pos;
781 }
782
783 void
784 GameSession::start_sequence(const std::string& sequencename)
785 {
786   if(sequencename == "endsequence" || sequencename == "fireworks") {
787     if(end_sequence)
788       return;
789     
790     end_sequence = ENDSEQUENCE_RUNNING;
791     endsequence_timer.start(7.0); // 7 seconds until we finish the map
792     last_x_pos = -1;
793     SoundManager::get()->play_music(level_end_song, 0);
794     currentsector->player->invincible_timer.start(7.0);
795
796     // add left time to stats
797     global_stats.set_points(TIME_NEEDED_STAT,
798         int(time_left.get_period() - time_left.get_timeleft()));
799
800     if(sequencename == "fireworks") {
801       currentsector->add_object(new Fireworks());
802     }
803   } else if(sequencename == "stoptux") {
804     end_sequence =  ENDSEQUENCE_WAITING;
805   } else {
806     std::cout << "Unknown sequence '" << sequencename << "'.\n";
807   }
808 }
809
810 /* (Status): */
811 void
812 GameSession::drawstatus(DrawingContext& context)
813 {
814   char str[60];
815   
816   snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
817   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
818   context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
819
820   if(st_gl_mode == ST_GL_TEST)
821     {
822       context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
823           LEFT_ALLIGN, LAYER_FOREGROUND1);
824     }
825
826   if(time_left.get_timeleft() < 0) {
827     context.draw_text(white_text, _("TIME's UP"), Vector(screen->w/2, 0),
828         CENTER_ALLIGN, LAYER_FOREGROUND1);
829   } else if (time_left.get_timeleft() > TIME_WARNING
830       || int(global_time * 2.5) % 2) {
831     sprintf(str, " %d", int(time_left.get_timeleft()));
832     context.draw_text(white_text, _("TIME"),
833         Vector(screen->w/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
834     context.draw_text(gold_text, str,
835         Vector(screen->w/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
836   }
837
838   sprintf(str, " %d", player_status.distros);
839   context.draw_text(white_text, _("COINS"),
840       Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
841         LEFT_ALLIGN, LAYER_FOREGROUND1);
842   context.draw_text(gold_text, str,
843       Vector(screen->w - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
844
845   if (player_status.lives >= 5)
846     {
847       sprintf(str, "%dx", player_status.lives);
848       float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
849       context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
850       context.draw_surface(tux_life, Vector(screen->w - 16, 20),
851           LAYER_FOREGROUND1);
852     }
853   else
854     {
855       for(int i= 0; i < player_status.lives; ++i)
856         context.draw_surface(tux_life, 
857             Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
858             LAYER_FOREGROUND1);
859     }
860
861   context.draw_text(white_text, _("LIVES"),
862       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
863       LEFT_ALLIGN, LAYER_FOREGROUND1);
864
865   if(show_fps)
866     {
867       sprintf(str, "%2.1f", fps_fps);
868       context.draw_text(white_text, "FPS", 
869           Vector(screen->w - white_text->get_text_width("FPS     "), 40),
870           LEFT_ALLIGN, LAYER_FOREGROUND1);
871       context.draw_text(gold_text, str,
872           Vector(screen->w-4*16, 40), LEFT_ALLIGN, LAYER_FOREGROUND1);
873     }
874 }
875
876 void
877 GameSession::drawresultscreen()
878 {
879   char str[80];
880
881   DrawingContext context;
882   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
883       i != currentsector->gameobjects.end(); ++i) {
884     Background* background = dynamic_cast<Background*> (*i);
885     if(background) {
886       background->draw(context);
887     }
888   }
889
890   context.draw_text(blue_text, _("Result:"), Vector(screen->w/2, 200),
891       CENTER_ALLIGN, LAYER_FOREGROUND1);
892
893   sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
894   context.draw_text(gold_text, str, Vector(screen->w/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
895
896   sprintf(str, _("COINS: %d"), player_status.distros);
897   context.draw_text(gold_text, str, Vector(screen->w/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
898
899   context.do_drawing();
900   
901   SDL_Event event;
902   wait_for_event(event,2000,5000,true);
903 }
904
905 std::string slotinfo(int slot)
906 {
907   std::string tmp;
908   std::string slotfile;
909   std::string title;
910   std::stringstream stream;
911   stream << slot;
912   slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
913
914   try {
915     lisp::Parser parser;
916     std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
917
918     const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
919     if(!savegame)
920       throw std::runtime_error("file is not a supertux-savegame.");
921
922     savegame->get("title", title);
923   } catch(std::exception& e) {
924     return std::string(_("Slot")) + " " + stream.str() + " - " +
925       std::string(_("Free"));
926   }
927
928   return std::string("Slot ") + stream.str() + " - " + title;
929 }
930
931 bool process_load_game_menu()
932 {
933   int slot = load_game_menu->check();
934
935   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
936     {
937       std::stringstream stream;
938       stream << slot;
939       std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
940
941       fadeout(256);
942       DrawingContext context;
943       context.draw_text(white_text, "Loading...",
944                         Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
945       context.do_drawing();
946
947       WorldMapNS::WorldMap worldmap;
948
949       worldmap.set_map_filename("/levels/world1/worldmap.stwm");
950       // Load the game or at least set the savegame_file variable
951       worldmap.loadgame(slotfile);
952
953       worldmap.display();
954
955       Menu::set_current(main_menu);
956
957       Ticks::pause_stop();
958       return true;
959     }
960   else
961     {
962       return false;
963     }
964 }