applied a patch by Duong-Khang NGUYEN <neoneurone@users.sf.net> and fixed it. ;)...
[supertux.git] / src / sound.c
1 /*
2   sound.c
3   
4   Super Tux - Audio Functions
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9  
10   April 22, 2000 - July 15, 2002
11 */
12
13 /*
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 */
19
20 #ifdef LINUX
21 #include <pwd.h>
22 #include <sys/types.h>
23 #include <ctype.h>
24 #endif
25
26 #include "defines.h"
27 #include "globals.h"
28 #include "sound.h"
29 #include "setup.h"
30
31 #ifndef NOSOUND
32
33 /* --- OPEN THE AUDIO DEVICE --- */
34
35 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
36 {
37   return Mix_OpenAudio( frequency, format, channels, chunksize );
38 }
39
40
41 /* --- LOAD A SOUND --- */
42
43 Mix_Chunk * load_sound(char * file)
44 {
45   Mix_Chunk * snd;
46
47   snd = Mix_LoadWAV(file);
48
49   /* printf message and abort if there is an initialized audio device */
50   if ((snd == NULL) && (audio_device == YES))
51     st_abort("Can't load", file);
52
53   return(snd);
54 }
55
56
57 /* --- LOAD A SONG --- */
58
59 Mix_Music * load_song(char * file)
60 {
61   Mix_Music * sng;
62
63   sng = Mix_LoadMUS(file);
64
65   /* printf message and abort if there is an initialized audio device */
66   if ((sng == NULL) && (audio_device == YES))
67     st_abort("Can't load", file);
68   return (sng);
69 }
70
71
72 /* --- PLAY A SOUND --- */
73
74 void play_sound(Mix_Chunk * snd)
75 {
76   /* this won't call the function if the user has disabled sound
77    * either via menu or via command-line option
78    */
79   if ((use_sound == YES) && (audio_device == YES))
80     {
81       Mix_PlayChannel(-1, snd, 0);
82     }
83 }
84
85
86 void free_chunk(Mix_Chunk *chunk)
87 {
88   if (chunk != NULL)
89     {
90       DEBUG_MSG( __PRETTY_FUNCTION__ );
91       Mix_FreeChunk( chunk );
92       chunk = NULL;
93     }
94 }
95
96 int playing_music(void)
97 {
98   if (use_music == YES)
99     {
100       return Mix_PlayingMusic();
101     }
102   else
103     {
104       /* we are in --disable-music we can't be playing music */
105       return 0;
106     }
107 }
108
109
110 int halt_music(void)
111 {
112   if ((use_music == YES) && (audio_device == YES))
113     {
114       return Mix_HaltMusic();
115     }
116   else
117     {
118       return 0;
119     }
120 }
121
122
123 int play_music(Mix_Music *music, int loops)
124 {
125   if ((use_music == YES) && (audio_device == YES))
126     {
127       DEBUG_MSG(__PRETTY_FUNCTION__);
128       return Mix_PlayMusic(music, loops);
129     }
130   else
131     {
132       /* return error since you're trying to play music in --disable-sound mode */
133       return -1;
134     }
135 }
136
137
138 void free_music(Mix_Music *music)
139 {
140   if ( music != NULL )
141     {
142       DEBUG_MSG(__PRETTY_FUNCTION__);
143       Mix_FreeMusic( music );
144       music = NULL;
145     }
146 }
147
148 #else
149
150 int open_audio (int frequency, int format, int channels, int chunksize)
151 {
152   return -1;
153 }
154
155 void* load_sound(void* file)
156 {
157   return NULL;
158 }
159 void play_sound(void * snd)
160 {}
161 void* load_song(void* file)
162 {
163   return NULL;
164 }
165
166 int playing_music()
167 {
168   return 0;
169 }
170 void halt_music()
171 {}
172 int play_music(void *music, int loops)
173 {
174   return 0;
175 }
176 void free_music(void *music)
177 {}
178 void free_chunk(void *chunk)
179 {}
180
181 #endif