Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages

animator.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                animator.h  -  Template base for animator controllers
00003                              -------------------
00004     begin                : Sun Jan 26 2003
00005     copyright            : (C) 2003 by Reality Rift Studios
00006     email                : mattias@realityrift.com
00007  ***************************************************************************
00008 
00009  The contents of this file are subject to the Mozilla Public License Version
00010  1.1 (the "License"); you may not use this file except in compliance with
00011  the License. You may obtain a copy of the License at 
00012  http://www.mozilla.org/MPL/
00013 
00014  Software distributed under the License is distributed on an "AS IS" basis,
00015  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  for the specific language governing rights and limitations under the
00017  License.
00018 
00019  The Original Code is the NeoEngine, animator.h
00020 
00021  The Initial Developer of the Original Code is Mattias Jansson.
00022  Portions created by Mattias Jansson are Copyright (C) 2003
00023  Reality Rift Studios. All Rights Reserved.
00024 
00025  ***************************************************************************/
00026 
00027 #ifndef __NEANIMATOR_H
00028 #define __NEANIMATOR_H
00029 
00030 
00031 #include "base.h"
00032 #include "updateentity.h"
00033 #include "hashstring.h"
00034 
00035 #include <vector>
00036 
00037 
00046 namespace NeoEngine
00047 {
00048 
00049 
00054 template <class AnimationType> class AnimatorController : public virtual UpdateEntity
00055 {
00056     protected:
00057 
00059         std::vector< AnimationType* >         m_vpkAnimations;
00060 
00062         unsigned int                          m_uiCurAnim;
00063 
00065         AnimationType                        *m_pkCurAnim;
00066 
00067 
00068     public:
00069 
00072                                               AnimatorController() : m_vpkAnimations(), m_uiCurAnim( 0 ), m_pkCurAnim( 0 ) {}
00073 
00078                                               AnimatorController( const AnimatorController<AnimationType> &rkController )
00079         {
00080             m_vpkAnimations.resize( rkController.m_vpkAnimations.size() );
00081 
00082             typename std::vector< AnimationType* >::iterator       ppkDest = m_vpkAnimations.begin();
00083             typename std::vector< AnimationType* >::const_iterator ppkAnim = rkController.m_vpkAnimations.begin();
00084             typename std::vector< AnimationType* >::const_iterator ppkEnd  = rkController.m_vpkAnimations.end();
00085 
00086             for( ; ppkAnim != ppkEnd; ++ppkAnim, ++ppkDest )
00087                 *ppkDest = new AnimationType( *(*ppkAnim) );
00088 
00089             if( ( m_uiCurAnim = rkController.m_uiCurAnim ) < m_vpkAnimations.size() )
00090                 m_pkCurAnim = m_vpkAnimations[ m_uiCurAnim ];
00091             else
00092                 m_pkCurAnim = 0;
00093         }
00094 
00098         virtual                              ~AnimatorController()
00099         {
00100             typename std::vector< AnimationType* >::iterator ppkAnim = m_vpkAnimations.begin();
00101             typename std::vector< AnimationType* >::iterator ppkEnd  = m_vpkAnimations.end();
00102 
00103             for( ; ppkAnim != ppkEnd; ++ppkAnim )
00104                 delete( *ppkAnim );
00105         }
00106 
00112         inline virtual bool                   AddAnimation( AnimationType *pkAnimation )
00113         {
00114             typename std::vector< AnimationType* >::iterator ppkAnim = m_vpkAnimations.begin();
00115             typename std::vector< AnimationType* >::iterator ppkEnd  = m_vpkAnimations.end();
00116 
00117             for( ; ppkAnim != ppkEnd; ++ppkAnim )
00118             {
00119                 if( (*ppkAnim) == pkAnimation )
00120                     return true;
00121 
00122                 if( ( (*ppkAnim)->m_uiID == pkAnimation->m_uiID ) || ( (*ppkAnim)->m_strName == pkAnimation->m_strName ) )
00123                     return false;
00124             }
00125 
00126             m_vpkAnimations.push_back( pkAnimation );
00127 
00128             if( m_vpkAnimations.size() == 1 )
00129                 SetCurrentAnimation( pkAnimation->m_uiID );
00130 
00131             return true;
00132         }
00133 
00139         inline virtual bool                   SetCurrentAnimation( unsigned int uiID )
00140         {
00141             typename std::vector< AnimationType* >::iterator ppkAnim = m_vpkAnimations.begin();
00142             typename std::vector< AnimationType* >::iterator ppkEnd  = m_vpkAnimations.end();
00143 
00144             for( unsigned int i = 0; ppkAnim != ppkEnd; ++ppkAnim, ++i )
00145             {
00146                 if( (*ppkAnim)->m_uiID == uiID )
00147                 {
00148                     m_uiCurAnim = i;
00149                     m_pkCurAnim = (*ppkAnim);
00150 
00151                     m_pkCurAnim->m_fCurTime = 0.0f;
00152 
00153                     return true;
00154                 }
00155             }
00156 
00157             return false;
00158         }
00159 
00165         inline virtual bool                   SetCurrentAnimation( const HashString &rstrName )
00166         {
00167             typename std::vector< AnimationType* >::iterator ppkAnim = m_vpkAnimations.begin();
00168             typename std::vector< AnimationType* >::iterator ppkEnd  = m_vpkAnimations.end();
00169 
00170             for( unsigned int i = 0; ppkAnim != ppkEnd; ++ppkAnim, ++i )
00171             {
00172                 if( (*ppkAnim)->m_strName == rstrName )
00173                 {
00174                     m_uiCurAnim = i;
00175                     m_pkCurAnim = (*ppkAnim);
00176 
00177                     m_pkCurAnim->m_fCurTime = 0.0f;
00178 
00179                     return true;
00180                 }
00181             }
00182 
00183             return false;
00184         }
00185 
00190         virtual void                          Update( float fDeltaTime )
00191         {
00192             if( m_pkCurAnim )
00193                 m_pkCurAnim->Update( fDeltaTime );
00194         }
00195 
00199         const std::vector< AnimationType* >  &GetAnimations() const { return m_vpkAnimations; }
00200 
00204         AnimationType                        *GetCurrentAnimation() { return m_pkCurAnim; }
00205 };
00206 
00207 
00208 }; // namespace NeoEngine
00209 
00210 
00211 #endif

Generated on Wed Jan 21 14:21:06 2004 for NeoEngine by doxygen 1.3.5