TODO update, fix tux doesn't stop at igloo anymore
[supertux.git] / src / gameloop.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 <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 #ifndef WIN32
38 #include <sys/types.h>
39 #include <ctype.h>
40 #endif
41
42 #include "app/globals.h"
43 #include "gameloop.h"
44 #include "video/screen.h"
45 #include "app/setup.h"
46 #include "gui/menu.h"
47 #include "sector.h"
48 #include "level.h"
49 #include "tile.h"
50 #include "player_status.h"
51 #include "object/particlesystem.h"
52 #include "object/background.h"
53 #include "object/tilemap.h"
54 #include "object/camera.h"
55 #include "object/player.h"
56 #include "lisp/lisp.h"
57 #include "lisp/parser.h"
58 #include "resources.h"
59 #include "app/gettext.h"
60 #include "worldmap.h"
61 #include "misc.h"
62 #include "statistics.h"
63 #include "timer.h"
64 #include "object/fireworks.h"
65
66 GameSession* GameSession::current_ = 0;
67
68 bool compare_last(std::string& haystack, std::string needle)
69 {
70   int haystack_size = haystack.size();
71   int needle_size = needle.size();
72
73   if(haystack_size < needle_size)
74     return false;
75
76   if(haystack.compare(haystack_size-needle_size, needle_size, needle) == 0)
77     return true;
78   return false;
79 }
80
81 GameSession::GameSession(const std::string& levelfile_, int mode,
82     Statistics* statistics)
83   : level(0), currentsector(0), st_gl_mode(mode),
84     end_sequence(NO_ENDSEQUENCE), levelfile(levelfile_),
85     best_level_statistics(statistics)
86 {
87   current_ = this;
88   
89   game_pause = false;
90   fps_fps = 0;
91
92   context = new DrawingContext();
93
94   last_swap_point = Vector(-1, -1);
95   last_swap_stats.reset();
96
97   restart_level();
98 }
99
100 void
101 GameSession::restart_level()
102 {
103   game_pause   = false;
104   exit_status  = ES_NONE;
105   end_sequence = NO_ENDSEQUENCE;
106
107   last_keys.clear();
108
109 #if 0
110   Vector tux_pos = Vector(-1,-1);
111   if (currentsector)
112     { 
113       // Tux has lost a life, so we try to respawn him at the nearest reset point
114       tux_pos = currentsector->player->base;
115     }
116 #endif
117   
118   delete level;
119   currentsector = 0;
120
121   level = new Level;
122   level->load(levelfile);
123
124   global_stats.reset();
125   global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
126   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
127   global_stats.set_total_points(TIME_NEEDED_STAT, level->timelimit);
128
129   currentsector = level->get_sector("main");
130   if(!currentsector)
131     Termination::abort("Level has no main sector.", "");
132   currentsector->activate("main");
133
134 #if 0
135   // Set Tux to the nearest reset point
136   if(tux_pos.x != -1)
137     {
138     tux_pos = currentsector->get_best_spawn_point(tux_pos);
139
140     if(last_swap_point.x > tux_pos.x)
141       tux_pos = last_swap_point;
142     else  // new swap point
143       {
144       last_swap_point = tux_pos;
145
146       last_swap_stats += global_stats;
147       }
148
149     currentsector->player->base.x = tux_pos.x;
150     currentsector->player->base.y = tux_pos.y;
151
152     // has to reset camera on swapping
153     currentsector->camera->reset(Vector(currentsector->player->base.x,
154                                         currentsector->player->base.y));
155     }
156 #endif
157
158   if (st_gl_mode != ST_GL_DEMO_GAME)
159     {
160       if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
161         levelintro();
162     }
163
164   start_timers();
165   currentsector->play_music(LEVEL_MUSIC);
166 }
167
168 GameSession::~GameSession()
169 {
170   delete level;
171   delete context;
172 }
173
174 void
175 GameSession::levelintro(void)
176 {
177   SoundManager::get()->halt_music();
178   
179   char str[60];
180
181   DrawingContext context;
182   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
183       i != currentsector->gameobjects.end(); ++i) {
184     Background* background = dynamic_cast<Background*> (*i);
185     if(background) {
186       background->draw(context);
187     }
188   }
189
190 //  context.draw_text(gold_text, level->get_name(), Vector(screen->w/2, 160),
191 //      CENTER_ALLIGN, LAYER_FOREGROUND1);
192   context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
193       LAYER_FOREGROUND1);
194
195   sprintf(str, "TUX x %d", player_status.lives);
196   context.draw_text(white_text, str, Vector(screen->w/2, 210),
197       CENTER_ALLIGN, LAYER_FOREGROUND1);
198
199   if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
200     //TODO make author check case/blank-insensitive
201     context.draw_text(white_small_text,
202       std::string(_("contributed by ")) + level->get_author(), 
203       Vector(screen->w/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
204
205
206   if(best_level_statistics != NULL)
207     best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
208
209   context.do_drawing();
210
211   SDL_Event event;
212   wait_for_event(event,1000,3000,true);
213 }
214
215 /* Reset Timers */
216 void
217 GameSession::start_timers()
218 {
219   time_left.start(level->timelimit);
220   Ticks::pause_init();
221 }
222
223 void
224 GameSession::on_escape_press()
225 {
226   if(currentsector->player->is_dying() || end_sequence != NO_ENDSEQUENCE)
227     return;   // don't let the player open the menu, when he is dying
228   
229   if(game_pause)
230     return;
231
232   if(st_gl_mode == ST_GL_TEST) {
233     exit_status = ES_LEVEL_ABORT;
234   } else if (!Menu::current()) {
235     Menu::set_current(game_menu);
236     Ticks::pause_start();
237   }
238 }
239
240 void
241 GameSession::process_events()
242 {
243   if (end_sequence != NO_ENDSEQUENCE)
244     {
245       Player& tux = *currentsector->player;
246          
247       tux.input.fire  = false;
248       tux.input.left  = false;
249       tux.input.right = true;
250       tux.input.down  = false; 
251
252       if (int(last_x_pos) == int(tux.get_pos().x))
253         tux.input.up    = true; 
254       else
255         tux.input.up    = false;
256
257       last_x_pos = tux.get_pos().x;
258
259       SDL_Event event;
260       while (SDL_PollEvent(&event))
261         {
262           /* Check for menu-events, if the menu is shown */
263           if (Menu::current())
264             {
265               Menu::current()->event(event);
266               if(!Menu::current())
267               Ticks::pause_stop();
268             }
269
270           switch(event.type)
271             {
272             case SDL_QUIT:        /* Quit event - quit: */
273               Termination::abort("Received window close", "");
274               break;
275               
276             case SDL_KEYDOWN:     /* A keypress! */
277               {
278                 SDLKey key = event.key.keysym.sym;
279            
280                 switch(key)
281                   {
282                   case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
283                     on_escape_press();
284                     break;
285                   default:
286                     break;
287                   }
288               }
289           
290             case SDL_JOYBUTTONDOWN:
291               if (event.jbutton.button == joystick_keymap.start_button)
292                 on_escape_press();
293               break;
294             }
295         }
296     }
297   else // normal mode
298     {
299       if(!Menu::current() && !game_pause)
300         Ticks::pause_stop();
301
302       SDL_Event event;
303       while (SDL_PollEvent(&event))
304         {
305           /* Check for menu-events, if the menu is shown */
306           if (Menu::current())
307             {
308               Menu::current()->event(event);
309               if(!Menu::current())
310                 Ticks::pause_stop();
311             }
312           else
313             {
314               Player& tux = *currentsector->player;
315   
316               switch(event.type)
317                 {
318                 case SDL_QUIT:        /* Quit event - quit: */
319                   Termination::abort("Received window close", "");
320                   break;
321
322                 case SDL_KEYDOWN:     /* A keypress! */
323                   {
324                     SDLKey key = event.key.keysym.sym;
325             
326                     if(tux.key_event(key, true))
327                       break;
328
329                     switch(key)
330                       {
331                       case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
332                         on_escape_press();
333                         break;
334                       default:
335                         break;
336                       }
337                   }
338                   break;
339                 case SDL_KEYUP:      /* A keyrelease! */
340                   {
341                     SDLKey key = event.key.keysym.sym;
342
343                     if(tux.key_event(key, false))
344                       break;
345
346                     switch(key)
347                       {
348                       case SDLK_a:
349                         if(debug_mode)
350                         {
351                           char buf[160];
352                           snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
353                               tux.get_pos().x, tux.get_pos().y);
354                           context->draw_text(white_text, buf,
355                               Vector(0, screen->h - white_text->get_height()),
356                               LEFT_ALLIGN, LAYER_FOREGROUND1);
357                           context->do_drawing();
358                           SDL_Delay(1000);
359                         }
360                         break;
361                       case SDLK_p:
362                         if(!Menu::current())
363                           {
364                           // "lifeup" cheat activates pause cause of the 'p'
365                           // so work around to ignore it
366                             if(compare_last(last_keys, "lifeu"))
367                               break;
368
369                             if(game_pause)
370                               {
371                                 game_pause = false;
372                                 Ticks::pause_stop();
373                               }
374                             else
375                               {
376                                 game_pause = true;
377                                 Ticks::pause_start();
378                               }
379                           }
380                         break;
381                       default:
382                         break;
383                       }
384                   }
385
386                         /* Check if chacrater is ASCII */
387                         char ch[2];
388                         if((event.key.keysym.unicode & 0xFF80) == 0)
389                           {
390                           ch[0] = event.key.keysym.unicode & 0x7F;
391                           ch[1] = '\0';
392                           }
393                         last_keys.append(ch);  // add to cheat keys
394                         handle_cheats();
395                   break;
396
397                 case SDL_JOYAXISMOTION:
398                   if (event.jaxis.axis == joystick_keymap.x_axis)
399                     {
400                       if (event.jaxis.value < -joystick_keymap.dead_zone)
401                         {
402                           tux.input.left  = true;
403                           tux.input.right = false;
404                         }
405                       else if (event.jaxis.value > joystick_keymap.dead_zone)
406                         {
407                           tux.input.left  = false;
408                           tux.input.right = true;
409                         }
410                       else
411                         {
412                           tux.input.left  = false;
413                           tux.input.right = false;
414                         }
415                     }
416                   else if (event.jaxis.axis == joystick_keymap.y_axis)
417                     {
418                       if (event.jaxis.value > joystick_keymap.dead_zone)
419                         {
420                         tux.input.up = true;
421                         tux.input.down = false;
422                         }
423                       else if (event.jaxis.value < -joystick_keymap.dead_zone)
424                         {
425                         tux.input.up = false;
426                         tux.input.down = true;
427                         }
428                       else
429                         {
430                         tux.input.up = false;
431                         tux.input.down = false;
432                         }
433                     }
434                   break;
435
436                 case SDL_JOYHATMOTION:
437                   if(event.jhat.value & SDL_HAT_UP) {
438                     tux.input.up = true;
439                     tux.input.down = false;
440                   } else if(event.jhat.value & SDL_HAT_DOWN) {
441                     tux.input.up = false;
442                     tux.input.down = true;
443                   } else if(event.jhat.value & SDL_HAT_LEFT) {
444                     tux.input.left = true;
445                     tux.input.right = false;
446                   } else if(event.jhat.value & SDL_HAT_RIGHT) {
447                     tux.input.left = false;
448                     tux.input.right = true;
449                   } else if(event.jhat.value == SDL_HAT_CENTERED) {
450                     tux.input.left = false;
451                     tux.input.right = false;
452                     tux.input.up = false;
453                     tux.input.down = false;
454                   }
455                   break;
456             
457                 case SDL_JOYBUTTONDOWN:
458                   // FIXME: I assume we have to set old_jump and stuff here?!?
459                   if (event.jbutton.button == joystick_keymap.a_button)
460                     tux.input.jump = true;
461                   else if (event.jbutton.button == joystick_keymap.b_button)
462                     tux.input.fire = true;
463                   else if (event.jbutton.button == joystick_keymap.start_button)
464                     on_escape_press();
465                   break;
466                 case SDL_JOYBUTTONUP:
467                   if (event.jbutton.button == joystick_keymap.a_button)
468                     tux.input.jump = false;
469                   else if (event.jbutton.button == joystick_keymap.b_button)
470                     tux.input.fire = false;
471                   break;
472
473                 default:
474                   break;
475                 }  /* switch */
476             }
477         } /* while */
478     }
479 }
480
481 void
482 GameSession::handle_cheats()
483 {
484   Player& tux = *currentsector->player;
485   
486   // Cheating words (the goal of this is really for debugging,
487   // but could be used for some cheating, nothing wrong with that)
488   if(compare_last(last_keys, "grow")) {
489     tux.grow(false);
490     last_keys.clear();
491   }
492   if(compare_last(last_keys, "fire")) {
493     tux.grow(false);
494     tux.got_power = tux.FIRE_POWER;
495     last_keys.clear();
496   }
497   if(compare_last(last_keys, "ice")) {
498     tux.grow(false);
499     tux.got_power = tux.ICE_POWER;
500     last_keys.clear();
501   }
502   if(compare_last(last_keys, "lifeup")) {
503     player_status.lives++;
504     last_keys.clear();
505   }
506   if(compare_last(last_keys, "lifedown")) {
507     player_status.lives--;
508     last_keys.clear();
509   }
510   if(compare_last(last_keys, "grease")) {
511     tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
512     last_keys.clear();
513   }
514   if(compare_last(last_keys, "invincible")) {
515     // be invincle for the rest of the level
516     tux.invincible_timer.start(10000);
517     last_keys.clear();
518   }
519   if(compare_last(last_keys, "shrink")) {
520     // remove powerups
521     tux.kill(tux.SHRINK);
522     last_keys.clear();
523   }
524   if(compare_last(last_keys, "kill")) {
525     // kill Tux, but without losing a life
526     player_status.lives++;
527     tux.kill(tux.KILL);
528     last_keys.clear();
529   }
530   if(compare_last(last_keys, "grid")) {
531     // toggle debug grid
532     debug_grid = !debug_grid;
533     last_keys.clear();
534   }
535   if(compare_last(last_keys, "hover")) {
536     // toggle hover ability on/off
537     tux.enable_hover = !tux.enable_hover;
538     last_keys.clear();
539   }
540   if(compare_last(last_keys, "gotoend")) {
541     // goes to the end of the level
542     tux.move(Vector(
543           (currentsector->solids->get_width()*32) - (screen->w*2), 0));
544     currentsector->camera->reset(
545         Vector(tux.get_pos().x, tux.get_pos().y));
546     last_keys.clear();
547   }
548   if(compare_last(last_keys, "finish")) {
549     // finish current sector
550     exit_status = ES_LEVEL_FINISHED;
551     // don't add points to stats though...
552   }
553   // temporary to help player's choosing a flapping
554   if(compare_last(last_keys, "marek")) {
555     tux.flapping_mode = Player::MAREK_FLAP;
556     last_keys.clear();
557   }
558   if(compare_last(last_keys, "ricardo")) {
559     tux.flapping_mode = Player::RICARDO_FLAP;
560     last_keys.clear();
561   }
562   if(compare_last(last_keys, "ryan")) {
563     tux.flapping_mode = Player::RYAN_FLAP;
564     last_keys.clear();
565   }
566 }
567
568 void
569 GameSession::check_end_conditions()
570 {
571   Player* tux = currentsector->player;
572
573   /* End of level? */
574   if(end_sequence && endsequence_timer.check()) {
575       exit_status = ES_LEVEL_FINISHED;
576       global_stats += last_swap_stats;  // add swap points stats
577       return;
578   } else if (!end_sequence && tux->is_dead()) {
579     player_status.bonus = PlayerStatus::NO_BONUS;
580
581     if (player_status.lives < 0) { // No more lives!?
582       exit_status = ES_GAME_OVER;
583     } else { // Still has lives, so reset Tux to the levelstart
584       restart_level();
585     }
586     
587     return;
588   }
589 }
590
591 void
592 GameSession::action(float elapsed_time)
593 {
594   // advance timers
595   if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
596     {
597       // Update Tux and the World
598       currentsector->action(elapsed_time);
599     }
600
601   // respawning in new sector?
602   if(newsector != "" && newspawnpoint != "") {
603     Sector* sector = level->get_sector(newsector);
604     currentsector = sector;
605     currentsector->activate(newspawnpoint);
606     currentsector->play_music(LEVEL_MUSIC);
607     newsector = newspawnpoint = "";
608   }
609 }
610
611 void 
612 GameSession::draw()
613 {
614   currentsector->draw(*context);
615   drawstatus(*context);
616
617   if(game_pause)
618     {
619       int x = screen->h / 20;
620       for(int i = 0; i < x; ++i)
621         {
622           context->draw_filled_rect(
623               Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
624                 -((pause_menu_frame * i)%screen->w)
625                 ,(i*20+pause_menu_frame)%screen->h),
626               Vector(screen->w,10),
627               Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
628         }
629       context->draw_filled_rect(
630           Vector(0,0), Vector(screen->w, screen->h),
631           Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
632       context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
633           Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
634
635       char str1[60];
636       char str2[124];
637       sprintf(str1, _("Playing: "));
638       sprintf(str2, level->name.c_str());
639
640       context->draw_text(blue_text, str1,
641           Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
642           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
643       context->draw_text(white_text, str2,
644           Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
645           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
646     }
647
648   if(Menu::current())
649     {
650       Menu::current()->draw(*context);
651       mouse_cursor->draw(*context);
652     }
653
654   context->do_drawing();
655 }
656
657 void
658 GameSession::process_menu()
659 {
660   Menu* menu = Menu::current();
661   if(menu)
662     {
663       menu->action();
664
665       if(menu == game_menu)
666         {
667           switch (game_menu->check())
668             {
669             case MNID_CONTINUE:
670               Ticks::pause_stop();
671               break;
672             case MNID_ABORTLEVEL:
673               Ticks::pause_stop();
674               exit_status = ES_LEVEL_ABORT;
675               break;
676             }
677         }
678       else if(menu == options_menu)
679         {
680           process_options_menu();
681         }
682       else if(menu == load_game_menu )
683         {
684           process_load_game_menu();
685         }
686     }
687 }
688
689 GameSession::ExitStatus
690 GameSession::run()
691 {
692   Menu::set_current(0);
693   current_ = this;
694   
695   int fps_cnt = 0;
696
697   // Eat unneeded events
698   SDL_Event event;
699   while(SDL_PollEvent(&event))
700   {}
701
702   draw();
703
704   Uint32 lastticks = SDL_GetTicks();
705   fps_ticks = SDL_GetTicks();
706
707   while (exit_status == ES_NONE) {
708     Uint32 ticks = SDL_GetTicks();
709     float elapsed_time = float(ticks - lastticks) / 1000.;
710     if(!game_pause)
711       global_time += elapsed_time;
712     lastticks = ticks;
713
714     // 40fps is minimum
715     if(elapsed_time > .025)
716       elapsed_time = .025;
717     
718     /* Handle events: */
719     currentsector->player->input.old_fire = currentsector->player->input.fire;
720     currentsector->player->input.old_up = currentsector->player->input.old_up;
721
722     process_events();
723     process_menu();
724
725     // Update the world state and all objects in the world
726     // Do that with a constante time-delta so that the game will run
727     // determistic and not different on different machines
728     if(!game_pause && !Menu::current())
729     {
730       // Update the world
731       check_end_conditions();
732       if (end_sequence == ENDSEQUENCE_RUNNING)
733         action(elapsed_time/2);
734       else if(end_sequence == NO_ENDSEQUENCE)
735         action(elapsed_time);
736     }
737     else
738     {
739       ++pause_menu_frame;
740       SDL_Delay(50);
741     }
742
743     draw();
744
745     /* Time stops in pause mode */
746     if(game_pause || Menu::current())
747     {
748       continue;
749     }
750
751     //frame_rate.update();
752     
753     /* Handle time: */
754     if (time_left.check() && !end_sequence)
755       currentsector->player->kill(Player::KILL);
756     
757     /* Handle music: */
758     if (currentsector->player->invincible_timer.started() && !end_sequence)
759     {
760       currentsector->play_music(HERRING_MUSIC);
761     }
762     /* are we low on time ? */
763     else if (time_left.get_timeleft() < TIME_WARNING && !end_sequence)
764     {
765       currentsector->play_music(HURRYUP_MUSIC);
766     }
767     /* or just normal music? */
768     else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
769     {
770       currentsector->play_music(LEVEL_MUSIC);
771     }
772
773     /* Calculate frames per second */
774     if(show_fps)
775     {
776       ++fps_cnt;
777       
778       if(SDL_GetTicks() - fps_ticks >= 500)
779       {
780         fps_fps = (float) fps_cnt / .5;
781         fps_cnt = 0;
782         fps_ticks = SDL_GetTicks();
783       }
784     }
785   }
786   
787   return exit_status;
788 }
789
790 void
791 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
792 {
793   newsector = sector;
794   newspawnpoint = spawnpoint;
795 }
796
797 void
798 GameSession::start_sequence(const std::string& sequencename)
799 {
800   if(sequencename == "endsequence" || sequencename == "fireworks") {
801     if(end_sequence)
802       return;
803     
804     end_sequence = ENDSEQUENCE_RUNNING;
805     endsequence_timer.start(7.0); // 7 seconds until we finish the map
806     last_x_pos = -1;
807     SoundManager::get()->play_music(level_end_song, 0);
808     currentsector->player->invincible_timer.start(7.0);
809
810     // add left time to stats
811     global_stats.set_points(TIME_NEEDED_STAT,
812         int(time_left.get_period() - time_left.get_timeleft()));
813
814     if(sequencename == "fireworks") {
815       currentsector->add_object(new Fireworks());
816     }
817   } else if(sequencename == "stoptux") {
818     end_sequence =  ENDSEQUENCE_WAITING;
819   } else {
820     std::cout << "Unknown sequence '" << sequencename << "'.\n";
821   }
822 }
823
824 /* (Status): */
825 void
826 GameSession::drawstatus(DrawingContext& context)
827 {
828   char str[60];
829   
830   snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
831   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
832   context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
833
834   if(st_gl_mode == ST_GL_TEST)
835     {
836       context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
837           LEFT_ALLIGN, LAYER_FOREGROUND1);
838     }
839
840   if(time_left.get_timeleft() < 0) {
841     context.draw_text(white_text, _("TIME's UP"), Vector(screen->w/2, 0),
842         CENTER_ALLIGN, LAYER_FOREGROUND1);
843   } else if (time_left.get_timeleft() > TIME_WARNING
844       || int(global_time * 2.5) % 2) {
845     sprintf(str, " %d", int(time_left.get_timeleft()));
846     context.draw_text(white_text, _("TIME"),
847         Vector(screen->w/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
848     context.draw_text(gold_text, str,
849         Vector(screen->w/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
850   }
851
852   sprintf(str, " %d", player_status.distros);
853   context.draw_text(white_text, _("COINS"),
854       Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
855         LEFT_ALLIGN, LAYER_FOREGROUND1);
856   context.draw_text(gold_text, str,
857       Vector(screen->w - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
858
859   if (player_status.lives >= 5)
860     {
861       sprintf(str, "%dx", player_status.lives);
862       float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
863       context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
864       context.draw_surface(tux_life, Vector(screen->w - 16, 20),
865           LAYER_FOREGROUND1);
866     }
867   else
868     {
869       for(int i= 0; i < player_status.lives; ++i)
870         context.draw_surface(tux_life, 
871             Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
872             LAYER_FOREGROUND1);
873     }
874
875   context.draw_text(white_text, _("LIVES"),
876       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
877       LEFT_ALLIGN, LAYER_FOREGROUND1);
878
879   if(show_fps)
880     {
881       sprintf(str, "%2.1f", fps_fps);
882       context.draw_text(white_text, "FPS", 
883           Vector(screen->w - white_text->get_text_width("FPS     "), 40),
884           LEFT_ALLIGN, LAYER_FOREGROUND1);
885       context.draw_text(gold_text, str,
886           Vector(screen->w-4*16, 40), LEFT_ALLIGN, LAYER_FOREGROUND1);
887     }
888 }
889
890 void
891 GameSession::drawresultscreen()
892 {
893   char str[80];
894
895   DrawingContext context;
896   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
897       i != currentsector->gameobjects.end(); ++i) {
898     Background* background = dynamic_cast<Background*> (*i);
899     if(background) {
900       background->draw(context);
901     }
902   }
903
904   context.draw_text(blue_text, _("Result:"), Vector(screen->w/2, 200),
905       CENTER_ALLIGN, LAYER_FOREGROUND1);
906
907   sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
908   context.draw_text(gold_text, str, Vector(screen->w/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
909
910   sprintf(str, _("COINS: %d"), player_status.distros);
911   context.draw_text(gold_text, str, Vector(screen->w/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
912
913   context.do_drawing();
914   
915   SDL_Event event;
916   wait_for_event(event,2000,5000,true);
917 }
918
919 std::string slotinfo(int slot)
920 {
921   std::string tmp;
922   std::string slotfile;
923   std::string title;
924   std::stringstream stream;
925   stream << slot;
926   slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
927
928   try {
929     lisp::Parser parser;
930     std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
931
932     const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
933     if(!savegame)
934       throw std::runtime_error("file is not a supertux-savegame.");
935
936     savegame->get("title", title);
937   } catch(std::exception& e) {
938     return std::string(_("Slot")) + " " + stream.str() + " - " +
939       std::string(_("Free"));
940   }
941
942   return std::string("Slot ") + stream.str() + " - " + title;
943 }
944
945 bool process_load_game_menu()
946 {
947   int slot = load_game_menu->check();
948
949   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
950     {
951       std::stringstream stream;
952       stream << slot;
953       std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
954
955       fadeout(256);
956       DrawingContext context;
957       context.draw_text(white_text, "Loading...",
958                         Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
959       context.do_drawing();
960
961       WorldMapNS::WorldMap worldmap;
962
963       worldmap.set_map_filename("/levels/world1/worldmap.stwm");
964       // Load the game or at least set the savegame_file variable
965       worldmap.loadgame(slotfile);
966
967       worldmap.display();
968
969       Menu::set_current(main_menu);
970
971       Ticks::pause_stop();
972       return true;
973     }
974   else
975     {
976       return false;
977     }
978 }