From f10749d220fb08939e08f24b19134f62e366d8f7 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Mon, 28 Sep 2015 17:59:57 +0200 Subject: [PATCH] Refactoring SoundSystem and its events, components --- include/Sound/CCollisionSound.h | 8 +-- include/Sound/EMasterVolume.h | 10 +--- include/Sound/EPlaySound.h | 14 ++--- include/Sound/EStopSound.h | 6 +- include/Sound/Sound.h | 9 +-- include/Sound/SoundSystem.h | 41 +++++++------- src/game/Core/OBJ.cpp | 6 +- src/game/Game/LevelSystem.cpp | 2 +- src/game/Sound/SoundSystem.cpp | 97 +++++++++++++-------------------- 9 files changed, 77 insertions(+), 116 deletions(-) diff --git a/include/Sound/CCollisionSound.h b/include/Sound/CCollisionSound.h index 4a631a3..3bf312a 100644 --- a/include/Sound/CCollisionSound.h +++ b/include/Sound/CCollisionSound.h @@ -1,7 +1,3 @@ -// -// Created by Adam on 2015-09-17. -// - #ifndef COMPONENTS_CCOLLISIONSOUND_H__ #define COMPONENTS_CCOLLISIONSOUND_H__ @@ -9,17 +5,15 @@ namespace dd { - namespace Components { struct CollisionSound : public Component { - std::string filePath; + std::string FilePath; }; } - } #endif diff --git a/include/Sound/EMasterVolume.h b/include/Sound/EMasterVolume.h index 3f95e6d..6c7999b 100644 --- a/include/Sound/EMasterVolume.h +++ b/include/Sound/EMasterVolume.h @@ -1,7 +1,3 @@ -// -// Created by Adam on 2015-09-24. -// - #ifndef EVENTS_EMASTERVOLUME_H__ #define EVENTS_EMASTERVOLUME_H__ @@ -9,19 +5,17 @@ namespace dd { - namespace Events { struct MasterVolume : public Event { - float gain = 1; + float Gain = 1; //To determine what channel group to apply the change to. - bool isAmbient = false; + bool IsAmbient = false; }; } - } #endif diff --git a/include/Sound/EPlaySound.h b/include/Sound/EPlaySound.h index a66300d..ed7ab76 100644 --- a/include/Sound/EPlaySound.h +++ b/include/Sound/EPlaySound.h @@ -1,24 +1,22 @@ -#ifndef Events_PlaySFX_h__ -#define Events_PlaySFX_h__ +#ifndef EVENTS_PLAYSFX_H__ +#define EVENTS_PLAYSFX_H__ #include "Core/EventBroker.h" namespace dd { - namespace Events { struct PlaySound : Event { - std::string path; - float volume = 1.f; - float pitch = 1.f; - bool isAmbient = false; + std::string FilePath; + float Gain = 1.f; + float Pitch = 1.f; + bool IsAmbient = false; }; } - } #endif \ No newline at end of file diff --git a/include/Sound/EStopSound.h b/include/Sound/EStopSound.h index 81ff474..8f7bbd8 100644 --- a/include/Sound/EStopSound.h +++ b/include/Sound/EStopSound.h @@ -8,16 +8,12 @@ namespace dd namespace Events { - struct StopSound : public Event { - std::string path; + std::string FilePath; }; - } - - } #endif diff --git a/include/Sound/Sound.h b/include/Sound/Sound.h index 7084e64..0992948 100644 --- a/include/Sound/Sound.h +++ b/include/Sound/Sound.h @@ -12,12 +12,17 @@ namespace dd class Sound : public Resource { friend class ResourceManager; + public: ALuint Buffer() { return m_Buffer; }; std::string Path() { return m_Path; }; + private: Sound(std::string path); + ALuint m_Buffer; + std::string m_Path; + //File-info char m_Type[4]; unsigned long m_Size, m_ChunkSize; @@ -27,12 +32,8 @@ private: unsigned long m_DataSize; std::map m_BufferCache; ALuint LoadFile(std::string path); - - ALuint m_Buffer; - std::string m_Path; }; - } #endif diff --git a/include/Sound/SoundSystem.h b/include/Sound/SoundSystem.h index 32dd05e..89713f5 100644 --- a/include/Sound/SoundSystem.h +++ b/include/Sound/SoundSystem.h @@ -1,61 +1,60 @@ -#ifndef DAYDREAM_SOUND_H -#define DAYDREAM_SOUND_H - +#ifndef SYSTEMS_SOUNDSYSTEM_H__ +#define SYSTEMS_SOUNDSYSTEM_H__ #include "AL/al.h" #include "AL/alc.h" + #include "Core/System.h" #include "Core/World.h" #include "Core/EventBroker.h" +#include "Core/ResourceManager.h" #include "Sound.h" #include "Sound/EPlaySound.h" #include "Sound/EStopSound.h" #include "Sound/EMasterVolume.h" +#include "Sound/CCollisionSound.h" #include "Physics/EContact.h" #include "Game/CBall.h" #include "Game/CBrick.h" #include "Game/CPad.h" -#include "Sound/CCollisionSound.h" -#include "Core/ResourceManager.h" - namespace dd { namespace Systems { + class SoundSystem : public System { public: SoundSystem(World *world, std::shared_ptr eventBroker) : System(world, eventBroker) { } + ~SoundSystem(); + void Initialize() override; void Update(double dt) override; - - - private: + float m_BGMMasterVolume = 1.f; + float m_SFXMasterVolume = 1.f; + std::map m_BGMSourcesToBuffers; + std::map m_SFXSourcesToBuffers; + ALCdevice* m_Device = nullptr; + //Events dd::EventRelay m_EPlaySFX; - dd::EventRelay m_EContact; - dd::EventRelay m_EStopSound; - dd::EventRelay m_EMasterVolume; bool OnPlaySound(const dd::Events::PlaySound &event); + //On contact: play the sound given in the CCOllisionSound. + dd::EventRelay m_EContact; bool OnContact(const dd::Events::Contact &event); + dd::EventRelay m_EStopSound; bool OnStopSound(const dd::Events::StopSound &event); + dd::EventRelay m_EMasterVolume; bool OnMasterVolume(const dd::Events::MasterVolume &event); ALuint CreateSource(); - - std::map m_BGMSourcesToBuffers; - std::map m_SFXSourcesToBuffers; - - ALCdevice* m_Device; - - float m_BGMMasterVolume, m_SFXMasterVolume; - }; + } } -#endif //DAYDREAM_SOUND_H +#endif diff --git a/src/game/Core/OBJ.cpp b/src/game/Core/OBJ.cpp index fe6a818..0dd75a5 100644 --- a/src/game/Core/OBJ.cpp +++ b/src/game/Core/OBJ.cpp @@ -263,7 +263,7 @@ void dd::OBJ::ParseMaterial() ParseColorMap(currentLine, ss, prefix, arg, colorMap); } - // HACK: Should we really have to specify the full path here? + // HACK: Should we really have to specify the full FilePath here? colorMap.FileName = (m_MaterialPath.branch_path() / colorMap.FileName).string(); currentMaterial->DiffuseTexture = colorMap; continue; @@ -280,7 +280,7 @@ void dd::OBJ::ParseMaterial() ParseColorMap(currentLine, ss, prefix, arg, colorMap); } - // HACK: Should we really have to specify the full path here? + // HACK: Should we really have to specify the full FilePath here? colorMap.FileName = (m_MaterialPath.branch_path() / colorMap.FileName).string(); currentMaterial->SpecularMap = colorMap; continue; @@ -297,7 +297,7 @@ void dd::OBJ::ParseMaterial() ParseBumpMap(currentLine, ss, prefix, arg, bumpMap); } - // HACK: Should we really have to specify the full path here? + // HACK: Should we really have to specify the full FilePath here? bumpMap.FileName = (m_MaterialPath.branch_path() / bumpMap.FileName).string(); currentMaterial->NormalMap = bumpMap; continue; diff --git a/src/game/Game/LevelSystem.cpp b/src/game/Game/LevelSystem.cpp index 0389cca..86451b3 100755 --- a/src/game/Game/LevelSystem.cpp +++ b/src/game/Game/LevelSystem.cpp @@ -191,7 +191,7 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe //sound auto collisionSound = m_World->AddComponent(brick); - collisionSound->filePath = "Sounds/Brick/shortbrickbreak.wav"; + collisionSound->FilePath = "Sounds/Brick/shortbrickbreak.wav"; m_World->CommitEntity(brick); return; diff --git a/src/game/Sound/SoundSystem.cpp b/src/game/Sound/SoundSystem.cpp index 8594d92..8fb89fd 100644 --- a/src/game/Sound/SoundSystem.cpp +++ b/src/game/Sound/SoundSystem.cpp @@ -4,7 +4,7 @@ dd::Systems::SoundSystem::~SoundSystem() { alcCloseDevice(m_Device); -} +}; void dd::Systems::SoundSystem::Initialize() { @@ -19,41 +19,26 @@ void dd::Systems::SoundSystem::Initialize() else { LOG_ERROR("OpenAL failed to initialize."); } - alGetError(); - //Probably unnecessary. vec3(0) probably default. - const ALfloat pos[3] = {0, 0, 0}; - //alListenerfv(AL_POSITION, pos); - - m_SFXMasterVolume = 1.f; - m_BGMMasterVolume = 1.f; - //Subscribe to events EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact); EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySound); EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound); EVENT_SUBSCRIBE_MEMBER(m_EMasterVolume, &SoundSystem::OnMasterVolume); - //Todo: Move this { dd::Events::PlaySound e; - e.path = "Sounds/BGM/under-the-sea-instrumental.wav"; - e.isAmbient = true; + e.FilePath = "Sounds/BGM/under-the-sea-instrumental.wav"; + e.IsAmbient = true; EventBroker->Publish(e); } { dd::Events::PlaySound e; - e.path = "Sounds/BGM/water-flowing.wav"; - e.volume = 0.3f; - e.isAmbient = true; - EventBroker->Publish(e); - } - { - dd::Events::MasterVolume e; - e.isAmbient = true; - e.gain = 1.f; + e.FilePath = "Sounds/BGM/water-flowing.wav"; + e.Gain = 0.3f; + e.IsAmbient = true; EventBroker->Publish(e); } } @@ -63,7 +48,7 @@ void dd::Systems::SoundSystem::Update(double dt) //Clean up none-active sources //Only used for SFX's. BGM's are handled on stop sound. std::vector deleteList; - for (auto item : m_SFXSourcesToBuffers) { + for (auto& item : m_SFXSourcesToBuffers) { ALint sourceState; alGetSourcei(item.first, AL_SOURCE_STATE, &sourceState); if (sourceState == AL_STOPPED) { @@ -89,7 +74,7 @@ ALuint dd::Systems::SoundSystem::CreateSource() bool dd::Systems::SoundSystem::OnPlaySound(const dd::Events::PlaySound &event) { //Loading and binding sound buffer to source - Sound *sound = ResourceManager::Load(event.path); + Sound *sound = ResourceManager::Load(event.FilePath); if (sound == nullptr) { return false; } @@ -99,21 +84,21 @@ bool dd::Systems::SoundSystem::OnPlaySound(const dd::Events::PlaySound &event) //Sound settings float relativeVolume = 1.f; - if (event.isAmbient) { + if (event.IsAmbient) { alSourcei(source, AL_LOOPING, AL_TRUE); 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); relativeVolume = m_SFXMasterVolume; } - alSourcef(source, AL_GAIN, (event.volume * relativeVolume)); - alSourcef(source, AL_PITCH, event.pitch); + alSourcef(source, AL_GAIN, (event.Gain * relativeVolume)); + alSourcef(source, AL_PITCH, event.Pitch); //Play @@ -123,53 +108,52 @@ bool dd::Systems::SoundSystem::OnPlaySound(const dd::Events::PlaySound &event) bool dd::Systems::SoundSystem::OnStopSound(const dd::Events::StopSound &event) { - - //TODO: Delete sources for bgms - ALuint itemToDelete; - for (auto item : m_BGMSourcesToBuffers) + ALuint itemToDeleted; + for (auto& item : m_BGMSourcesToBuffers) { - if (item.second->Path() == event.path) { - itemToDelete = item.first; + if (item.second->Path() == event.FilePath) { + itemToDeleted = item.first; break; } } - alSourceStop(itemToDelete); - alDeleteSources(1, &itemToDelete); - m_BGMSourcesToBuffers.erase(itemToDelete); + alSourceStop(itemToDeleted); + alDeleteSources(1, &itemToDeleted); + m_BGMSourcesToBuffers.erase(itemToDeleted); - //Should not because SFX's should be very short. + //Should not happen because SFX's should be very short. for (auto item : m_SFXSourcesToBuffers) { - if (item.second->Path() == event.path) { + if (item.second->Path() == event.FilePath) { alSourceStop(item.first); return true; } } - return false; } bool dd::Systems::SoundSystem::OnMasterVolume(const dd::Events::MasterVolume &event) { - //TODO: Make the volume depend on the value given when stored. - //TODO: .. now it ONLY uses the master volume - if (event.isAmbient) { - m_BGMMasterVolume = event.gain; - for (auto item : m_BGMSourcesToBuffers) + //TODO: Make the Gain depend on the value given when stored. + //TODO: .. now it ONLY uses the master Gain + if (event.IsAmbient) { + m_BGMMasterVolume = event.Gain; + for (auto& item : m_BGMSourcesToBuffers) { - alSourcef(item.first, AL_GAIN, event.gain); + alSourcef(item.first, AL_GAIN, event.Gain); } + return true; } - else if (!event.isAmbient) { - m_SFXMasterVolume = event.gain; - for (auto item : m_SFXSourcesToBuffers) + else if (!event.IsAmbient) { + m_SFXMasterVolume = event.Gain; + for (auto& item : m_SFXSourcesToBuffers) { - alSourcef(item.first, AL_GAIN, event.gain); + alSourcef(item.first, AL_GAIN, event.Gain); } + return true; } + return false; } -//On contact: play the sound given in the CCOllisionSound. bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event) { //Check which entity has the collisionSound component. @@ -182,18 +166,13 @@ 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 dd::Events::PlaySound e; - e.path = collisionSound->filePath; - e.isAmbient = false; + e.FilePath = collisionSound->FilePath; + e.IsAmbient = false; EventBroker->Publish(e); - //return true; + return true; } +