- Change ScriptInterpreter to a gameobject, so we can now have several script
[supertux.git] / src / game_session.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 <fstream>
25 #include <sstream>
26 #include <cassert>
27 #include <cstdio>
28 #include <cstdlib>
29 #include <cmath>
30 #include <cstring>
31 #include <cerrno>
32 #include <unistd.h>
33 #include <ctime>
34 #include <stdexcept>
35
36 #include <SDL.h>
37
38 #ifndef WIN32
39 #include <sys/types.h>
40 #include <ctype.h>
41 #endif
42
43 #include "game_session.h"
44 #include "video/screen.h"
45 #include "gui/menu.h"
46 #include "sector.h"
47 #include "level.h"
48 #include "tile.h"
49 #include "player_status.h"
50 #include "object/particlesystem.h"
51 #include "object/background.h"
52 #include "object/tilemap.h"
53 #include "object/camera.h"
54 #include "object/player.h"
55 #include "lisp/lisp.h"
56 #include "lisp/parser.h"
57 #include "resources.h"
58 #include "worldmap.h"
59 #include "misc.h"
60 #include "statistics.h"
61 #include "timer.h"
62 #include "object/fireworks.h"
63 #include "textscroller.h"
64 #include "control/codecontroller.h"
65 #include "control/joystickkeyboardcontroller.h"
66 #include "main.h"
67 #include "gameconfig.h"
68 #include "gettext.h"
69
70 // the engine will be run with a lofical framerate of 64fps.
71 // We choose 64fps here because it is a power of 2, so 1/64 gives an "even"
72 // binary fraction...
73 static const float LOGICAL_FPS = 64.0;
74
75 GameSession* GameSession::current_ = 0;
76
77 GameSession::GameSession(const std::string& levelfile_, GameSessionMode mode,
78     Statistics* statistics)
79   : level(0), currentsector(0), mode(mode),
80     end_sequence(NO_ENDSEQUENCE), end_sequence_controller(0),
81     levelfile(levelfile_), best_level_statistics(statistics),
82     capture_demo_stream(0), playback_demo_stream(0), demo_controller(0)
83 {
84   current_ = this;
85   
86   game_pause = false;
87   fps_fps = 0;
88
89   context = new DrawingContext();
90
91   restart_level();
92 }
93
94 void
95 GameSession::restart_level()
96 {
97   game_pause   = false;
98   exit_status  = ES_NONE;
99   end_sequence = NO_ENDSEQUENCE;
100
101   main_controller->reset();
102
103   delete level;
104   currentsector = 0;
105
106   level = new Level;
107   level->load(levelfile);
108
109   global_stats.reset();
110   global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
111   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
112   global_stats.set_total_points(TIME_NEEDED_STAT, level->timelimit);
113
114   if(reset_sector != "") {
115     currentsector = level->get_sector(reset_sector);
116     if(!currentsector) {
117       std::stringstream msg;
118       msg << "Couldn't find sector '" << reset_sector << "' for resetting tux.";
119       throw std::runtime_error(msg.str());
120     }
121     currentsector->activate(reset_pos);
122   } else {
123     currentsector = level->get_sector("main");
124     if(!currentsector)
125       throw std::runtime_error("Couldn't find main sector");
126     currentsector->activate("main");
127   }
128   
129   if(mode == ST_GL_PLAY || mode == ST_GL_LOAD_LEVEL_FILE)
130     levelintro();
131
132   start_timers();
133   currentsector->play_music(LEVEL_MUSIC);
134
135   if(capture_file != "")
136     record_demo(capture_file);
137 }
138
139 GameSession::~GameSession()
140 {
141   delete capture_demo_stream;
142   delete playback_demo_stream;
143   delete demo_controller;
144
145   delete end_sequence_controller;
146   delete level;
147   delete context;
148 }
149
150 void
151 GameSession::record_demo(const std::string& filename)
152 {
153   delete capture_demo_stream;
154   
155   capture_demo_stream = new std::ofstream(filename.c_str()); 
156   if(!capture_demo_stream->good()) {
157     std::stringstream msg;
158     msg << "Couldn't open demo file '" << filename << "' for writing.";
159     throw std::runtime_error(msg.str());
160   }
161   capture_file = filename;
162 }
163
164 void
165 GameSession::play_demo(const std::string& filename)
166 {
167   delete playback_demo_stream;
168   delete demo_controller;
169   
170   playback_demo_stream = new std::ifstream(filename.c_str());
171   if(!playback_demo_stream->good()) {
172     std::stringstream msg;
173     msg << "Couldn't open demo file '" << filename << "' for reading.";
174     throw std::runtime_error(msg.str());
175   }
176
177   Player& tux = *currentsector->player;
178   demo_controller = new CodeController();
179   tux.set_controller(demo_controller);
180 }
181
182 void
183 GameSession::levelintro()
184 {
185   sound_manager->halt_music();
186   
187   char str[60];
188
189   DrawingContext context;
190   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
191       i != currentsector->gameobjects.end(); ++i) {
192     Background* background = dynamic_cast<Background*> (*i);
193     if(background) {
194       background->draw(context);
195     }
196   }
197
198 //  context.draw_text(gold_text, level->get_name(), Vector(SCREEN_WIDTH/2, 160),
199 //      CENTER_ALLIGN, LAYER_FOREGROUND1);
200   context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
201       LAYER_FOREGROUND1);
202
203   sprintf(str, "TUX x %d", player_status.lives);
204   context.draw_text(white_text, str, Vector(SCREEN_WIDTH/2, 210),
205       CENTER_ALLIGN, LAYER_FOREGROUND1);
206
207   if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
208     //TODO make author check case/blank-insensitive
209     context.draw_text(white_small_text,
210       std::string(_("contributed by ")) + level->get_author(), 
211       Vector(SCREEN_WIDTH/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
212
213
214   if(best_level_statistics != NULL)
215     best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
216
217   context.do_drawing();
218
219   wait_for_event(1.0, 3.0);
220 }
221
222 /* Reset Timers */
223 void
224 GameSession::start_timers()
225 {
226   time_left.start(level->timelimit);
227 }
228
229 void
230 GameSession::on_escape_press()
231 {
232   if(currentsector->player->is_dying() || end_sequence != NO_ENDSEQUENCE)
233     return;   // don't let the player open the menu, when he is dying
234   
235   if(mode == ST_GL_TEST) {
236     exit_status = ES_LEVEL_ABORT;
237   } else if (!Menu::current()) {
238     Menu::set_current(game_menu);
239     game_menu->set_active_item(MNID_CONTINUE);
240     game_pause = true;
241   } else {
242     game_pause = false;
243   }
244 }
245
246 void
247 GameSession::process_events()
248 {
249   Player& tux = *currentsector->player;
250   main_controller->update();
251
252   // end of pause mode?
253   if(!Menu::current() && game_pause) {
254     game_pause = false;
255   }
256
257   if (end_sequence != NO_ENDSEQUENCE) {
258     if(end_sequence_controller == 0) {
259       end_sequence_controller = new CodeController();
260       tux.set_controller(end_sequence_controller);
261     }
262
263     end_sequence_controller->press(Controller::RIGHT);
264     
265     if (int(last_x_pos) == int(tux.get_pos().x))
266       end_sequence_controller->press(Controller::JUMP);    
267     last_x_pos = tux.get_pos().x;
268   }
269
270   main_controller->update();
271   SDL_Event event;
272   while (SDL_PollEvent(&event)) {
273     /* Check for menu-events, if the menu is shown */
274     if (Menu::current())
275       Menu::current()->event(event);
276     main_controller->process_event(event);
277     if(event.type == SDL_QUIT)
278       throw std::runtime_error("Received window close");  
279   }
280
281   // playback a demo?
282   if(playback_demo_stream != 0) {
283     demo_controller->update();
284     char left = false;
285     char right = false;
286     char up = false;
287     char down = false;
288     char jump = false;
289     char action = false;
290     playback_demo_stream->get(left);
291     playback_demo_stream->get(right);
292     playback_demo_stream->get(up);
293     playback_demo_stream->get(down);
294     playback_demo_stream->get(jump);
295     playback_demo_stream->get(action);
296     demo_controller->press(Controller::LEFT, left);
297     demo_controller->press(Controller::RIGHT, right);
298     demo_controller->press(Controller::UP, up);
299     demo_controller->press(Controller::DOWN, down);
300     demo_controller->press(Controller::JUMP, jump);
301     demo_controller->press(Controller::ACTION, action);
302   }
303
304   // save input for demo?
305   if(capture_demo_stream != 0) {
306     capture_demo_stream ->put(main_controller->hold(Controller::LEFT));
307     capture_demo_stream ->put(main_controller->hold(Controller::RIGHT));
308     capture_demo_stream ->put(main_controller->hold(Controller::UP));
309     capture_demo_stream ->put(main_controller->hold(Controller::DOWN));
310     capture_demo_stream ->put(main_controller->hold(Controller::JUMP));   
311     capture_demo_stream ->put(main_controller->hold(Controller::ACTION));
312   }
313 }
314
315 void
316 GameSession::try_cheats()
317 {
318   Player& tux = *currentsector->player;
319   
320   // Cheating words (the goal of this is really for debugging,
321   // but could be used for some cheating, nothing wrong with that)
322   if(main_controller->check_cheatcode("grow")) {
323     tux.set_bonus(GROWUP_BONUS, false);
324   }
325   if(main_controller->check_cheatcode("fire")) {
326     tux.set_bonus(FIRE_BONUS, false);
327   }
328   if(main_controller->check_cheatcode("ice")) {
329     tux.set_bonus(ICE_BONUS, false);
330   }
331   if(main_controller->check_cheatcode("lifeup")) {
332     player_status.lives++;
333   }
334   if(main_controller->check_cheatcode("lifedown")) {
335     player_status.lives--;
336   }
337   if(main_controller->check_cheatcode("grease")) {
338     tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
339   }
340   if(main_controller->check_cheatcode("invincible")) {
341     // be invincle for the rest of the level
342     tux.invincible_timer.start(10000);
343   }
344   if(main_controller->check_cheatcode("shrink")) {
345     // remove powerups
346     tux.kill(tux.SHRINK);
347   }
348   if(main_controller->check_cheatcode("kill")) {
349     // kill Tux, but without losing a life
350     player_status.lives++;
351     tux.kill(tux.KILL);
352   }
353 #if 0
354   if(main_controller->check_cheatcode("grid")) {
355     // toggle debug grid
356     debug_grid = !debug_grid;
357   }
358 #endif
359   if(main_controller->check_cheatcode("hover")) {
360     // toggle hover ability on/off
361     tux.enable_hover = !tux.enable_hover;
362   }
363   if(main_controller->check_cheatcode("gotoend")) {
364     // goes to the end of the level
365     tux.move(Vector(
366           (currentsector->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
367     currentsector->camera->reset(
368         Vector(tux.get_pos().x, tux.get_pos().y));
369   }
370   if(main_controller->check_cheatcode("finish")) {
371     // finish current sector
372     exit_status = ES_LEVEL_FINISHED;
373     // don't add points to stats though...
374   }
375   // temporary to help player's choosing a flapping
376   if(main_controller->check_cheatcode("marek")) {
377     tux.flapping_mode = Player::MAREK_FLAP;
378   }
379   if(main_controller->check_cheatcode("ricardo")) {
380     tux.flapping_mode = Player::RICARDO_FLAP;
381   }
382   if(main_controller->check_cheatcode("ryan")) {
383     tux.flapping_mode = Player::RYAN_FLAP;
384   }
385 }
386
387 void
388 GameSession::check_end_conditions()
389 {
390   Player* tux = currentsector->player;
391
392   /* End of level? */
393   if(end_sequence && endsequence_timer.check()) {
394     exit_status = ES_LEVEL_FINISHED;
395     return;
396   } else if (!end_sequence && tux->is_dead()) {
397     if (player_status.lives < 0) { // No more lives!?
398       exit_status = ES_GAME_OVER;
399     } else { // Still has lives, so reset Tux to the levelstart
400       restart_level();
401     }
402     
403     return;
404   }
405 }
406
407 void
408 GameSession::action(float elapsed_time)
409 {
410   // handle controller
411   if(main_controller->pressed(Controller::PAUSE_MENU))
412     on_escape_press();
413   
414   // advance timers
415   if(!currentsector->player->growing_timer.started()) {
416     // Update Tux and the World
417     currentsector->action(elapsed_time);
418   }
419
420   // respawning in new sector?
421   if(newsector != "" && newspawnpoint != "") {
422     Sector* sector = level->get_sector(newsector);
423     if(sector == 0) {
424       std::cerr << "Sector '" << newsector << "' not found.\n";
425     }
426     sector->activate(newspawnpoint);
427     sector->play_music(LEVEL_MUSIC);
428     currentsector = sector;
429     newsector = "";
430     newspawnpoint = "";
431   }
432 }
433
434 void 
435 GameSession::draw()
436 {
437   currentsector->draw(*context);
438   drawstatus(*context);
439
440   if(game_pause)
441     draw_pause();
442
443   if(Menu::current()) {
444     Menu::current()->draw(*context);
445     mouse_cursor->draw(*context);
446   }
447
448   context->do_drawing();
449 }
450
451 void
452 GameSession::draw_pause()
453 {
454   int x = SCREEN_HEIGHT / 20;
455   for(int i = 0; i < x; ++i) {
456     context->draw_filled_rect(
457         Vector(i % 2 ? (pause_menu_frame * i)%SCREEN_WIDTH :
458           -((pause_menu_frame * i)%SCREEN_WIDTH)
459           ,(i*20+pause_menu_frame)%SCREEN_HEIGHT),
460         Vector(SCREEN_WIDTH,10),
461         Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
462   }
463
464   context->draw_filled_rect(
465       Vector(0,0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
466       Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
467 #if 0
468   context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
469       Vector(SCREEN_WIDTH/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
470
471   const char* str1 = _("Playing: ");
472   const char* str2 = level->get_name().c_str();
473
474   context->draw_text(blue_text, str1,
475       Vector((SCREEN_WIDTH - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
476       LEFT_ALLIGN, LAYER_FOREGROUND1+2);
477   context->draw_text(white_text, str2,
478       Vector(((SCREEN_WIDTH - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
479       LEFT_ALLIGN, LAYER_FOREGROUND1+2);
480 #endif
481 }
482   
483 void
484 GameSession::process_menu()
485 {
486   Menu* menu = Menu::current();
487   if(menu) {
488     menu->action();
489
490     if(menu == game_menu) {
491       switch (game_menu->check()) {
492         case MNID_CONTINUE:
493           Menu::set_current(0);
494           break;
495         case MNID_ABORTLEVEL:
496           Menu::set_current(0);
497           exit_status = ES_LEVEL_ABORT;
498           break;
499       }
500     } else if(menu == options_menu) {
501       process_options_menu();
502     } else if(menu == load_game_menu ) {
503       process_load_game_menu();
504     }
505   }
506 }
507
508
509 GameSession::ExitStatus
510 GameSession::run()
511 {
512   Menu::set_current(0);
513   current_ = this;
514   
515   int fps_cnt = 0;
516
517   // Eat unneeded events
518   SDL_Event event;
519   while(SDL_PollEvent(&event))
520   {}
521
522   draw();
523
524   Uint32 fps_ticks = SDL_GetTicks();
525   Uint32 fps_nextframe_ticks = SDL_GetTicks();
526   Uint32 ticks;
527   bool skipdraw = false;
528
529   while (exit_status == ES_NONE) {
530     // we run in a logical framerate so elapsed time is a constant
531     static const float elapsed_time = 1.0 / LOGICAL_FPS;
532     // old code... float elapsed_time = float(ticks - lastticks) / 1000.;
533     if(!game_pause)
534       global_time += elapsed_time;
535
536     // regulate fps
537     ticks = SDL_GetTicks();
538     if(ticks > fps_nextframe_ticks) {
539       if(skipdraw == true) {
540         // already skipped last frame? we have to slow down the game then...
541         skipdraw = false;
542         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
543       } else {
544         // don't draw all frames when we're getting too slow
545         skipdraw = true;
546       }
547     } else {
548       skipdraw = false;
549       while(fps_nextframe_ticks > ticks) {
550         /* just wait */
551         // If we really have to wait long, then do an imprecise SDL_Delay()
552         Uint32 diff = fps_nextframe_ticks - ticks;
553         if(diff > 15) {
554           SDL_Delay(diff - 10);
555         } 
556         ticks = SDL_GetTicks();
557       }
558     }
559     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
560
561 #if 0
562     float diff = SDL_GetTicks() - fps_nextframe_ticks;
563     if (diff > 5.0) {
564          // sets the ticks that must have elapsed
565         fps_nextframe_ticks = SDL_GetTicks() + (1000.0 / LOGICAL_FPS);
566     } else {
567         // sets the ticks that must have elapsed
568         // in order for the next frame to start.
569         fps_nextframe_ticks += 1000.0 / LOGICAL_FPS;
570     }
571 #endif
572
573     process_events();
574     process_menu();
575
576     // Update the world state and all objects in the world
577     // Do that with a constante time-delta so that the game will run
578     // determistic and not different on different machines
579     if(!game_pause && !Menu::current())
580     {
581       // Update the world
582       check_end_conditions();
583       if (end_sequence == ENDSEQUENCE_RUNNING)
584         action(elapsed_time/2);
585       else if(end_sequence == NO_ENDSEQUENCE)
586         action(elapsed_time);
587     }
588     else
589     {
590       ++pause_menu_frame;
591     }
592
593     if(!skipdraw)
594       draw();
595
596     /* Time stops in pause mode */
597     if(game_pause || Menu::current())
598     {
599       continue;
600     }
601
602     //frame_rate.update();
603     
604     /* Handle time: */
605     if (time_left.check() && !end_sequence)
606       currentsector->player->kill(Player::KILL);
607     
608     /* Handle music: */
609     if (currentsector->player->invincible_timer.started() && !end_sequence)
610     {
611       currentsector->play_music(HERRING_MUSIC);
612     }
613     /* or just normal music? */
614     else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
615     {
616       currentsector->play_music(LEVEL_MUSIC);
617     }
618
619     /* Calculate frames per second */
620     if(config->show_fps)
621     {
622       ++fps_cnt;
623       
624       if(SDL_GetTicks() - fps_ticks >= 500)
625       {
626         fps_fps = (float) fps_cnt / .5;
627         fps_cnt = 0;
628         fps_ticks = SDL_GetTicks();
629       }
630     }
631   }
632  
633   // just in case
634   main_controller->reset();
635   return exit_status;
636 }
637
638 void
639 GameSession::finish()
640 {
641   exit_status = ES_LEVEL_FINISHED;
642 }
643
644 void
645 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
646 {
647   newsector = sector;
648   newspawnpoint = spawnpoint;
649 }
650
651 void
652 GameSession::set_reset_point(const std::string& sector, const Vector& pos)
653 {
654   reset_sector = sector;
655   reset_pos = pos;
656 }
657
658 void
659 GameSession::display_info_box(const std::string& text)
660 {
661   InfoBox* box = new InfoBox(text);
662
663   bool running = true;
664   while(running)  {
665
666     main_controller->update();
667     SDL_Event event;
668     while (SDL_PollEvent(&event)) {
669       main_controller->process_event(event);
670       if(event.type == SDL_QUIT)
671         throw std::runtime_error("Received window close event");
672     }
673
674     if(main_controller->pressed(Controller::JUMP)
675         || main_controller->pressed(Controller::ACTION)
676         || main_controller->pressed(Controller::PAUSE_MENU)
677         || main_controller->pressed(Controller::MENU_SELECT))
678       running = false;
679     box->draw(*context);
680     draw();
681   }
682
683   delete box;
684 }
685
686 void
687 GameSession::start_sequence(const std::string& sequencename)
688 {
689   if(sequencename == "endsequence" || sequencename == "fireworks") {
690     if(end_sequence)
691       return;
692     
693     end_sequence = ENDSEQUENCE_RUNNING;
694     endsequence_timer.start(7.0); // 7 seconds until we finish the map
695     last_x_pos = -1;
696     sound_manager->play_music(level_end_song, 0);
697     currentsector->player->invincible_timer.start(7.0);
698
699     // add left time to stats
700     global_stats.set_points(TIME_NEEDED_STAT,
701         int(time_left.get_period() - time_left.get_timeleft()));
702
703     if(sequencename == "fireworks") {
704       currentsector->add_object(new Fireworks());
705     }
706   } else if(sequencename == "stoptux") {
707     end_sequence =  ENDSEQUENCE_WAITING;
708   } else {
709     std::cout << "Unknown sequence '" << sequencename << "'.\n";
710   }
711 }
712
713 /* (Status): */
714 void
715 GameSession::drawstatus(DrawingContext& context)
716 {
717   char str[60];
718   
719   snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
720   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
721   context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
722
723   if(mode == ST_GL_TEST) {
724     context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
725                       LEFT_ALLIGN, LAYER_FOREGROUND1);
726   }
727
728   if(time_left.get_timeleft() < 0) {
729     context.draw_text(white_text, _("TIME's UP"), Vector(SCREEN_WIDTH/2, 0),
730         CENTER_ALLIGN, LAYER_FOREGROUND1);
731   } else if (time_left.get_timeleft() > TIME_WARNING
732       || int(global_time * 2.5) % 2) {
733     sprintf(str, " %d", int(time_left.get_timeleft()));
734     context.draw_text(white_text, _("TIME"),
735         Vector(SCREEN_WIDTH/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
736     context.draw_text(gold_text, str,
737         Vector(SCREEN_WIDTH/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
738   }
739
740   sprintf(str, " %d", player_status.coins);
741   context.draw_text(white_text, _("COINS"),
742       Vector(SCREEN_WIDTH - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
743         LEFT_ALLIGN, LAYER_FOREGROUND1);
744   context.draw_text(gold_text, str,
745       Vector(SCREEN_WIDTH - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
746
747   if (player_status.lives >= 5)
748     {
749       sprintf(str, "%dx", player_status.lives);
750       float x = SCREEN_WIDTH - gold_text->get_text_width(str) - tux_life->w;
751       context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
752       context.draw_surface(tux_life, Vector(SCREEN_WIDTH - 16, 20),
753           LAYER_FOREGROUND1);
754     }
755   else
756     {
757       for(int i= 0; i < player_status.lives; ++i)
758         context.draw_surface(tux_life, 
759             Vector(SCREEN_WIDTH - tux_life->w*4 +(tux_life->w*i), 20),
760             LAYER_FOREGROUND1);
761     }
762
763   context.draw_text(white_text, _("LIVES"),
764       Vector(SCREEN_WIDTH - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
765       LEFT_ALLIGN, LAYER_FOREGROUND1);
766
767   if(config->show_fps) {
768     sprintf(str, "%2.1f", fps_fps);
769     context.draw_text(white_text, "FPS", 
770                       Vector(SCREEN_WIDTH -
771                              white_text->get_text_width("FPS     "), 40),
772                       LEFT_ALLIGN, LAYER_FOREGROUND1);
773     context.draw_text(gold_text, str,
774                       Vector(SCREEN_WIDTH-4*16, 40),
775                       LEFT_ALLIGN, LAYER_FOREGROUND1);
776   }
777 }
778
779 void
780 GameSession::drawresultscreen()
781 {
782   char str[80];
783
784   DrawingContext context;
785   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
786       i != currentsector->gameobjects.end(); ++i) {
787     Background* background = dynamic_cast<Background*> (*i);
788     if(background) {
789       background->draw(context);
790     }
791   }
792
793   context.draw_text(blue_text, _("Result:"), Vector(SCREEN_WIDTH/2, 200),
794       CENTER_ALLIGN, LAYER_FOREGROUND1);
795
796   sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
797   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
798
799   sprintf(str, _("COINS: %d"), player_status.coins);
800   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
801
802   context.do_drawing();
803   
804   wait_for_event(2.0, 5.0);
805 }
806
807 std::string slotinfo(int slot)
808 {
809   std::string tmp;
810   std::string slotfile;
811   std::string title;
812   std::stringstream stream;
813   stream << slot;
814   slotfile = user_dir + "/save/slot" + stream.str() + ".stsg";
815
816   try {
817     lisp::Parser parser;
818     std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
819
820     const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
821     if(!savegame)
822       throw std::runtime_error("file is not a supertux-savegame.");
823
824     savegame->get("title", title);
825   } catch(std::exception& e) {
826     return std::string(_("Slot")) + " " + stream.str() + " - " +
827       std::string(_("Free"));
828   }
829
830   return std::string("Slot ") + stream.str() + " - " + title;
831 }
832
833 bool process_load_game_menu()
834 {
835   int slot = load_game_menu->check();
836
837   if(slot == -1)
838     return false;
839   
840   if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
841     return false;
842   
843   std::stringstream stream;
844   stream << slot;
845   std::string slotfile = user_dir + "/save/slot" + stream.str() + ".stsg";
846
847   fadeout(256);
848   DrawingContext context;
849   context.draw_text(white_text, "Loading...",
850                     Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2),
851                     CENTER_ALLIGN, LAYER_FOREGROUND1);
852   context.do_drawing();
853
854   WorldMapNS::WorldMap worldmap;
855
856   worldmap.set_map_filename("/levels/world1/worldmap.stwm");
857   // Load the game or at least set the savegame_file variable
858   worldmap.loadgame(slotfile);
859
860   worldmap.display();
861
862   Menu::set_current(main_menu);
863
864   return true;
865 }