-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
229 lines (195 loc) · 5.6 KB
/
audio.c
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
#include "audio.h"
#include <string.h>
#include <SDL_timer.h>
#include <stdbool.h>
extern char *dataFolder;
Sound *sound_pop;
Sound *sound_slide;
Sound *sound_bam1;
Sound *sound_fff;
Sound *sound_yahoohoo1[NB_YAHOOHOO1];
Sound *sound_yahoohoo2[NB_YAHOOHOO2];
Sound *sound_yahoohoo3[NB_YAHOOHOO3];
Sound *sound_splash[8];
Sound *sound_bim[2];
#ifdef USE_AUDIO
static Mix_Music *music[4];
static const char *MUSIC_THEME[2][4] = {
{ "flobopuyo_menu.xm", "flobopuyo_game1.xm", "flobopuyo_game2.xm", "flobopuyo_gameover.xm" },
{ "flobopuyo_menu.xm", "strange_fear.xm", "strange_fear2.xm", "strange_gameover.xm" }
};
static int sound_supported;
static int volume;
static int sound_on;
static int music_on;
static int currentMus = -1;
static Sound *CustomMix_LoadWAV(const char *path, const char *fileName, int volume)
{
Sound *result;
char temp[1024];
if (!sound_supported) return NULL;
snprintf(temp, 1024, "%s/sfx/%s", path, fileName);
result = Mix_LoadWAV(temp);
if (result)
Mix_VolumeChunk (result, volume);
return result;
}
static Mix_Music *CustomMix_LoadMUS(const char *path, const char *fileName)
{
Mix_Music *result;
char temp[1024];
if (!sound_supported) return NULL;
snprintf(temp, 1024, "%s/sfx/%s", path, fileName);
result = Mix_LoadMUS(temp);
return result;
}
#endif
void
audio_init (void)
{
#ifdef USE_AUDIO
int audio_rate;
Uint16 audio_format;
int audio_channels;
sound_supported =
(Mix_OpenAudio (MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) == 0);
if (!sound_supported) return;
sound_pop = CustomMix_LoadWAV (dataFolder, "pop.wav", 30);
sound_bam1 = CustomMix_LoadWAV (dataFolder, "bam1.wav", 12);
sound_fff = CustomMix_LoadWAV (dataFolder, "fff.wav", 48);
sound_slide = CustomMix_LoadWAV (dataFolder, "slide.wav", 128);
sound_yahoohoo1[0] = NULL;
sound_yahoohoo1[1] = CustomMix_LoadWAV (dataFolder, "yahoohoo.wav", 50);
sound_yahoohoo1[2] = CustomMix_LoadWAV (dataFolder, "woho.wav", 32);
sound_yahoohoo1[3] = CustomMix_LoadWAV (dataFolder, "pastaga.wav", 70);
sound_yahoohoo2[0] = sound_yahoohoo1[2];
sound_yahoohoo2[1] = CustomMix_LoadWAV (dataFolder, "woo.wav", 45);
sound_yahoohoo3[0] = CustomMix_LoadWAV (dataFolder, "applose.wav", 96);
sound_splash[0] = CustomMix_LoadWAV (dataFolder, "splash1.wav",72);
sound_splash[1] = CustomMix_LoadWAV (dataFolder, "splash2.wav",72);
sound_splash[2] = CustomMix_LoadWAV (dataFolder, "splash3.wav",72);
sound_splash[3] = CustomMix_LoadWAV (dataFolder, "splash4.wav",72);
sound_splash[4] = CustomMix_LoadWAV (dataFolder, "splash5.wav",72);
sound_splash[5] = CustomMix_LoadWAV (dataFolder, "splash6.wav",72);
sound_splash[6] = CustomMix_LoadWAV (dataFolder, "splash7.wav",72);
sound_splash[7] = CustomMix_LoadWAV (dataFolder, "splash8.wav",72);
sound_bim[0] = CustomMix_LoadWAV (dataFolder, "bim1.wav",72);
sound_bim[1] = CustomMix_LoadWAV (dataFolder, "bim2.wav",72);
music[0] = CustomMix_LoadMUS (dataFolder, "flobopuyo_menu.xm");
music[1] = CustomMix_LoadMUS (dataFolder, "flobopuyo_game1.xm");
music[2] = CustomMix_LoadMUS (dataFolder, "flobopuyo_game2.xm");
music[3] = CustomMix_LoadMUS (dataFolder, "flobopuyo_gameover.xm");
Mix_QuerySpec (&audio_rate, &audio_format, &audio_channels);
sound_on = 1;
music_on = 1;
audio_music_set_volume (100);
audio_set_volume (50);
#endif
}
void
audio_music_start (int num)
{
#ifdef USE_AUDIO
if (!sound_supported) return;
if ((currentMus != num) && (music_on==true)) {
currentMus = num;
Mix_HaltMusic ();
if (music[num])
Mix_PlayMusic (music[num], -1);
}
// audio_music_set_volume (50);
#endif
}
void
audio_sound_play (Sound * s)
{
#ifdef USE_AUDIO
if (!sound_supported) return;
if (s)
Mix_PlayChannel (-1, s, 0);
#endif
}
void
audio_close (void)
{
#ifdef USE_AUDIO
if (!sound_supported) return;
Mix_CloseAudio ();
#endif
}
// vol compris entre 0 et 100;
void
audio_music_set_volume (int vol)
{
#ifdef USE_AUDIO
if (!sound_supported) return;
Mix_VolumeMusic ((int) (128.0 * (double) vol / 100.0));
#endif
}
// vol compris entre 0 et 100;
void
audio_set_volume (int vol)
{
#ifdef USE_AUDIO
if (!sound_supported) return;
if (sound_on) {
volume = Mix_Volume (-1, (int) (128.0 * (double) vol / 100.0));
}
else {
volume = (int) (128.0 * (double) vol / 100.0);
}
#endif
}
void
audio_set_music_on_off (int on)
{
#ifdef USE_AUDIO
if (!sound_supported) return;
if (on) {
if (Mix_PausedMusic()) Mix_ResumeMusic();
else
{
while (Mix_FadingMusic() == MIX_FADING_OUT) SDL_Delay(100);
}
if (music[currentMus])
Mix_FadeInMusic (music[currentMus], -1, 1000);
}
else {
while (Mix_FadingMusic() == MIX_FADING_IN) SDL_Delay(100);
Mix_FadeOutMusic (1000);
}
music_on = on;
#endif
}
void
audio_set_sound_on_off (int on)
{
#ifdef USE_AUDIO
if (!sound_supported) return;
if (on) {
Mix_Volume (-1, volume);
}
else {
volume = Mix_Volume (-1, -1);
Mix_Volume (-1, 0);
}
sound_on = on;
#endif
}
void audio_music_switch_theme(int theme_number)
{
#ifdef USE_AUDIO
int i;
if (!sound_supported) return;
Mix_Music * themusic[4];
for (i=0; i<4; ++i) {
themusic[i] = music[i];
music[i] = CustomMix_LoadMUS (dataFolder, MUSIC_THEME[theme_number][i]);
}
Mix_HaltMusic();
for (i=0; i<4; ++i) {
Mix_FreeMusic(themusic[i]);
}
// Mix_PlayMusic();
#endif
}