Fixed OpenAL Clean-up device. Added playBGM-event.
This commit is contained in:
@@ -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<Sound>(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<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;
|
||||
}
|
||||
|
||||
bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event)
|
||||
{
|
||||
//Check which entity has the collisionSound component.
|
||||
auto collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity1);
|
||||
if (collisionSound == NULL) {
|
||||
if (collisionSound == nullptr) {
|
||||
collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity2);
|
||||
if (collisionSound == NULL) {
|
||||
if (collisionSound == nullptr) {
|
||||
//None of the entities had a collisionSound component.
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user