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