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