-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.h
53 lines (40 loc) · 1005 Bytes
/
audio.h
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
#define AUDIO 1
#include "constants.h"
void playEffect(char *effectName) {
int audioRate = 44100;
uint16_t audioFormat = AUDIO_S16SYS;
int audioChannels = 2;
int audioBuffers = 4096;
if (Mix_OpenAudio(audioRate, audioFormat, audioChannels, audioBuffers)) {
printf("Error initializing audio: %s\n",Mix_GetError());
}
Mix_Chunk *sound = NULL;
sound = Mix_LoadWAV(effectName);
if (sound == NULL) {
printf("Error loading sound effect %s: %s\n",effectName, Mix_GetError());
}
int channel = Mix_PlayChannel(-1, sound, 0);
if (channel == -1) {
printf("Error playing sound effect %s: %s\n",effectName, Mix_GetError());
}
while (!Mix_Playing(channel)) {
Mix_FreeChunk(sound);
Mix_CloseAudio();
}
}
/* Convenient wrappers for playEffect() */
void playInit(void) {
if (AUDIO_ENABLE) {
playEffect(INIT_SOUND);
}
}
void playExplode(void) {
if (AUDIO_ENABLE) {
playEffect(EXPLODE_SOUND);
}
}
void playFiring(void) {
if (AUDIO_ENABLE) {
playEffect(FIRING_SOUND);
}
}