-
Notifications
You must be signed in to change notification settings - Fork 1
/
Audio.cpp
299 lines (251 loc) · 6.95 KB
/
Audio.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//All music and sound effect is from https://freesound.org/
#include "Include/Audio.h"
audioManager::audioManager()
{
//The music that will be played
music = NULL;
//The sound effects that will be used
radio = NULL;
walk = NULL;
reload = NULL;
swapWeapon = NULL;
gunEmpty = NULL;
};
bool audioManager::loadMultiFile(std::vector<Mix_Chunk*>& target, std::string name, std::string format, int total)
{
for (int i = 0; i < total; i++)
{
std::string path = "Assets/Audio/" + name + " (" + std::to_string(i + 1) + ")." + format;
target.push_back(Mix_LoadWAV(path.c_str()));
if (target[i] == NULL)
{
printf("Failed to load file %i!\n", i + 1);
return false;
}
}
return true;
}
void audioManager::freeSound(Mix_Chunk* target)
{
Mix_FreeChunk(target);
target = NULL;
}
void audioManager::freeMultiSounds(std::vector<Mix_Chunk*>& target)
{
for (int i = 0; i < target.size(); i++)
{
Mix_FreeChunk(target[i]);
target[i] = NULL;
}
}
void audioManager::loadAudio()
{
//load music
music = Mix_LoadMUS("Assets/Audio/background music.mp3");
if (music == NULL)
{
printf("Failed to load background music! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
//load sound effects
ambient = Mix_LoadWAV("Assets/Audio/background ambient.mp3");
if (ambient == NULL)
{
printf("Failed to load background ambient sound effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
radio = Mix_LoadWAV("Assets/Audio/radio beep.mp3");
if (radio == NULL)
{
printf("Failed to load radio sound effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
gunEmpty = Mix_LoadWAV("Assets/Audio/gun empty.wav");
if (gunEmpty == NULL)
{
printf("Failed to load gun empty sound effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
reload = Mix_LoadWAV("Assets/Audio/reload.wav");
if (reload == NULL)
{
printf("Failed to load reload sound effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
swapWeapon = Mix_LoadWAV("Assets/Audio/swap weapon.mp3");
if (swapWeapon == NULL)
{
printf("Failed to load swap weapon sound effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
walk = Mix_LoadWAV("Assets/Audio/walk.mp3");
if (walk == NULL)
{
printf("Failed to load walk effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
hitTree = Mix_LoadWAV("Assets/Audio/hit tree.mp3");
if (walk == NULL)
{
printf("Failed to load hit tree effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
collectObject = Mix_LoadWAV("Assets/Audio/collect object.mp3");
if (walk == NULL)
{
printf("Failed to load collect object effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
gameLose = Mix_LoadWAV("Assets/Audio/game lose.wav");
if (walk == NULL)
{
printf("Failed to load game lose effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
gameWin = Mix_LoadWAV("Assets/Audio/game win.wav");
if (walk == NULL)
{
printf("Failed to load game win effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
if (!loadMultiFile(playerHurt, "player hurt", "mp3", PLAYER_HURT_VARIATION))
{
printf("Failed to load player hurt effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
if (!loadMultiFile(gunshot, "gunshot", "mp3", GUNSHOT_VARIATION))
{
printf("Failed to load walk effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
if (!loadMultiFile(zombieAttack, "zombie attack", "mp3", ZOMBIE_ATTACK_VARIATION))
{
printf("Failed to load zombie attack effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
if (!loadMultiFile(hitZombie, "hit zombie", "wav", HIT_ZOMBIE_VARIATION))
{
printf("Failed to load hit zombie effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
//set music volume
Mix_VolumeMusic(48);
//set sound effect volume
Mix_Volume(-1, 64);
}
void audioManager::playMainMusic()
{
//Play the music
Mix_PlayMusic(music, -1);
}
void audioManager::playBackgroundLoop()
{
//Play ambient sound and walk loop
Mix_PlayChannel(AMBIENT_CHANNEL, ambient, -1); //start the infinite ambient sound effect
Mix_PlayChannel(WALK_CHANNEL, walk, -1); //start the infinite walk sound effect
}
void audioManager::stopBackgroundLoop()
{
Mix_HaltChannel(AMBIENT_CHANNEL);
Mix_HaltChannel(WALK_CHANNEL);
}
void audioManager::setSoundEffect(int channel, Mix_Chunk* soundEffect)
{
//Mix_HaltChannel(channel); //to stop the last sound effect from playing
Mix_SetPanning(channel, 255, 255); //reset the directional sound effect
Mix_PlayChannel(channel, soundEffect, 0);
}
void audioManager::setSoundEffect(int channel, Mix_Chunk* soundEffect, gameObject source)
{
int right = calOnScreenXPosition(camera, source.px);
right = map(right, 0, SCREEN_WIDTH, 0, 255);
//Mix_HaltChannel(channel); //to stop the last sound effect from playing
Mix_SetPanning(channel, 255 - right, right); //for the directional sound effect
Mix_PlayChannel(channel, soundEffect, 0);
}
void audioManager::playRadio()
{
setSoundEffect(-1, radio);
}
void audioManager::pauseWalk()
{
Mix_Pause(WALK_CHANNEL);
}
void audioManager::resumeWalk(gameObject source)
{
int right = calOnScreenXPosition(camera, source.px);
right = map(right, 0, SCREEN_WIDTH, 0, 255);
Mix_SetPanning(WALK_CHANNEL, 255 - right, right); //for the directional sound effect
Mix_Resume(WALK_CHANNEL);
}
void audioManager::playPlayerHurt(gameObject source)
{
if (Mix_Playing(PLAYER_HURT_CHANNEL) == 0)
{
int i = GetRandomInt(0, PLAYER_HURT_VARIATION - 2, 1);
setSoundEffect(PLAYER_HURT_CHANNEL, playerHurt[i], source);
}
}
void audioManager::playHitTree(gameObject source)
{
setSoundEffect(-1, hitTree, source);
}
void audioManager::playReload()
{
setSoundEffect(RELOAD_CHANNEL, reload);
}
void audioManager::stopReload()
{
Mix_HaltChannel(RELOAD_CHANNEL); //to stop the last sound effect from playing
}
void audioManager::playSwapWeapon() {
setSoundEffect(SWAP_WEAPON_CHANNEL, swapWeapon);
}
void audioManager::playGunshot(gameObject source) {
int i = GetRandomInt(0, GUNSHOT_VARIATION - 2, 1);
setSoundEffect(-1, gunshot[i], source);
}
void audioManager::playZombieAttack(gameObject source) {
int i = GetRandomInt(0, ZOMBIE_ATTACK_VARIATION - 2, 1);
setSoundEffect(-1, zombieAttack[i], source);
}
void audioManager::playHitZombie(gameObject source) {
int i = GetRandomInt(0, HIT_ZOMBIE_VARIATION - 2, 1);
setSoundEffect(-1, hitZombie[i], source);
}
void audioManager::playGunEmpty()
{
setSoundEffect(4, gunEmpty);
}
void audioManager::playCollectObject()
{
setSoundEffect(-1, collectObject);
}
void audioManager::playGameLose()
{
setSoundEffect(-1, gameLose);
}
void audioManager::playGameWin()
{
setSoundEffect(-1, gameWin);
}
void audioManager::freeAudio()
{
freeSound(ambient);
freeSound(radio);
freeSound(walk);
freeSound(reload);
freeSound(swapWeapon);
freeSound(gunEmpty);
freeSound(hitTree);
freeSound(collectObject);
freeSound(gameLose);
freeSound(gameWin);
freeMultiSounds(playerHurt);
freeMultiSounds(gunshot);
freeMultiSounds(zombieAttack);
freeMultiSounds(hitZombie);
//Free the music
Mix_FreeMusic(music);
music = NULL;
}