Time is not stopped during menu display in gameloop (fix).
[supertux.git] / src / gameloop.cpp
index 4b8f577..40a609b 100644 (file)
@@ -1,14 +1,23 @@
-/*
-  gameloop.c
-  
-  Super Tux - Game Loop!
-  
-  by Bill Kendrick & Tobias Glaesser
-  bill@newbreedsoftware.com
-  http://www.newbreedsoftware.com/supertux/
-  
-  April 11, 2000 - March 15, 2004
-*/
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
+//  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
+//  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+// 
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include <iostream>
 #include <assert.h>
@@ -63,6 +72,13 @@ GameSession::restart_level()
   fps_timer.init(true);
   frame_timer.init(true);
 
+  float old_x_pos = -1;
+
+  if (world)
+    { // Tux has lost a life, so we try to respawn him at the nearest reset point
+      old_x_pos = world->get_tux()->base.x;
+    }
+  
   delete world;
 
   if (st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
@@ -77,12 +93,28 @@ GameSession::restart_level()
     {
       world = new World(subset, levelnb);
     }
+
+  // Set Tux to the nearest reset point
+  if (old_x_pos != -1)
+    {
+      ResetPoint best_reset_point = { -1, -1 };
+      for(std::vector<ResetPoint>::iterator i = get_level()->reset_points.begin();
+          i != get_level()->reset_points.end(); ++i)
+        {
+          if (i->x < old_x_pos && best_reset_point.x < i->x)
+            best_reset_point = *i;
+        }
+      
+      if (best_reset_point.x != -1)
+        {
+          world->get_tux()->base.x = best_reset_point.x;
+          world->get_tux()->base.y = best_reset_point.y;
+        }
+    }
+
     
   if (st_gl_mode != ST_GL_DEMO_GAME)
     {
-      if(st_gl_mode != ST_GL_TEST)
-        load_hs();
-
       if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
         levelintro();
     }
@@ -131,22 +163,13 @@ GameSession::start_timers()
 void
 GameSession::on_escape_press()
 {
-  if(!game_pause)
+  if(st_gl_mode == ST_GL_TEST)
     {
-      if(st_gl_mode == ST_GL_TEST)
-        {
-          exit_status = LEVEL_ABORT;
-        }
-      else if (!Menu::current())
-        {
-          Menu::set_current(game_menu);
-          st_pause_ticks_stop();
-        }
-      else
-        {
-          Menu::set_current(NULL);
-          st_pause_ticks_start();
-        }
+      exit_status = LEVEL_ABORT;
+    }
+  else if (!Menu::current())
+    {
+      Menu::set_current(game_menu);
     }
 }
 
@@ -160,151 +183,153 @@ GameSession::process_events()
     {
       /* Check for menu-events, if the menu is shown */
       if (Menu::current())
-        Menu::current()->event(event);
-
-      switch(event.type)
         {
-        case SDL_QUIT:        /* Quit event - quit: */
-          st_abort("Received window close", "");
-          break;
+          Menu::current()->event(event);
+          st_pause_ticks_start();
+        }
+      else
+        {
+          st_pause_ticks_stop();
 
-        case SDL_KEYDOWN:     /* A keypress! */
-          {
-            SDLKey key = event.key.keysym.sym;
-            
-            if(tux.key_event(key,DOWN))
+          switch(event.type)
+            {
+            case SDL_QUIT:        /* Quit event - quit: */
+              st_abort("Received window close", "");
               break;
 
-            switch(key)
+            case SDL_KEYDOWN:     /* A keypress! */
               {
-              case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
-                on_escape_press();
-                break;
-              default:
-                break;
-              }
-          }
-          break;
-        case SDL_KEYUP:      /* A keyrelease! */
-          {
-            SDLKey key = event.key.keysym.sym;
+                SDLKey key = event.key.keysym.sym;
+            
+                if(tux.key_event(key,DOWN))
+                  break;
 
-            if(tux.key_event(key, UP))
+                switch(key)
+                  {
+                  case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
+                    on_escape_press();
+                    break;
+                  default:
+                    break;
+                  }
+              }
               break;
-
-            switch(key)
+            case SDL_KEYUP:      /* A keyrelease! */
               {
-              case SDLK_p:
-                if(!Menu::current())
+                SDLKey key = event.key.keysym.sym;
+
+                if(tux.key_event(key, UP))
+                  break;
+
+                switch(key)
                   {
-                    if(game_pause)
+                  case SDLK_p:
+                    if(!Menu::current())
                       {
-                        game_pause = false;
-                        st_pause_ticks_stop();
+                        if(game_pause)
+                          {
+                            game_pause = false;
+                            st_pause_ticks_stop();
+                          }
+                        else
+                          {
+                            game_pause = true;
+                            st_pause_ticks_start();
+                          }
                       }
-                    else
-                      {
-                        game_pause = true;
-                        st_pause_ticks_start();
-                      }
-                  }
-                break;
-              case SDLK_TAB:
-                if(debug_mode)
-                  {
-                    tux.size = !tux.size;
-                    if(tux.size == BIG)
+                    break;
+                  case SDLK_TAB:
+                    if(debug_mode)
                       {
-                        tux.base.height = 64;
+                        tux.size = !tux.size;
+                        if(tux.size == BIG)
+                          {
+                            tux.base.height = 64;
+                          }
+                        else
+                          tux.base.height = 32;
                       }
+                    break;
+                  case SDLK_END:
+                    if(debug_mode)
+                      player_status.distros += 50;
+                    break;
+                  case SDLK_DELETE:
+                    if(debug_mode)
+                      tux.got_coffee = 1;
+                    break;
+                  case SDLK_INSERT:
+                    if(debug_mode)
+                      tux.invincible_timer.start(TUX_INVINCIBLE_TIME);
+                    break;
+                  case SDLK_l:
+                    if(debug_mode)
+                      --player_status.lives;
+                    break;
+                  case SDLK_s:
+                    if(debug_mode)
+                      player_status.score += 1000;
+                  case SDLK_f:
+                    if(debug_fps)
+                      debug_fps = false;
                     else
-                      tux.base.height = 32;
+                      debug_fps = true;
+                    break;
+                  default:
+                    break;
                   }
-                break;
-              case SDLK_END:
-                if(debug_mode)
-                  player_status.distros += 50;
-                break;
-              case SDLK_DELETE:
-                if(debug_mode)
-                  tux.got_coffee = 1;
-                break;
-              case SDLK_INSERT:
-                if(debug_mode)
-                  tux.invincible_timer.start(TUX_INVINCIBLE_TIME);
-                break;
-              case SDLK_l:
-                if(debug_mode)
-                  --player_status.lives;
-                break;
-              case SDLK_s:
-                if(debug_mode)
-                  player_status.score += 1000;
-              case SDLK_f:
-                if(debug_fps)
-                  debug_fps = false;
-                else
-                  debug_fps = true;
-                break;
-              default:
-                break;
               }
-          }
-          break;
+              break;
 
-        case SDL_JOYAXISMOTION:
-          switch(event.jaxis.axis)
-            {
-            case JOY_X:
-              if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
-                {
-                  tux.input.left  = DOWN;
-                  tux.input.right = UP;
-                }
-              else if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
+            case SDL_JOYAXISMOTION:
+              if (event.jaxis.axis == joystick_keymap.x_axis)
                 {
-                  tux.input.left  = UP;
-                  tux.input.right = DOWN;
+                  if (event.jaxis.value < -joystick_keymap.dead_zone)
+                    {
+                      tux.input.left  = DOWN;
+                      tux.input.right = UP;
+                    }
+                  else if (event.jaxis.value > joystick_keymap.dead_zone)
+                    {
+                      tux.input.left  = UP;
+                      tux.input.right = DOWN;
+                    }
+                  else
+                    {
+                      tux.input.left  = DOWN;
+                      tux.input.right = DOWN;
+                    }
                 }
-              else
+              else if (event.jaxis.axis == joystick_keymap.y_axis)
                 {
-                  tux.input.left  = DOWN;
-                  tux.input.right = DOWN;
+                  if (event.jaxis.value > joystick_keymap.dead_zone)
+                    tux.input.down = DOWN;
+                  else if (event.jaxis.value < -joystick_keymap.dead_zone)
+                    tux.input.down = UP;
+                  else
+                    tux.input.down = UP;
                 }
               break;
-            case JOY_Y:
-              if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
-                tux.input.down = DOWN;
-              else if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
-                tux.input.down = UP;
-              else
-                tux.input.down = UP;
-              
-             break;
-            default:
+            
+            case SDL_JOYBUTTONDOWN:
+              if (event.jbutton.button == joystick_keymap.a_button)
+                tux.input.up = DOWN;
+              else if (event.jbutton.button == joystick_keymap.b_button)
+                tux.input.fire = DOWN;
+              else if (event.jbutton.button == joystick_keymap.start_button)
+                on_escape_press();
+              break;
+            case SDL_JOYBUTTONUP:
+              if (event.jbutton.button == joystick_keymap.a_button)
+                tux.input.up = UP;
+              else if (event.jbutton.button == joystick_keymap.b_button)
+                tux.input.fire = UP;
               break;
-            }
-          break;
-        case SDL_JOYBUTTONDOWN:
-          if (event.jbutton.button == JOY_A)
-            tux.input.up = DOWN;
-          else if (event.jbutton.button == JOY_B)
-            tux.input.fire = DOWN;
-          else if (event.jbutton.button == JOY_START)
-            on_escape_press();
-          break;
-        case SDL_JOYBUTTONUP:
-          if (event.jbutton.button == JOY_A)
-            tux.input.up = UP;
-          else if (event.jbutton.button == JOY_B)
-            tux.input.fire = UP;
-          break;
-
-        default:
-          break;
-
-        }  /* switch */
 
+            default:
+              break;
+            }  /* switch */
+        }
     } /* while */
 }
 
@@ -331,14 +356,6 @@ GameSession::check_end_conditions()
             { // No more lives!?
               if(st_gl_mode != ST_GL_TEST)
                 drawendscreen();
-          
-              if(st_gl_mode != ST_GL_TEST)
-                {
-                  // FIXME: highscore soving doesn't make sense in its
-                  // current form
-                  //if (player_status.score > hs_score)
-                  //save_hs(player_status.score);
-                }
               
               exit_status = GAME_OVER;
             }
@@ -381,7 +398,7 @@ GameSession::draw()
 
   if(Menu::current())
     {
-      menu_process_current();
+      Menu::current()->draw();
       mouse_cursor->draw();
     }
 
@@ -392,6 +409,7 @@ GameSession::draw()
 GameSession::ExitStatus
 GameSession::run()
 {
+  Menu::set_current(0);
   Player* tux = world->get_tux();
   current_ = this;
   
@@ -435,10 +453,13 @@ GameSession::run()
       tux->input.old_fire = tux->input.fire;
 
       process_events();
-
-      if(Menu::current())
+      
+      Menu* menu = Menu::current();
+      if(menu)
         {
-          if(Menu::current() == game_menu)
+          menu->action();
+
+          if(menu == game_menu)
             {
               switch (game_menu->check())
                 {
@@ -451,11 +472,11 @@ GameSession::run()
                   break;
                 }
             }
-          else if(Menu::current() == options_menu)
+          else if(menu == options_menu)
             {
               process_options_menu();
             }
-          else if(Menu::current() == load_game_menu )
+          else if(menu == load_game_menu )
             {
               process_load_game_menu();
             }
@@ -571,13 +592,7 @@ GameSession::drawstatus()
   white_text->draw("SCORE", 0, 0, 1);
   gold_text->draw(str, 96, 0, 1);
 
-  if(st_gl_mode != ST_GL_TEST)
-    {
-      sprintf(str, "%d", hs_score);
-      white_text->draw("HIGH", 0, 20, 1);
-      gold_text->draw(str, 96, 20, 1);
-    }
-  else
+  if(st_gl_mode == ST_GL_TEST)
     {
       white_text->draw("Press ESC To Return",0,20,1);
     }