Back to index

extremetuxracer  0.5beta
main.cpp
Go to the documentation of this file.
00001 /* 
00002  * ETRacer 
00003  * Copyright (C) 2007-2008 The ETRacer Team <www.extremetuxracer.com>
00004  *
00005  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00006  *
00007  * Copyright (C) 1999-2001 Jasmin F. Patry
00008  * 
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License
00011  * as published by the Free Software Foundation; either version 2
00012  * of the License, or (at your option) any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022  */
00023 
00024 #include "course_load.h"
00025 #include "course_render.h"
00026 #include "textures.h"
00027 #include "model_hndl.h"
00028 #include "phys_sim.h"
00029 #include "part_sys.h"
00030 #include "keyframe.h"
00031 #include "gl_util.h"
00032 #include "game_config.h"
00033 #include "loop.h"
00034 #include "render_util.h"
00035 
00036 #include "bench.h"
00037 
00038 #include "fog.h"
00039 #include "lights.h"
00040 
00041 #include "ppgltk/ui_mgr.h"
00042 
00043 #include "ppgltk/audio/audio_data.h"
00044 #include "ppgltk/audio/audio.h"
00045 
00046 #include "course_mgr.h"
00047 
00048 #include "game_mgr.h"
00049 
00050 #include "joystick.h"
00051 
00052 #include "translation.h"
00053 
00054 #include "callbacks.h"
00055 
00056 #include "tcl_util.h"
00057 
00058 #include "highscore.h"
00059 
00060 // Pointer to an instance of the tcl interpreter
00061 Tcl_Interp *tclInterp;
00062 
00063 #define WINDOW_TITLE "Extreme Tux Racer"
00064 
00065 #define GAME_INIT_SCRIPT "etracer_init.tcl"
00066 
00067 std::string configurationFile;
00068 
00069 /* Summary of command-line arguments:
00070  * -h or --help  Help message
00071  * -c  Sets the configuration file
00072  * -f  Sets the course in benchmark mode
00073  * -m  Maximum number of frames in benchmark mode
00074  * -a  Benchmark mode auto
00075  * -p  Sets the position in benchmark mode
00076  * -t  Sets timestamping in benchmark mode
00077  * -rc  Sets the race conditions in benchmark mode
00078 
00079  * This function returns true if the game should continue, false if it should not (such as when the user
00080  * specifies --help)
00081  */
00082 static bool handleCommandLineOptions( int argc, char *argv[] )
00083 {
00084   //skip the first argument, since it's the command we were invoked with (e.g, etracer or path/etracer)
00085        for(int i=1; i<argc; i++){
00086               if( !strcmp(argv[i],"-c") ){
00087                      i++;
00088                      configurationFile = argv[i]; //do a null check?
00089               }
00090     else if( !strcmp( argv[i],"-f") ){
00091                      i++;
00092                      if(argv[i] != NULL){ //if the user specified a course, set it
00093                             Benchmark::setCourse(argv[i]);
00094                      }
00095               }
00096     else if( !strcmp( argv[i],"-m") ){
00097                      i++;
00098                      if(argv[i] != NULL){ //if the user specified a maximum frame number, set it
00099                             Benchmark::setMaxFrames(atoi(argv[i]));
00100                      }
00101               }
00102     else if( !strcmp( argv[i],"-a") ){
00103                      Benchmark::setMode(Benchmark::AUTO);
00104               }
00105     else if( !strcmp( argv[i],"-p") ){ //Benchmark position - read the x and y next
00106                      i++;
00107                      pp::Vec2d pos;
00108                      if(argv[i] != NULL){
00109                             pos.x = atoi(argv[i]);
00110                             i++;
00111                             if(argv[i] != NULL){
00112                                    pos.y = atoi(argv[i]) * (-1);
00113                                    Benchmark::setPosition(pos);
00114                                    Benchmark::setMode(Benchmark::PAUSED);
00115                             }                    
00116                      }             
00117               }
00118     else if( !strcmp( argv[i],"-t") ){
00119                      i++;
00120                      if(argv[i] != NULL){ //if the user specified a benchmark time stamp, set it
00121                             Benchmark::setTimeStep(atof(argv[i]));
00122                      }
00123               }
00124     else if( !strcmp( argv[i],"-rc") ){
00125                      i++;
00126                      if(argv[i] != NULL){ //if the user specified a race condition, set it
00127                             Benchmark::setRaceCondition(atoi(argv[i]));
00128                      }
00129               }
00130     else if( !strcmp(argv[i], "-h") || !strcmp(argv[i], "--help") ){ //print help message
00131       fprintf(stdout, "Usage: etracer [arguments] \n"
00132                " -h or --help  This help message"
00133                " -c [file]  Sets the configuration file\n"
00134                " -f [course]  Sets the course in benchmark mode\n"
00135                " -m [frames]  Maximum number of frames in benchmark mode\n"
00136                " -a  Benchmark mode auto\n"
00137                " -p [x y] Sets the position in benchmark mode\n"
00138                " -t [timestamp]  Sets timestamping in benchmark mode\n"
00139                " -rc [condition]  Sets the race conditions in benchmark mode\n");
00140       return(false);
00141     }
00142     else{
00143       printf("Unknown argument: %s\n", argv[i]);
00144       return(false);
00145     }
00146 
00147        }// END iterating through parameters
00148   return(true);
00149 }//END handleCommandLineOptions()
00150 
00151 
00152 /* This function is called on exit */
00153 void cleanup(void)
00154 {
00155     write_config_file();
00156 
00157     shutdown_audio();
00158 
00159     winsys_shutdown();
00160 }
00161 
00162 void read_game_init_script()
00163 {
00164     char cwd[BUFF_LEN];
00165     if ( getcwd( cwd, BUFF_LEN ) == NULL ) {
00166            handle_system_error( 1, "getcwd failed" );
00167     }
00168 
00169     if ( chdir( getparam_data_dir() ) != 0 ) {
00170            /* Print a more informative warning since this is a common error */
00171            handle_system_error( 
00172                1, "Can't find the ETRacer data directory.  Please check the\n"
00173           "value of `data_dir' in ~/.etracer/options and set it to the location where you\n"
00174                "installed the ETRacer-data files.\n\n"
00175                "Couldn't chdir to %s", getparam_data_dir() );
00176     } 
00177 
00178     if ( Tcl_EvalFile( tclInterp, GAME_INIT_SCRIPT) == TCL_ERROR ) {
00179         handle_error( 1, "error running %s/%s: %s\n"
00180                     "Please check the value of `data_dir' in ~/.etracer/options "
00181                     "and make sure it\npoints to the location of the "
00182                     "latest version of the ETRacer-data files.", 
00183                     getparam_data_dir(), GAME_INIT_SCRIPT, 
00184                     Tcl_GetStringResult( tclInterp ) );
00185     } 
00186 
00187     check_assertion( !Tcl_InterpDeleted( tclInterp ),
00188                    "Tcl interpreter deleted" );
00189 
00190     if ( chdir( cwd ) != 0 ) {
00191            handle_system_error( 1, "couldn't chdir to %s", cwd );
00192     } 
00193 }
00194 
00195     
00196 
00197 int main( int argc, char *argv[] ) 
00198 {
00199   /* Print copyright notice */
00200   fprintf( stderr, "Extreme TuxRacer " VERSION " --  http://www.extremetuxracer.com \n"
00201      "(c) 2007-2008 The ETRacer team\n"
00202      "(c) 2004-2005 The PPRacer team\n"
00203      "(c) 1999-2001 Jasmin F. Patry"
00204      "<jfpatry@sunspirestudios.com>\n"
00205      "ETRacer comes with ABSOLUTELY NO WARRANTY. "
00206      "This is free software,\nand you are welcome to redistribute "
00207      "it under certain conditions.\n"
00208      "See http://www.gnu.org/copyleft/gpl.html for details.\n\n" );
00209 
00210        gameMgr = new GameMgr();
00211        Highscore = new highscore();
00212        ModelHndl = new model_hndl();
00213        
00214   /* Seed the random number generator */
00215   srand( time(NULL) );
00216 
00217 
00218   /*
00219    * Set up the game configuration
00220    */
00221 
00222   /* Don't support multiplayer, yet... */
00223   gameMgr->numPlayers = 1;
00224 
00225   /* Create a Tcl interpreter */
00226   tclInterp = Tcl_CreateInterp();
00227 
00228   if ( tclInterp == NULL ) {
00229        handle_error( 1, "cannot create Tcl interpreter" ); 
00230   }
00231 
00232   /* Setup the configuration variables and read the ~/.etracer/options file */
00233     
00234        bool continuePlaying = handleCommandLineOptions(argc,argv);
00235   if(!continuePlaying){
00236     return(0);
00237   }
00238        
00239        init_game_configuration();
00240   read_config_file(configurationFile);
00241 
00242   /* Set up the debugging modes */
00243   init_debug();
00244 
00245   /* Setup diagnostic log if requested */
00246   if ( getparam_write_diagnostic_log() ) {
00247        setup_diagnostic_log();
00248   }
00249 
00250   /*
00251    * Setup Tcl stdout and stderr channels to point to C stdout and stderr 
00252    * streams
00253    */
00254   setup_tcl_std_channels();
00255 
00256   /* 
00257    * Initialize rendering context, create window
00258    */
00259   winsys_init( &argc, argv, WINDOW_TITLE, WINDOW_TITLE );
00260 
00261 
00262   /* Ingore key-repeat messages */
00263   winsys_enable_key_repeat(false);
00264 
00265   /* 
00266    * Initial OpenGL settings 
00267    */
00268   glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00269 
00270   init_opengl_extensions();
00271 
00272   /* Print OpenGL debugging information if requested */
00273   if ( debug_mode_is_active( DEBUG_GL_INFO ) ) {
00274        print_debug( DEBUG_GL_INFO, 
00275             "OpenGL information:" );
00276        print_gl_info();
00277   }
00278 
00279   /* 
00280    * Load the game data and initialize game state
00281    */
00282   register_game_config_callbacks( tclInterp );
00283   register_course_load_tcl_callbacks( tclInterp );
00284   register_key_frame_callbacks( tclInterp );
00285 
00286        FogPlane::registerCallbacks( tclInterp );
00287     
00288        register_course_light_callbacks( tclInterp );
00289   register_particle_callbacks( tclInterp );
00290   register_texture_callbacks( tclInterp );
00291   register_sound_tcl_callbacks( tclInterp );
00292   register_sound_data_tcl_callbacks( tclInterp );
00293   register_course_manager_callbacks( tclInterp );
00294        register_common_callbacks( tclInterp );
00295        
00296        
00297        // Setup class for translation
00298        //translation.getLanguages();
00299        //translation.load( getparam_ui_language() );
00300        setlocale (LC_MESSAGES, "");
00301         setlocale (LC_CTYPE, "");
00302        bindtextdomain (PACKAGE, LOCALEDIR);
00303         textdomain (PACKAGE);
00304        
00305   // Load model
00306   ModelHndl->init_models();
00307   // ModelHndl->load_model(0); Loaded in players[0]::loadData()
00308   
00309   init_textures();
00310   init_audio_data();
00311   init_audio();
00312     
00313        init_course_manager();
00314   init_joystick();
00315        init_ui_snow();
00316 
00317        // Read the etracer_init.tcl file
00318   read_game_init_script();
00319 
00320   // Set a temporary name until user enters another name
00321   players[0].name = "Tux";
00322        players[0].loadData();
00323        
00324        //Ugly hax to prevent from segfault, fix to later version
00325        players[0].saveData();
00326        players[0].loadData();
00327        
00328        
00329        /* Init highscore */
00330        Highscore->loadData();
00331 
00332        /*debug highscore:*/
00333        //Highscore->debug();
00334        //Highscore->printlist();
00335 
00336   GameMode::mode = NO_MODE;
00337        
00338        if(Benchmark::getMode()==Benchmark::NONE){
00339               set_game_mode( SPLASH );
00340        }else{
00341               set_game_mode( BENCHMARK );
00342        }
00343        
00344   gameMgr->difficulty = DIFFICULTY_LEVEL_NORMAL;
00345        
00346        winsys_show_cursor( false );
00347 
00348        /* 
00349    * ...and off we go!
00350    */
00351   winsys_process_events();
00352 
00353   return 0;
00354 }