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