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