2021-10-17 19:38:16 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 17.10.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "SoundController.h"
|
|
|
|
#include "ResourceManager.h"
|
2021-10-30 21:29:42 +03:00
|
|
|
#include "utils/Log.h"
|
2021-10-17 19:38:16 +03:00
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
SoundController *SoundController::_instance = nullptr;
|
2021-10-29 23:44:37 +03:00
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
|
|
|
|
void SoundController::init() {
|
2021-11-09 22:54:20 +03:00
|
|
|
delete _instance;
|
2021-10-17 19:38:16 +03:00
|
|
|
_instance = new SoundController();
|
2021-10-30 21:29:42 +03:00
|
|
|
|
|
|
|
Log::log("SoundController::init(): sound controller was initialized");
|
2021-10-17 19:38:16 +03:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:29:42 +03:00
|
|
|
void SoundController::loadAndPlay(const SoundTag &soundTag, const std::string& filename) {
|
2021-11-09 22:54:20 +03:00
|
|
|
if (_instance == nullptr) {
|
2021-10-17 19:38:16 +03:00
|
|
|
return;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2022-02-23 17:29:42 +03:00
|
|
|
if (_instance->_sounds.count(soundTag) != 0) {
|
|
|
|
_instance->_sounds[soundTag] = sf::Sound(*ResourceManager::loadSoundBuffer(filename));
|
|
|
|
} else {
|
2021-10-30 21:54:55 +03:00
|
|
|
_instance->_sounds.emplace(soundTag, sf::Sound(*ResourceManager::loadSoundBuffer(filename)));
|
|
|
|
}
|
2022-02-23 17:29:42 +03:00
|
|
|
|
2021-10-17 19:38:16 +03:00
|
|
|
_instance->_sounds[soundTag].play();
|
|
|
|
}
|
|
|
|
|
2022-02-23 17:29:42 +03:00
|
|
|
void SoundController::playSound(const SoundTag &soundTag) {
|
|
|
|
if (_instance == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_instance->_sounds.count(soundTag) != 0) {
|
|
|
|
_instance->_sounds[soundTag].play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void SoundController::pauseSound(const SoundTag &soundTag) {
|
2021-11-09 22:54:20 +03:00
|
|
|
if (_instance == nullptr) {
|
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
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
if (_instance->_sounds.count(soundTag) > 0) {
|
2021-10-17 19:38:16 +03:00
|
|
|
_instance->_sounds[soundTag].pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void SoundController::stopSound(const SoundTag &soundTag) {
|
2021-11-09 22:54:20 +03:00
|
|
|
if (_instance == nullptr) {
|
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
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
if (_instance->_sounds.count(soundTag) > 0) {
|
2021-10-17 19:38:16 +03:00
|
|
|
_instance->_sounds[soundTag].stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
sf::Sound::Status SoundController::getStatus(const SoundTag &soundTag) {
|
2021-11-09 22:54:20 +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-31 11:39:08 +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 {
|
2021-10-17 19:38:16 +03:00
|
|
|
return sf::Sound::Status::Stopped;
|
2021-10-18 06:44:04 +03:00
|
|
|
}
|
2021-10-17 19:38:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundController::free() {
|
2021-11-09 22:54:20 +03:00
|
|
|
if (_instance != nullptr) {
|
2021-10-31 11:39:08 +03:00
|
|
|
for (auto&[soundTag, sound] : _instance->_sounds) {
|
2021-10-30 21:54:55 +03:00
|
|
|
stopSound(soundTag);
|
2021-10-29 23:44:37 +03:00
|
|
|
}
|
2021-10-30 21:35:46 +03:00
|
|
|
_instance->_sounds.clear();
|
2021-10-17 19:38:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
delete _instance;
|
2021-11-09 22:54:20 +03:00
|
|
|
_instance = nullptr;
|
2021-10-30 21:29:42 +03:00
|
|
|
|
|
|
|
Log::log("SoundController::free(): pointer to 'SoundController' was freed");
|
2021-10-17 19:38:16 +03:00
|
|
|
}
|