Implemented --help and --disable-sound (patch by Duong-Khang NGUYEN <neoneurone@users...
[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 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <SDL.h>
19 #include <SDL_image.h>
20
21 #ifdef LINUX
22 #include <pwd.h>
23 #include <sys/types.h>
24 #include <ctype.h>
25 #endif
26
27 #include "defines.h"
28 #include "globals.h"
29 #include "sound.h"
30 #include "setup.h"
31
32 #ifndef NOSOUND
33
34 /* --- LOAD A SOUND --- */
35
36 Mix_Chunk * load_sound(char * file)
37 {
38   Mix_Chunk * snd;
39   
40   if (use_sound)
41   {
42     snd = Mix_LoadWAV(file);
43   
44     if (snd == NULL)
45       st_abort("Can't load", file);
46   }
47   else
48     snd = NULL;
49
50   return(snd);
51 }
52
53
54 /* --- LOAD A SONG --- */
55
56 Mix_Music * load_song(char * file)
57 {
58   Mix_Music * sng;
59
60   if (use_sound)
61   {
62     sng = Mix_LoadMUS(file);
63
64     if (sng == NULL)
65       st_abort("Can't load", file);
66   }
67   else
68     sng = NULL;
69
70   return (sng);
71 }
72
73
74 /* --- PLAY A SOUND --- */
75   
76  void playsound(Mix_Chunk * snd)
77  {
78   // this won't call the function if the user has disabled sound
79   if (use_sound) {
80     Mix_PlayChannel(-1, snd, 0);
81   }
82 }
83  
84  
85 void free_chunk(Mix_Chunk *chunk)
86 {
87   if (use_sound)
88     Mix_FreeChunk( chunk );
89 }
90     
91 int playing_music(void)
92 {
93  if (use_sound) {
94     return Mix_PlayingMusic();
95   }
96   else {
97     // we are in --disable-sound or NOSOUND, we can't be playing music !
98     return 0;
99   }
100 }
101
102  
103 int halt_music(void)
104 {
105   if (use_sound) {
106     return Mix_HaltMusic();
107   }
108   else {
109     return 0;
110   }
111 }
112  
113  
114 int play_music(Mix_Music *music, int loops)
115 {
116   if (use_sound) {
117     return Mix_PlayMusic(music, loops);
118   }
119   else {
120     // return error since you're trying to play music in --disable-sound mode
121     return -1;
122   }
123 }
124  
125
126 void free_music(Mix_Music *music)
127 {
128   if (use_sound)
129     Mix_FreeMusic( music );
130 }
131
132 #else
133
134 void* load_sound(void* file) { return NULL; }
135 void playsound(void * snd) {}
136 void* load_song(void* file) { return NULL; }
137 int Mix_PlayingMusic() { return 0; }
138 void Mix_HaltMusic() {}
139 int Mix_PlayMusic() { return -1; }
140 void Mix_FreeMusic() {}
141 void Mix_FreeChunk() {}
142 int Mix_OpenAudio(int a, int b, int c, int d) { return -1; }
143
144 int playing_music() { return 0; }
145 void halt_music() {}
146 int play_music(int *music, int loops) { return 0;}
147 void free_music(int *music) {}
148 void free_chunk(int *chunk) {}
149
150 #endif