vectozavr-shooter/engine/SoundController.cpp

67 lines
1.5 KiB
C++
Raw Normal View History

2021-10-17 19:38:16 +03:00
//
// Created by Иван Ильин on 17.10.2021.
//
#include "SoundController.h"
#include "ResourceManager.h"
SoundController* SoundController::_instance = nullptr;
void SoundController::init() {
_instance = new SoundController();
}
void SoundController::playSound(const SoundTag& soundTag, const std::string& filename) {
2021-10-28 16:58:02 +03:00
if(!_instance) {
2021-10-17 19:38:16 +03:00
return;
2021-10-28 16:58:02 +03:00
}
2021-10-17 19:38:16 +03:00
stopSound(soundTag);
_instance->_sounds.emplace(soundTag, sf::Sound(*ResourceManager::loadSoundBuffer(filename)));
_instance->_sounds[soundTag].play();
}
void SoundController::pauseSound(const SoundTag& soundTag) {
2021-10-28 16:58:02 +03:00
if(!_instance) {
2021-10-17 19:38:16 +03:00
return;
2021-10-28 16:58:02 +03:00
}
2021-10-17 19:38:16 +03:00
if(_instance->_sounds.count(soundTag) > 0) {
_instance->_sounds[soundTag].pause();
}
}
void SoundController::stopSound(const SoundTag& soundTag) {
2021-10-28 16:58:02 +03:00
if(!_instance) {
2021-10-17 19:38:16 +03:00
return;
2021-10-28 16:58:02 +03:00
}
2021-10-17 19:38:16 +03:00
if(_instance->_sounds.count(soundTag) > 0) {
_instance->_sounds[soundTag].stop();
}
_instance->_sounds.erase(soundTag);
}
sf::Sound::Status SoundController::getStatus(const SoundTag& soundTag) {
2021-10-28 16:58:02 +03:00
if(_instance == nullptr) {
2021-10-17 19:38:16 +03:00
return sf::Sound::Status::Stopped;
2021-10-28 16:58:02 +03:00
}
2021-10-17 19:38:16 +03:00
2021-10-28 16:58:02 +03:00
if(_instance->_sounds.count(soundTag) > 0) {
2021-10-17 19:38:16 +03:00
return _instance->_sounds[soundTag].getStatus();
2021-10-28 16:58:02 +03:00
} else {
_instance->_sounds.erase(soundTag);
2021-10-17 19:38:16 +03:00
return sf::Sound::Status::Stopped;
}
2021-10-17 19:38:16 +03:00
}
void SoundController::free() {
for(auto& [soundTag, sound] : _instance->_sounds) {
sound.stop();
}
_instance->_sounds.clear();
delete _instance;
_instance = nullptr;
}