Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / sound / sound_anim_manager.cpp
blob423212c8fe807ed53f0196677f47602e825587a6
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdsound.h"
18 #include "nel/sound/sound_anim_manager.h"
19 #include "nel/sound/sound_animation.h"
20 //#include "nel/sound/sound_anim_player.h"
21 #include "nel/misc/common.h"
22 #include "nel/misc/path.h"
24 using namespace std;
25 using namespace NLSOUND;
26 using namespace NLMISC;
28 namespace NLSOUND {
30 CSoundAnimManager* CSoundAnimManager::_Instance = 0;
32 // ********************************************************
34 CSoundAnimManager::CSoundAnimManager(NLSOUND::UAudioMixer* mixer) : _Mixer(mixer)
36 if (_Instance != 0)
38 throw Exception("Duplicate instanciation of CSoundAnimManager singleton");
41 _Instance = this;
42 //_PlayerId = 0;
45 // ********************************************************
47 CSoundAnimManager::~CSoundAnimManager()
49 if (_Instance == NULL)
50 return;
52 set<CSoundAnimPlayer*>::iterator iter;
54 for (iter = _Players.begin(); iter != _Players.end(); iter++)
56 CSoundAnimPlayer* player = *iter;
57 delete player;
60 _Players.clear();
63 _Instance = NULL;
66 // ********************************************************
68 TSoundAnimId CSoundAnimManager::loadAnimation(std::string& name)
70 nlassert(!name.empty());
72 string filename;
73 if (name.find(".anim") != name.npos)
75 filename = CFile::getFilenameWithoutExtension(name);
76 filename.append(".sound_anim");
78 else
80 filename = name;
81 filename.append(".sound_anim");
84 // throws exception if file not found
85 filename = CPath::lookup(filename, false, false, true);
86 if (filename.empty())
88 return CSoundAnimationNoId;
91 TSoundAnimId id = createAnimation(name);
92 if (id == CSoundAnimationNoId)
94 return CSoundAnimationNoId;
97 CSoundAnimation* anim = _Animations[id];
99 if (anim == NULL)
100 return CSoundAnimationNoId;
102 anim->setFilename(filename);
103 anim->load();
105 return id;
108 // ********************************************************
110 TSoundAnimId CSoundAnimManager::createAnimation(std::string& name)
112 nlassert(!name.empty());
114 // create and insert animations
115 TSoundAnimId id = (TSoundAnimId)_Animations.size();
116 CSoundAnimation* anim = new CSoundAnimation(name, id);
117 _Animations.push_back(anim);
119 // insert the name and id in the id table
120 pair<TSoundAnimMap::iterator, bool> inserted;
121 inserted =_IdMap.insert(make_pair(anim->getName().c_str(), id));
122 if (!inserted.second)
124 nlwarning("Duplicate sound animation \"%s\"", name.c_str());
125 delete anim;
126 return CSoundAnimationNoId;
129 return id;
132 // ********************************************************
134 CSoundAnimation* CSoundAnimManager::findAnimation(std::string& name)
136 TSoundAnimMap::iterator iter = _IdMap.find(name.c_str());
137 return (iter == _IdMap.end())? 0 : _Animations[(*iter).second];
140 // ********************************************************
142 TSoundAnimId CSoundAnimManager::getAnimationFromName(std::string& name)
144 TSoundAnimMap::iterator iter = _IdMap.find(name.c_str());
145 return (iter == _IdMap.end())? CSoundAnimationNoId : (*iter).second;
148 // ********************************************************
150 void CSoundAnimManager::saveAnimation(CSoundAnimation* anim, std::string& filename)
152 nlassert(anim);
153 nlassert(!filename.empty());
155 anim->setFilename(filename);
156 anim->save ();
159 // ********************************************************
161 TSoundAnimPlayId CSoundAnimManager::playAnimation(TSoundAnimId id, float time, CVector* position)
163 nlassert(id != CSoundAnimationNoId);
164 nlassert((uint32) id < _Animations.size());
165 nlassert(position);
167 CSoundAnimation* anim = _Animations[id];
168 nlassert(anim);
171 _PlayerId++;
172 CSoundAnimPlayer* player = new CSoundAnimPlayer(anim, time, position, _Mixer, _PlayerId);
173 nlassert(player);
175 _Players.insert(player);
177 return _PlayerId;
181 struct TFindId : std::unary_function<TSoundAnimMap::value_type, bool>
183 TSoundAnimId Id;
185 TFindId(TSoundAnimId id)
186 : Id(id)
189 bool operator () (const TSoundAnimMap::value_type &value) const
191 return value.second == Id;
195 std::string CSoundAnimManager::idToName(TSoundAnimId id)
197 static string empty;
198 TSoundAnimMap::iterator it(std::find_if(_IdMap.begin(), _IdMap.end(), TFindId(id)));
200 if (it != _IdMap.end())
201 return it->first;
202 else
203 return empty;
207 void CSoundAnimManager::playAnimation(TSoundAnimId id, float lastTime, float curTime, NL3D::CCluster *cluster, CSoundContext &context)
209 //nlassert(id != CSoundAnimationNoId);
210 if (id == CSoundAnimationNoId)
212 return;
215 if ((uint32) id >= _Animations.size())
216 return;
217 nlassert((uint32) id < _Animations.size());
219 CSoundAnimation* anim = _Animations[id];
220 nlassert(anim);
222 anim->play(_Mixer, lastTime, curTime, cluster, context);
226 // ********************************************************
227 TSoundAnimPlayId CSoundAnimManager::playAnimation(TSoundAnimId /* id */, float /* time */, NL3D::CCluster * /* cluster */, CSoundContext &/* context */)
229 return 0;
232 // ********************************************************
233 TSoundAnimPlayId CSoundAnimManager::playAnimation(string& /* name */, float /* time */, NL3D::CCluster * /* cluster */, CSoundContext &/* context */)
235 /* nlassert(position);
237 TSoundAnimId id = getAnimationFromName(name);
238 return (id == CSoundAnimation::NoId)? -1 : _PlayerId;*/
239 return 0;
243 // ********************************************************
244 void CSoundAnimManager::stopAnimation(TSoundAnimPlayId /* playbackId */)
246 /* nlassert(playbackId >= 0);
248 set<CSoundAnimPlayer*>::iterator iter;
250 for (iter = _Players.begin(); iter != _Players.end(); )
252 CSoundAnimPlayer* player = *iter;
253 if (player->getId() == playbackId)
255 _Players.erase(iter);
256 break;
261 // ********************************************************
262 bool CSoundAnimManager::isPlaying(TSoundAnimPlayId /* playbackId */)
264 /*nlassert(playbackId >= 0);
266 set<CSoundAnimPlayer*>::iterator iter;
268 for (iter = _Players.begin(); iter != _Players.end(); )
270 CSoundAnimPlayer* player = *iter;
271 if (player->getId() == playbackId)
273 return player->isPlaying();
277 return false;
280 // ********************************************************
281 void CSoundAnimManager::update(float /* lastTime */, float /* curTime */)
283 /* set<CSoundAnimPlayer*>::iterator iter;
285 _Garbage.clear();
287 for (iter = _Players.begin(); iter != _Players.end(); iter++)
289 CSoundAnimPlayer* player = *iter;
290 if (player->isPlaying())
292 player->update(lastTime, curTime);
294 else
296 _Garbage.push_back(player);
300 vector<CSoundAnimPlayer*>::iterator iter2;
302 for (iter2 = _Garbage.begin(); iter2 != _Garbage.end(); iter2++)
304 iter = _Players.find(*iter2);
305 if (iter != _Players.end())
307 _Players.erase(iter);
313 } // namespace NLSOUND