Changed MAX_BULLETS from 2 to 1.
[supertux.git] / src / sound.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Duong-Khang NGUYEN <neoneurone@users.sf.net>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 #include "defines.h"
22 #include "globals.h"
23 #include "sound.h"
24 #include "setup.h"
25
26 /*global variable*/
27 bool use_sound;           /* handle sound on/off menu and command-line option */
28 bool use_music;           /* handle music on/off menu and command-line option */
29 bool audio_device;        /* != 0: available and initialized */
30 int current_music;
31
32 char * soundfilenames[NUM_SOUNDS] = {
33                                        "/sounds/jump.wav",
34                                        "/sounds/bigjump.wav",
35                                        "/sounds/skid.wav",
36                                        "/sounds/distro.wav",
37                                        "/sounds/herring.wav",
38                                        "/sounds/brick.wav",
39                                        "/sounds/hurt.wav",
40                                        "/sounds/squish.wav",
41                                        "/sounds/fall.wav",
42                                        "/sounds/ricochet.wav",
43                                        "/sounds/bump-upgrade.wav",
44                                        "/sounds/upgrade.wav",
45                                        "/sounds/excellent.wav",
46                                        "/sounds/coffee.wav",
47                                        "/sounds/shoot.wav",
48                                        "/sounds/lifeup.wav",
49                                        "/sounds/stomp.wav",
50                                        "/sounds/kick.wav"
51                                     };
52
53
54 #include <SDL_mixer.h>
55
56 Mix_Chunk * sounds[NUM_SOUNDS];
57 Mix_Music * herring_song = 0;
58 Mix_Music * current_song = 0;
59
60 /* --- OPEN THE AUDIO DEVICE --- */
61
62 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
63 {
64   /* if success we reserved some channels and register panning effects */
65   if (Mix_OpenAudio( frequency, format, channels, chunksize ) == 0)
66     {
67       if (Mix_ReserveChannels( SOUND_RESERVED_CHANNELS )
68                             != SOUND_RESERVED_CHANNELS )
69         {
70           DEBUG_MSG( "Warning: open_audio could'nt reserve channels" );
71         }
72
73       /* prepare the spanning effects, no error checking */
74       Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
75       Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
76       return 0;
77     }
78   else
79     {
80       return -1;
81     }
82 }
83
84
85 /* --- CLOSE THE AUDIO DEVICE --- */
86
87 void close_audio( void )
88 {
89   if (audio_device) {
90     Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER );
91     Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER );
92     Mix_CloseAudio();
93   }
94 }
95
96
97 /* --- LOAD A SOUND --- */
98
99 Mix_Chunk * load_sound(const std::string& file)
100 {
101   Mix_Chunk * snd;
102
103   snd = Mix_LoadWAV(file.c_str());
104
105   if ((snd == NULL) && audio_device)
106     st_abort("Can't load", file);
107
108   return(snd);
109 }
110
111
112 /* --- LOAD A SONG --- */
113
114 Mix_Music * load_song(const std::string& file)
115 {
116   if(!audio_device)
117     return (Mix_Music*) ~0;
118   
119   Mix_Music* song = Mix_LoadMUS(file.c_str());
120   return song;
121 }
122
123
124 /* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */
125
126 void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker)
127 {
128   /* this won't call the function if the user has disabled sound
129    * either via menu or via command-line option
130    */
131   if (use_sound && audio_device)
132     {
133       Mix_PlayChannel( whichSpeaker, snd, 0);
134
135       /* prepare for panning effects for next call */
136       /* warning: currently, I do not check for errors here */
137       switch (whichSpeaker) {
138         case SOUND_LEFT_SPEAKER:
139           Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
140           break;
141         case SOUND_RIGHT_SPEAKER:
142           Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
143           break;
144         default:  // keep the compiler happy
145           break;
146       }
147     }
148 }
149
150
151 void free_chunk(Mix_Chunk *chunk)
152 {
153   if (chunk != NULL)
154     {
155       Mix_FreeChunk( chunk );
156       chunk = NULL;
157     }
158 }
159
160 void halt_music(void)
161 {
162   if (!use_music || !audio_device)
163     return;
164
165   Mix_HaltMusic();
166   current_song = 0;
167 }
168
169
170 void play_music(Mix_Music *music)
171 {
172   if (!audio_device)
173     return;
174   if(music == current_song)
175     return;
176
177   if (use_music && Mix_PlayMusic(music, -1) < 0)
178     st_abort("Couldn't play music: ", Mix_GetError());
179
180   current_song = music;
181 }
182
183
184 void free_music(Mix_Music *music)
185 {
186   if(!audio_device)
187     return;
188
189   Mix_FreeMusic( music );
190 }
191
192 void enable_music(bool enable)
193 {
194   if(!audio_device)
195     return;
196   
197   use_music = enable;
198   if(!use_music)
199     Mix_HaltMusic();
200   else
201     Mix_PlayMusic(current_song, -1);
202 }
203