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