From c9f11f62d277f760b6747fce490e53d8de9cd9f7 Mon Sep 17 00:00:00 2001 From: stiffly Date: Tue, 12 Jan 2016 15:17:06 +0100 Subject: [PATCH] Does not delete emitters that are paused and has not yet been played. --- include/Engine/Sound/SoundSystem.h | 3 +- src/Engine/Sound/SoundSystem.cpp | 49 ++++++++++++++++-------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/include/Engine/Sound/SoundSystem.h b/include/Engine/Sound/SoundSystem.h index 21968cd5..43d670d0 100644 --- a/include/Engine/Sound/SoundSystem.h +++ b/include/Engine/Sound/SoundSystem.h @@ -26,8 +26,9 @@ struct Source { Source() { } - Sound* SoundResource; + Sound* SoundResource = nullptr; ALuint ALsource; + bool HasBeenPlayed = false; }; class SoundSystem diff --git a/src/Engine/Sound/SoundSystem.cpp b/src/Engine/Sound/SoundSystem.cpp index ce93515f..b5c63d13 100644 --- a/src/Engine/Sound/SoundSystem.cpp +++ b/src/Engine/Sound/SoundSystem.cpp @@ -65,28 +65,30 @@ void SoundSystem::stopEmitters() void SoundSystem::Update() { - deleteInactiveEmitters(); addNewEmitters(); + deleteInactiveEmitters(); updateEmitters(); updateListener(); } void SoundSystem::deleteInactiveEmitters() { - //auto emitterComponents = m_World->GetComponents("SoundEmitter"); - //for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { - // EntityID emitter = (*it).EntityID; - // if (isPlaying(m_Sources[emitter]->ALsource)) { // The sound is still playing, do not remove - // it++; - // continue; - // } else { - // alDeleteBuffers(1, &m_Sources[emitter]->ALsource); - // alDeleteSources(1, &m_Sources[emitter]->ALsource); - // //delete m_Sources[emitter]->SoundResource; - // m_Sources.erase(emitter); - // m_World->DeleteEntity(emitter); - // } - //} + auto emitterComponents = m_World->GetComponents("SoundEmitter"); + for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { + EntityID emitter = (*it).EntityID; + Source* source = m_Sources[emitter]; + if (isPlaying(source->ALsource) || !source->HasBeenPlayed) { // The sound is still playing, do not remove + it++; + continue; + } + else { + alDeleteBuffers(1, &source->ALsource); + alDeleteSources(1, &source->ALsource); + //delete m_Sources[emitter]->SoundResource; + m_Sources.erase(emitter); + m_World->DeleteEntity(emitter); + } + } std::unordered_map::iterator it; for (it = m_Sources.begin(); it != m_Sources.end();) { @@ -140,15 +142,15 @@ void SoundSystem::updateEmitters() if (i != m_Sources.end()) { // Get previous pos glm::vec3 previousPos; - alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); + alGetSource3f(i->second->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z); // Get next pos glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); // Calculate velocity glm::vec3 velocity = nextPos - previousPos; - setSourcePos(m_Sources[emitter]->ALsource, nextPos); - setSourceVel(m_Sources[emitter]->ALsource, velocity); + setSourcePos(i->second->ALsource, nextPos); + setSourceVel(i->second->ALsource, velocity); setSoundProperties( - m_Sources[emitter]->ALsource, + i->second->ALsource, (float)(double)(*it)["Gain"], (float)(double)(*it)["Pitch"], (bool)(*it)["Loop"], @@ -160,9 +162,11 @@ void SoundSystem::updateEmitters() // To make an emitter play when spawned in editor mode if (m_EditorEnabled) { // Path changed - if (m_Sources[emitter]->SoundResource->Path() != (std::string)(*it)["FilePath"]) { - m_Sources[emitter]->SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); - playSound(m_Sources[emitter]); + if (i->second->SoundResource->Path() != (std::string)(*it)["FilePath"]) { + i->second->SoundResource = ResourceManager::Load((std::string)(*it)["FilePath"]); + if (i->second->SoundResource->Buffer() != 0) { + playSound(i->second); + } } } it++; @@ -203,6 +207,7 @@ void SoundSystem::playSound(Source* source) { alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer()); alSourcePlay(source->ALsource); + source->HasBeenPlayed = true; } void SoundSystem::stopSound(Source* source)