Back to index

supertuxkart  0.5+dfsg1
replay_recorder.cpp
Go to the documentation of this file.
00001 //  $Id$
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2007 Maik Semder <ikework@gmx.de>
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #ifdef HAVE_GHOST_REPLAY
00021 
00022 #include <cassert>
00023 
00024 #include "replay_recorder.hpp"
00025 #include "world.hpp"
00026 
00027 const float ReplayRecorder::REPLAY_TIME_STEP_MIN = 1.0f / (float)ReplayRecorder::REPLAY_FREQUENCY_MAX;
00028 
00029 ReplayRecorder::ReplayRecorder()
00030 : ReplayBase()
00031 {
00032 }
00033 
00034 ReplayRecorder::~ReplayRecorder()
00035 {
00036     destroy();
00037 }
00038 
00039 void ReplayRecorder::destroy()
00040 {
00041     ReplayBase::destroy();
00042 }
00043 
00044 bool ReplayRecorder::initRecorder( unsigned int number_karts, size_t number_preallocated_frames )
00045 {
00046     assert( number_karts );
00047 
00048     destroy();
00049 
00050     if( !m_ReplayBuffers.init( number_karts, number_preallocated_frames ) ) return false;
00051 
00052     return true;
00053 }
00054 
00055 bool ReplayRecorder::pushFrame()
00056 {
00057        // we dont record the startphase ..
00058     assert( world->getPhase() != World::START_PHASE );
00059     assert( world->getNumKarts() == m_ReplayBuffers.getNumberKarts() );
00060 
00061     // make sure we're not under time-step-min 
00062     if( m_ReplayBuffers.getNumberFrames() )
00063     {
00064         ReplayFrame const *last_Frame = m_ReplayBuffers.getFrameAt( m_ReplayBuffers.getNumberFrames() - 1 );
00065         if( (world->getTime() - last_Frame->time) < REPLAY_TIME_STEP_MIN ) return true;
00066     }
00067 
00068     ReplayFrame *pFrame = getNewFrame();
00069     if( !pFrame ) return false;
00070     pFrame->time = world->getClock();
00071 
00072     Kart const *kart;
00073     int number_karts = world->getNumKarts();
00074     for( int kart_index = 0; kart_index < number_karts; ++kart_index )
00075     {
00076         kart = world->getKart( kart_index );
00077         sgCopyCoord( &( pFrame->p_kart_states[ kart_index ].position ), 
00078                      kart->getCoord() );
00079     }
00080 
00081     return true;
00082 }
00083 
00084 
00085 #endif // HAVE_GHOST_REPLAY