Combine some duplicate code

openmw-38
Chris Robinson 9 years ago
parent febc7b510a
commit e362896817

@ -722,12 +722,7 @@ namespace MWSound
Environment env = Env_Normal; Environment env = Env_Normal;
if (mListenerUnderwater) if (mListenerUnderwater)
{
env = Env_Underwater; env = Env_Underwater;
//play underwater sound
if(!(mUnderwaterSound && mUnderwaterSound->isPlaying()))
mUnderwaterSound = playSound("Underwater", 1.0f, 1.0f, Play_TypeSfx, Play_LoopNoEnv);
}
else if(mUnderwaterSound) else if(mUnderwaterSound)
{ {
mUnderwaterSound->stop(); mUnderwaterSound->stop();
@ -741,48 +736,24 @@ namespace MWSound
env 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 // Check if any sounds are finished playing, and trash them
// Lower volume on fading out sounds
SoundMap::iterator snditer = mActiveSounds.begin(); SoundMap::iterator snditer = mActiveSounds.begin();
while(snditer != mActiveSounds.end()) while(snditer != mActiveSounds.end())
{ {
SoundNamePairList::iterator sndname = snditer->second.begin(); SoundNamePairList::iterator sndname = snditer->second.begin();
while(sndname != snditer->second.end()) while(sndname != snditer->second.end())
{ {
MWBase::SoundPtr sound = sndname->first; if(!updateSound(sndname->first, snditer->first, duration))
if(!sound->isPlaying())
{
sndname = snditer->second.erase(sndname); sndname = snditer->second.erase(sndname);
continue; else
} ++sndname;
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;
} }
if(snditer->second.empty()) if(snditer->second.empty())
mActiveSounds.erase(snditer++); mActiveSounds.erase(snditer++);
@ -793,43 +764,46 @@ namespace MWSound
SaySoundMap::iterator sayiter = mActiveSaySounds.begin(); SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
while(sayiter != mActiveSaySounds.end()) while(sayiter != mActiveSaySounds.end())
{ {
MWBase::SoundPtr sound = sayiter->second.first; if(!updateSound(sayiter->second.first, sayiter->first, duration))
if(!sound->isPlaying())
{
mActiveSaySounds.erase(sayiter++); mActiveSaySounds.erase(sayiter++);
continue; else
} ++sayiter;
}
}
const MWWorld::Ptr &ptr = sayiter->first; bool SoundManager::updateSound(MWBase::SoundPtr sound, const MWWorld::Ptr& ptr, float duration)
if(!ptr.isEmpty()) {
{ if(!ptr.isEmpty())
const ESM::Position &pos = ptr.getRefData().getPosition(); {
const osg::Vec3f objpos(pos.asVec3()); const ESM::Position &pos = ptr.getRefData().getPosition();
sound->setPosition(objpos); 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;
}
}
//update fade out if((sound->mFlags&Play_RemoveAtDistance))
if(sound->mFadeOutTime > 0.0f)
{ {
float soundDuration = duration; osg::Vec3f diff = mListenerPos - ptr.getRefData().getPosition().asVec3();
if(soundDuration > sound->mFadeOutTime) if(diff.length2() > 2000*2000)
soundDuration = sound->mFadeOutTime; sound->stop();
sound->setVolume(sound->mVolume - soundDuration/sound->mFadeOutTime*sound->mVolume);
sound->mFadeOutTime -= soundDuration;
} }
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) void SoundManager::update(float duration)
{ {
if(!mOutput->isInitialized()) if(!mOutput->isInitialized())

@ -74,6 +74,7 @@ namespace MWSound
const Sound_Buffer *lookupVoice(const std::string &voicefile); const Sound_Buffer *lookupVoice(const std::string &voicefile);
void streamMusicFull(const std::string& filename); void streamMusicFull(const std::string& filename);
bool updateSound(MWBase::SoundPtr sound, const MWWorld::Ptr &ptr, float duration);
void updateSounds(float duration); void updateSounds(float duration);
void updateRegionSound(float duration); void updateRegionSound(float duration);

Loading…
Cancel
Save