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>
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.
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.
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.
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 */
32 char * soundfilenames[NUM_SOUNDS] = {
34 "/sounds/bigjump.wav",
37 "/sounds/herring.wav",
42 "/sounds/ricochet.wav",
43 "/sounds/bump-upgrade.wav",
44 "/sounds/upgrade.wav",
45 "/sounds/excellent.wav",
54 #include <SDL_mixer.h>
56 Mix_Chunk * sounds[NUM_SOUNDS];
57 Mix_Music * herring_song = 0;
58 Mix_Music * current_song = 0;
60 /* --- OPEN THE AUDIO DEVICE --- */
62 int open_audio (int frequency, Uint16 format, int channels, int chunksize)
64 /* if success we reserved some channels and register panning effects */
65 if (Mix_OpenAudio( frequency, format, channels, chunksize ) == 0)
67 if (Mix_ReserveChannels( SOUND_RESERVED_CHANNELS )
68 != SOUND_RESERVED_CHANNELS )
70 DEBUG_MSG( "Warning: open_audio could'nt reserve channels" );
73 /* prepare the spanning effects, no error checking */
74 Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
75 Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
85 /* --- CLOSE THE AUDIO DEVICE --- */
87 void close_audio( void )
90 Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER );
91 Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER );
97 /* --- LOAD A SOUND --- */
99 Mix_Chunk * load_sound(const std::string& file)
103 snd = Mix_LoadWAV(file.c_str());
105 if ((snd == NULL) && audio_device)
106 st_abort("Can't load", file);
112 /* --- LOAD A SONG --- */
114 Mix_Music * load_song(const std::string& file)
117 return (Mix_Music*) ~0;
119 Mix_Music* song = Mix_LoadMUS(file.c_str());
124 /* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */
126 void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker)
128 /* this won't call the function if the user has disabled sound
129 * either via menu or via command-line option
131 if (use_sound && audio_device)
133 Mix_PlayChannel( whichSpeaker, snd, 0);
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 );
141 case SOUND_RIGHT_SPEAKER:
142 Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
144 default: // keep the compiler happy
151 void free_chunk(Mix_Chunk *chunk)
155 Mix_FreeChunk( chunk );
160 void halt_music(void)
162 if (!use_music || !audio_device)
170 void play_music(Mix_Music *music)
174 if(music == current_song)
177 if (use_music && Mix_PlayMusic(music, -1) < 0)
178 st_abort("Couldn't play music: ", Mix_GetError());
180 current_song = music;
184 void free_music(Mix_Music *music)
189 Mix_FreeMusic( music );
192 void enable_music(bool enable)
201 Mix_PlayMusic(current_song, -1);