Back to index

extremetuxracer  0.5beta
reset.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 "reset.h"
00023 
00024 
00025 #include "game_config.h"
00026 #include "ppgltk/audio/audio.h"
00027 #include "ppgltk/alg/defs.h"
00028 
00029 #include "gl_util.h"
00030 #include "fps.h"
00031 #include "render_util.h"
00032 #include "phys_sim.h"
00033 #include "view.h"
00034 #include "course_render.h"
00035 #include "model_hndl.h"
00036 #include "tux_shadow.h"
00037 #include "loop.h"
00038 #include "fog.h"
00039 #include "viewfrustum.h"
00040 #include "hud.h"
00041 #include "course_load.h"
00042 #include "winsys.h"
00043 
00044 #include "stuff.h"
00045 
00046 #include "game_mgr.h"
00047 
00048 #include "ppgltk/ppgltk.h"
00049 
00050 
00051 #define BLINK_IN_PLACE_TIME 0.5
00052 #define TOTAL_RESET_TIME 1.0
00053 
00054 Reset::Reset()
00055  : m_textColor(0.0, 0.0, 0.0),
00056    m_resetStartTime(getClockTime()),
00057    m_positionReset(false)
00058 {
00059 }
00060 
00061 Reset::~Reset()
00062 {
00063        
00064 }
00065 
00066 void
00067 Reset::loop(float timeStep)
00068 {
00069        int width, height;
00070     float elapsed_time = getClockTime() - m_resetStartTime;
00071     float course_width, course_length;
00072     static bool tux_visible = true; 
00073     static int tux_visible_count = 0;
00074     item_type_t  *item_types;
00075     Item       *item_locs;
00076     int  i, first_reset, last_reset, num_item_types;
00077     int best_loc;
00078 
00079     width = getparam_x_resolution();
00080     height = getparam_y_resolution();
00081 
00082     fpsCounter.update();
00083 
00084     update_audio();
00085 
00086     clear_rendering_context();
00087 
00088     fogPlane.setup();
00089 
00090     update_player_pos( players[0], EPS );
00091        
00092     update_view( players[0], EPS );
00093 
00094     setup_view_frustum( players[0], NEAR_CLIP_DIST, 
00095                      getparam_forward_clip_distance() );
00096 
00097     draw_sky(players[0].view.pos);
00098 
00099     draw_fog_plane();
00100 
00101     set_course_clipping( true );
00102     set_course_eye_point( players[0].view.pos );
00103     setup_course_lighting();
00104     render_course();
00105     draw_trees();
00106 
00107     if ((elapsed_time > BLINK_IN_PLACE_TIME) && (!m_positionReset)) {
00108        item_types = get_item_types();
00109        item_locs  = get_item_locs();
00110        num_item_types = get_num_item_types();
00111        first_reset = 0;
00112        last_reset = 0;
00113        for ( i = 0; i < num_item_types; i++ ) {
00114            if (item_types[i].reset_point == true) {
00115               last_reset = first_reset + item_types[i].num_items - 1;
00116               break;
00117            } else {
00118               first_reset += item_types[i].num_items;
00119            }
00120        }
00121 
00122        if (last_reset == 0) {
00123            // didn't find a reset point item type 
00124            get_course_dimensions( &course_width, &course_length );
00125            players[0].pos.x = course_width/2.0;
00126            players[0].pos.z = MIN(players[0].pos.z + 10, -1.0);
00127        } else {
00128            // BFI 
00129            best_loc = -1;
00130            for ( i = first_reset; i <= last_reset; i++) {
00131               if (item_locs[i].ray.pt.z > players[0].pos.z ) { 
00132                   if (best_loc == -1 || 
00133                      item_locs[i].ray.pt.z < item_locs[best_loc].ray.pt.z)
00134                   {
00135                      best_loc = i;
00136                   }
00137               }
00138            }
00139 
00140            if ( best_loc == -1 ) {
00141               get_course_dimensions( &course_width, &course_length );
00142               players[0].pos.x = course_width/2.0;
00143               players[0].pos.z = MIN(players[0].pos.z + 10, -1.0);
00144            } else if ( item_locs[best_loc].ray.pt.z <= players[0].pos.z ) {
00145               get_course_dimensions( &course_width, &course_length );
00146               players[0].pos.x = course_width/2.0;
00147               players[0].pos.z = MIN(players[0].pos.z + 10, -1.0);
00148            } else {
00149               players[0].pos.x = item_locs[best_loc].ray.pt.x;
00150               players[0].pos.z = item_locs[best_loc].ray.pt.z;
00151            }
00152        }
00153 
00154        // Re-initialize the camera 
00155        players[0].view.initialized = false;
00156 
00157        init_physical_simulation();
00158        m_positionReset = true;
00159     }
00160 
00161     if (tux_visible) { 
00162        ModelHndl->draw_tux();
00163        draw_tux_shadow();
00164     } 
00165     if (++tux_visible_count > 3) {
00166        tux_visible = (bool) !tux_visible;
00167        tux_visible_count = 0;
00168     }
00169 
00170     HUD1.draw(players[0]);
00171 
00172     reshape( width, height );
00173 
00174     winsys_swap_buffers();
00175 
00176     gameMgr->time += timeStep;
00177 
00178     if (elapsed_time > TOTAL_RESET_TIME) {
00179               set_game_mode( RACING );
00180               winsys_post_redisplay();
00181     }  
00182 }