1 /*
2 Pacman Arena
3 Copyright (C) 2003 Nuno Subtil
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 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 static const char cvsid[] =
21 "$Id: audio.c,v 1.7 2003/11/22 17:32:09 nsubtil Exp $";
22
23 #include <SDL.h>
24 #include <SDL_mixer.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "audio.h"
30 #include "linked-lists.h"
31 #include "file.h"
32
33 /* n� de canais de som */
34 #define NUM_CHANNELS 16
35
36 static Mix_Music *music;
37
38 struct audio_sample
39 {
40 char *name;
41 Mix_Chunk *sample;
42
43 struct audio_sample *next;
44 };
45
46 static struct audio_sample *samples = NULL;
47
48 static int audio_mute_samples = 0;
49 static int audio_mute_music = 0;
50
51 /*
52 audio_init
53 inicializa as rotinas de �udio
54 */
audio_init(void)55 void audio_init(void)
56 {
57 if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 512 * 4) == -1)
58 {
59 printf("erro: audio_init: Mix_OpenAudio() -> %s\n", Mix_GetError());
60 SDL_Quit();
61 exit(1);
62 }
63
64 Mix_AllocateChannels(NUM_CHANNELS);
65 }
66
67 /*
68 audio_play_music
69 inicia a reprodu��o de m�sica de um ficheiro
70 */
audio_play_music(char * fname)71 void audio_play_music(char *fname)
72 {
73 if(audio_mute_music)
74 return;
75
76 music = Mix_LoadMUS((const char *)file_make_fname(fname));
77 if(music == NULL)
78 {
79 printf("erro: audio_play_music: Mix_LoadMUS() -> %s\n", Mix_GetError());
80 SDL_Quit();
81 exit(1);
82 }
83
84 if(Mix_PlayMusic(music, -1) == -1)
85 {
86 printf("erro: audio_play_music: Mix_PlayMusic() -> %s\n", Mix_GetError());
87 SDL_Quit();
88 exit(1);
89 }
90 }
91
92 /*
93 audio_pause_music
94 suspende a reprodu��o de m�sica
95 */
audio_pause_music(void)96 void audio_pause_music(void)
97 {
98 Mix_PauseMusic();
99 }
100
101 /*
102 audio_resume_music
103 continua a reprodu��o de m�sica
104 */
audio_resume_music(void)105 void audio_resume_music(void)
106 {
107 Mix_ResumeMusic();
108 }
109
110 /*
111 audio_stop_music
112 termina a reprodu��o de m�sica
113 */
audio_stop_music(void)114 void audio_stop_music(void)
115 {
116 Mix_HaltMusic();
117 }
118
audio_fade_music(int msec)119 void audio_fade_music(int msec)
120 {
121 Mix_FadeOutMusic(msec);
122 }
123
124 /*
125 audio_find_sample
126 procura um sample de som pelo seu nome na lista
127 */
audio_find_sample(char * name)128 struct audio_sample *audio_find_sample(char *name)
129 {
130 struct audio_sample *cur;
131
132 cur = samples;
133 while(cur)
134 {
135 if(strcmp(cur->name, name) == 0)
136 return cur;
137
138 cur = cur->next;
139 }
140
141 return NULL;
142 }
143
144 /*
145 audio_play_sample
146 reproduz um sample de som, carregando-o caso necess�rio
147 */
audio_play_sample(char * name)148 void audio_play_sample(char *name)
149 {
150 struct audio_sample *sm;
151
152 sm = audio_find_sample(name);
153 if(sm == NULL)
154 {
155 /* carregar sample do ficheiro */
156 sm = malloc(sizeof(struct audio_sample));
157 sm->name = strdup(name);
158 sm->sample = Mix_LoadWAV((const char *)file_make_fname(name));
159 if(sm->sample == NULL)
160 {
161 printf("erro: audio_play_sample(%s): Mix_LoadWAV() -> %s\n",
162 name, Mix_GetError());
163 SDL_Quit();
164 exit(1);
165 }
166
167 LLIST_ADD(struct audio_sample, samples, sm);
168 }
169
170 if(audio_mute_samples)
171 return;
172
173 Mix_PlayChannel(-1, sm->sample, 0);
174 }
175