Updated playsound events.

This commit is contained in:
Stiffly
2015-09-23 12:08:05 +02:00
parent a010a8be77
commit 4f341250d7
4 changed files with 31 additions and 58 deletions
-25
View File
@@ -1,25 +0,0 @@
#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
+2
View File
@@ -30,6 +30,8 @@ namespace Events
struct PlaySFX : Event struct PlaySFX : Event
{ {
std::string path; std::string path;
float volume = 1.f;
bool loop = false;
}; };
} }
-3
View File
@@ -8,7 +8,6 @@
#include "Core/World.h" #include "Core/World.h"
#include "Sound.h" #include "Sound.h"
#include "Sound/EPlaySFX.h" #include "Sound/EPlaySFX.h"
#include "Sound/EPlayBGM.h"
#include "Physics/EContact.h" #include "Physics/EContact.h"
#include "Core/EventBroker.h" #include "Core/EventBroker.h"
#include "Game/CBall.h" #include "Game/CBall.h"
@@ -37,10 +36,8 @@ private:
//Events //Events
dd::EventRelay<SoundSystem, dd::Events::PlaySFX> m_EPlaySFX; dd::EventRelay<SoundSystem, dd::Events::PlaySFX> m_EPlaySFX;
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact; dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
dd::EventRelay<SoundSystem, dd::Events::PlayBGM> m_EPlayBGM;
bool OnPlaySFX(const dd::Events::PlaySFX &event); bool OnPlaySFX(const dd::Events::PlaySFX &event);
bool OnContact(const dd::Events::Contact &event); bool OnContact(const dd::Events::Contact &event);
bool OnPlayBGM(const dd::Events::PlayBGM &event);
ALuint CreateSource(); ALuint CreateSource();
+29 -30
View File
@@ -38,19 +38,21 @@ void dd::Systems::SoundSystem::Initialize()
//Subscribe to events //Subscribe to events
EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact); EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact);
EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySFX); EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySFX);
EVENT_SUBSCRIBE_MEMBER(m_EPlayBGM, &SoundSystem::OnPlayBGM);
//Todo: Move this //Todo: Move this
dd::Events::PlayBGM e; {
e.path = "Sounds/BGM/soft-guitar.wav"; dd::Events::PlaySFX e;
e.SupportBGM = false; e.path = "Sounds/BGM/soft-guitar.wav";
EventBroker->Publish(e); e.loop = true;
EventBroker->Publish(e);
dd::Events::PlayBGM e2; }
e2.path = "Sounds/BGM/water-flowing.wav"; {
e2.SupportBGM = true; dd::Events::PlaySFX e;
e2.volume = 0.3; e.path = "Sounds/BGM/water-flowing.wav";
EventBroker->Publish(e2); e.volume = 0.3f;
e.loop = true;
EventBroker->Publish(e);
}
} }
void dd::Systems::SoundSystem::Update(double dt) void dd::Systems::SoundSystem::Update(double dt)
@@ -74,31 +76,28 @@ void dd::Systems::SoundSystem::Update(double dt)
} }
bool dd::Systems::SoundSystem::OnPlayBGM(const dd::Events::PlayBGM &event)
{
Sound *sound = ResourceManager::Load<Sound>(event.path);
ALuint buffer = sound->Buffer();
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) bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event)
{ {
//Loading and binding sound buffer to source
Sound *sound = ResourceManager::Load<Sound>(event.path); Sound *sound = ResourceManager::Load<Sound>(event.path);
ALuint buffer = sound->Buffer(); ALuint buffer = sound->Buffer();
ALuint source = CreateSource(); ALuint source = CreateSource();
alSourcei(source, AL_BUFFER, buffer);
alSourcei(source, AL_LOOPING, AL_FALSE);
alSourcePlay(source);
m_SourcesToBuffers[source] = buffer; m_SourcesToBuffers[source] = buffer;
alSourcei(source, AL_BUFFER, buffer);
//Sound settings
alSourcef(source, AL_GAIN, event.volume);
if (event.loop) {
alSourcei(source, AL_LOOPING, AL_TRUE);
}
else if (!event.loop)
{
alSourcei(source, AL_LOOPING, AL_FALSE);
}
//Play
alSourcePlay(source);
} }
bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event) bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event)