1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-04 03:40:14 +00:00

Use a pair to match the MWWorld::Ptr object and sound ID, instead of nested maps

This commit is contained in:
Chris Robinson 2012-03-27 02:50:45 -07:00
parent 0d552c10bc
commit c2611d035c
2 changed files with 40 additions and 64 deletions

View File

@ -114,14 +114,10 @@ namespace MWSound
bool SoundManager::isPlaying(MWWorld::Ptr ptr, const std::string &id) const bool SoundManager::isPlaying(MWWorld::Ptr ptr, const std::string &id) const
{ {
SoundMap::const_iterator snditer = mActiveSounds.find(ptr); SoundMap::const_iterator snditer = mActiveSounds.find(std::make_pair(ptr, id));
if(snditer == mActiveSounds.end()) if(snditer == mActiveSounds.end())
return false; return false;
IDMap::const_iterator iditer = snditer->second.find(id);
if(iditer == snditer->second.end())
return false;
return true; return true;
} }
@ -185,7 +181,7 @@ namespace MWSound
std::string filePath = std::string("Sound/")+filename; std::string filePath = std::string("Sound/")+filename;
SoundPtr sound(mOutput->playSound3D(filePath, pos.pos, 1.0f, 1.0f, 100.0f, 20000.0f, false)); SoundPtr sound(mOutput->playSound3D(filePath, pos.pos, 1.0f, 1.0f, 100.0f, 20000.0f, false));
mActiveSounds[ptr]["_say_sound"] = sound; mActiveSounds[std::make_pair(ptr, std::string("_say_sound"))] = sound;
} }
catch(std::exception &e) catch(std::exception &e)
{ {
@ -205,8 +201,8 @@ namespace MWSound
try try
{ {
std::string file = lookup(soundId, volume, min, max); std::string file = lookup(soundId, volume, min, max);
Sound *sound = mOutput->playSound(file, volume, pitch, loop); SoundPtr sound = SoundPtr(mOutput->playSound(file, volume, pitch, loop));
mActiveSounds[MWWorld::Ptr()][soundId] = SoundPtr(sound); mActiveSounds[std::make_pair(MWWorld::Ptr(), soundId)] = sound;
} }
catch(std::exception &e) catch(std::exception &e)
{ {
@ -225,7 +221,7 @@ namespace MWSound
std::string file = lookup(soundId, volume, min, max); std::string file = lookup(soundId, volume, min, max);
SoundPtr sound(mOutput->playSound3D(file, pos.pos, volume, pitch, min, max, loop)); SoundPtr sound(mOutput->playSound3D(file, pos.pos, volume, pitch, min, max, loop));
mActiveSounds[untracked?MWWorld::Ptr():ptr][soundId] = sound; mActiveSounds[std::make_pair((untracked?MWWorld::Ptr():ptr), soundId)] = sound;
} }
catch(std::exception &e) catch(std::exception &e)
{ {
@ -237,30 +233,28 @@ namespace MWSound
{ {
// Stop a sound and remove it from the list. If soundId="" then // Stop a sound and remove it from the list. If soundId="" then
// stop all its sounds. // stop all its sounds.
SoundMap::iterator snditer = mActiveSounds.find(ptr);
if(snditer == mActiveSounds.end())
return;
if(!soundId.empty()) if(!soundId.empty())
{ {
IDMap::iterator iditer = snditer->second.find(soundId); SoundMap::iterator snditer = mActiveSounds.find(std::make_pair(ptr, soundId));
if(iditer != snditer->second.end()) if(snditer == mActiveSounds.end())
{ return;
iditer->second->stop();
snditer->second.erase(iditer); snditer->second->stop();
if(snditer->second.empty()) mActiveSounds.erase(snditer);
mActiveSounds.erase(snditer);
}
} }
else else
{ {
IDMap::iterator iditer = snditer->second.begin(); SoundMap::iterator snditer = mActiveSounds.begin();
while(iditer != snditer->second.end()) while(snditer != mActiveSounds.end())
{ {
iditer->second->stop(); if(snditer->first.first == ptr)
iditer++; {
snditer->second->stop();
mActiveSounds.erase(snditer++);
}
else
snditer++;
} }
mActiveSounds.erase(snditer);
} }
} }
@ -270,14 +264,10 @@ namespace MWSound
SoundMap::iterator snditer = mActiveSounds.begin(); SoundMap::iterator snditer = mActiveSounds.begin();
while(snditer != mActiveSounds.end()) while(snditer != mActiveSounds.end())
{ {
if(snditer->first != MWWorld::Ptr() && snditer->first.getCell() == cell) if(snditer->first.first != MWWorld::Ptr() &&
snditer->first.first.getCell() == cell)
{ {
IDMap::iterator iditer = snditer->second.begin(); snditer->second->stop();
while(iditer != snditer->second.end())
{
iditer->second->stop();
iditer++;
}
mActiveSounds.erase(snditer++); mActiveSounds.erase(snditer++);
} }
else else
@ -287,18 +277,12 @@ namespace MWSound
void SoundManager::stopSound(const std::string& soundId) void SoundManager::stopSound(const std::string& soundId)
{ {
SoundMap::iterator snditer = mActiveSounds.find(MWWorld::Ptr()); SoundMap::iterator snditer = mActiveSounds.find(std::make_pair(MWWorld::Ptr(), soundId));
if(snditer == mActiveSounds.end()) if(snditer == mActiveSounds.end())
return; return;
IDMap::iterator iditer = snditer->second.find(soundId); snditer->second->stop();
if(iditer != snditer->second.end()) mActiveSounds.erase(snditer);
{
iditer->second->stop();
snditer->second.erase(iditer);
if(snditer->second.empty())
mActiveSounds.erase(snditer);
}
} }
bool SoundManager::getSoundPlaying(MWWorld::Ptr ptr, const std::string& soundId) const bool SoundManager::getSoundPlaying(MWWorld::Ptr ptr, const std::string& soundId) const
@ -308,16 +292,16 @@ namespace MWSound
void SoundManager::updateObject(MWWorld::Ptr ptr) void SoundManager::updateObject(MWWorld::Ptr ptr)
{ {
SoundMap::iterator snditer = mActiveSounds.find(ptr); SoundMap::iterator snditer = mActiveSounds.begin();
if(snditer == mActiveSounds.end()) while(snditer != mActiveSounds.end())
return;
const ESM::Position &pos = ptr.getCellRef().pos;
IDMap::iterator iditer = snditer->second.begin();
while(iditer != snditer->second.end())
{ {
iditer->second->update(pos.pos); if(snditer->first.first == ptr)
iditer++; {
snditer->second->stop();
mActiveSounds.erase(snditer++);
}
else
snditer++;
} }
} }
@ -406,15 +390,7 @@ namespace MWSound
SoundMap::iterator snditer = mActiveSounds.begin(); SoundMap::iterator snditer = mActiveSounds.begin();
while(snditer != mActiveSounds.end()) while(snditer != mActiveSounds.end())
{ {
IDMap::iterator iditer = snditer->second.begin(); if(!snditer->second->isPlaying())
while(iditer != snditer->second.end())
{
if(!iditer->second->isPlaying())
snditer->second.erase(iditer++);
else
iditer++;
}
if(snditer->second.empty())
mActiveSounds.erase(snditer++); mActiveSounds.erase(snditer++);
else else
snditer++; snditer++;

View File

@ -2,11 +2,11 @@
#define GAME_SOUND_SOUNDMANAGER_H #define GAME_SOUND_SOUNDMANAGER_H
#include <string> #include <string>
#include <utility>
#include <map>
#include <OgreResourceGroupManager.h> #include <OgreResourceGroupManager.h>
#include <components/files/filelibrary.hpp>
#include "../mwworld/ptr.hpp" #include "../mwworld/ptr.hpp"
@ -41,8 +41,8 @@ namespace MWSound
std::string mCurrentPlaylist; std::string mCurrentPlaylist;
typedef boost::shared_ptr<Sound> SoundPtr; typedef boost::shared_ptr<Sound> SoundPtr;
typedef std::map<std::string,SoundPtr> IDMap; typedef std::pair<MWWorld::Ptr,std::string> PtrIDPair;
typedef std::map<MWWorld::Ptr,IDMap> SoundMap; typedef std::map<PtrIDPair,SoundPtr> SoundMap;
SoundMap mActiveSounds; SoundMap mActiveSounds;
std::string lookup(const std::string &soundId, std::string lookup(const std::string &soundId,