Stop sound event up and running.
This commit is contained in:
Binary file not shown.
@@ -27,7 +27,7 @@ namespace dd
|
|||||||
namespace Events
|
namespace Events
|
||||||
{
|
{
|
||||||
|
|
||||||
struct PlaySFX : Event
|
struct PlaySound : Event
|
||||||
{
|
{
|
||||||
std::string path;
|
std::string path;
|
||||||
float volume = 1.f;
|
float volume = 1.f;
|
||||||
@@ -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
|
||||||
@@ -14,6 +14,7 @@ class Sound : public Resource
|
|||||||
friend class ResourceManager;
|
friend class ResourceManager;
|
||||||
public:
|
public:
|
||||||
ALuint Buffer() { return m_Buffer; };
|
ALuint Buffer() { return m_Buffer; };
|
||||||
|
std::string Path() { return m_Path; };
|
||||||
private:
|
private:
|
||||||
Sound(std::string path);
|
Sound(std::string path);
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ private:
|
|||||||
ALuint LoadFile(std::string path);
|
ALuint LoadFile(std::string path);
|
||||||
|
|
||||||
ALuint m_Buffer;
|
ALuint m_Buffer;
|
||||||
|
std::string m_Path;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,11 @@
|
|||||||
#include "AL/alc.h"
|
#include "AL/alc.h"
|
||||||
#include "Core/System.h"
|
#include "Core/System.h"
|
||||||
#include "Core/World.h"
|
#include "Core/World.h"
|
||||||
#include "Sound.h"
|
|
||||||
#include "Sound/EPlaySFX.h"
|
|
||||||
#include "Physics/EContact.h"
|
|
||||||
#include "Core/EventBroker.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/CBall.h"
|
||||||
#include "Game/CBrick.h"
|
#include "Game/CBrick.h"
|
||||||
#include "Game/CPad.h"
|
#include "Game/CPad.h"
|
||||||
@@ -34,19 +35,20 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
//Events
|
//Events
|
||||||
dd::EventRelay<SoundSystem, dd::Events::PlaySFX> m_EPlaySFX;
|
dd::EventRelay<SoundSystem, dd::Events::PlaySound> m_EPlaySFX;
|
||||||
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
|
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
|
||||||
bool OnPlaySFX(const dd::Events::PlaySFX &event);
|
dd::EventRelay<SoundSystem, dd::Events::StopSound> m_EStopSound;
|
||||||
|
bool OnPlaySound(const dd::Events::PlaySound &event);
|
||||||
bool OnContact(const dd::Events::Contact &event);
|
bool OnContact(const dd::Events::Contact &event);
|
||||||
|
bool OnStopSound(const dd::Events::StopSound &event);
|
||||||
|
|
||||||
ALuint CreateSource();
|
ALuint CreateSource();
|
||||||
|
|
||||||
std::map<ALuint, ALuint> m_SourcesToBuffers;
|
std::map<ALuint, Sound*> m_SourcesToBuffers;
|
||||||
|
|
||||||
ALCdevice* m_Device;
|
ALCdevice* m_Device;
|
||||||
|
|
||||||
//Temp
|
//Temp
|
||||||
ALuint m_Source, m_BGMSource, m_BGMSupportSource;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
dd::Sound::Sound(std::string path)
|
dd::Sound::Sound(std::string path)
|
||||||
{
|
{
|
||||||
m_Buffer = LoadFile(path);
|
m_Buffer = LoadFile(path);
|
||||||
|
m_Path = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALuint dd::Sound::LoadFile(std::string path)
|
ALuint dd::Sound::LoadFile(std::string path)
|
||||||
|
|||||||
@@ -3,13 +3,12 @@
|
|||||||
|
|
||||||
dd::Systems::SoundSystem::~SoundSystem()
|
dd::Systems::SoundSystem::~SoundSystem()
|
||||||
{
|
{
|
||||||
alSourcei(m_Source, AL_BUFFER, 0);
|
|
||||||
alDeleteSources(1, &m_Source);
|
|
||||||
alcCloseDevice(m_Device);
|
alcCloseDevice(m_Device);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dd::Systems::SoundSystem::Initialize()
|
void dd::Systems::SoundSystem::Initialize()
|
||||||
{ //initialize OpenAL
|
{
|
||||||
|
//initialize OpenAL
|
||||||
m_Device = alcOpenDevice(NULL);
|
m_Device = alcOpenDevice(NULL);
|
||||||
ALCcontext* context;
|
ALCcontext* context;
|
||||||
|
|
||||||
@@ -23,31 +22,23 @@ void dd::Systems::SoundSystem::Initialize()
|
|||||||
|
|
||||||
alGetError();
|
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};
|
const ALfloat pos[3] = {0, 0, 0};
|
||||||
alListenerfv(AL_POSITION, pos);
|
alListenerfv(AL_POSITION, pos);
|
||||||
alSourcefv(m_Source, AL_POSITION, pos);
|
|
||||||
|
|
||||||
//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::OnPlaySound);
|
||||||
|
EVENT_SUBSCRIBE_MEMBER(m_EStopSound, &SoundSystem::OnStopSound);
|
||||||
|
|
||||||
//Todo: Move this
|
//Todo: Move this
|
||||||
{
|
{
|
||||||
dd::Events::PlaySFX e;
|
dd::Events::PlaySound e;
|
||||||
e.path = "Sounds/BGM/soft-guitar.wav";
|
e.path = "Sounds/BGM/soft-guitar.wav";
|
||||||
e.loop = true;
|
e.loop = true;
|
||||||
EventBroker->Publish(e);
|
EventBroker->Publish(e);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
dd::Events::PlaySFX e;
|
dd::Events::PlaySound e;
|
||||||
e.path = "Sounds/BGM/water-flowing.wav";
|
e.path = "Sounds/BGM/water-flowing.wav";
|
||||||
e.volume = 0.3f;
|
e.volume = 0.3f;
|
||||||
e.loop = true;
|
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
|
//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();
|
if (sound == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
ALuint source = CreateSource();
|
ALuint source = CreateSource();
|
||||||
m_SourcesToBuffers[source] = buffer;
|
m_SourcesToBuffers[source] = sound;
|
||||||
|
ALuint buffer = sound->Buffer();
|
||||||
alSourcei(source, AL_BUFFER, buffer);
|
alSourcei(source, AL_BUFFER, buffer);
|
||||||
|
|
||||||
//Sound settings
|
//Sound settings
|
||||||
@@ -98,6 +100,18 @@ bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event)
|
|||||||
|
|
||||||
//Play
|
//Play
|
||||||
alSourcePlay(source);
|
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)
|
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
|
//Send play-sound event
|
||||||
dd::Events::PlaySFX e;
|
dd::Events::PlaySound e;
|
||||||
e.path = collisionSound->filePath;
|
e.path = collisionSound->filePath;
|
||||||
EventBroker->Publish(e);
|
EventBroker->Publish(e);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALuint dd::Systems::SoundSystem::CreateSource()
|
|
||||||
{
|
|
||||||
ALuint source;
|
|
||||||
alGenSources((ALuint)1, &source);
|
|
||||||
|
|
||||||
return source;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user