Back to index

extremetuxracer  0.5beta
loop.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  *
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "loop.h"
00023 #include "game_config.h"
00024 
00025 #include "ppgltk/ui_snow.h"
00026 #include "ppgltk/alg/defs.h"
00027 
00028 #include "SDL.h"
00029 
00030 
00031 #include "render_util.h"
00032 #include "splash_screen.h"
00033 #include "game_type_select.h"
00034 #include "credits.h"
00035 #include "race_select.h"
00036 #include "loading.h"
00037 #include "intro.h"
00038 #include "racing.h"
00039 #include "paused.h"
00040 #include "game_over.h"
00041 #include "reset.h"
00042 #include "event_select.h"
00043 #include "event_race_select.h"
00044 #include "bench.h"
00045 #include "highscore.h"
00046 
00047 #include "configuration.h"
00048 #include "graphicsconfig.h"
00049 #include "videoconfig.h"
00050 #include "audioconfig.h"
00051 #include "keyboardconfig.h"
00052 #include "joystickconfig.h"
00053 
00054 #include "game_mgr.h"
00055 
00056 
00057 GameMode* GameMode::currentMode = NULL;
00058 
00059 game_mode_t GameMode::mode;
00060 game_mode_t GameMode::prevmode;
00061 
00062 static game_mode_t new_mode = NO_MODE;
00063 
00064 void set_game_mode( game_mode_t mode ) 
00065 {
00066     new_mode = mode;
00067 }
00068 
00069 void main_loop()
00070 {
00071        if ( getparam_capture_mouse() ) {
00072               const int w = getparam_x_resolution();
00073               const int h = getparam_y_resolution();
00074               pp::Vec2d pos = UIMgr.getMousePosition();
00075 
00076               // Flip y coordinates
00077               pos.y = h - pos.y;
00078 
00079               if ( pos.x < 0 ) {
00080                   pos.x = 0;
00081               }
00082               if ( pos.x > w-1 ) {
00083                   pos.x = w-1;
00084               }
00085               if ( pos.y < 0 ) {
00086                   pos.y = 0;
00087               }
00088               if ( pos.y > h-1 ) {
00089                   pos.y = h-1;
00090               }
00091 
00092               winsys_warp_pointer( int(pos.x), int(pos.y) );
00093     }
00094        
00095        if ( GameMode::mode != new_mode ) {
00096               
00097               if (GameMode::currentMode!=NULL){
00098                      delete GameMode::currentMode;
00099               }
00100               GameMode::prevmode = GameMode::mode;
00101 
00102               switch(new_mode){
00103                      case SPLASH:
00104                             GameMode::currentMode = new SplashScreen();
00105                             break;               
00106                      case GAME_TYPE_SELECT:
00107                             GameMode::currentMode = new GameTypeSelect();
00108                             break;
00109                      case CREDITS:
00110                             GameMode::currentMode = new Credits();
00111                             break;
00112                      case CREDITS_TRANS:
00113                             GameMode::currentMode = new CreditsTrans();
00114                             break;
00115                      case CONFIGURATION:
00116                             GameMode::currentMode = new Configuration();
00117                             break;
00118                      case RACE_SELECT:
00119                             GameMode::currentMode = new RaceSelect();
00120                             break;
00121                      case LOADING:
00122                             GameMode::currentMode = new Loading();
00123                             break;
00124                      case INTRO:
00125                             GameMode::currentMode = new Intro();
00126                             break;
00127                      case RACING:
00128                             GameMode::currentMode = new Racing();
00129                             break;
00130                      case PAUSED:
00131                             GameMode::currentMode = new Paused();
00132                             break;
00133                      case GAME_OVER:
00134                             GameMode::currentMode = new GameOver();
00135                             break;               
00136                      case RESET:
00137                             GameMode::currentMode = new Reset();
00138                             break;
00139                      case EVENT_SELECT:
00140                             GameMode::currentMode = new EventSelect();
00141                             break;
00142                      case CONFIG_GRAPHICS:
00143                             GameMode::currentMode = new GraphicsConfig();
00144                             break;
00145                      case CONFIG_VIDEO:
00146                             GameMode::currentMode = new VideoConfig();
00147                             break;
00148                      case CONFIG_AUDIO:
00149                             GameMode::currentMode = new AudioConfig();
00150                             break;
00151                      case CONFIG_KEYBOARD:
00152                             GameMode::currentMode = new KeyboardConfig();
00153                             break;
00154                      case CONFIG_JOYSTICK:
00155                             GameMode::currentMode = new JoystickConfig();
00156                             break;
00157                      case EVENT_RACE_SELECT:
00158                             GameMode::currentMode = new EventRaceSelect();
00159                             break;
00160                      case BENCHMARK:
00161                             GameMode::currentMode = new Benchmark();
00162                             break;
00163                      case HIGHSCORE:
00164                             GameMode::currentMode = new HighscoreShow();
00165                             break;
00166                      default:{}
00167                             //todo: add fallback               
00168               }
00169               
00170               GameMode::mode = new_mode;
00171        
00172               // Reset time step clock so that there isn't a sudden
00173               //  jump when we start the new mode 
00174               gameMgr->resetTimeStep();
00175        }
00176        
00177        gameMgr->updateTimeStep();
00178        
00179        if ( Benchmark::getTimeStep() >0.0 ){
00180               GameMode::currentMode->loop(Benchmark::getTimeStep());
00181        }else{ 
00182               GameMode::currentMode->loop( gameMgr->getTimeStep() );
00183        }
00184 }
00185 
00186 /*---------------------------------------------------------------------------*/
00192 bool is_mode_change_pending()
00193 {
00194     return bool(GameMode::mode != new_mode);
00195 }
00196 
00197 GameMode::GameMode()
00198 {
00199 }
00200 
00201        
00202 void
00203 GameMode::drawSnow( float timeStep, bool windy )
00204 {
00205        // check wether ui snow is 
00206        if(getparam_ui_snow()){
00207               // update and draw snow
00208               update_ui_snow( timeStep, windy );
00209               draw_ui_snow();
00210     }
00211 }
00212 
00213 void loop_mouse_func (int button, int state, int x, int y)
00214 {
00215        if(GameMode::currentMode!=NULL){
00216               bool pressed = state && SDL_PRESSED;
00217               if(GameMode::currentMode->mouseButtonEvent(button,x,y,pressed)) return;
00218               else if(pressed){
00219                      if(GameMode::currentMode->mouseButtonPressEvent(button,x,y)) return;
00220               }else{
00221                      if(GameMode::currentMode->mouseButtonReleaseEvent(button,x,y)) return;
00222               }      
00223               UIMgr.mouseEvent(button,state,x,y);
00224        }
00225 }
00226 
00227 void loop_mouse_motion_func( int x, int y )
00228 {
00229        UIMgr.motionEvent(x,y);
00230 }
00231 
00232 void loop_keyboard_func(SDLKey key, SDLMod mod, bool release, int x, int y)
00233 {
00234        if(GameMode::currentMode!=NULL){          
00235               if (key < SDLK_UP) {
00236             if ( isalpha( key ) ) {
00237                 key = SDLKey(tolower( key ));
00238             }
00239         }
00240               
00241               if(GameMode::currentMode->keyboardEvent(key,release)) return;
00242               else if(release){
00243                      if(GameMode::currentMode->keyReleaseEvent(key)) return;
00244               }else{
00245                      if(GameMode::currentMode->keyPressEvent(key)) return;
00246               }
00247               UIMgr.keyboardEvent(key,mod,release);
00248        }
00249 }