You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.8 KiB
C++

#include "audio.h"
Audio::Audio(const std::string &background_path, std::array<std::string, N_SOUNDS> &&sounds_paths)
{
std::unique_ptr<sf::Music> music = std::make_unique<sf::Music>();
if (music->openFromFile(background_path))
music->setLoop(true);
else
music = nullptr;
background_music = std::move(music);
std::unique_ptr<SoundEffect> effect;
for (int i = 0; i < N_SOUNDS; ++i)
{
effect = std::make_unique<SoundEffect>();
if (effect->buffer.loadFromFile(sounds_paths[i]))
{
effect->sound.setBuffer(effect->buffer);
array_sounds[i] = std::move(effect);
}
else
{
array_sounds[i] = nullptr;
}
}
}
bool Audio::setSound(const SOUND_TYPE &type, const std::string &sound_file_path)
{
std::unique_ptr<SoundEffect> effect = std::make_unique<SoundEffect>();
if (!effect->buffer.loadFromFile(sound_file_path))
return false;
effect->sound.setBuffer(effect->buffer);
array_sounds[type] = std::move(effect);
return true;
}
void Audio::playSound(const SOUND_TYPE &type)
{
if (array_sounds[type])
array_sounds[type]->sound.play();
}
bool Audio::setBackground(const std::string &music_file_path)
{
std::unique_ptr<sf::Music> music = std::make_unique<sf::Music>();
if (!music->openFromFile(music_file_path))
return false;
background_music = std::move(music);
return true;
}
void Audio::playBackground()
{
if (background_music)
background_music->play();
}
void Audio::stopBackground()
{
if (background_music)
background_music->stop();
}
void Audio::pauseBackground()
{
if (background_music)
background_music->pause();
}
void Audio::setBackgroundVolume(const float &volume)
{
if (background_music)
background_music->setVolume(volume);
}