# 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  

sound_anim_manager.cpp

Go to the documentation of this file.
00001 
00008 /* Copyright, 2000 Nevrax Ltd.
00009  *
00010  * This file is part of NEVRAX NEL.
00011  * NEVRAX NEL is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2, or (at your option)
00014  * any later version.
00015 
00016  * NEVRAX NEL is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00019  * General Public License for more details.
00020 
00021  * You should have received a copy of the GNU General Public License
00022  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00023  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00024  * MA 02111-1307, USA.
00025  */
00026 
00027 #include "stdsound.h"
00028 #include "nel/sound/sound_anim_manager.h"
00029 #include "nel/sound/sound_animation.h"
00030 //#include "nel/sound/sound_anim_player.h"
00031 #include "nel/misc/common.h"
00032 #include "nel/misc/path.h"
00033 
00034 using namespace std;
00035 using namespace NLSOUND;
00036 using namespace NLMISC;
00037 
00038 namespace NLSOUND {
00039 
00040 CSoundAnimManager*      CSoundAnimManager::_Instance = 0;
00041 
00042 // ********************************************************
00043 
00044 CSoundAnimManager::CSoundAnimManager(NLSOUND::UAudioMixer* mixer) : _Mixer(mixer) 
00045 {
00046         if (_Instance != 0)
00047         {
00048                 throw Exception("Duplicate instanciation of CSoundAnimManager singleton");
00049         }
00050 
00051         _Instance = this;
00052         //_PlayerId = 0;
00053 }
00054 
00055 // ********************************************************
00056 
00057 CSoundAnimManager::~CSoundAnimManager()
00058 {
00059         /*
00060         set<CSoundAnimPlayer*>::iterator iter;
00061 
00062         for (iter = _Players.begin(); iter != _Players.end(); iter++)
00063         {
00064                 CSoundAnimPlayer* player = *iter; 
00065                 delete player;
00066         }
00067 
00068         _Players.clear();
00069         */
00070 
00071         _Instance = NULL;
00072 }
00073 
00074 // ********************************************************
00075 
00076 TSoundAnimId CSoundAnimManager::loadAnimation(std::string& name)
00077 {
00078         nlassert(!name.empty());
00079 
00080         string filename;
00081         if (name.find(".anim") != name.npos)
00082         {
00083                 filename = CFile::getFilenameWithoutExtension(name);
00084                 filename.append(".sound_anim");
00085         }
00086         else
00087         {
00088                 filename = name;
00089                 filename.append(".sound_anim");
00090         }
00091 
00092         // throws exception if file not found
00093         filename = CPath::lookup(filename, false, false, true);
00094         if (filename.empty())
00095         {
00096                 return CSoundAnimationNoId;
00097         }
00098 
00099         TSoundAnimId id = createAnimation(name);
00100         if (id == CSoundAnimationNoId)
00101         {
00102                 return CSoundAnimationNoId;
00103         }
00104 
00105         CSoundAnimation* anim = _Animations[id];
00106         
00107         if (anim == NULL)
00108                 return CSoundAnimationNoId;
00109 
00110         anim->setFilename(filename);
00111         anim->load();
00112 
00113         return id;
00114 }
00115 
00116 // ********************************************************
00117 
00118 TSoundAnimId CSoundAnimManager::createAnimation(std::string& name)
00119 {
00120         nlassert(!name.empty());
00121 
00122         // create and insert animations
00123         TSoundAnimId id = _Animations.size();
00124         CSoundAnimation* anim = new CSoundAnimation(name, id);
00125         _Animations.push_back(anim);
00126 
00127         // insert the name and id in the id table
00128         pair<TSoundAnimMap::iterator, bool> inserted;
00129         inserted =_IdMap.insert(make_pair(anim->getName().c_str(), id));
00130         if (!inserted.second) 
00131         {
00132                 nlwarning("Duplicate sound animation \"%s\"", name.c_str());
00133                 delete anim;
00134                 return CSoundAnimationNoId;
00135         }
00136 
00137         return id;
00138 }
00139 
00140 // ********************************************************
00141 
00142 CSoundAnimation* CSoundAnimManager::findAnimation(std::string& name)
00143 {
00144         TSoundAnimMap::iterator iter = _IdMap.find(name.c_str());
00145         return (iter == _IdMap.end())? 0 : _Animations[(*iter).second]; 
00146 }
00147 
00148 // ********************************************************
00149 
00150 TSoundAnimId CSoundAnimManager::getAnimationFromName(std::string& name)
00151 {
00152         TSoundAnimMap::iterator iter = _IdMap.find(name.c_str());
00153         return (iter == _IdMap.end())? CSoundAnimationNoId : (*iter).second;    
00154 }
00155 
00156 // ********************************************************
00157 
00158 void CSoundAnimManager::saveAnimation(CSoundAnimation* anim, std::string& filename)
00159 {
00160         nlassert(anim);
00161         nlassert(!filename.empty());
00162 
00163         anim->setFilename(filename);
00164         anim->save ();
00165 }
00166 
00167 // ********************************************************
00168 /*
00169 TSoundAnimPlayId CSoundAnimManager::playAnimation(TSoundAnimId id, float time, CVector* position)
00170 {
00171         nlassert(id != CSoundAnimationNoId);
00172         nlassert((uint32) id < _Animations.size());
00173         nlassert(position);
00174 
00175         CSoundAnimation* anim = _Animations[id];
00176         nlassert(anim);
00177 
00178 
00179         _PlayerId++;
00180         CSoundAnimPlayer* player = new CSoundAnimPlayer(anim, time, position, _Mixer, _PlayerId);
00181         nlassert(player);
00182 
00183         _Players.insert(player);
00184 
00185         return _PlayerId;
00186 }
00187 */
00188 
00189 struct TFindId : std::unary_function<TSoundAnimMap::value_type, bool>
00190 {
00191         TSoundAnimId    Id;
00192 
00193         TFindId(TSoundAnimId id)
00194                 : Id(id)
00195         {}
00196 
00197         bool operator () (const TSoundAnimMap::value_type &value) const
00198         {
00199                 return value.second == Id;
00200         }
00201 };
00202 
00203 std::string     CSoundAnimManager::idToName(TSoundAnimId id)
00204 {
00205         static string empty;
00206         TSoundAnimMap::iterator it(std::find_if(_IdMap.begin(), _IdMap.end(), TFindId(id)));
00207 
00208         if (it != _IdMap.end())
00209                 return it->first;
00210         else
00211                 return empty;
00212 }
00213 
00214 
00215 void CSoundAnimManager::playAnimation(TSoundAnimId id, float lastTime, float curTime, CSoundContext &context)
00216 {
00217         //nlassert(id != CSoundAnimationNoId);
00218         if (id == CSoundAnimationNoId) 
00219         {
00220                 return;
00221         }
00222 
00223         nlassert((uint32) id < _Animations.size());
00224 
00225         CSoundAnimation* anim = _Animations[id];
00226         nlassert(anim);
00227 
00228         anim->play(_Mixer, lastTime, curTime, context);
00229 }
00230 
00231 
00232 // ********************************************************
00233 TSoundAnimPlayId                CSoundAnimManager::playAnimation(TSoundAnimId id, float time, CSoundContext &context)
00234 {
00235         return 0;
00236 }
00237 
00238 // ********************************************************
00239 TSoundAnimPlayId CSoundAnimManager::playAnimation(string& name, float time, CSoundContext &context)
00240 {
00241 /*      nlassert(position);
00242 
00243         TSoundAnimId id = getAnimationFromName(name);
00244         return (id == CSoundAnimation::NoId)? -1 : _PlayerId;*/
00245         return 0; 
00246 }
00247 
00248 
00249 // ********************************************************
00250 void CSoundAnimManager::stopAnimation(TSoundAnimPlayId playbackId)
00251 {
00252 /*      nlassert(playbackId >= 0);
00253 
00254         set<CSoundAnimPlayer*>::iterator iter;
00255 
00256         for (iter = _Players.begin(); iter != _Players.end(); )
00257         {
00258                 CSoundAnimPlayer* player = *iter; 
00259                 if (player->getId() == playbackId)
00260                 {
00261                         _Players.erase(iter);
00262                         break;
00263                 }
00264         }*/
00265 }
00266 
00267 // ********************************************************
00268 bool CSoundAnimManager::isPlaying(TSoundAnimPlayId playbackId)
00269 {
00270         /*nlassert(playbackId >= 0);
00271 
00272         set<CSoundAnimPlayer*>::iterator iter;
00273 
00274         for (iter = _Players.begin(); iter != _Players.end(); )
00275         {
00276                 CSoundAnimPlayer* player = *iter; 
00277                 if (player->getId() == playbackId)
00278                 {
00279                         return player->isPlaying();
00280                 }
00281         }*/
00282 
00283         return false;
00284 }
00285 
00286 // ********************************************************
00287 void CSoundAnimManager::update(float lastTime, float curTime)
00288 {
00289 /*      set<CSoundAnimPlayer*>::iterator iter;
00290 
00291         _Garbage.clear();
00292 
00293         for (iter = _Players.begin(); iter != _Players.end(); iter++)
00294         {
00295                 CSoundAnimPlayer* player = *iter; 
00296                 if (player->isPlaying())
00297                 {
00298                         player->update(lastTime, curTime);
00299                 }
00300                 else
00301                 {
00302                         _Garbage.push_back(player);
00303                 }
00304         }
00305 
00306         vector<CSoundAnimPlayer*>::iterator iter2;
00307 
00308         for (iter2 = _Garbage.begin(); iter2 != _Garbage.end(); iter2++)
00309         {
00310                 iter = _Players.find(*iter2);
00311                 if (iter != _Players.end())
00312                 {
00313                         _Players.erase(iter);
00314                 }
00315         }*/
00316 }
00317 
00318 
00319 } // namespace NLSOUND 
00320 
00321