-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.cpp
185 lines (169 loc) · 3.85 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
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
#include "Sound.h"
Uint32 Sound::timer;
Mix_Chunk* Sound::sfx[];
Mix_Music* Sound::bgm[];
Sound::Sound()
{
//if(!load())
// cerr << "Sound loading failed" << endl;
}
void Sound::free()
{
for (int i = 0; i < 25; i++) {
Mix_FreeChunk(sfx[i]);
sfx[i] = NULL;
}
for (int i = 0; i < 10; i++) {
Mix_FreeMusic(bgm[i]);
bgm[i] = NULL;
}
Mix_Quit();
}
void Sound::play(SFX sound)
{
switch(sound){
case BUMPFX:
Mix_PlayChannel(-1, sfx[0], 0);
break;
case SJUMPFX:
Mix_PlayChannel(-1, sfx[1], 0);
break;
case LJUMPFX:
Mix_PlayChannel(-1, sfx[2], 0);
break;
case ONEUPFX:
Mix_PlayChannel(-1, sfx[3], 0);
break;
case COINFX:
Mix_PlayChannel(-1, sfx[4], 0);
break;
case BREAKFX:
Mix_PlayChannel(-1, sfx[5], 0);
break;
case FIREBALLFX:
Mix_PlayChannel(-1, sfx[6], 0);
break;
case FALLFX:
Mix_PlayChannel(-1, sfx[7], 0);
break;
case FLAGPOLEFX:
Mix_PlayChannel(-1, sfx[8], 0);
break;
case GAMEOVERFX:
Mix_PlayChannel(-1, sfx[9], 0);
break;
case FIREFX:
Mix_PlayChannel(-1, sfx[10], 0);
break;
case KICKFX:
Mix_PlayChannel(-1, sfx[11], 0);
break;
case DEATHFX:
Mix_PlayChannel(-1, sfx[12], 0);
break;
case PAUSEFX:
Mix_PlayChannel(-1, sfx[13], 0);
break;
case PIPEFX:
Mix_PlayChannel(-1, sfx[14], 0);
break;
case POWERUPFX:
Mix_PlayChannel(-1, sfx[15], 0);
break;
case UPFX:
Mix_PlayChannel(-1, sfx[16], 0);
break;
case DOWNFX:
Mix_PlayChannel(-1, sfx[17], 0);
break;
case STOMPFX:
Mix_PlayChannel(-1, sfx[18], 0);
break;
case STAGECLEARFX:
Mix_PlayChannel(-1, sfx[19], 0);
break;
case WORLDCLEARFX:
Mix_PlayChannel(-1, sfx[20], 0);
break;
}
}
bool Sound::playing(){
return Mix_PlayingMusic();
}
void Sound::music(Music music){
if(Mix_PlayingMusic()){
Mix_ResumeMusic();
}else {
switch(music){
case OVERWORLD:
Mix_PlayMusic(bgm[0], -1);
break;
case UNDERWORLD:
Mix_PlayMusic(bgm[1], -1);
break;
case WATER:
Mix_PlayMusic(bgm[2], -1);
break;
case CASTLE:
Mix_PlayMusic(bgm[3], -1);
break;
}
}
}
void Sound::pause(){
Mix_PauseMusic();
}
Sound::~Sound()
{
//free();
}
bool Sound::load()
{
bool success = true;
if(SDL_Init(SDL_INIT_AUDIO)<0){
cerr << "SDL Audio error: " << SDL_GetError() << endl;
success = false;
}
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048)<0){
cerr << "Audio error: " << Mix_GetError() << endl;
success = false;
}
sfx[0] = Mix_LoadWAV("SFX/smb_bump.wav");
sfx[1] = Mix_LoadWAV("SFX/smb_jump-small.wav");
sfx[2] = Mix_LoadWAV("SFX/smb_jump-super.wav");
sfx[3] = Mix_LoadWAV("SFX/smb_1-up.wav");
sfx[4] = Mix_LoadWAV("SFX/smb_coin.wav");
sfx[5] = Mix_LoadWAV("SFX/smb_breakblock.wav");
sfx[6] = Mix_LoadWAV("SFX/smb_fireball.wav");
sfx[7] = Mix_LoadWAV("SFX/smb_bowserfalls.wav");
sfx[8] = Mix_LoadWAV("SFX/smb_flagpole.wav");
sfx[9] = Mix_LoadWAV("SFX/smb_gameover.wav");
sfx[10] = Mix_LoadWAV("SFX/smb_bowserfire.wav");
sfx[11] = Mix_LoadWAV("SFX/smb_kick.wav");
sfx[12] = Mix_LoadWAV("SFX/smb_mariodie.wav");
sfx[13] = Mix_LoadWAV("SFX/smb_pause.wav");
sfx[14] = Mix_LoadWAV("SFX/smb_pipe.wav");
sfx[15] = Mix_LoadWAV("SFX/smb_powerup_appears.wav");
sfx[16] = Mix_LoadWAV("SFX/smb_powerup.wav");
sfx[17] = Mix_LoadWAV("SFX/smb_pipe.wav");
sfx[18] = Mix_LoadWAV("SFX/smb_stomp.wav");
sfx[19] = Mix_LoadWAV("SFX/smb_stage_clear.wav");
sfx[20] = Mix_LoadWAV("SFX/smb_world_clear.wav");
bgm[0] = Mix_LoadMUS("SFX/01-overworld.mp3");
bgm[1] = Mix_LoadMUS("SFX/02-underworld.mp3");
bgm[2] = Mix_LoadMUS("SFX/03-underwater.mp3");
bgm[3] = Mix_LoadMUS("SFX/04-castle.mp3");
for(int i = 0; i < 21; ++i){
if(sfx[i]==NULL){
cerr << "SFX error: " << Mix_GetError() << endl;
success = false;
}
}
for(int i = 0; i < 4; ++i){
if(bgm[i]==NULL){
cerr << "BGM error: " << Mix_GetError() << endl;
success = false;
}
}
return success;
}