Refactoring. Splitting up storage and handling into SFX- and BGM sources.
This commit is contained in:
@@ -47,7 +47,8 @@ private:
|
|||||||
|
|
||||||
ALuint CreateSource();
|
ALuint CreateSource();
|
||||||
|
|
||||||
std::map<ALuint, Sound*> m_SourcesToBuffers;
|
std::map<ALuint, Sound*> m_BGMSourcesToBuffers;
|
||||||
|
std::map<ALuint, Sound*> m_SFXSourcesToBuffers;
|
||||||
|
|
||||||
ALCdevice* m_Device;
|
ALCdevice* m_Device;
|
||||||
|
|
||||||
|
|||||||
@@ -50,19 +50,20 @@ void dd::Systems::SoundSystem::Initialize()
|
|||||||
e.isAmbient = true;
|
e.isAmbient = true;
|
||||||
EventBroker->Publish(e);
|
EventBroker->Publish(e);
|
||||||
}
|
}
|
||||||
{
|
/*{
|
||||||
dd::Events::MasterVolume e;
|
dd::Events::MasterVolume e;
|
||||||
e.isAmbient = true;
|
e.isAmbient = true;
|
||||||
e.gain = 0.001f;
|
e.gain = 0.001f;
|
||||||
EventBroker->Publish(e);
|
EventBroker->Publish(e);
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void dd::Systems::SoundSystem::Update(double dt)
|
void dd::Systems::SoundSystem::Update(double dt)
|
||||||
{
|
{
|
||||||
//Clean up none-active sources
|
//Clean up none-active sources
|
||||||
|
//Only used for SFX's. BGM's are handled on stop sound.
|
||||||
std::vector<ALuint> deleteList;
|
std::vector<ALuint> deleteList;
|
||||||
for (auto item : m_SourcesToBuffers) {
|
for (auto item : m_SFXSourcesToBuffers) {
|
||||||
ALint sourceState;
|
ALint sourceState;
|
||||||
alGetSourcei(item.first, AL_SOURCE_STATE, &sourceState);
|
alGetSourcei(item.first, AL_SOURCE_STATE, &sourceState);
|
||||||
if (sourceState == AL_STOPPED) {
|
if (sourceState == AL_STOPPED) {
|
||||||
@@ -73,7 +74,7 @@ void dd::Systems::SoundSystem::Update(double dt)
|
|||||||
for (int i = 0; i < deleteList.size(); i++) {
|
for (int i = 0; i < deleteList.size(); i++) {
|
||||||
alDeleteSources(1, &deleteList[i]);
|
alDeleteSources(1, &deleteList[i]);
|
||||||
//alDeleteBuffers(1, &m_SourcesToBuffers[deleteList[i]]);
|
//alDeleteBuffers(1, &m_SourcesToBuffers[deleteList[i]]);
|
||||||
m_SourcesToBuffers.erase(deleteList[i]);
|
m_SFXSourcesToBuffers.erase(deleteList[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +94,6 @@ bool dd::Systems::SoundSystem::OnPlaySound(const dd::Events::PlaySound &event)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ALuint source = CreateSource();
|
ALuint source = CreateSource();
|
||||||
m_SourcesToBuffers[source] = sound;
|
|
||||||
ALuint buffer = sound->Buffer();
|
ALuint buffer = sound->Buffer();
|
||||||
alSourcei(source, AL_BUFFER, buffer);
|
alSourcei(source, AL_BUFFER, buffer);
|
||||||
|
|
||||||
@@ -102,9 +102,12 @@ bool dd::Systems::SoundSystem::OnPlaySound(const dd::Events::PlaySound &event)
|
|||||||
if (event.isAmbient) {
|
if (event.isAmbient) {
|
||||||
alSourcei(source, AL_LOOPING, AL_TRUE);
|
alSourcei(source, AL_LOOPING, AL_TRUE);
|
||||||
relativeVolume = m_BGMMasterVolume;
|
relativeVolume = m_BGMMasterVolume;
|
||||||
|
m_BGMSourcesToBuffers[source] = sound;
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (!event.isAmbient)
|
else if (!event.isAmbient)
|
||||||
{
|
{
|
||||||
|
m_SFXSourcesToBuffers[source] = sound;
|
||||||
alSourcei(source, AL_LOOPING, AL_FALSE);
|
alSourcei(source, AL_LOOPING, AL_FALSE);
|
||||||
relativeVolume = m_SFXMasterVolume;
|
relativeVolume = m_SFXMasterVolume;
|
||||||
}
|
}
|
||||||
@@ -120,12 +123,29 @@ bool dd::Systems::SoundSystem::OnPlaySound(const dd::Events::PlaySound &event)
|
|||||||
|
|
||||||
bool dd::Systems::SoundSystem::OnStopSound(const dd::Events::StopSound &event)
|
bool dd::Systems::SoundSystem::OnStopSound(const dd::Events::StopSound &event)
|
||||||
{
|
{
|
||||||
for (auto item : m_SourcesToBuffers) {
|
|
||||||
|
//TODO: Delete sources for bgms
|
||||||
|
ALuint itemToDelete;
|
||||||
|
for (auto item : m_BGMSourcesToBuffers)
|
||||||
|
{
|
||||||
|
if (item.second->Path() == event.path) {
|
||||||
|
itemToDelete = item.first;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
alSourceStop(itemToDelete);
|
||||||
|
alDeleteSources(1, &itemToDelete);
|
||||||
|
m_BGMSourcesToBuffers.erase(itemToDelete);
|
||||||
|
|
||||||
|
//Should not because SFX's should be very short.
|
||||||
|
for (auto item : m_SFXSourcesToBuffers)
|
||||||
|
{
|
||||||
if (item.second->Path() == event.path) {
|
if (item.second->Path() == event.path) {
|
||||||
alSourceStop(item.first);
|
alSourceStop(item.first);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,6 +173,12 @@ bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
dd::Events::StopSound e;
|
||||||
|
e.path = "Sounds/BGM/water-flowing.wav";
|
||||||
|
EventBroker->Publish(e);
|
||||||
|
}
|
||||||
|
|
||||||
//Send play-sound event
|
//Send play-sound event
|
||||||
dd::Events::PlaySound e;
|
dd::Events::PlaySound e;
|
||||||
e.path = collisionSound->filePath;
|
e.path = collisionSound->filePath;
|
||||||
|
|||||||
Reference in New Issue
Block a user