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