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