#include "SoundManager.h"
SoundManager::SoundManager()
: m_Running(true)
, m_Thread(std::bind(&SoundManager::th_worker, this))
{
}
SoundManager::~SoundManager()
{
m_Running = false;
m_Thread.join();
}
void SoundManager::th_worker()
{
while (m_Running)
{
{
lock_t lock(m_Mutex);
for (auto& tuple_sound : m_ListSounds)
{
auto& queue_sound = std::get<1>(tuple_sound.second);
if (!queue_sound.empty())
{
if (queue_sound.front().getStatus() == sf::Sound::Status::Stopped)
{
queue_sound.pop();
}
}
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
void SoundManager::registerSound(const sound_id_t id, const sf::SoundBuffer& sound_buffer)
{
m_ListSounds.insert
(
std::make_pair
(
id,
std::make_tuple
(
sf::Sound
(
sound_buffer
),
std::queue<sf::Sound>()
)
)
);
}
void SoundManager::play(const sound_id_t id)
{
lock_t lock(m_Mutex);
auto& iter = m_ListSounds.find(id);
assert(iter != m_ListSounds.end());
auto& queue_sounds = std::get<1>(iter->second);
queue_sounds.push(std::get<0>(iter->second));
queue_sounds.back().play();
}