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