-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.cpp
84 lines (64 loc) · 1.24 KB
/
sound.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 <stdio.h>
#include <map>
#include <string>
#include <SDL.h>
#include <SDL_mixer.h>
#include "sound.h"
typedef std::map<SoundEffects,Mix_Chunk *> SoundMap;
Mix_Music *song;
Sound sound;
SoundMap soundMap;
const char *sfxPath[]={
"data/none.wav",
"data/welcome.wav",
"data/ugh1.wav",
"data/ugh2.wav",
"data/ugh3.wav",
"data/hah.wav",
"data/firebreath.wav",
"data/zap.wav",
"data/missed.wav",
"data/flames.wav",
"data/die.wav",
"data/win.wav",
"data/lose.wav",
};
Sound::Sound()
{
}
Sound::~Sound()
{
}
void Sound::loadAll()
{
song=Mix_LoadMUS("data/cave themeb4.ogg");
if(song) {
printf("Starting background music\n");
Mix_PlayMusic(song,-1);
}
// need to load the sound effects here.
for(int i=0;i<SFX_MAX;i++) {
FILE *file=fopen(sfxPath[i],"r");
if(file) {
fclose(file);
Mix_Chunk *chunk=Mix_LoadWAV(sfxPath[i]);
if(chunk) {
soundMap[(SoundEffects)i]=chunk;
} else {
printf("File '%s' invalid\n",sfxPath[i]);
}
} else {
printf("File '%s' not found.\n",sfxPath[i]);
}
}
}
int Sound::playOnce(SoundEffects sfx)
{
Mix_Chunk *chunk=NULL;
if(soundMap.find(sfx)!=soundMap.end()) {
chunk=soundMap[sfx];
}
if(chunk==NULL) return -1;
Mix_PlayChannel(-1,chunk,0);
return 0;
}