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