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