diff --git a/assets/Sounds/BGM/under-the-sea-instrumental.wav b/assets/Sounds/BGM/under-the-sea-instrumental.wav new file mode 100644 index 0000000..e8d2983 Binary files /dev/null and b/assets/Sounds/BGM/under-the-sea-instrumental.wav differ diff --git a/include/Sound/EPlaySFX.h b/include/Sound/EPlaySound.h similarity index 93% rename from include/Sound/EPlaySFX.h rename to include/Sound/EPlaySound.h index 8ab697a..a5457f0 100644 --- a/include/Sound/EPlaySFX.h +++ b/include/Sound/EPlaySound.h @@ -27,7 +27,7 @@ namespace dd namespace Events { -struct PlaySFX : Event +struct PlaySound : Event { std::string path; float volume = 1.f; diff --git a/include/Sound/EStopSound.h b/include/Sound/EStopSound.h new file mode 100644 index 0000000..81ff474 --- /dev/null +++ b/include/Sound/EStopSound.h @@ -0,0 +1,23 @@ +#ifndef EVENTS_ESTOPSOUND_H__ +#define EVENTS_ESTOPSOUND_H__ + +#include "Core/EventBroker.h" + +namespace dd +{ + +namespace Events +{ + +struct StopSound : public Event +{ + std::string path; +}; + + +} + + +} + +#endif diff --git a/include/Sound/Sound.h b/include/Sound/Sound.h index 328925a..7084e64 100644 --- a/include/Sound/Sound.h +++ b/include/Sound/Sound.h @@ -14,6 +14,7 @@ class Sound : public Resource friend class ResourceManager; public: ALuint Buffer() { return m_Buffer; }; + std::string Path() { return m_Path; }; private: Sound(std::string path); @@ -28,6 +29,7 @@ private: ALuint LoadFile(std::string path); ALuint m_Buffer; + std::string m_Path; }; diff --git a/include/Sound/SoundSystem.h b/include/Sound/SoundSystem.h index 5a68300..af70ed6 100644 --- a/include/Sound/SoundSystem.h +++ b/include/Sound/SoundSystem.h @@ -6,10 +6,11 @@ #include "AL/alc.h" #include "Core/System.h" #include "Core/World.h" -#include "Sound.h" -#include "Sound/EPlaySFX.h" -#include "Physics/EContact.h" #include "Core/EventBroker.h" +#include "Sound.h" +#include "Sound/EPlaySound.h" +#include "Sound/EStopSound.h" +#include "Physics/EContact.h" #include "Game/CBall.h" #include "Game/CBrick.h" #include "Game/CPad.h" @@ -34,19 +35,20 @@ public: private: //Events - dd::EventRelay m_EPlaySFX; + dd::EventRelay m_EPlaySFX; dd::EventRelay m_EContact; - bool OnPlaySFX(const dd::Events::PlaySFX &event); + dd::EventRelay m_EStopSound; + bool OnPlaySound(const dd::Events::PlaySound &event); bool OnContact(const dd::Events::Contact &event); + bool OnStopSound(const dd::Events::StopSound &event); ALuint CreateSource(); - std::map m_SourcesToBuffers; + std::map m_SourcesToBuffers; ALCdevice* m_Device; //Temp - ALuint m_Source, m_BGMSource, m_BGMSupportSource; }; } diff --git a/src/game/Sound/Sound.cpp b/src/game/Sound/Sound.cpp index d2012c3..972f71b 100644 --- a/src/game/Sound/Sound.cpp +++ b/src/game/Sound/Sound.cpp @@ -3,6 +3,7 @@ dd::Sound::Sound(std::string path) { m_Buffer = LoadFile(path); + m_Path = path; } ALuint dd::Sound::LoadFile(std::string path) diff --git a/src/game/Sound/SoundSystem.cpp b/src/game/Sound/SoundSystem.cpp index db60ea9..c471f07 100644 --- a/src/game/Sound/SoundSystem.cpp +++ b/src/game/Sound/SoundSystem.cpp @@ -3,13 +3,12 @@ dd::Systems::SoundSystem::~SoundSystem() { - alSourcei(m_Source, AL_BUFFER, 0); - alDeleteSources(1, &m_Source); alcCloseDevice(m_Device); } void dd::Systems::SoundSystem::Initialize() -{ //initialize OpenAL +{ + //initialize OpenAL m_Device = alcOpenDevice(NULL); ALCcontext* context; @@ -23,31 +22,23 @@ void dd::Systems::SoundSystem::Initialize() alGetError(); - //Create sources - m_Source = CreateSource(); - m_BGMSource = CreateSource(); - m_BGMSupportSource = CreateSource(); - - alSpeedOfSound(340.29f); // Speed of sound m/s - alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); - const ALfloat pos[3] = {0, 0, 0}; alListenerfv(AL_POSITION, pos); - alSourcefv(m_Source, AL_POSITION, pos); //Subscribe to events EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact); - EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySFX); + EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySound); + EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound); //Todo: Move this { - dd::Events::PlaySFX e; + dd::Events::PlaySound e; e.path = "Sounds/BGM/soft-guitar.wav"; e.loop = true; EventBroker->Publish(e); } { - dd::Events::PlaySFX e; + dd::Events::PlaySound e; e.path = "Sounds/BGM/water-flowing.wav"; e.volume = 0.3f; e.loop = true; @@ -76,13 +67,24 @@ void dd::Systems::SoundSystem::Update(double dt) } -bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event) +ALuint dd::Systems::SoundSystem::CreateSource() +{ + ALuint source; + alGenSources((ALuint)1, &source); + + return source; +} + +bool dd::Systems::SoundSystem::OnPlaySound(const dd::Events::PlaySound &event) { //Loading and binding sound buffer to source Sound *sound = ResourceManager::Load(event.path); - ALuint buffer = sound->Buffer(); + if (sound == nullptr) { + return false; + } ALuint source = CreateSource(); - m_SourcesToBuffers[source] = buffer; + m_SourcesToBuffers[source] = sound; + ALuint buffer = sound->Buffer(); alSourcei(source, AL_BUFFER, buffer); //Sound settings @@ -98,6 +100,18 @@ bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event) //Play alSourcePlay(source); + return true; +} + +bool dd::Systems::SoundSystem::OnStopSound(const dd::Events::StopSound &event) +{ + for (auto item : m_SourcesToBuffers) { + if (item.second->Path() == event.path) { + alSourceStop(item.first); + return true; + } + } + return false; } bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event) @@ -112,17 +126,16 @@ bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event) } } + { + dd::Events::StopSound e; + e.path = "Sounds/BGM/soft-guitar.wav"; + EventBroker->Publish(e); + } //Send play-sound event - dd::Events::PlaySFX e; + dd::Events::PlaySound e; e.path = collisionSound->filePath; EventBroker->Publish(e); return true; } -ALuint dd::Systems::SoundSystem::CreateSource() -{ - ALuint source; - alGenSources((ALuint)1, &source); - return source; -}