1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-04-18 05:42:40 +00:00

Avoid more explicit loops

This commit is contained in:
Chris Robinson 2017-09-16 01:56:58 -07:00
parent 9dbb713b7c
commit 1e729e8da9

View File

@ -99,20 +99,16 @@ namespace MWSound
std::vector<std::string> names = mOutput->enumerate(); std::vector<std::string> names = mOutput->enumerate();
std::cout <<"Enumerated output devices:\n"; std::cout <<"Enumerated output devices:\n";
std::for_each(names.cbegin(), names.cend(), for(const std::string &name : names)
[](const std::string &name) -> void std::cout <<" "<<name<<"\n";
{ std::cout <<" "<<name<<"\n"; }
);
std::cout.flush(); std::cout.flush();
names = mOutput->enumerateHrtf(); names = mOutput->enumerateHrtf();
if(!names.empty()) if(!names.empty())
{ {
std::cout<< "Enumerated HRTF names:\n"; std::cout<< "Enumerated HRTF names:\n";
std::for_each(names.cbegin(), names.cend(), for(const std::string &name : names)
[](const std::string &name) -> void std::cout <<" "<<name<<"\n";
{ std::cout<< " "<<name<<"\n"; }
);
std::cout.flush(); std::cout.flush();
} }
} }
@ -120,12 +116,11 @@ namespace MWSound
SoundManager::~SoundManager() SoundManager::~SoundManager()
{ {
clear(); clear();
SoundBufferList::element_type::iterator sfxiter = mSoundBuffers->begin(); for(Sound_Buffer &sfx : *mSoundBuffers)
for(;sfxiter != mSoundBuffers->end();++sfxiter)
{ {
if(sfxiter->mHandle) if(sfx.mHandle)
mOutput->unloadSound(sfxiter->mHandle); mOutput->unloadSound(sfx.mHandle);
sfxiter->mHandle = 0; sfx.mHandle = 0;
} }
mUnusedBuffers.clear(); mUnusedBuffers.clear();
mOutput.reset(); mOutput.reset();
@ -668,11 +663,10 @@ namespace MWSound
if(snditer != mActiveSounds.end()) if(snditer != mActiveSounds.end())
{ {
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId)); Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
SoundBufferRefPairList::iterator sndidx = snditer->second.begin(); for(SoundBufferRefPair &snd : snditer->second)
for(;sndidx != snditer->second.end();++sndidx)
{ {
if(sndidx->second == sfx) if(snd.second == sfx)
mOutput->finishSound(sndidx->first); mOutput->finishSound(snd.first);
} }
} }
} }
@ -682,33 +676,28 @@ namespace MWSound
SoundMap::iterator snditer = mActiveSounds.find(ptr); SoundMap::iterator snditer = mActiveSounds.find(ptr);
if(snditer != mActiveSounds.end()) if(snditer != mActiveSounds.end())
{ {
SoundBufferRefPairList::iterator sndidx = snditer->second.begin(); for(SoundBufferRefPair &snd : snditer->second)
for(;sndidx != snditer->second.end();++sndidx) mOutput->finishSound(snd.first);
mOutput->finishSound(sndidx->first);
} }
SaySoundMap::iterator sayiter = mActiveSaySounds.find(ptr);
if(sayiter != mActiveSaySounds.end())
mOutput->finishStream(sayiter->second);
} }
void SoundManager::stopSound(const MWWorld::CellStore *cell) void SoundManager::stopSound(const MWWorld::CellStore *cell)
{ {
SoundMap::iterator snditer = mActiveSounds.begin(); for(SoundMap::value_type &snd : mActiveSounds)
for(;snditer != mActiveSounds.end();++snditer)
{ {
if(snditer->first != MWWorld::ConstPtr() && if(!snd.first.isEmpty() && snd.first != MWMechanics::getPlayer() && snd.first.getCell() == cell)
snditer->first != MWMechanics::getPlayer() &&
snditer->first.getCell() == cell)
{ {
SoundBufferRefPairList::iterator sndidx = snditer->second.begin(); for(SoundBufferRefPair &sndbuf : snd.second)
for(;sndidx != snditer->second.end();++sndidx) mOutput->finishSound(sndbuf.first);
mOutput->finishSound(sndidx->first);
} }
} }
SaySoundMap::iterator sayiter = mActiveSaySounds.begin(); for(SaySoundMap::value_type &snd : mActiveSaySounds)
for(;sayiter != mActiveSaySounds.end();++sayiter)
{ {
if(sayiter->first != MWWorld::ConstPtr() && if(!snd.first.isEmpty() && snd.first != MWMechanics::getPlayer() && snd.first.getCell() == cell)
sayiter->first != MWMechanics::getPlayer() && mOutput->finishStream(snd.second);
sayiter->first.getCell() == cell)
mOutput->finishStream(sayiter->second);
} }
} }
@ -718,11 +707,10 @@ namespace MWSound
if(snditer != mActiveSounds.end()) if(snditer != mActiveSounds.end())
{ {
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId)); Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
SoundBufferRefPairList::iterator sndidx = snditer->second.begin(); for(SoundBufferRefPair &sndbuf : snditer->second)
for(;sndidx != snditer->second.end();++sndidx)
{ {
if(sndidx->second == sfx) if(sndbuf.second == sfx)
mOutput->finishSound(sndidx->first); mOutput->finishSound(sndbuf.first);
} }
} }
} }
@ -734,11 +722,10 @@ namespace MWSound
if(snditer != mActiveSounds.end()) if(snditer != mActiveSounds.end())
{ {
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId)); Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
SoundBufferRefPairList::iterator sndidx = snditer->second.begin(); for(SoundBufferRefPair &sndbuf : snditer->second)
for(;sndidx != snditer->second.end();++sndidx)
{ {
if(sndidx->second == sfx) if(sndbuf.second == sfx)
sndidx->first->setFadeout(duration); sndbuf.first->setFadeout(duration);
} }
} }
} }
@ -749,12 +736,10 @@ namespace MWSound
if(snditer != mActiveSounds.end()) if(snditer != mActiveSounds.end())
{ {
Sound_Buffer *sfx = lookupSound(Misc::StringUtils::lowerCase(soundId)); Sound_Buffer *sfx = lookupSound(Misc::StringUtils::lowerCase(soundId));
SoundBufferRefPairList::const_iterator sndidx = snditer->second.begin(); return std::find_if(snditer->second.cbegin(), snditer->second.cend(),
for(;sndidx != snditer->second.end();++sndidx) [this,sfx](const SoundBufferRefPair &snd) -> bool
{ { return snd.second == sfx && mOutput->isSoundPlaying(snd.first); }
if(sndidx->second == sfx && mOutput->isSoundPlaying(sndidx->first)) ) != snditer->second.cend();
return true;
}
} }
return false; return false;
} }
@ -813,10 +798,8 @@ namespace MWSound
if(total == 0) if(total == 0)
{ {
std::for_each(regn->mSoundList.cbegin(), regn->mSoundList.cend(), for(const ESM::Region::SoundRef &sndref : regn->mSoundList)
[](const ESM::Region::SoundRef &sndref) -> void total += (int)sndref.mChance;
{ total += (int)sndref.mChance; }
);
if(total == 0) if(total == 0)
return; return;
} }
@ -824,20 +807,15 @@ namespace MWSound
int r = Misc::Rng::rollDice(total); int r = Misc::Rng::rollDice(total);
int pos = 0; int pos = 0;
std::find_if_not(regn->mSoundList.cbegin(), regn->mSoundList.cend(), for(const ESM::Region::SoundRef &sndref : regn->mSoundList)
[&pos, r, this](const ESM::Region::SoundRef &sndref) -> bool {
if(r - pos < sndref.mChance)
{ {
if(r - pos < sndref.mChance) playSound(sndref.mSound.toString(), 1.0f, 1.0f);
{ break;
playSound(sndref.mSound.toString(), 1.0f, 1.0f);
// Played this sound, stop iterating
return false;
}
pos += sndref.mChance;
// Not this sound, keep iterating
return true;
} }
); pos += sndref.mChance;
}
} }
void SoundManager::updateWaterSound(float /*duration*/) void SoundManager::updateWaterSound(float /*duration*/)
@ -1125,28 +1103,23 @@ namespace MWSound
if(!mOutput->isInitialized()) if(!mOutput->isInitialized())
return; return;
mOutput->startUpdate(); mOutput->startUpdate();
SoundMap::iterator snditer = mActiveSounds.begin(); for(SoundMap::value_type &snd : mActiveSounds)
for(;snditer != mActiveSounds.end();++snditer)
{ {
SoundBufferRefPairList::iterator sndidx = snditer->second.begin(); for(SoundBufferRefPair &sndbuf : snd.second)
for(;sndidx != snditer->second.end();++sndidx)
{ {
Sound *sound = sndidx->first; Sound *sound = sndbuf.first;
sound->setBaseVolume(volumeFromType(sound->getPlayType())); sound->setBaseVolume(volumeFromType(sound->getPlayType()));
mOutput->updateSound(sound); mOutput->updateSound(sound);
} }
} }
SaySoundMap::iterator sayiter = mActiveSaySounds.begin(); for(SaySoundMap::value_type &snd : mActiveSaySounds)
for(;sayiter != mActiveSaySounds.end();++sayiter)
{ {
Stream *sound = sayiter->second; Stream *sound = snd.second;
sound->setBaseVolume(volumeFromType(sound->getPlayType())); sound->setBaseVolume(volumeFromType(sound->getPlayType()));
mOutput->updateStream(sound); mOutput->updateStream(sound);
} }
TrackList::iterator trkiter = mActiveTracks.begin(); for(Stream *sound : mActiveTracks)
for(;trkiter != mActiveTracks.end();++trkiter)
{ {
Stream *sound = *trkiter;
sound->setBaseVolume(volumeFromType(sound->getPlayType())); sound->setBaseVolume(volumeFromType(sound->getPlayType()));
mOutput->updateStream(sound); mOutput->updateStream(sound);
} }
@ -1252,36 +1225,35 @@ namespace MWSound
void SoundManager::clear() void SoundManager::clear()
{ {
SoundMap::iterator snditer = mActiveSounds.begin(); stopMusic();
for(;snditer != mActiveSounds.end();++snditer)
for(SoundMap::value_type &snd : mActiveSounds)
{ {
SoundBufferRefPairList::iterator sndidx = snditer->second.begin(); for(SoundBufferRefPair &sndbuf : snd.second)
for(;sndidx != snditer->second.end();++sndidx)
{ {
mOutput->finishSound(sndidx->first); mOutput->finishSound(sndbuf.first);
mUnusedSounds.push_back(sndidx->first); mUnusedSounds.push_back(sndbuf.first);
Sound_Buffer *sfx = sndidx->second; Sound_Buffer *sfx = sndbuf.second;
if(sfx->mUses-- == 1) if(sfx->mUses-- == 1)
mUnusedBuffers.push_front(sfx); mUnusedBuffers.push_front(sfx);
} }
} }
mActiveSounds.clear(); mActiveSounds.clear();
SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
for(;sayiter != mActiveSaySounds.end();++sayiter)
{
mOutput->finishStream(sayiter->second);
mUnusedStreams.push_back(sayiter->second);
}
mActiveSaySounds.clear();
TrackList::iterator trkiter = mActiveTracks.begin();
for(;trkiter != mActiveTracks.end();++trkiter)
{
mOutput->finishStream(*trkiter);
mUnusedStreams.push_back(*trkiter);
}
mActiveTracks.clear();
mUnderwaterSound = nullptr; mUnderwaterSound = nullptr;
mNearWaterSound = nullptr; mNearWaterSound = nullptr;
stopMusic();
for(SaySoundMap::value_type &snd : mActiveSaySounds)
{
mOutput->finishStream(snd.second);
mUnusedStreams.push_back(snd.second);
}
mActiveSaySounds.clear();
for(Stream *sound : mActiveTracks)
{
mOutput->finishStream(sound);
mUnusedStreams.push_back(sound);
}
mActiveTracks.clear();
} }
} }