Deleting an entity now tells openal to behave accordingly.

This commit is contained in:
stiffly
2016-01-12 14:51:10 +01:00
parent 729ebe6c8b
commit 5697e8bc85
+32 -13
View File
@@ -65,7 +65,7 @@ void SoundSystem::stopEmitters()
void SoundSystem::Update()
{
//deleteInactiveEmitters(); // Not tested
deleteInactiveEmitters();
addNewEmitters();
updateEmitters();
updateListener();
@@ -73,19 +73,33 @@ void SoundSystem::Update()
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
//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);
// }
//}
std::unordered_map<EntityID, Source*>::iterator it;
for (it = m_Sources.begin(); it != m_Sources.end();) {
if (m_World->ValidEntity((*it).first)) {
// Entity is valid, move on.
it++;
continue;
}
else {
alDeleteBuffers(1, &m_Sources[emitter]->ALsource);
alDeleteSources(1, &m_Sources[emitter]->ALsource);
} else {
stopSound((*it).second);
alDeleteBuffers(1, &m_Sources[(*it).first]->ALsource);
alDeleteSources(1, &m_Sources[(*it).first]->ALsource);
//delete m_Sources[emitter]->SoundResource;
m_Sources.erase(emitter);
m_World->DeleteEntity(emitter);
it = m_Sources.erase(it);
}
}
}
@@ -99,7 +113,7 @@ void SoundSystem::addNewEmitters()
i = m_Sources.find(emitter);
if (i == m_Sources.end()) { // Did not exist, add it
Source* source = createSource((std::string)(*it)["FilePath"]);
setSoundProperties (
setSoundProperties(
source->ALsource,
(float)(double)(*it)["Gain"],
(float)(double)(*it)["Pitch"],
@@ -116,8 +130,11 @@ void SoundSystem::addNewEmitters()
void SoundSystem::updateEmitters()
{
auto emitterComponents = m_World->GetComponents("SoundEmitter");
for (auto it = emitterComponents->begin(); it != emitterComponents->end(); it++) {
for (auto it = emitterComponents->begin(); it != emitterComponents->end();) {
EntityID emitter = (*it).EntityID;
if (!m_World->ValidEntity(emitter)) { // Entity has been deleted
// Delete
} else {
std::unordered_map<EntityID, Source*>::iterator i;
i = m_Sources.find(emitter);
if (i != m_Sources.end()) {
@@ -148,6 +165,8 @@ void SoundSystem::updateEmitters()
playSound(m_Sources[emitter]);
}
}
it++;
}
}
}
}