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