From e36289681776bdbe4556a98816029fe71596e511 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 23 Nov 2015 04:31:15 -0800 Subject: [PATCH] Combine some duplicate code --- apps/openmw/mwsound/soundmanagerimp.cpp | 108 +++++++++--------------- apps/openmw/mwsound/soundmanagerimp.hpp | 1 + 2 files changed, 42 insertions(+), 67 deletions(-) diff --git a/apps/openmw/mwsound/soundmanagerimp.cpp b/apps/openmw/mwsound/soundmanagerimp.cpp index 73c9060bd..e00e5eecd 100644 --- a/apps/openmw/mwsound/soundmanagerimp.cpp +++ b/apps/openmw/mwsound/soundmanagerimp.cpp @@ -722,12 +722,7 @@ namespace MWSound Environment env = Env_Normal; if (mListenerUnderwater) - { env = Env_Underwater; - //play underwater sound - if(!(mUnderwaterSound && mUnderwaterSound->isPlaying())) - mUnderwaterSound = playSound("Underwater", 1.0f, 1.0f, Play_TypeSfx, Play_LoopNoEnv); - } else if(mUnderwaterSound) { mUnderwaterSound->stop(); @@ -741,48 +736,24 @@ namespace MWSound env ); + if(mListenerUnderwater) + { + // Play underwater sound (after updating listener) + if(!(mUnderwaterSound && mUnderwaterSound->isPlaying())) + mUnderwaterSound = playSound("Underwater", 1.0f, 1.0f, Play_TypeSfx, Play_LoopNoEnv); + } + // Check if any sounds are finished playing, and trash them - // Lower volume on fading out sounds SoundMap::iterator snditer = mActiveSounds.begin(); while(snditer != mActiveSounds.end()) { SoundNamePairList::iterator sndname = snditer->second.begin(); while(sndname != snditer->second.end()) { - MWBase::SoundPtr sound = sndname->first; - if(!sound->isPlaying()) - { + if(!updateSound(sndname->first, snditer->first, duration)) sndname = snditer->second.erase(sndname); - continue; - } - - const MWWorld::Ptr &ptr = snditer->first; - if(!ptr.isEmpty()) - { - const ESM::Position &pos = ptr.getRefData().getPosition(); - const osg::Vec3f objpos(pos.asVec3()); - sound->setPosition(objpos); - - if ((sound->mFlags & Play_RemoveAtDistance) - && (mListenerPos - ptr.getRefData().getPosition().asVec3()).length2() > 2000*2000) - { - sndname = snditer->second.erase(sndname); - continue; - } - } - - //update fade out - if(sound->mFadeOutTime > 0.0f) - { - float soundDuration = duration; - if(soundDuration > sound->mFadeOutTime) - soundDuration = sound->mFadeOutTime; - sound->setVolume(sound->mVolume - soundDuration/sound->mFadeOutTime*sound->mVolume); - sound->mFadeOutTime -= soundDuration; - } - sound->update(); - - ++sndname; + else + ++sndname; } if(snditer->second.empty()) mActiveSounds.erase(snditer++); @@ -793,43 +764,46 @@ namespace MWSound SaySoundMap::iterator sayiter = mActiveSaySounds.begin(); while(sayiter != mActiveSaySounds.end()) { - MWBase::SoundPtr sound = sayiter->second.first; - if(!sound->isPlaying()) - { + if(!updateSound(sayiter->second.first, sayiter->first, duration)) mActiveSaySounds.erase(sayiter++); - continue; - } + else + ++sayiter; + } + } - const MWWorld::Ptr &ptr = sayiter->first; - if(!ptr.isEmpty()) - { - const ESM::Position &pos = ptr.getRefData().getPosition(); - const osg::Vec3f objpos(pos.asVec3()); - sound->setPosition(objpos); - - if ((sound->mFlags & Play_RemoveAtDistance) - && (mListenerPos - ptr.getRefData().getPosition().asVec3()).length2() > 2000*2000) - { - mActiveSaySounds.erase(sayiter++); - continue; - } - } + bool SoundManager::updateSound(MWBase::SoundPtr sound, const MWWorld::Ptr& ptr, float duration) + { + if(!ptr.isEmpty()) + { + const ESM::Position &pos = ptr.getRefData().getPosition(); + const osg::Vec3f objpos(pos.asVec3()); + sound->setPosition(objpos); - //update fade out - if(sound->mFadeOutTime > 0.0f) + if((sound->mFlags&Play_RemoveAtDistance)) { - float soundDuration = duration; - if(soundDuration > sound->mFadeOutTime) - soundDuration = sound->mFadeOutTime; - sound->setVolume(sound->mVolume - soundDuration/sound->mFadeOutTime*sound->mVolume); - sound->mFadeOutTime -= soundDuration; + osg::Vec3f diff = mListenerPos - ptr.getRefData().getPosition().asVec3(); + if(diff.length2() > 2000*2000) + sound->stop(); } - sound->update(); + } + + if(!sound->isPlaying()) + return false; - ++sayiter; + // Update fade out + if(sound->mFadeOutTime > 0.0f) + { + float soundDuration = duration; + if(soundDuration > sound->mFadeOutTime) + soundDuration = sound->mFadeOutTime; + sound->setVolume(sound->mVolume - soundDuration/sound->mFadeOutTime*sound->mVolume); + sound->mFadeOutTime -= soundDuration; } + sound->update(); + return true; } + void SoundManager::update(float duration) { if(!mOutput->isInitialized()) diff --git a/apps/openmw/mwsound/soundmanagerimp.hpp b/apps/openmw/mwsound/soundmanagerimp.hpp index 896cf48a7..db60df044 100644 --- a/apps/openmw/mwsound/soundmanagerimp.hpp +++ b/apps/openmw/mwsound/soundmanagerimp.hpp @@ -74,6 +74,7 @@ namespace MWSound const Sound_Buffer *lookupVoice(const std::string &voicefile); void streamMusicFull(const std::string& filename); + bool updateSound(MWBase::SoundPtr sound, const MWWorld::Ptr &ptr, float duration); void updateSounds(float duration); void updateRegionSound(float duration);