-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundManager.cpp
117 lines (87 loc) · 2.31 KB
/
SoundManager.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "SoundManager.h"
#ifdef USE_SOUND
SoundManager::SoundManager() {
std::string soundBase = "Sounds/target";
std::string musicBase = "Music/music";
// load target sounds
for (int i = 0; i <= TARGET_SOUND_7; i++) {
sf::SoundBuffer* soundBuffer = new sf::SoundBuffer();
std::stringstream s;
s << i;
if (!soundBuffer->LoadFromFile(soundBase + s.str() + ".wav")) {
printf("Error loading audio files\n");
}
sf::Sound* sound = new sf::Sound();
sound->SetBuffer(*soundBuffer);
sounds.push_back(sound);
}
// load music
for (int i = MUSIC_0; i <= MUSIC_0; i++) {
sf::Music* m = new sf::Music();
std::stringstream s;
s << i;
if (!m->OpenFromFile(musicBase + s.str() + ".wav")) {
printf("Error loading audio files\n");
}
m->SetVolume(15);
m->SetLoop(true);
music.push_back(m);
}
}
SoundManager::~SoundManager() {
for (int i = 0; i < sounds.size(); i++) {
if (sounds.at(i)) { delete sounds[i]; }
}
for (int i = 0; i < music.size(); i++) {
if (music.at(i)) { delete music[i]; }
}
}
bool SoundManager::playSound(int type) {
if (type < sounds.size()) {
if (sounds[type]->GetStatus() == sf::Sound::Playing) {
sounds[type]->Stop();
}
sounds[type]->SetLoop(false);
sounds[type]->Play();
} else {
return false;
}
return true;
}
bool SoundManager::loopSound(int type) {
if (type < sounds.size()) {
if (sounds[type]->GetStatus() == sf::Sound::Playing) {
return false;
}
sounds[type]->SetLoop(true);
sounds[type]->Play();
} else {
return false;
}
return true;
}
void SoundManager::stopSound(int type) {
if (type <= sounds.size()) {
sounds[type]->Stop();
}
}
bool SoundManager::playMusic(int type) {
if (type < music.size()) {
if (music[type]->GetStatus() == sf::Music::Playing) {
return false;
}
music[type]->Play();
} else {
return false;
}
return true;
}
bool SoundManager::stopMusic(int type) {
if (type < music.size()) {
music[type]->Stop();
} else {
return false;
}
return true;
}
#endif