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