diff --git a/assets/Sounds/BGM/soft-guitar.wav b/assets/Sounds/BGM/soft-guitar.wav new file mode 100644 index 0000000..a537397 Binary files /dev/null and b/assets/Sounds/BGM/soft-guitar.wav differ diff --git a/assets/Sounds/BGM/water-flowing.wav b/assets/Sounds/BGM/water-flowing.wav new file mode 100644 index 0000000..5c5d671 Binary files /dev/null and b/assets/Sounds/BGM/water-flowing.wav differ diff --git a/include/Sound/EPlayBGM.h b/include/Sound/EPlayBGM.h new file mode 100644 index 0000000..7fdc059 --- /dev/null +++ b/include/Sound/EPlayBGM.h @@ -0,0 +1,25 @@ +#ifndef EVENTS_EPLAYBGM_H__ +#define EVENTS_EPLAYBGM_H__ + +#include "Core/EventBroker.h" + +namespace dd +{ + +namespace Events +{ + +struct PlayBGM : public Event +{ + std::string path; + float volume = 1; + //TODO: Rethink this + bool SupportBGM; +}; + +} + +} + +#endif + diff --git a/include/Sound/SoundSystem.h b/include/Sound/SoundSystem.h index 26d02e1..4468c9d 100644 --- a/include/Sound/SoundSystem.h +++ b/include/Sound/SoundSystem.h @@ -8,6 +8,7 @@ #include "Core/World.h" #include "Sound.h" #include "Sound/EPlaySFX.h" +#include "Sound/EPlayBGM.h" #include "Physics/EContact.h" #include "Core/EventBroker.h" #include "Game/CBall.h" @@ -36,15 +37,19 @@ private: //Events dd::EventRelay m_EPlaySFX; dd::EventRelay m_EContact; + dd::EventRelay m_EPlayBGM; bool OnPlaySFX(const dd::Events::PlaySFX &event); bool OnContact(const dd::Events::Contact &event); + bool OnPlayBGM(const dd::Events::PlayBGM &event); + ALuint CreateSource(); - std::map m_Sources; + std::map m_SourcesToBuffers; + + ALCdevice* m_Device; - //std::list //Temp - ALuint m_Source; + ALuint m_Source, m_BGMSource, m_BGMSupportSource; }; } diff --git a/src/game/Sound/SoundSystem.cpp b/src/game/Sound/SoundSystem.cpp index 8b25cea..e2da67a 100644 --- a/src/game/Sound/SoundSystem.cpp +++ b/src/game/Sound/SoundSystem.cpp @@ -5,15 +5,16 @@ dd::Systems::SoundSystem::~SoundSystem() { alSourcei(m_Source, AL_BUFFER, 0); alDeleteSources(1, &m_Source); + alcCloseDevice(m_Device); } void dd::Systems::SoundSystem::Initialize() { //initialize OpenAL - ALCdevice* device = alcOpenDevice(NULL); + m_Device = alcOpenDevice(NULL); ALCcontext* context; - if (device) { - context = alcCreateContext(device, NULL); + if (m_Device) { + context = alcCreateContext(m_Device, NULL); alcMakeContextCurrent(context); } else { @@ -22,8 +23,10 @@ void dd::Systems::SoundSystem::Initialize() alGetError(); - //Create source + //Create sources m_Source = CreateSource(); + m_BGMSource = CreateSource(); + m_BGMSupportSource = CreateSource(); alSpeedOfSound(340.29f); // Speed of sound m/s alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); @@ -35,6 +38,19 @@ void dd::Systems::SoundSystem::Initialize() //Subscribe to events EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact); EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySFX); + EVENT_SUBSCRIBE_MEMBER(m_EPlayBGM, &SoundSystem::OnPlayBGM); + + //Todo: Move this + dd::Events::PlayBGM e; + e.path = "Sounds/BGM/soft-guitar.wav"; + e.SupportBGM = false; + EventBroker->Publish(e); + + dd::Events::PlayBGM e2; + e2.path = "Sounds/BGM/water-flowing.wav"; + e2.SupportBGM = true; + e2.volume = 0.1; + EventBroker->Publish(e2); } void dd::Systems::SoundSystem::Update(double dt) @@ -42,22 +58,40 @@ void dd::Systems::SoundSystem::Update(double dt) } -bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event) +bool dd::Systems::SoundSystem::OnPlayBGM(const dd::Events::PlayBGM &event) { - //m_Source = CreateSource(); Sound *sound = ResourceManager::Load(event.path); ALuint buffer = sound->Buffer(); - alSourcei(m_Source, AL_BUFFER, buffer); - alSourcePlay(m_Source); + ALuint source = CreateSource(); + + alSourcei(source, AL_BUFFER, buffer); + alSourcei(source, AL_LOOPING, AL_TRUE); + alSourcef(source, AL_GAIN, event.volume); + alSourcePlay(source); + + m_SourcesToBuffers[source] = buffer; +} + +bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event) +{ + Sound *sound = ResourceManager::Load(event.path); + ALuint buffer = sound->Buffer(); + ALuint source = CreateSource(); + + alSourcei(source, AL_BUFFER, buffer); + alSourcei(source, AL_LOOPING, AL_FALSE); + alSourcePlay(source); + + m_SourcesToBuffers[source] = buffer; } bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event) { //Check which entity has the collisionSound component. auto collisionSound = m_World->GetComponent(event.Entity1); - if (collisionSound == NULL) { + if (collisionSound == nullptr) { collisionSound = m_World->GetComponent(event.Entity2); - if (collisionSound == NULL) { + if (collisionSound == nullptr) { //None of the entities had a collisionSound component. return false; }