]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CAudio.cpp
e4ee1501818e1efed162c6ce99eac3a3ddb146f2
[quix0rs-blobwars.git] / src / CAudio.cpp
1 /*
2 Copyright (C) 2004 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "headers.h"
22
23 Audio::Audio()
24 {
25         output = 2;
26         useSound = true;
27         useMusic = true;
28
29         for (int i = 0 ; i < MAX_SOUNDS ; i++)
30         {
31                 sound[i] = NULL;
32         }
33
34         music = NULL;
35         quickSound = NULL;
36         
37         levelMusicName[0] = 0;
38         songtitle[0] = 0;
39         songalbum[0] = 0;
40         songartist[0] = 0;
41         songlicense = -1;
42 }
43
44 void Audio::setSoundVolume(int soundVolume)
45 {
46         this->soundVolume = soundVolume;
47         if (engine->useAudio)
48                 Mix_Volume(-1, soundVolume);
49 }
50
51 void Audio::setMusicVolume(int musicVolume)
52 {
53         this->musicVolume = musicVolume;
54         if (engine->useAudio)
55         {
56                 Mix_VolumeMusic(musicVolume);
57         }
58 }
59
60 void Audio::registerEngine(Engine *engine)
61 {
62         this->engine = engine;
63 }
64
65 bool Audio::loadSound(int i, const char *filename)
66 {
67         if (!engine->useAudio)
68         {
69                 return true;
70         }
71                 
72         if (i >= MAX_SOUNDS)
73         {
74                 printf("ERROR: SOUND INDEX IS HIGHER THAN MAXIMUM ALLOWED %d >= %d\n", i, MAX_SOUNDS);
75                 exit(1);
76         }
77
78         if (sound[i] != NULL)
79         {
80                 Mix_FreeChunk(sound[i]);
81                 sound[i] = NULL;
82         }
83
84         #if USEPAK
85                 engine->unpack(filename, PAK_SOUND);
86                 sound[i] = Mix_LoadWAV_RW(engine->sdlrw, 1);
87         #else
88                 sound[i] = Mix_LoadWAV(filename);
89         #endif
90
91         if (!sound[i])
92         {
93                 debug(("WARNING - Failed to load %s\n", filename));
94                 return false;
95         }
96         
97         return true;
98 }
99
100 bool Audio::loadMusic(const char *filename)
101 {
102         char tempPath[PATH_MAX];
103         
104         snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
105         
106         if (!engine->useAudio)
107         {
108                 return true;
109         }
110
111         remove(tempPath);
112         
113         SDL_Delay(250); // wait a bit, just to be sure!
114
115         if (music != NULL)
116         {
117                 Mix_HaltMusic();
118                 SDL_Delay(5);
119                 Mix_FreeMusic(music);
120                 music = NULL;
121         }
122
123         #if USEPAK
124                 engine->unpack(filename, PAK_MUSIC);
125                 music = Mix_LoadMUS(tempPath);
126         #else
127                 music = Mix_LoadMUS(filename);
128         #endif
129
130         songtitle[0] = 0;
131         songalbum[0] = 0;
132         songartist[0] = 0;
133         songlicense = -1;
134
135         if (!music)
136         {
137                 debug(("WARNING - Failed to load %s\n", filename));
138                 return false;
139         }
140
141         snprintf(tempPath, sizeof tempPath, "%s.tags", filename);
142         FILE *fp = fopen(tempPath, "r");
143         char line[1024];
144         
145         while(fp && fgets(line, sizeof line, fp))
146         {
147                 int l = strlen(line);
148                 if(line[l - 1] == '\n')
149                         line[l - 1] = 0;
150
151                 if(!strncasecmp(line, "title=", 6))
152                          strncpy(songtitle, line + 6, sizeof songtitle);
153                 else if(!strncasecmp(line, "album=", 6))
154                          strncpy(songalbum, line + 6, sizeof songalbum);
155                 else if(!strncasecmp(line, "artist=", 7))
156                          strncpy(songartist, line + 7, sizeof songartist);
157                 else if(!strncasecmp(line, "license=", 8))
158                 {
159                         if(!strncasecmp(line + 8, "CC-BY ", 6))
160                                 songlicense = 0;
161                         else if(!strncasecmp(line + 8, "CC-BY-SA ", 9))
162                                 songlicense = 1;
163                 }
164         }
165
166         fprintf(stderr, "%s\n%s\n\"%s\"\n%d\n", songartist, songalbum, songtitle, songlicense);
167
168         if(fp)
169                 fclose(fp);
170         
171         strncpy(levelMusicName, filename, sizeof levelMusicName);
172
173         return true;
174 }
175
176 void Audio::playSound(int snd, int channel)
177 {
178         if ((!engine->useAudio) || (soundVolume == 0))
179                 return;
180         
181         if (!output)
182         {
183                 return;
184         }
185
186         Mix_Volume(channel, soundVolume);
187
188         Mix_PlayChannel(channel, sound[snd], 0);
189 }
190
191 void Audio::playMusic()
192 {
193         if (!engine->useAudio)
194                 return;
195         
196         if (!output)
197         {
198                 return;
199         }
200
201         Mix_PlayMusic(music, -1);
202
203         Mix_VolumeMusic(musicVolume);
204 }
205
206 void Audio::playMusicOnce()
207 {
208         if (!engine->useAudio)
209                 return;
210         
211         if (!output)
212         {
213                 return;
214         }
215
216         Mix_PlayMusic(music, 0);
217
218         Mix_VolumeMusic(musicVolume);
219 }
220
221 bool Audio::loadGameOverMusic()
222 {
223         char tempPath[PATH_MAX];
224         
225         snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
226         
227         if (!engine->useAudio)
228         {
229                 return true;
230         }
231
232         remove(tempPath);
233         SDL_Delay(250); // wait a bit, just to be sure!
234
235         if (music != NULL)
236         {
237                 Mix_HaltMusic();
238                 SDL_Delay(5);
239                 Mix_FreeMusic(music);
240                 music = NULL;
241         }
242
243         #if USEPAK
244                 engine->unpack("music/gameover", PAK_MUSIC);
245                 music = Mix_LoadMUS(tempPath);
246         #else
247                 music = Mix_LoadMUS("music/gameover");
248         #endif
249
250         if (!music)
251         {
252                 return false;
253         }
254
255         return true;
256 }
257
258 bool Audio::reloadLevelMusic()
259 {
260         // remove the Game Over music first...
261
262         if (music != NULL)
263         {
264                 Mix_HaltMusic();
265                 SDL_Delay(5);
266                 Mix_FreeMusic(music);
267                 music = NULL;
268         }
269
270         return loadMusic(levelMusicName);
271 }
272
273 void Audio::playAmbiance()
274 {
275         if ((!engine->useAudio) || (soundVolume == 0))
276         {
277                 return;
278         }
279         
280         if (!output)
281         {
282                 return;
283         }
284
285         Mix_PlayChannel(CH_AMBIANCE, sound[SND_AMBIANCE], -1);
286 }
287
288 void Audio::stopAmbiance()
289 {
290         if ((!engine->useAudio) || (soundVolume == 0))
291                 return;
292
293         Mix_HaltChannel(CH_AMBIANCE);
294 }
295
296 int Audio::playMenuSound(int sound)
297 {
298         if ((!engine->useAudio) || (soundVolume == 0))
299                 return sound;
300
301         if ((sound == 0) || (sound == 3))
302                 return sound;
303
304         if (sound == 1)
305                 playSound(SND_HIGHLIGHT, CH_ANY);
306
307         if (sound == 2)
308                 playSound(SND_SELECT, CH_ANY);
309                 
310         return sound;
311 }
312
313 void Audio::pause()
314 {
315         if (!engine->useAudio)
316                 return;
317
318         for (int i = 0 ; i < 8 ; i++)
319                 Mix_Pause(i);
320
321         Mix_PauseMusic();
322 }
323
324 void Audio::resume()
325 {
326         if (!engine->useAudio)
327                 return;
328         
329         if (!output)
330         {
331                 return;
332         }
333
334         for (int i = 0 ; i < 8 ; i++)
335                 Mix_Resume(i);
336
337         Mix_ResumeMusic();
338 }
339
340 void Audio::stopMusic()
341 {
342         if (!engine->useAudio)
343                 return;
344
345         Mix_HaltMusic();
346 }
347
348 void Audio::fadeMusic()
349 {
350         if (!engine->useAudio)
351                 return;
352
353         Mix_FadeOutMusic(3500);
354 }
355
356 void Audio::free()
357 {
358         for (int i = 0 ; i < MAX_SOUNDS - 3 ; i++)
359         {
360                 if (sound[i] != NULL)
361                 {
362                         Mix_FreeChunk(sound[i]);
363                         sound[i] = NULL;
364                 }
365         }
366
367         if (music != NULL)
368         {
369                 Mix_HaltMusic();
370                 SDL_Delay(5);
371                 Mix_FreeMusic(music);
372         }
373
374         music = NULL;
375
376         if (quickSound != NULL)
377                 Mix_FreeChunk(quickSound);
378
379         quickSound = NULL;
380 }
381
382 void Audio::destroy()
383 {
384         free();
385
386         for (int i = MAX_SOUNDS - 3 ; i < MAX_SOUNDS ; i++)
387         {
388                 if (sound[i] != NULL)
389                 {
390                         Mix_FreeChunk(sound[i]);
391                         sound[i] = NULL;
392                 }
393         }
394 }