-
Notifications
You must be signed in to change notification settings - Fork 1
/
soundmanager.cpp
83 lines (70 loc) · 1.76 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
#include "soundmanager.hpp"
void SoundManager::lowerMusicVolume(bool lower) {
if (lower) {
sounds.at(MENU).setVolume(50);
sounds.at(LEVEL).setVolume(50);
} else {
sounds.at(MENU).setVolume(100);
sounds.at(LEVEL).setVolume(100);
}
}
SoundManager::SoundManager()
{
menuSB.loadFromFile(SOUNDS_DIR + "menu.ogg");
levelSB.loadFromFile(SOUNDS_DIR + "level.ogg");
levelCompletedSB.loadFromFile(SOUNDS_DIR + "level_completed.ogg");
gameOverSB.loadFromFile(SOUNDS_DIR + "game_over.ogg");
sounds.at(MENU).setLoop(true);
sounds.at(LEVEL).setLoop(true);
}
SoundManager* SoundManager::instance = 0;
SoundManager& SoundManager::getInstance() {
if (!instance) {
instance = new SoundManager();
}
return *instance;
}
void SoundManager::destroyInstance() {
if (instance) {
delete instance;
instance = 0;
}
}
void SoundManager::changeMusic(Sound music) {
if (musicPlaying == music) {
return;
}
switch (music) {
case NONE:
sounds.at(musicPlaying).stop();
break;
case MENU:
case LEVEL:
if (musicPlaying != NONE) {
sounds.at(musicPlaying).stop();
}
sounds.at(music).play();
musicPlaying = music;
break;
default:
break;
}
}
void SoundManager::playEffect(Sound effect) {
switch (effect) {
case LEVEL_COMPLETED:
case GAME_OVER:
lowerMusicVolume(true);
sounds.at(effect).play();
effectPlaying = effect;
break;
default:
break;
}
}
void SoundManager::update() {
if (effectPlaying != NONE && sounds.at(effectPlaying).getStatus() == sf::SoundSource::Stopped) {
lowerMusicVolume(false);
effectPlaying = NONE;
}
}