-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioManager.cpp
More file actions
88 lines (76 loc) · 2.3 KB
/
Copy pathAudioManager.cpp
File metadata and controls
88 lines (76 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "AudioManager.h"
#include "GameEngineConfig.h"
#include <iostream>
AudioManager::AudioManager() {}
AudioManager* AudioManager::instance = 0;
AudioManager* AudioManager::getInstance() {
if (instance == 0) {
instance = new AudioManager();
}
return instance;
}
AudioManager::~AudioManager() {
std::map<std::string, sf::SoundBuffer*>::iterator itb;
for (itb = soundBuffers.begin(); itb != soundBuffers.end(); ++itb) {
delete itb->second;
}
std::map<std::string, sf::Music*>::iterator itm;
for (itm = musics.begin(); itm != musics.end(); ++itm) {
delete itm->second;
}
}
bool AudioManager::loadSoundEffect(const std::string& name, const std::string& filename) {
sf::SoundBuffer* buffer = new sf::SoundBuffer();
if (!buffer->loadFromFile(GameEngineConfig::AUDIO_PATH + filename)) {
delete buffer;
return false;
}
soundBuffers[name] = buffer;
return true;
}
void AudioManager::playSoundEffect(const std::string& name) {
auto it = soundBuffers.find(name);
if (it == soundBuffers.end()) return;
sf::Sound* sound = new sf::Sound(*it->second);
sound->setVolume(20);
sound->play();
sounds.push_back(sound);
}
bool AudioManager::loadMusic(const std::string& name, const std::string& filename) {
sf::Music* music = new sf::Music();
if (!music->openFromFile(GameEngineConfig::AUDIO_PATH + filename)) {
delete music;
return false;
}
musics[name] = music;
return true;
}
void AudioManager::playMusic(const std::string& name, bool loop) {
stopMusic();
std::map<std::string, sf::Music*>::iterator it = musics.find(name);
if (it == musics.end()) return;
if (currentMusic != NULL && currentMusic != it->second) {
currentMusic->stop();
}
currentMusic = it->second;
currentMusic->setLooping(loop);
currentMusic->play();
}
void AudioManager::stopMusic() {
if (currentMusic != NULL) {
currentMusic->stop();
currentMusic = NULL;
}
}
void AudioManager::update() {
std::list<sf::Sound*>::iterator it = sounds.begin();
while (it != sounds.end()) {
if ((*it)->getStatus() == sf::Sound::Status::Stopped) {
delete* it;
it = sounds.erase(it);
}
else {
++it;
}
}
}