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