Deleting an entity now tells openal to behave accordingly.
This commit is contained in:
@@ -65,7 +65,7 @@ void SoundSystem::stopEmitters()
|
|||||||
|
|
||||||
void SoundSystem::Update()
|
void SoundSystem::Update()
|
||||||
{
|
{
|
||||||
//deleteInactiveEmitters(); // Not tested
|
deleteInactiveEmitters();
|
||||||
addNewEmitters();
|
addNewEmitters();
|
||||||
updateEmitters();
|
updateEmitters();
|
||||||
updateListener();
|
updateListener();
|
||||||
@@ -73,19 +73,33 @@ void SoundSystem::Update()
|
|||||||
|
|
||||||
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
|
// 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++;
|
it++;
|
||||||
continue;
|
continue;
|
||||||
}
|
} else {
|
||||||
else {
|
stopSound((*it).second);
|
||||||
alDeleteBuffers(1, &m_Sources[emitter]->ALsource);
|
alDeleteBuffers(1, &m_Sources[(*it).first]->ALsource);
|
||||||
alDeleteSources(1, &m_Sources[emitter]->ALsource);
|
alDeleteSources(1, &m_Sources[(*it).first]->ALsource);
|
||||||
//delete m_Sources[emitter]->SoundResource;
|
//delete m_Sources[emitter]->SoundResource;
|
||||||
m_Sources.erase(emitter);
|
it = m_Sources.erase(it);
|
||||||
m_World->DeleteEntity(emitter);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -99,7 +113,7 @@ void SoundSystem::addNewEmitters()
|
|||||||
i = m_Sources.find(emitter);
|
i = m_Sources.find(emitter);
|
||||||
if (i == m_Sources.end()) { // Did not exist, add it
|
if (i == m_Sources.end()) { // Did not exist, add it
|
||||||
Source* source = createSource((std::string)(*it)["FilePath"]);
|
Source* source = createSource((std::string)(*it)["FilePath"]);
|
||||||
setSoundProperties (
|
setSoundProperties(
|
||||||
source->ALsource,
|
source->ALsource,
|
||||||
(float)(double)(*it)["Gain"],
|
(float)(double)(*it)["Gain"],
|
||||||
(float)(double)(*it)["Pitch"],
|
(float)(double)(*it)["Pitch"],
|
||||||
@@ -107,7 +121,7 @@ void SoundSystem::addNewEmitters()
|
|||||||
(float)(double)(*it)["MaxDistance"],
|
(float)(double)(*it)["MaxDistance"],
|
||||||
(float)(double)(*it)["RollOffFactor"],
|
(float)(double)(*it)["RollOffFactor"],
|
||||||
(float)(double)(*it)["ReferenceDistance"]
|
(float)(double)(*it)["ReferenceDistance"]
|
||||||
);
|
);
|
||||||
m_Sources[emitter] = source;
|
m_Sources[emitter] = source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,37 +130,42 @@ void SoundSystem::addNewEmitters()
|
|||||||
void SoundSystem::updateEmitters()
|
void SoundSystem::updateEmitters()
|
||||||
{
|
{
|
||||||
auto emitterComponents = m_World->GetComponents("SoundEmitter");
|
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;
|
EntityID emitter = (*it).EntityID;
|
||||||
std::unordered_map<EntityID, Source*>::iterator i;
|
if (!m_World->ValidEntity(emitter)) { // Entity has been deleted
|
||||||
i = m_Sources.find(emitter);
|
// Delete
|
||||||
if (i != m_Sources.end()) {
|
} else {
|
||||||
// Get previous pos
|
std::unordered_map<EntityID, Source*>::iterator i;
|
||||||
glm::vec3 previousPos;
|
i = m_Sources.find(emitter);
|
||||||
alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z);
|
if (i != m_Sources.end()) {
|
||||||
// Get next pos
|
// Get previous pos
|
||||||
glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter);
|
glm::vec3 previousPos;
|
||||||
// Calculate velocity
|
alGetSource3f(m_Sources[emitter]->ALsource, AL_POSITION, &previousPos.x, &previousPos.y, &previousPos.z);
|
||||||
glm::vec3 velocity = nextPos - previousPos;
|
// Get next pos
|
||||||
setSourcePos(m_Sources[emitter]->ALsource, nextPos);
|
glm::vec3 nextPos = RenderQueueFactory::AbsolutePosition(m_World, emitter);
|
||||||
setSourceVel(m_Sources[emitter]->ALsource, velocity);
|
// Calculate velocity
|
||||||
setSoundProperties(
|
glm::vec3 velocity = nextPos - previousPos;
|
||||||
m_Sources[emitter]->ALsource,
|
setSourcePos(m_Sources[emitter]->ALsource, nextPos);
|
||||||
(float)(double)(*it)["Gain"],
|
setSourceVel(m_Sources[emitter]->ALsource, velocity);
|
||||||
(float)(double)(*it)["Pitch"],
|
setSoundProperties(
|
||||||
(bool)(*it)["Loop"],
|
m_Sources[emitter]->ALsource,
|
||||||
(float)(double)(*it)["MaxDistance"],
|
(float)(double)(*it)["Gain"],
|
||||||
(float)(double)(*it)["RollOffFactor"],
|
(float)(double)(*it)["Pitch"],
|
||||||
(float)(double)(*it)["ReferenceDistance"]
|
(bool)(*it)["Loop"],
|
||||||
);
|
(float)(double)(*it)["MaxDistance"],
|
||||||
|
(float)(double)(*it)["RollOffFactor"],
|
||||||
|
(float)(double)(*it)["ReferenceDistance"]
|
||||||
|
);
|
||||||
|
|
||||||
// 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 (m_Sources[emitter]->SoundResource->Path() != (std::string)(*it)["FilePath"]) {
|
||||||
m_Sources[emitter]->SoundResource = ResourceManager::Load<Sound>((std::string)(*it)["FilePath"]);
|
m_Sources[emitter]->SoundResource = ResourceManager::Load<Sound>((std::string)(*it)["FilePath"]);
|
||||||
playSound(m_Sources[emitter]);
|
playSound(m_Sources[emitter]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
it++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user