Just fixed two small bugs in the gameplay during pause:
[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                                        "/sounds/explode.wav"
52                                     };
53
54
55 #include <SDL_mixer.h>
56
57 Mix_Chunk * sounds[NUM_SOUNDS];
58 Mix_Music * herring_song = 0;
59 Mix_Music * current_song = 0;
60
61 /* --- OPEN THE AUDIO DEVICE --- */
62
63 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
64 {
65   /* if success we reserved some channels and register panning effects */
66   if (Mix_OpenAudio( frequency, format, channels, chunksize ) == 0)
67     {
68       if (Mix_ReserveChannels( SOUND_RESERVED_CHANNELS )
69                             != SOUND_RESERVED_CHANNELS )
70         {
71           DEBUG_MSG( "Warning: open_audio could'nt reserve channels" );
72         }
73
74       /* prepare the spanning effects, no error checking */
75       Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
76       Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
77       return 0;
78     }
79   else
80     {
81       return -1;
82     }
83 }
84
85
86 /* --- CLOSE THE AUDIO DEVICE --- */
87
88 void close_audio( void )
89 {
90   if (audio_device) {
91     Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER );
92     Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER );
93     Mix_CloseAudio();
94   }
95 }
96
97
98 /* --- LOAD A SOUND --- */
99
100 Mix_Chunk * load_sound(const std::string& file)
101 {
102   Mix_Chunk * snd;
103
104   snd = Mix_LoadWAV(file.c_str());
105
106   if ((snd == NULL) && audio_device)
107     st_abort("Can't load", file);
108
109   return(snd);
110 }
111
112
113 /* --- LOAD A SONG --- */
114
115 Mix_Music * load_song(const std::string& file)
116 {
117   if(!audio_device)
118     return (Mix_Music*) ~0;
119   
120   Mix_Music* song = Mix_LoadMUS(file.c_str());
121   return song;
122 }
123
124
125 /* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */
126
127 void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker)
128 {
129   /* this won't call the function if the user has disabled sound
130    * either via menu or via command-line option
131    */
132   if (use_sound && audio_device)
133     {
134       Mix_PlayChannel( whichSpeaker, snd, 0);
135
136       /* prepare for panning effects for next call */
137       /* warning: currently, I do not check for errors here */
138       switch (whichSpeaker) {
139         case SOUND_LEFT_SPEAKER:
140           Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
141           break;
142         case SOUND_RIGHT_SPEAKER:
143           Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
144           break;
145         default:  // keep the compiler happy
146           break;
147       }
148     }
149 }
150
151
152 void free_chunk(Mix_Chunk *chunk)
153 {
154   if (chunk != NULL)
155     {
156       Mix_FreeChunk( chunk );
157       chunk = NULL;
158     }
159 }
160
161 void halt_music(void)
162 {
163   if (!use_music || !audio_device)
164     return;
165
166   Mix_HaltMusic();
167   current_song = 0;
168 }
169
170
171 void play_music(Mix_Music *music)
172 {
173   if (!audio_device)
174     return;
175   if(music == current_song)
176     return;
177
178   if (use_music && Mix_PlayMusic(music, -1) < 0)
179     st_abort("Couldn't play music: ", Mix_GetError());
180
181   current_song = music;
182 }
183
184
185 void free_music(Mix_Music *music)
186 {
187   if(!audio_device)
188     return;
189
190   Mix_FreeMusic( music );
191 }
192
193 void enable_music(bool enable)
194 {
195   if(!audio_device)
196     return;
197   
198   use_music = enable;
199   if(!use_music)
200     Mix_HaltMusic();
201   else
202     Mix_PlayMusic(current_song, -1);
203 }
204