Does not delete emitters that are paused and has not yet been played.

This commit is contained in:
stiffly
2016-01-12 15:17:06 +01:00
parent 5697e8bc85
commit c9f11f62d2
2 changed files with 29 additions and 23 deletions
+2 -1
View File
@@ -26,8 +26,9 @@
struct Source struct Source
{ {
Source() { } Source() { }
Sound* SoundResource; Sound* SoundResource = nullptr;
ALuint ALsource; ALuint ALsource;
bool HasBeenPlayed = false;
}; };
class SoundSystem class SoundSystem
+27 -22
View File
@@ -65,28 +65,30 @@ void SoundSystem::stopEmitters()
void SoundSystem::Update() void SoundSystem::Update()
{ {
deleteInactiveEmitters();
addNewEmitters(); addNewEmitters();
deleteInactiveEmitters();
updateEmitters(); updateEmitters();
updateListener(); updateListener();
} }
void SoundSystem::deleteInactiveEmitters() void SoundSystem::deleteInactiveEmitters()
{ {
//auto emitterComponents = m_World->GetComponents("SoundEmitter"); auto emitterComponents = m_World->GetComponents("SoundEmitter");
//for (auto it = emitterComponents->begin(); it != emitterComponents->end();) { for (auto it = emitterComponents->begin(); it != emitterComponents->end();) {
// EntityID emitter = (*it).EntityID; EntityID emitter = (*it).EntityID;
// if (isPlaying(m_Sources[emitter]->ALsource)) { // The sound is still playing, do not remove Source* source = m_Sources[emitter];
// it++; if (isPlaying(source->ALsource) || !source->HasBeenPlayed) { // The sound is still playing, do not remove
// continue; it++;
// } else { continue;
// alDeleteBuffers(1, &m_Sources[emitter]->ALsource); }
// alDeleteSources(1, &m_Sources[emitter]->ALsource); else {
// //delete m_Sources[emitter]->SoundResource; alDeleteBuffers(1, &source->ALsource);
// m_Sources.erase(emitter); alDeleteSources(1, &source->ALsource);
// m_World->DeleteEntity(emitter); //delete m_Sources[emitter]->SoundResource;
// } m_Sources.erase(emitter);
//} m_World->DeleteEntity(emitter);
}
}
std::unordered_map<EntityID, Source*>::iterator it; std::unordered_map<EntityID, Source*>::iterator it;
for (it = m_Sources.begin(); it != m_Sources.end();) { for (it = m_Sources.begin(); it != m_Sources.end();) {
@@ -140,15 +142,15 @@ void SoundSystem::updateEmitters()
if (i != m_Sources.end()) { if (i != m_Sources.end()) {
// Get previous pos // Get previous pos
glm::vec3 previousPos; 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 // Get next pos
glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter); glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter);
// Calculate velocity // Calculate velocity
glm::vec3 velocity = nextPos - previousPos; glm::vec3 velocity = nextPos - previousPos;
setSourcePos(m_Sources[emitter]->ALsource, nextPos); setSourcePos(i->second->ALsource, nextPos);
setSourceVel(m_Sources[emitter]->ALsource, velocity); setSourceVel(i->second->ALsource, velocity);
setSoundProperties( setSoundProperties(
m_Sources[emitter]->ALsource, i->second->ALsource,
(float)(double)(*it)["Gain"], (float)(double)(*it)["Gain"],
(float)(double)(*it)["Pitch"], (float)(double)(*it)["Pitch"],
(bool)(*it)["Loop"], (bool)(*it)["Loop"],
@@ -160,9 +162,11 @@ void SoundSystem::updateEmitters()
// To make an emitter play when spawned in editor mode // To make an emitter play when spawned in editor mode
if (m_EditorEnabled) { if (m_EditorEnabled) {
// Path changed // Path changed
if (m_Sources[emitter]->SoundResource->Path() != (std::string)(*it)["FilePath"]) { if (i->second->SoundResource->Path() != (std::string)(*it)["FilePath"]) {
m_Sources[emitter]->SoundResource = ResourceManager::Load<Sound>((std::string)(*it)["FilePath"]); i->second->SoundResource = ResourceManager::Load<Sound>((std::string)(*it)["FilePath"]);
playSound(m_Sources[emitter]); if (i->second->SoundResource->Buffer() != 0) {
playSound(i->second);
}
} }
} }
it++; it++;
@@ -203,6 +207,7 @@ void SoundSystem::playSound(Source* source)
{ {
alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer()); alSourcei(source->ALsource, AL_BUFFER, source->SoundResource->Buffer());
alSourcePlay(source->ALsource); alSourcePlay(source->ALsource);
source->HasBeenPlayed = true;
} }
void SoundSystem::stopSound(Source* source) void SoundSystem::stopSound(Source* source)