1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
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"
25 using namespace NLSOUND
;
26 using namespace NLMISC
;
30 CSoundAnimManager
* CSoundAnimManager::_Instance
= 0;
32 // ********************************************************
34 CSoundAnimManager::CSoundAnimManager(NLSOUND::UAudioMixer
* mixer
) : _Mixer(mixer
)
38 throw Exception("Duplicate instanciation of CSoundAnimManager singleton");
45 // ********************************************************
47 CSoundAnimManager::~CSoundAnimManager()
49 if (_Instance
== NULL
)
52 set<CSoundAnimPlayer*>::iterator iter;
54 for (iter = _Players.begin(); iter != _Players.end(); iter++)
56 CSoundAnimPlayer* player = *iter;
66 // ********************************************************
68 TSoundAnimId
CSoundAnimManager::loadAnimation(std::string
& name
)
70 nlassert(!name
.empty());
73 if (name
.find(".anim") != name
.npos
)
75 filename
= CFile::getFilenameWithoutExtension(name
);
76 filename
.append(".sound_anim");
81 filename
.append(".sound_anim");
84 // throws exception if file not found
85 filename
= CPath::lookup(filename
, false, false, true);
88 return CSoundAnimationNoId
;
91 TSoundAnimId id
= createAnimation(name
);
92 if (id
== CSoundAnimationNoId
)
94 return CSoundAnimationNoId
;
97 CSoundAnimation
* anim
= _Animations
[id
];
100 return CSoundAnimationNoId
;
102 anim
->setFilename(filename
);
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());
126 return CSoundAnimationNoId
;
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
)
153 nlassert(!filename
.empty());
155 anim
->setFilename(filename
);
159 // ********************************************************
161 TSoundAnimPlayId CSoundAnimManager::playAnimation(TSoundAnimId id, float time, CVector* position)
163 nlassert(id != CSoundAnimationNoId);
164 nlassert((uint32) id < _Animations.size());
167 CSoundAnimation* anim = _Animations[id];
172 CSoundAnimPlayer* player = new CSoundAnimPlayer(anim, time, position, _Mixer, _PlayerId);
175 _Players.insert(player);
181 struct TFindId
: std::unary_function
<TSoundAnimMap::value_type
, bool>
185 TFindId(TSoundAnimId id
)
189 bool operator () (const TSoundAnimMap::value_type
&value
) const
191 return value
.second
== Id
;
195 std::string
CSoundAnimManager::idToName(TSoundAnimId id
)
198 TSoundAnimMap::iterator
it(std::find_if(_IdMap
.begin(), _IdMap
.end(), TFindId(id
)));
200 if (it
!= _IdMap
.end())
207 void CSoundAnimManager::playAnimation(TSoundAnimId id
, float lastTime
, float curTime
, NL3D::CCluster
*cluster
, CSoundContext
&context
)
209 //nlassert(id != CSoundAnimationNoId);
210 if (id
== CSoundAnimationNoId
)
215 if ((uint32
) id
>= _Animations
.size())
217 nlassert((uint32
) id
< _Animations
.size());
219 CSoundAnimation
* anim
= _Animations
[id
];
222 anim
->play(_Mixer
, lastTime
, curTime
, cluster
, context
);
226 // ********************************************************
227 TSoundAnimPlayId
CSoundAnimManager::playAnimation(TSoundAnimId
/* id */, float /* time */, NL3D::CCluster
* /* cluster */, CSoundContext
&/* context */)
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;*/
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);
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();
280 // ********************************************************
281 void CSoundAnimManager::update(float /* lastTime */, float /* curTime */)
283 /* set<CSoundAnimPlayer*>::iterator iter;
287 for (iter = _Players.begin(); iter != _Players.end(); iter++)
289 CSoundAnimPlayer* player = *iter;
290 if (player->isPlaying())
292 player->update(lastTime, curTime);
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