-/*
- sound.c
-
- Super Tux - Audio Functions
-
- by Bill Kendrick
- bill@newbreedsoftware.com
- http://www.newbreedsoftware.com/supertux/
-
- April 22, 2000 - March 15, 2004
-*/
+// $Id$
+//
+// SuperTux - A Jump'n Run
+// Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
+// Copyright (C) 2004 Duong-Khang NGUYEN <neoneurone@users.sf.net>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "defines.h"
#include "globals.h"
#include "setup.h"
/*global variable*/
-int use_sound; /* handle sound on/off menu and command-line option */
-int use_music; /* handle music on/off menu and command-line option */
-int audio_device; /* != 0: available and initialized */
-int current_music;
+bool use_sound = true; /* handle sound on/off menu and command-line option */
+bool use_music = true; /* handle music on/off menu and command-line option */
+bool audio_device = true; /* != 0: available and initialized */
char * soundfilenames[NUM_SOUNDS] = {
"/sounds/jump.wav",
"/sounds/bigjump.wav",
"/sounds/skid.wav",
- "/sounds/distro.wav",
- "/sounds/herring.wav",
+ "/sounds/coin.wav",
+ "/sounds/invincible.wav",
"/sounds/brick.wav",
"/sounds/hurt.wav",
"/sounds/squish.wav",
"/sounds/ricochet.wav",
"/sounds/bump-upgrade.wav",
"/sounds/upgrade.wav",
- "/sounds/excellent.wav",
- "/sounds/coffee.wav",
+ "/sounds/grow.wav",
+ "/sounds/fire-flower.wav",
"/sounds/shoot.wav",
"/sounds/lifeup.wav",
"/sounds/stomp.wav",
- "/sounds/kick.wav"
+ "/sounds/kick.wav",
+ "/sounds/explosion.wav"
};
-#ifndef NOSOUND
-
#include <SDL_mixer.h>
Mix_Chunk * sounds[NUM_SOUNDS];
-Mix_Music * level_song, * level_song_fast, * herring_song;
/* --- OPEN THE AUDIO DEVICE --- */
int open_audio (int frequency, Uint16 format, int channels, int chunksize)
{
- /* if success we reserved some channels and register panning effects */
- if (Mix_OpenAudio( frequency, format, channels, chunksize ) == 0)
- {
- if (Mix_ReserveChannels( SOUND_RESERVED_CHANNELS )
- != SOUND_RESERVED_CHANNELS )
- {
- DEBUG_MSG( "Warning: open_audio could'nt reserve channels" );
- }
+ if (Mix_OpenAudio( frequency, format, channels, chunksize ) < 0)
+ return -1;
- /* prepare the spanning effects, no error checking */
- Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
- Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
- return 0;
- }
- else
- {
- return -1;
- }
+ // allocate 16 channels for mixing
+ if (Mix_AllocateChannels(8) != 8)
+ return -2;
+
+ /* reserve some channels and register panning effects */
+ if (Mix_ReserveChannels(SOUND_RESERVED_CHANNELS) != SOUND_RESERVED_CHANNELS)
+ return -3;
+
+ /* prepare the spanning effects */
+ Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
+ Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
+ return 0;
}
void close_audio( void )
{
- if (audio_device == YES) {
+ if (audio_device) {
Mix_UnregisterAllEffects( SOUND_LEFT_SPEAKER );
Mix_UnregisterAllEffects( SOUND_RIGHT_SPEAKER );
Mix_CloseAudio();
/* --- LOAD A SOUND --- */
-Mix_Chunk * load_sound(const std::string& file)
+Mix_Chunk* load_sound(const std::string& file)
{
- Mix_Chunk * snd;
-
- snd = Mix_LoadWAV(file.c_str());
+ if(!audio_device)
+ return 0;
+
+ Mix_Chunk* snd = Mix_LoadWAV(file.c_str());
- /* printf message and abort if there is an initialized audio device */
- if ((snd == NULL) && (audio_device == YES))
- st_abort("Can't load", file.c_str());
+ if (snd == 0)
+ st_abort("Can't load", file);
return(snd);
}
-
-/* --- LOAD A SONG --- */
-
-Mix_Music * load_song(const std::string& file)
-{
- Mix_Music * sng;
-
- sng = Mix_LoadMUS(file.c_str());
-
- /* printf message and abort if there is an initialized audio device */
- if ((sng == NULL) && (audio_device == YES))
- st_abort("Can't load", file.c_str());
- return (sng);
-}
-
-
/* --- PLAY A SOUND ON LEFT OR RIGHT OR CENTER SPEAKER --- */
void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker)
/* this won't call the function if the user has disabled sound
* either via menu or via command-line option
*/
- if ((use_sound == YES) && (audio_device == YES))
- {
- Mix_PlayChannel( whichSpeaker, snd, 0);
-
- /* prepare for panning effects for next call */
- /* warning: currently, I do not check for errors here */
- switch (whichSpeaker) {
- case SOUND_LEFT_SPEAKER:
- Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
- break;
- case SOUND_RIGHT_SPEAKER:
- Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
- break;
- default: // keep the compiler happy
- break;
- }
- }
-}
-
-
-void free_chunk(Mix_Chunk *chunk)
-{
- if (chunk != NULL)
- {
- DEBUG_MSG( __PRETTY_FUNCTION__ );
- Mix_FreeChunk( chunk );
- chunk = NULL;
- }
-}
-
-
-int playing_music(void)
-{
- if (use_music == YES)
- {
- return Mix_PlayingMusic();
- }
- else
- {
- /* we are in --disable-music we can't be playing music */
- return 0;
- }
-}
-
-
-int halt_music(void)
-{
- if ((use_music == YES) && (audio_device == YES))
- {
- return Mix_HaltMusic();
- }
- else
- {
- return 0;
- }
-}
-
-
-int play_music(Mix_Music *music, int loops)
-{
- if ((use_music == YES) && (audio_device == YES))
- {
- DEBUG_MSG(__PRETTY_FUNCTION__);
- return Mix_PlayMusic(music, loops);
- }
- else
- {
- /* return error since you're trying to play music in --disable-sound mode */
- return -1;
- }
-}
-
-
-void free_music(Mix_Music *music)
-{
- if ( music != NULL )
- {
- DEBUG_MSG(__PRETTY_FUNCTION__);
- Mix_FreeMusic( music );
- music = NULL;
- }
-}
- int get_current_music()
- {
- return current_music;
- }
-
- void set_current_music(int music)
- {
- current_music = music;
- }
-
- void play_current_music()
- {
- if(playing_music())
- halt_music();
-
- switch(current_music)
- {
- case LEVEL_MUSIC:
- play_music(level_song, -1); // -1 to play forever
- break;
- case HERRING_MUSIC:
- play_music(herring_song, -1);
- break;
- case HURRYUP_MUSIC:
- play_music(level_song_fast, -1);
- break;
- case NO_MUSIC: // keep the compiler happy for the moment :-)
- {}
- /*default:*/
- }
- /* use halt_music whenever you want to stop it */
-}
-
-#else
-
-void* sounds[NUM_SOUNDS];
-void* level_song, * level_song_fast, * herring_song;
-
-int open_audio (int frequency, int format, int channels, int chunksize)
-{
- return -1;
-}
-
-
-void close_audio(void)
-{}
-
-
-void* load_sound(void* file)
-{
- return NULL;
-}
-
-
-void play_sound(void * snd, enum Sound_Speaker whichSpeaker)
-{}
-
-
-void* load_song(void* file)
-{
- return NULL;
-}
-
+ if(!audio_device || !use_sound)
+ return;
-int playing_music()
-{
- return 0;
-}
-
-
-void halt_music()
-{}
-
-
-int play_music(void *music, int loops)
-{
- return 0;
-}
+ Mix_PlayChannel( whichSpeaker, snd, 0);
-
-void free_music(void *music)
-{}
-
-
-void free_chunk(void *chunk)
-{}
-
-int get_current_music()
-{
+ /* prepare for panning effects for next call */
+ switch (whichSpeaker) {
+ case SOUND_LEFT_SPEAKER:
+ Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 );
+ break;
+ case SOUND_RIGHT_SPEAKER:
+ Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 );
+ break;
+ default: // keep the compiler happy
+ break;
+ }
}
-void set_current_music(int music)
+void free_chunk(Mix_Chunk *chunk)
{
+ Mix_FreeChunk( chunk );
}
-void play_current_music(void)
-{}
-
-#endif