fix mkdir on win32
[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     skipdraw = false;
537
538     // regulate fps
539     ticks = SDL_GetTicks();
540     if(ticks > fps_nextframe_ticks) {
541       // don't draw all frames when we're getting too slow
542       skipdraw = true;
543     } else {
544       while(fps_nextframe_ticks > ticks) {
545         /* just wait */
546         // If we really have to wait long, then do an imprecise SDL_Delay()
547         Uint32 diff = fps_nextframe_ticks - ticks;
548         if(diff > 15) {
549           SDL_Delay(diff - 10);
550         } 
551         ticks = SDL_GetTicks();
552       }
553     }
554     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
555
556 #if 0
557     float diff = SDL_GetTicks() - fps_nextframe_ticks;
558     if (diff > 5.0) {
559          // sets the ticks that must have elapsed
560         fps_nextframe_ticks = SDL_GetTicks() + (1000.0 / LOGICAL_FPS);
561     } else {
562         // sets the ticks that must have elapsed
563         // in order for the next frame to start.
564         fps_nextframe_ticks += 1000.0 / LOGICAL_FPS;
565     }
566 #endif
567
568     process_events();
569     process_menu();
570
571     // Update the world state and all objects in the world
572     // Do that with a constante time-delta so that the game will run
573     // determistic and not different on different machines
574     if(!game_pause && !Menu::current())
575     {
576       // Update the world
577       check_end_conditions();
578       if (end_sequence == ENDSEQUENCE_RUNNING)
579         action(elapsed_time/2);
580       else if(end_sequence == NO_ENDSEQUENCE)
581         action(elapsed_time);
582     }
583     else
584     {
585       ++pause_menu_frame;
586     }
587
588     if(!skipdraw)
589       draw();
590
591     /* Time stops in pause mode */
592     if(game_pause || Menu::current())
593     {
594       continue;
595     }
596
597     //frame_rate.update();
598     
599     /* Handle time: */
600     if (time_left.check() && !end_sequence)
601       currentsector->player->kill(Player::KILL);
602     
603     /* Handle music: */
604     if (currentsector->player->invincible_timer.started() && !end_sequence)
605     {
606       currentsector->play_music(HERRING_MUSIC);
607     }
608     /* or just normal music? */
609     else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
610     {
611       currentsector->play_music(LEVEL_MUSIC);
612     }
613
614     /* Calculate frames per second */
615     if(config->show_fps)
616     {
617       ++fps_cnt;
618       
619       if(SDL_GetTicks() - fps_ticks >= 500)
620       {
621         fps_fps = (float) fps_cnt / .5;
622         fps_cnt = 0;
623         fps_ticks = SDL_GetTicks();
624       }
625     }
626   }
627  
628   // just in case
629   main_controller->reset();
630   return exit_status;
631 }
632
633 void
634 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
635 {
636   newsector = sector;
637   newspawnpoint = spawnpoint;
638 }
639
640 void
641 GameSession::set_reset_point(const std::string& sector, const Vector& pos)
642 {
643   reset_sector = sector;
644   reset_pos = pos;
645 }
646
647 void
648 GameSession::display_info_box(const std::string& text)
649 {
650   InfoBox* box = new InfoBox(text);
651
652   bool running = true;
653   while(running)  {
654
655     main_controller->update();
656     SDL_Event event;
657     while (SDL_PollEvent(&event)) {
658       main_controller->process_event(event);
659       if(event.type == SDL_QUIT)
660         throw std::runtime_error("Received window close event");
661     }
662
663     if(main_controller->pressed(Controller::JUMP)
664         || main_controller->pressed(Controller::ACTION)
665         || main_controller->pressed(Controller::PAUSE_MENU)
666         || main_controller->pressed(Controller::MENU_SELECT))
667       running = false;
668     box->draw(*context);
669     draw();
670   }
671
672   delete box;
673 }
674
675 void
676 GameSession::start_sequence(const std::string& sequencename)
677 {
678   if(sequencename == "endsequence" || sequencename == "fireworks") {
679     if(end_sequence)
680       return;
681     
682     end_sequence = ENDSEQUENCE_RUNNING;
683     endsequence_timer.start(7.0); // 7 seconds until we finish the map
684     last_x_pos = -1;
685     sound_manager->play_music(level_end_song, 0);
686     currentsector->player->invincible_timer.start(7.0);
687
688     // add left time to stats
689     global_stats.set_points(TIME_NEEDED_STAT,
690         int(time_left.get_period() - time_left.get_timeleft()));
691
692     if(sequencename == "fireworks") {
693       currentsector->add_object(new Fireworks());
694     }
695   } else if(sequencename == "stoptux") {
696     end_sequence =  ENDSEQUENCE_WAITING;
697   } else {
698     std::cout << "Unknown sequence '" << sequencename << "'.\n";
699   }
700 }
701
702 /* (Status): */
703 void
704 GameSession::drawstatus(DrawingContext& context)
705 {
706   char str[60];
707   
708   snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
709   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
710   context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
711
712   if(mode == ST_GL_TEST) {
713     context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
714                       LEFT_ALLIGN, LAYER_FOREGROUND1);
715   }
716
717   if(time_left.get_timeleft() < 0) {
718     context.draw_text(white_text, _("TIME's UP"), Vector(SCREEN_WIDTH/2, 0),
719         CENTER_ALLIGN, LAYER_FOREGROUND1);
720   } else if (time_left.get_timeleft() > TIME_WARNING
721       || int(global_time * 2.5) % 2) {
722     sprintf(str, " %d", int(time_left.get_timeleft()));
723     context.draw_text(white_text, _("TIME"),
724         Vector(SCREEN_WIDTH/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
725     context.draw_text(gold_text, str,
726         Vector(SCREEN_WIDTH/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
727   }
728
729   sprintf(str, " %d", player_status.coins);
730   context.draw_text(white_text, _("COINS"),
731       Vector(SCREEN_WIDTH - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
732         LEFT_ALLIGN, LAYER_FOREGROUND1);
733   context.draw_text(gold_text, str,
734       Vector(SCREEN_WIDTH - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
735
736   if (player_status.lives >= 5)
737     {
738       sprintf(str, "%dx", player_status.lives);
739       float x = SCREEN_WIDTH - gold_text->get_text_width(str) - tux_life->w;
740       context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
741       context.draw_surface(tux_life, Vector(SCREEN_WIDTH - 16, 20),
742           LAYER_FOREGROUND1);
743     }
744   else
745     {
746       for(int i= 0; i < player_status.lives; ++i)
747         context.draw_surface(tux_life, 
748             Vector(SCREEN_WIDTH - tux_life->w*4 +(tux_life->w*i), 20),
749             LAYER_FOREGROUND1);
750     }
751
752   context.draw_text(white_text, _("LIVES"),
753       Vector(SCREEN_WIDTH - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
754       LEFT_ALLIGN, LAYER_FOREGROUND1);
755
756   if(config->show_fps) {
757     sprintf(str, "%2.1f", fps_fps);
758     context.draw_text(white_text, "FPS", 
759                       Vector(SCREEN_WIDTH -
760                              white_text->get_text_width("FPS     "), 40),
761                       LEFT_ALLIGN, LAYER_FOREGROUND1);
762     context.draw_text(gold_text, str,
763                       Vector(SCREEN_WIDTH-4*16, 40),
764                       LEFT_ALLIGN, LAYER_FOREGROUND1);
765   }
766 }
767
768 void
769 GameSession::drawresultscreen()
770 {
771   char str[80];
772
773   DrawingContext context;
774   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
775       i != currentsector->gameobjects.end(); ++i) {
776     Background* background = dynamic_cast<Background*> (*i);
777     if(background) {
778       background->draw(context);
779     }
780   }
781
782   context.draw_text(blue_text, _("Result:"), Vector(SCREEN_WIDTH/2, 200),
783       CENTER_ALLIGN, LAYER_FOREGROUND1);
784
785   sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
786   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
787
788   sprintf(str, _("COINS: %d"), player_status.coins);
789   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
790
791   context.do_drawing();
792   
793   wait_for_event(2.0, 5.0);
794 }
795
796 std::string slotinfo(int slot)
797 {
798   std::string tmp;
799   std::string slotfile;
800   std::string title;
801   std::stringstream stream;
802   stream << slot;
803   slotfile = user_dir + "/save/slot" + stream.str() + ".stsg";
804
805   try {
806     lisp::Parser parser;
807     std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
808
809     const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
810     if(!savegame)
811       throw std::runtime_error("file is not a supertux-savegame.");
812
813     savegame->get("title", title);
814   } catch(std::exception& e) {
815     return std::string(_("Slot")) + " " + stream.str() + " - " +
816       std::string(_("Free"));
817   }
818
819   return std::string("Slot ") + stream.str() + " - " + title;
820 }
821
822 bool process_load_game_menu()
823 {
824   int slot = load_game_menu->check();
825
826   if(slot == -1)
827     return false;
828   
829   if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
830     return false;
831   
832   std::stringstream stream;
833   stream << slot;
834   std::string slotfile = user_dir + "/save/slot" + stream.str() + ".stsg";
835
836   fadeout(256);
837   DrawingContext context;
838   context.draw_text(white_text, "Loading...",
839                     Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2),
840                     CENTER_ALLIGN, LAYER_FOREGROUND1);
841   context.do_drawing();
842
843   WorldMapNS::WorldMap worldmap;
844
845   worldmap.set_map_filename("/levels/world1/worldmap.stwm");
846   // Load the game or at least set the savegame_file variable
847   worldmap.loadgame(slotfile);
848
849   worldmap.display();
850
851   Menu::set_current(main_menu);
852
853   return true;
854 }