fix particle system speeds
[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 "player.h"
51 #include "level.h"
52 #include "scene.h"
53 #include "collision.h"
54 #include "tile.h"
55 #include "particlesystem.h"
56 #include "resources.h"
57 #include "background.h"
58 #include "tilemap.h"
59 #include "app/gettext.h"
60 #include "worldmap.h"
61 #include "intro.h"
62 #include "misc.h"
63 #include "camera.h"
64 #include "statistics.h"
65 #include "timer.h"
66 #include "object/fireworks.h"
67
68 GameSession* GameSession::current_ = 0;
69
70 bool compare_last(std::string& haystack, std::string needle)
71 {
72   int haystack_size = haystack.size();
73   int needle_size = needle.size();
74
75   if(haystack_size < needle_size)
76     return false;
77
78   if(haystack.compare(haystack_size-needle_size, needle_size, needle) == 0)
79     return true;
80   return false;
81 }
82
83 GameSession::GameSession(const std::string& levelname_, int mode,
84     bool flip_level_, Statistics* statistics)
85   : level(0), currentsector(0), st_gl_mode(mode),
86     end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_),
87     best_level_statistics(statistics)
88 {
89   current_ = this;
90   
91   game_pause = false;
92   fps_fps = 0;
93
94   context = new DrawingContext();
95
96   if(flip_levels_mode)
97     flip_level = true;
98
99   last_swap_point = Vector(-1, -1);
100   last_swap_stats.reset();
101
102   restart_level();
103 }
104
105 void
106 GameSession::restart_level()
107 {
108   game_pause   = false;
109   exit_status  = ES_NONE;
110   end_sequence = NO_ENDSEQUENCE;
111
112   last_keys.clear();
113
114 #if 0
115   Vector tux_pos = Vector(-1,-1);
116   if (currentsector)
117     { 
118       // Tux has lost a life, so we try to respawn him at the nearest reset point
119       tux_pos = currentsector->player->base;
120     }
121 #endif
122   
123   delete level;
124   currentsector = 0;
125
126   level = new Level;
127   level->load(levelname);
128   if(flip_level)
129     level->do_vertical_flip();
130
131   global_stats.reset();
132   global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
133   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
134   global_stats.set_total_points(TIME_NEEDED_STAT, level->timelimit);
135
136   currentsector = level->get_sector("main");
137   if(!currentsector)
138     Termination::abort("Level has no main sector.", "");
139   currentsector->activate("main");
140
141 #if 0
142   // Set Tux to the nearest reset point
143   if(tux_pos.x != -1)
144     {
145     tux_pos = currentsector->get_best_spawn_point(tux_pos);
146
147     if(last_swap_point.x > tux_pos.x)
148       tux_pos = last_swap_point;
149     else  // new swap point
150       {
151       last_swap_point = tux_pos;
152
153       last_swap_stats += global_stats;
154       }
155
156     currentsector->player->base.x = tux_pos.x;
157     currentsector->player->base.y = tux_pos.y;
158
159     // has to reset camera on swapping
160     currentsector->camera->reset(Vector(currentsector->player->base.x,
161                                         currentsector->player->base.y));
162     }
163 #endif
164
165   if (st_gl_mode != ST_GL_DEMO_GAME)
166     {
167       if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
168         levelintro();
169     }
170
171   start_timers();
172   currentsector->play_music(LEVEL_MUSIC);
173 }
174
175 GameSession::~GameSession()
176 {
177   delete level;
178   delete context;
179 }
180
181 void
182 GameSession::levelintro(void)
183 {
184   SoundManager::get()->halt_music();
185   
186   char str[60];
187
188   DrawingContext context;
189   if(currentsector->background)
190     currentsector->background->draw(context);
191
192 //  context.draw_text(gold_text, level->get_name(), Vector(screen->w/2, 160),
193 //      CENTER_ALLIGN, LAYER_FOREGROUND1);
194   context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
195       LAYER_FOREGROUND1);
196
197   sprintf(str, "TUX x %d", player_status.lives);
198   context.draw_text(white_text, str, Vector(screen->w/2, 210),
199       CENTER_ALLIGN, LAYER_FOREGROUND1);
200
201   if(level->get_author().size())
202     context.draw_text(white_small_text,
203       std::string(_("by ")) + level->get_author(), 
204       Vector(screen->w/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
205
206
207   if(flip_level)
208     context.draw_text(white_text,
209       _("Level Vertically Flipped!"),
210       Vector(screen->w/2, 310), CENTER_ALLIGN, LAYER_FOREGROUND1);
211
212   if(best_level_statistics != NULL)
213     best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
214
215   context.do_drawing();
216
217   SDL_Event event;
218   wait_for_event(event,1000,3000,true);
219 }
220
221 /* Reset Timers */
222 void
223 GameSession::start_timers()
224 {
225   time_left.start(level->timelimit);
226   Ticks::pause_init();
227 }
228
229 void
230 GameSession::on_escape_press()
231 {
232   if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
233     return;   // don't let the player open the menu, when he is dying
234   
235   if(game_pause)
236     return;
237
238   if(st_gl_mode == ST_GL_TEST)
239     {
240       exit_status = ES_LEVEL_ABORT;
241     }
242   else if (!Menu::current())
243     {
244       /* Tell Tux that the keys are all down, otherwise
245         it could have nasty bugs, like going allways to the right
246         or whatever that key does */
247       Player& tux = *(currentsector->player);
248       tux.key_event((SDLKey)keymap.up, UP);
249       tux.key_event((SDLKey)keymap.down, UP);
250       tux.key_event((SDLKey)keymap.left, UP);
251       tux.key_event((SDLKey)keymap.right, UP);
252       tux.key_event((SDLKey)keymap.jump, UP);
253       tux.key_event((SDLKey)keymap.power, UP);
254
255       Menu::set_current(game_menu);
256       Ticks::pause_start();
257     }
258 }
259
260 void
261 GameSession::process_events()
262 {
263   if (end_sequence != NO_ENDSEQUENCE)
264     {
265       Player& tux = *currentsector->player;
266          
267       tux.input.fire  = UP;
268       tux.input.left  = UP;
269       tux.input.right = DOWN;
270       tux.input.down  = UP; 
271
272       if (int(last_x_pos) == int(tux.get_pos().x))
273         tux.input.up    = DOWN; 
274       else
275         tux.input.up    = UP; 
276
277       last_x_pos = tux.get_pos().x;
278
279       SDL_Event event;
280       while (SDL_PollEvent(&event))
281         {
282           /* Check for menu-events, if the menu is shown */
283           if (Menu::current())
284             {
285               Menu::current()->event(event);
286               if(!Menu::current())
287               Ticks::pause_stop();
288             }
289
290           switch(event.type)
291             {
292             case SDL_QUIT:        /* Quit event - quit: */
293               Termination::abort("Received window close", "");
294               break;
295               
296             case SDL_KEYDOWN:     /* A keypress! */
297               {
298                 SDLKey key = event.key.keysym.sym;
299            
300                 switch(key)
301                   {
302                   case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
303                     on_escape_press();
304                     break;
305                   default:
306                     break;
307                   }
308               }
309           
310             case SDL_JOYBUTTONDOWN:
311               if (event.jbutton.button == joystick_keymap.start_button)
312                 on_escape_press();
313               break;
314             }
315         }
316     }
317   else // normal mode
318     {
319       if(!Menu::current() && !game_pause)
320         Ticks::pause_stop();
321
322       SDL_Event event;
323       while (SDL_PollEvent(&event))
324         {
325           /* Check for menu-events, if the menu is shown */
326           if (Menu::current())
327             {
328               Menu::current()->event(event);
329               if(!Menu::current())
330                 Ticks::pause_stop();
331             }
332           else
333             {
334               Player& tux = *currentsector->player;
335   
336               switch(event.type)
337                 {
338                 case SDL_QUIT:        /* Quit event - quit: */
339                   Termination::abort("Received window close", "");
340                   break;
341
342                 case SDL_KEYDOWN:     /* A keypress! */
343                   {
344                     SDLKey key = event.key.keysym.sym;
345             
346                     if(tux.key_event(key,DOWN))
347                       break;
348
349                     switch(key)
350                       {
351                       case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
352                         on_escape_press();
353                         break;
354                       default:
355                         break;
356                       }
357                   }
358                   break;
359                 case SDL_KEYUP:      /* A keyrelease! */
360                   {
361                     SDLKey key = event.key.keysym.sym;
362
363                     if(tux.key_event(key, UP))
364                       break;
365
366                     switch(key)
367                       {
368                       case SDLK_a:
369                         if(debug_mode)
370                         {
371                           char buf[160];
372                           snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
373                               tux.get_pos().x, tux.get_pos().y);
374                           context->draw_text(white_text, buf,
375                               Vector(0, screen->h - white_text->get_height()),
376                               LEFT_ALLIGN, LAYER_FOREGROUND1);
377                           context->do_drawing();
378                           SDL_Delay(1000);
379                         }
380                         break;
381                       case SDLK_p:
382                         if(!Menu::current())
383                           {
384                           // "lifeup" cheat activates pause cause of the 'p'
385                           // so work around to ignore it
386                             if(compare_last(last_keys, "lifeu"))
387                               break;
388
389                             if(game_pause)
390                               {
391                                 game_pause = false;
392                                 Ticks::pause_stop();
393                               }
394                             else
395                               {
396                                 game_pause = true;
397                                 Ticks::pause_start();
398                               }
399                           }
400                         break;
401                       default:
402                         break;
403                       }
404                   }
405
406                         /* Check if chacrater is ASCII */
407                         char ch[2];
408                         if((event.key.keysym.unicode & 0xFF80) == 0)
409                           {
410                           ch[0] = event.key.keysym.unicode & 0x7F;
411                           ch[1] = '\0';
412                           }
413                         last_keys.append(ch);  // add to cheat keys
414
415                         // Cheating words (the goal of this is really for debugging,
416                         // but could be used for some cheating, nothing wrong with that)
417                         if(compare_last(last_keys, "grow"))
418                           {
419                           tux.grow(false);
420                           last_keys.clear();
421                           }
422                         if(compare_last(last_keys, "fire"))
423                           {
424                           tux.grow(false);
425                           tux.got_power = tux.FIRE_POWER;
426                           last_keys.clear();
427                           }
428                         if(compare_last(last_keys, "ice"))
429                           {
430                           tux.grow(false);
431                           tux.got_power = tux.ICE_POWER;
432                           last_keys.clear();
433                           }
434                         if(compare_last(last_keys, "lifeup"))
435                           {
436                           player_status.lives++;
437                           last_keys.clear();
438                           }
439                         if(compare_last(last_keys, "lifedown"))
440                           {
441                           player_status.lives--;
442                           last_keys.clear();
443                           }
444                         if(compare_last(last_keys, "grease"))
445                           {
446                           tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
447                           last_keys.clear();
448                           }
449                         if(compare_last(last_keys, "invincible"))
450                           {    // be invincle for the rest of the level
451                           tux.invincible_timer.start(10000);
452                           last_keys.clear();
453                           }
454                         if(compare_last(last_keys, "shrink"))
455                           {    // remove powerups
456                           tux.kill(tux.SHRINK);
457                           last_keys.clear();
458                           }
459                         if(compare_last(last_keys, "kill"))
460                           {    // kill Tux, but without losing a life
461                           player_status.lives++;
462                           tux.kill(tux.KILL);
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     global_time += elapsed_time;
724     lastticks = ticks;
725     // 40fps is minimum
726     if(elapsed_time > .05)
727       elapsed_time = .05;
728     
729     /* Handle events: */
730     currentsector->player->input.old_fire = currentsector->player->input.fire;
731     currentsector->player->input.old_up = currentsector->player->input.old_up;
732
733     process_events();
734     process_menu();
735
736     // Update the world state and all objects in the world
737     // Do that with a constante time-delta so that the game will run
738     // determistic and not different on different machines
739     if(!game_pause && !Menu::current())
740     {
741       // Update the world
742       check_end_conditions();
743       if (end_sequence == ENDSEQUENCE_RUNNING)
744         action(elapsed_time/2);
745       else if(end_sequence == NO_ENDSEQUENCE)
746         action(elapsed_time);
747     }
748     else
749     {
750       ++pause_menu_frame;
751       SDL_Delay(50);
752     }
753
754     draw();
755
756     /* Time stops in pause mode */
757     if(game_pause || Menu::current())
758     {
759       continue;
760     }
761
762     //frame_rate.update();
763     
764     /* Handle time: */
765     if (time_left.check() && currentsector->player->dying == DYING_NOT
766         && !end_sequence)
767       currentsector->player->kill(Player::KILL);
768     
769     /* Handle music: */
770     if(currentsector->player->invincible_timer.check() && !end_sequence)
771     {
772       currentsector->play_music(HERRING_MUSIC);
773     }
774     /* are we low on time ? */
775     else if (time_left.get_timeleft() < TIME_WARNING && !end_sequence)
776     {
777       currentsector->play_music(HURRYUP_MUSIC);
778     }
779     /* or just normal music? */
780     else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
781     {
782       currentsector->play_music(LEVEL_MUSIC);
783     }
784
785     /* Calculate frames per second */
786     if(show_fps)
787     {
788       ++fps_cnt;
789       
790       if(SDL_GetTicks() - fps_ticks >= 500)
791       {
792         fps_fps = (float) fps_cnt / .5;
793         fps_cnt = 0;
794         fps_ticks = SDL_GetTicks();
795       }
796     }
797   }
798   
799   return exit_status;
800 }
801
802 void
803 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
804 {
805   newsector = sector;
806   newspawnpoint = spawnpoint;
807 }
808
809 void
810 GameSession::start_sequence(const std::string& sequencename)
811 {
812   if(sequencename == "endsequence") {
813     if(end_sequence)
814       return;
815     
816     end_sequence = ENDSEQUENCE_RUNNING;
817     endsequence_timer.start(7.0); // 7 seconds until we finish the map
818     last_x_pos = -1;
819     SoundManager::get()->play_music(level_end_song, 0);
820     currentsector->player->invincible_timer.start(7.0);
821
822     // add left time to stats
823     global_stats.set_points(TIME_NEEDED_STAT,
824         int(time_left.get_period() - time_left.get_timeleft()));
825
826     if(level->get_end_sequence_type() == Level::FIREWORKS_ENDSEQ_ANIM) {
827       currentsector->add_object(new Fireworks());
828     }
829   } else {
830     std::cout << "Unknown sequence '" << sequencename << "'.\n";
831   }
832 }
833
834 /* (Status): */
835 void
836 GameSession::drawstatus(DrawingContext& context)
837 {
838   char str[60];
839   
840   snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
841   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
842   context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
843
844   if(st_gl_mode == ST_GL_TEST)
845     {
846       context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
847           LEFT_ALLIGN, LAYER_FOREGROUND1);
848     }
849
850   if(time_left.check()) {
851     context.draw_text(white_text, _("TIME's UP"), Vector(screen->w/2, 0),
852         CENTER_ALLIGN, LAYER_FOREGROUND1);
853   } else if (time_left.get_timeleft() > TIME_WARNING
854       || int(global_time * 2.5) % 2) {
855     sprintf(str, " %d", int(time_left.get_timeleft()));
856     context.draw_text(white_text, _("TIME"),
857         Vector(screen->w/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
858     context.draw_text(gold_text, str,
859         Vector(screen->w/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
860   }
861
862   sprintf(str, " %d", player_status.distros);
863   context.draw_text(white_text, _("COINS"),
864       Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
865         LEFT_ALLIGN, LAYER_FOREGROUND1);
866   context.draw_text(gold_text, str,
867       Vector(screen->w - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
868
869   if (player_status.lives >= 5)
870     {
871       sprintf(str, "%dx", player_status.lives);
872       float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
873       context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
874       context.draw_surface(tux_life, Vector(screen->w - 16, 20),
875           LAYER_FOREGROUND1);
876     }
877   else
878     {
879       for(int i= 0; i < player_status.lives; ++i)
880         context.draw_surface(tux_life, 
881             Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
882             LAYER_FOREGROUND1);
883     }
884
885   context.draw_text(white_text, _("LIVES"),
886       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
887       LEFT_ALLIGN, LAYER_FOREGROUND1);
888
889   if(show_fps)
890     {
891       sprintf(str, "%2.1f", fps_fps);
892       context.draw_text(white_text, "FPS", 
893           Vector(screen->w - white_text->get_text_width("FPS     "), 40),
894           LEFT_ALLIGN, LAYER_FOREGROUND1);
895       context.draw_text(gold_text, str,
896           Vector(screen->w-4*16, 40), LEFT_ALLIGN, LAYER_FOREGROUND1);
897     }
898 }
899
900 void
901 GameSession::drawresultscreen(void)
902 {
903   char str[80];
904
905   DrawingContext context;
906   currentsector->background->draw(context);  
907
908   context.draw_text(blue_text, _("Result:"), Vector(screen->w/2, 200),
909       CENTER_ALLIGN, LAYER_FOREGROUND1);
910
911   sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
912   context.draw_text(gold_text, str, Vector(screen->w/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
913
914   sprintf(str, _("COINS: %d"), player_status.distros);
915   context.draw_text(gold_text, str, Vector(screen->w/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
916
917   context.do_drawing();
918   
919   SDL_Event event;
920   wait_for_event(event,2000,5000,true);
921 }
922
923 std::string slotinfo(int slot)
924 {
925   std::string tmp;
926   std::string slotfile;
927   std::string title;
928   std::stringstream stream;
929   stream << slot;
930   slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
931
932   lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
933   if (savegame)
934     {
935       LispReader reader(lisp_cdr(savegame));
936       reader.read_string("title", title);
937       lisp_free(savegame);
938     }
939
940   if (access(slotfile.c_str(), F_OK) == 0)
941     {
942       if (!title.empty())
943         tmp = "Slot " + stream.str() + " - " + title;
944       else
945         tmp = "Slot " + stream.str() + " - Savegame";
946     }
947   else
948     tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
949
950   return tmp;
951 }
952
953 bool process_load_game_menu()
954 {
955   int slot = load_game_menu->check();
956
957   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
958     {
959       std::stringstream stream;
960       stream << slot;
961       std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
962
963       if (access(slotfile.c_str(), F_OK) != 0)
964         {
965           shrink_fade(Vector(screen->w/2,screen->h/2), 600);
966           draw_intro();
967         }
968
969       fadeout(256);
970       DrawingContext context;
971       context.draw_text(white_text, "Loading...",
972                         Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
973       context.do_drawing();
974
975       WorldMapNS::WorldMap worldmap;
976
977       worldmap.set_map_filename("icyisland.stwm");
978       // Load the game or at least set the savegame_file variable
979       worldmap.loadgame(slotfile);
980
981       worldmap.display();
982
983       Menu::set_current(main_menu);
984
985       Ticks::pause_stop();
986       return true;
987     }
988   else
989     {
990       return false;
991     }
992 }