HBTPlayer.cpp

Go to the documentation of this file.
00001 #include "HBTPlayer.h"
00002 #include "OBT_ASSERT.h"
00003 #include "HBTRecorder.h"
00004 
00005 using namespace HBT ;
00006 
00007 
00008 //=============================================================================
00009 
00010 Player::Player( const std::string& fileName )
00011 : _initRead( false ),
00012   _loop( false ),
00013   _file()
00014 { 
00015   open( fileName ) ;
00016 }
00017 //-----------------------------------------------------------------------------
00018 Player::Player()
00019 : _initRead( false ),
00020   _loop( false ),
00021   _file()
00022 { 
00023 }
00024 //-----------------------------------------------------------------------------
00025 Player::~Player() 
00026 {
00027   _file.close() ;
00028 }
00029 //-----------------------------------------------------------------------------
00030 void Player::open( const std::string& fileName ) 
00031 {
00032   _file.open( fileName.c_str(), std::ios::binary ) ;
00033   _initRead = false ;
00034 }
00035 //-----------------------------------------------------------------------------
00036 void Player::read( std::string& txt )
00037 {
00038   size_t size ;
00039   _file.read( (char*)&size, sizeof(size) ) ;
00040   char* txtTmp = new char[ size ] ;
00041   _file.read( txtTmp, size ) ;
00042   txt = txtTmp ;
00043   delete[] txtTmp ;
00044 }
00045 //-----------------------------------------------------------------------------
00046 void Player::read( bool& flag )
00047 {
00048   char tmp ;
00049   _file.read( &tmp, 1 ) ;
00050   flag = tmp == '1' ;
00051 }
00052 //-----------------------------------------------------------------------------
00053 template< typename T >
00054 void Player::read( T& val )
00055 {
00056   _file.read( (char*)&val, sizeof( T ) ) ;
00057 }
00058 //-----------------------------------------------------------------------------
00059 void Player::read( Ogre::Vector3& vect )
00060 {
00061   _file.read( (char*)&vect.x, sizeof( Ogre::Real ) ) ;
00062   _file.read( (char*)&vect.y, sizeof( Ogre::Real ) ) ;
00063   _file.read( (char*)&vect.z, sizeof( Ogre::Real ) ) ;
00064 }
00065 //-----------------------------------------------------------------------------
00066 void Player::read( Ogre::Quaternion& quat )
00067 {
00068   _file.read( (char*)&quat.w, sizeof( Ogre::Real ) ) ;
00069   _file.read( (char*)&quat.x, sizeof( Ogre::Real ) ) ;
00070   _file.read( (char*)&quat.y, sizeof( Ogre::Real ) ) ;
00071   _file.read( (char*)&quat.z, sizeof( Ogre::Real ) ) ;
00072 }
00073 //-----------------------------------------------------------------------------
00074 template< typename T >
00075 void Player::read( std::vector< T >& tab ) 
00076 {
00077   tab.clear() ;
00078   size_t size ;
00079   _file.read( (char*)&size, sizeof( size_t ) ) ;
00080   for( ; size != 0 ; size-- )
00081   {
00082     T val ;
00083     read( val ) ;
00084     tab.push_back( val ) ;
00085   }
00086 }
00087 
00088 //-----------------------------------------------------------------------------
00089 Player& Player::read( InitialisationSequence& initialisationSequence ) 
00090 {
00091   OBT_ASSERT( _file.is_open() && "The file is not opened" ) ;
00092   // Go to the beginning of the file
00093   _file.seekg( 0 ) ;
00094 
00095   // Tag
00096   std::string header ;
00097   read( header ) ;
00098   size_t length = Recorder::HEADER.length() ;
00099   OBT_ASSERT( header.substr( 0, length ) == Recorder::HEADER && "Not the good header" ) ;
00100   // Version
00101   if( header.substr( length ) == "0" )
00102   {
00103     read( initialisationSequence.meshFile ) ;
00104     read( initialisationSequence.humanName ) ;
00105     read( initialisationSequence.useShadows ) ;
00106     read( initialisationSequence.generalScale ) ;
00107     read( initialisationSequence.rootQuat ) ;
00108     read( initialisationSequence.rootPos ) ;
00109     read( initialisationSequence.boneNumber ) ;
00110     read( initialisationSequence.postureQuat ) ;
00111   }
00112   else
00113   {
00114     OBT_ASSERT( false && "not the good version" ) ; // version
00115   }
00116 
00117   _initRead = true ;
00118   return *this ;
00119 }
00120 //-----------------------------------------------------------------------------
00121 Player& Player::read( unsigned int& date, PostureData& posture ) 
00122 {
00123   OBT_ASSERT( _file.is_open() && "The file is not opened" ) ;
00124   OBT_ASSERT( _initRead && "You must read InitialisationSequence before reading the posture data" ) ;
00125   _file.peek() ;
00126   if( !_file.eof() )
00127   {
00128     read( _lastDate ) ; 
00129     read( _lastPosture.rootPos ) ; 
00130     read( _lastPosture.rootQuat ) ;    
00131     read( _lastPosture.generalScale ) ;
00132     read( _lastPosture.postureQuat ) ; 
00133     read( _lastPosture.quatModified ) ;
00134   }
00135   else if( _loop )
00136   {
00137     _file.clear() ;
00138     InitialisationSequence initialisationSequence ;
00139     read( initialisationSequence ) ;
00140   }
00141   date = _lastDate ;
00142   posture = _lastPosture ;
00143   return *this ;
00144 }
00145 //-----------------------------------------------------------------------------
00146 

logo OpenMask

Documentation generated on Mon Jun 9 11:45:55 2008

Generated with doxygen by Dimitri van Heesch ,   1997-2007