# 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_set.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_set.h"
00029 #include "nel/misc/stream.h"
00030 #include "nel/misc/path.h"
00031 #include "nel/misc/file.h"
00032 
00033 
00034 #include <memory>
00035 
00036 
00037 
00038 namespace NL3D 
00039 {
00040 
00041 // ***************************************************************************
00042 
00043 CAnimationSet::~CAnimationSet ()
00044 {
00045         // Erase all animations.
00046         for (uint a=0; a<_Animation.size(); a++)
00047                 delete _Animation[a];
00048         for (uint s=0; s<_SkeletonWeight.size(); s++)
00049                 delete _SkeletonWeight[s];
00050 }
00051 
00052 // ***************************************************************************
00053 
00054 uint CAnimationSet::getNumChannelId () const
00055 {
00056         return _ChannelIdByName.size ();
00057 }
00058 
00059 // ***************************************************************************
00060 
00061 uint CAnimationSet::addAnimation (const char* name, CAnimation* animation)
00062 {
00063         // Add an animation
00064         _Animation.push_back (animation);
00065         _AnimationName.push_back (name);
00066 
00067         // Add an entry name / animation
00068         _AnimationIdByName.insert (std::map <std::string, uint32>::value_type (name, _Animation.size()-1));
00069 
00070         // Return animation id
00071         return _Animation.size()-1;
00072 }
00073 
00074 // ***************************************************************************
00075 
00076 uint CAnimationSet::addSkeletonWeight (const char* name, CSkeletonWeight* skeletonWeight)
00077 {
00078         // Add an animation
00079         _SkeletonWeight.push_back (skeletonWeight);
00080         _SkeletonWeightName.push_back (name);
00081 
00082         // Add an entry name / animation
00083         _SkeletonWeightIdByName.insert (std::map <std::string, uint32>::value_type (name, _SkeletonWeight.size()-1));
00084 
00085         // Return animation id
00086         return _SkeletonWeight.size()-1;
00087 }
00088 
00089 // ***************************************************************************
00090 
00091 void CAnimationSet::reset ()
00092 {
00093         _Animation.clear();
00094         _SkeletonWeight.clear();
00095         _AnimationName.clear();
00096         _SkeletonWeightName.clear();
00097         _ChannelIdByName.clear();
00098         _AnimationIdByName.clear();
00099         _SkeletonWeightIdByName.clear();
00100 }
00101 
00102 // ***************************************************************************
00103 
00104 void CAnimationSet::build ()
00105 {
00106         // Clear the channel map
00107         _ChannelIdByName.clear ();
00108 
00109         // Set of names
00110         std::set<std::string> channelNames;
00111 
00112         // For each animation in the set
00113         for (uint a=0; a<_Animation.size(); a++)
00114         {
00115                 // Fill the set of channel names
00116                 getAnimation (a)->getTrackNames (channelNames);
00117         }
00118 
00119         // Add this name in the map with there iD
00120         uint id=0;
00121         std::set<std::string>::iterator ite=channelNames.begin ();
00122         while (ite!=channelNames.end ())
00123         {
00124                 // Insert an entry
00125                 _ChannelIdByName.insert (std::map <std::string, uint32>::value_type (*ite, id++));
00126 
00127                 // Next entry
00128                 ite++;
00129         }
00130 }
00131 
00132 // ***************************************************************************
00133 
00134 void CAnimationSet::serial (NLMISC::IStream& f)
00135 {
00136         // Serial an header
00137         f.serialCheck ((uint32)'_LEN');
00138         f.serialCheck ((uint32)'MINA');
00139         f.serialCheck ((uint32)'TES_');
00140 
00141         // Serial a version
00142         (void)f.serialVersion (0);
00143 
00144         // Serial the class
00145         f.serialContPtr (_Animation);
00146         f.serialContPtr (_SkeletonWeight);
00147         f.serialCont (_AnimationName);
00148         f.serialCont (_SkeletonWeightName);
00149         f.serialCont(_ChannelIdByName);
00150         f.serialCont(_AnimationIdByName);
00151         f.serialCont(_SkeletonWeightIdByName);
00152 }
00153 
00154 // ***************************************************************************
00155 bool CAnimationSet::loadFromFiles(const std::string &path, bool recurse /* = true*/, const char *ext /*= "anim"*/, bool wantWarningMessage /*= true*/)
00156 {
00157         bool everythingOk = true;
00158         std::vector<std::string> anims;
00159         NLMISC::CPath::getPathContent(path, recurse, false, true, anims);
00160         for (uint k = 0; k < anims.size(); ++k)
00161         {
00162                 std::string fileExt = NLMISC::CFile::getExtension(anims[k]);
00163                 if (fileExt == ext) // an animation file ?
00164                 {
00165                         try
00166                         {
00167                                 NLMISC::CIFile  iFile;
00168                                 iFile.open(anims[k]);
00169                                 std::auto_ptr<CAnimation> anim(new CAnimation);
00170                                 anim->serial(iFile);
00171                                 addAnimation(NLMISC::CFile::getFilenameWithoutExtension(anims[k]).c_str(), anim.release());
00172                                 iFile.close();
00173 
00174                         }
00175                         catch (NLMISC::EStream &e)
00176                         {
00177                                 if (wantWarningMessage)
00178                                 {
00179                                         nlinfo("Unable to load an automatic animation : %s", e.what());
00180                                 }
00181                                 everythingOk = false;
00182                         }
00183                 }
00184         }
00185         build();
00186         return everythingOk;
00187 }
00188 
00189 
00190 } // NL3D