# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

animation.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "std3d.h"
00027 
00028 #include "3d/animation.h"
00029 #include "3d/track.h"
00030 
00031 #include "nel/misc/file.h"
00032 #include "nel/misc/path.h"
00033 #include "nel/misc/hierarchical_timer.h"
00034 
00035 namespace NL3D 
00036 {
00037 
00038 H_AUTO_DECL( NL3D_UI_Animation )
00039 
00040 #define NL3D_HAUTO_UI_ANIMATION                                         H_AUTO_USE( NL3D_UI_Animation )
00041 
00042 
00043 // ***************************************************************************
00044 
00045 CAnimation::CAnimation() : _BeginTimeTouched(true), _EndTimeTouched(true), _AnimLoopTouched(true)
00046 {       
00047         NL3D_MEM_ANIMATION
00048         _MinEndTime = -FLT_MAX;
00049 }
00050 
00051 // ***************************************************************************
00052 
00053 CAnimation::~CAnimation ()
00054 {
00055         NL3D_MEM_ANIMATION
00056         // Delete all the pointers in the array
00057         for (uint i=0; i<_TrackVector.size(); i++)
00058                 // Delete
00059                 delete _TrackVector[i];
00060 }
00061 
00062 // ***************************************************************************
00063 
00064 void CAnimation::addTrack (const std::string& name, ITrack* pChannel)
00065 {
00066         NL3D_MEM_ANIMATION
00067         // Add an entry in the map
00068         _IdByName.insert (TMapStringUInt::value_type (name, (uint32)_TrackVector.size()));
00069 
00070         // Add an entry in the array
00071         _TrackVector.push_back (pChannel);
00072 
00073         // 
00074         _BeginTimeTouched = _EndTimeTouched = _AnimLoopTouched= true;
00075 
00076 }
00077 
00078 // ***************************************************************************
00079 
00080 void CAnimation::serial (NLMISC::IStream& f)
00081 {
00082         NL3D_MEM_ANIMATION
00083         // Serial a header
00084         f.serialCheck ((uint32)'_LEN');
00085         f.serialCheck ((uint32)'MINA');
00086 
00087         // Serial a version
00088         sint version=f.serialVersion (1);
00089 
00090         // Serial the name
00091         f.serial (_Name);
00092 
00093         // Serial the name/id map
00094         f.serialCont(_IdByName);
00095 
00096         // Serial the vector
00097         f.serialContPolyPtr (_TrackVector);
00098 
00099         // Serial the min end time
00100         if (version>=1)
00101         {
00102                 f.serial (_MinEndTime);
00103         }
00104 }
00105 
00106 // ***************************************************************************
00107 
00108 void CAnimation::getTrackNames (std::set<std::string>& setString) const
00109 {
00110         NL3D_MEM_ANIMATION
00111         // For each track name
00112         TMapStringUInt::const_iterator ite=_IdByName.begin();
00113         while (ite!=_IdByName.end())
00114         {
00115                 // Add the name in the map
00116                 setString.insert (ite->first);
00117 
00118                 // Next track
00119                 ite++;
00120         }
00121 }
00122 
00123 // ***************************************************************************
00124 
00125 TAnimationTime CAnimation::getBeginTime () const
00126 {
00127         NL3D_MEM_ANIMATION
00128         NL3D_HAUTO_UI_ANIMATION;
00129 
00130         if (_BeginTimeTouched)
00131         {
00132                 // Track count
00133                 uint trackCount=_TrackVector.size();
00134 
00135                 // Track count empty ?
00136                 if (trackCount==0)
00137                         return 0.f;
00138 
00139                 // Look for the lowest
00140                 _BeginTime=_TrackVector[0]->getBeginTime ();
00141 
00142                 // Scan all keys
00143                 for (uint t=1; t<trackCount; t++)
00144                 {
00145                         if (_TrackVector[t]->getBeginTime ()<_BeginTime)
00146                                 _BeginTime=_TrackVector[t]->getBeginTime ();
00147                 }
00148 
00149                 _BeginTimeTouched = false;
00150         }
00151 
00152         return _BeginTime;
00153 }
00154 
00155 // ***************************************************************************
00156 
00157 TAnimationTime CAnimation::getEndTime () const
00158 {
00159         NL3D_MEM_ANIMATION
00160         NL3D_HAUTO_UI_ANIMATION;
00161 
00162         if (_EndTimeTouched)
00163         {
00164                 // Track count
00165                 uint trackCount=_TrackVector.size();
00166 
00167                 // Track count empty ?
00168                 if (trackCount==0)
00169                         return 0.f;
00170 
00171                 // Look for the highest
00172                 _EndTime=_TrackVector[0]->getEndTime ();
00173 
00174                 // Scan tracks keys
00175                 for (uint t=1; t<trackCount; t++)
00176                 {
00177                         if (_TrackVector[t]->getEndTime ()>_EndTime)
00178                                 _EndTime=_TrackVector[t]->getEndTime ();
00179                 }
00180                 
00181                 // Check min end time
00182                 if (_EndTime < _MinEndTime)
00183                         _EndTime = _MinEndTime;
00184 
00185                 _EndTimeTouched = false;
00186         }
00187 
00188         return _EndTime;
00189 }
00190 
00191 // ***************************************************************************
00192 bool                    CAnimation::allTrackLoop() const
00193 {
00194         NL3D_MEM_ANIMATION
00195         NL3D_HAUTO_UI_ANIMATION;
00196 
00197         if(_AnimLoopTouched)
00198         {
00199                 // Track count
00200                 uint trackCount=_TrackVector.size();
00201 
00202                 // Default is true
00203                 _AnimLoop= true;
00204 
00205                 // Scan tracks keys
00206                 for (uint t=0; t<trackCount; t++)
00207                 {
00208                         if (!_TrackVector[t]->getLoopMode())
00209                         {
00210                                 _AnimLoop= false;
00211                                 break;
00212                         }
00213                 }
00214                 _AnimLoopTouched = false;
00215         }
00216 
00217         return _AnimLoop;
00218 }
00219 
00220 // ***************************************************************************
00221 
00222 UTrack* CAnimation::getTrackByName (const char* name)
00223 {
00224         NL3D_MEM_ANIMATION
00225         NL3D_HAUTO_UI_ANIMATION;
00226 
00227         // Get track id
00228         uint id=getIdTrackByName (name);
00229 
00230         // Not found ?
00231         if (id==CAnimation::NotFound)
00232                 // Error, return NULL
00233                 return NULL;
00234         else
00235                 // No error, return the track
00236                 return getTrack (id);
00237 }
00238 
00239 // ***************************************************************************
00240 
00241 void CAnimation::releaseTrack (UTrack* track)
00242 {
00243         NL3D_MEM_ANIMATION
00244         NL3D_HAUTO_UI_ANIMATION;
00245 
00246         // Nothing to do
00247 }
00248 
00249 // ***************************************************************************
00250 
00251 void CAnimation::setMinEndTime (TAnimationTime minEndTime)
00252 {
00253         NL3D_MEM_ANIMATION
00254         _MinEndTime = minEndTime;
00255 }
00256 
00257 // ***************************************************************************
00258 
00259 UAnimation* UAnimation::createAnimation (const char* sPath)
00260 {
00261         NL3D_MEM_ANIMATION
00262         NL3D_HAUTO_UI_ANIMATION;
00263 
00264         // Allocate an animation
00265         std::auto_ptr<CAnimation> anim (new CAnimation);
00266 
00267         // Read it
00268         NLMISC::CIFile file;
00269         if (file.open ( NLMISC::CPath::lookup( sPath ) ) )
00270         {
00271                 // Serial the animation
00272                 file.serial (*anim);
00273 
00274                 // Return pointer
00275                 CAnimation *ret=anim.release ();
00276 
00277                 // Return the animation interface
00278                 return ret;
00279         }
00280         else 
00281                 return NULL;
00282 }
00283 
00284 // ***************************************************************************
00285 
00286 void UAnimation::releaseAnimation (UAnimation* animation)
00287 {
00288         NL3D_MEM_ANIMATION
00289         NL3D_HAUTO_UI_ANIMATION;
00290 
00291         // Cast the pointer
00292         CAnimation* release=(CAnimation*)animation;
00293 
00294         // Delete it
00295         delete release;
00296 }
00297 
00298 // ***************************************************************************
00299 
00300 } // NL3D