Updated playsound events.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -30,6 +30,8 @@ namespace Events
|
||||
struct PlaySFX : Event
|
||||
{
|
||||
std::string path;
|
||||
float volume = 1.f;
|
||||
bool loop = false;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#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"
|
||||
@@ -37,10 +36,8 @@ private:
|
||||
//Events
|
||||
dd::EventRelay<SoundSystem, dd::Events::PlaySFX> m_EPlaySFX;
|
||||
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
|
||||
dd::EventRelay<SoundSystem, dd::Events::PlayBGM> 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();
|
||||
|
||||
|
||||
@@ -38,19 +38,21 @@ 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.3;
|
||||
EventBroker->Publish(e2);
|
||||
{
|
||||
dd::Events::PlaySFX e;
|
||||
e.path = "Sounds/BGM/soft-guitar.wav";
|
||||
e.loop = true;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
{
|
||||
dd::Events::PlaySFX e;
|
||||
e.path = "Sounds/BGM/water-flowing.wav";
|
||||
e.volume = 0.3f;
|
||||
e.loop = true;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
//Loading and binding sound buffer to source
|
||||
Sound *sound = ResourceManager::Load<Sound>(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;
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user