summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/sound.cpp
authorandyq <andyq>2003-01-20 23:11:56 (UTC)
committer andyq <andyq>2003-01-20 23:11:56 (UTC)
commit92419b025aa5d82bb7592281af5505ff931c2e58 (patch) (side-by-side diff)
tree374ee9ca86ef716ce388c8a28e38261f6a04ce7a /noncore/games/sfcave-sdl/sound.cpp
parent02090d2e63ad8398c0a8a9f1fb895a9c6e42514b (diff)
downloadopie-92419b025aa5d82bb7592281af5505ff931c2e58.zip
opie-92419b025aa5d82bb7592281af5505ff931c2e58.tar.gz
opie-92419b025aa5d82bb7592281af5505ff931c2e58.tar.bz2
Initial Revision
Diffstat (limited to 'noncore/games/sfcave-sdl/sound.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave-sdl/sound.cpp154
1 files changed, 154 insertions, 0 deletions
diff --git a/noncore/games/sfcave-sdl/sound.cpp b/noncore/games/sfcave-sdl/sound.cpp
new file mode 100644
index 0000000..5fda859
--- a/dev/null
+++ b/noncore/games/sfcave-sdl/sound.cpp
@@ -0,0 +1,154 @@
+#include "constants.h"
+#include "sound.h"
+
+Mix_Chunk *SoundHandler :: sounds[NR_SOUNDS];
+Mix_Music *SoundHandler :: music;
+int SoundHandler :: soundChannels[NR_SOUNDS];
+bool SoundHandler :: soundOn;
+bool SoundHandler :: musicOn;
+
+bool SoundHandler :: init( )
+{
+ // We're going to be requesting certain things from our audio
+ // device, so we set them up beforehand
+ int audio_rate = 22050;
+ Uint16 audio_format = AUDIO_S16; //AUDIO_S16; /* 16-bit stereo */
+ int audio_channels = 2;
+ int audio_buffers = 1024;//4096;
+
+ // This is where we open up our audio device. Mix_OpenAudio takes
+ // as its parameters the audio format we'd /like/ to have.
+ if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
+ {
+ printf("Unable to open audio!\n");
+ return false;
+ }
+
+ // We're going to pre-load the sound effects that we need right here
+ sounds[SND_EXPLOSION] = Mix_LoadWAV( SOUND_PATH "explosion.wav");
+ sounds[SND_THRUST] = Mix_LoadWAV( SOUND_PATH "thrust.wav");
+
+ music = 0;
+
+ soundOn = true;
+
+ return true;
+}
+
+void SoundHandler :: cleanUp()
+{
+ // Free audio sounds
+ Mix_FreeChunk( sounds[SND_EXPLOSION] );
+ Mix_FreeChunk( sounds[SND_THRUST] );
+
+ if ( music )
+ Mix_FreeMusic( music );
+
+ Mix_CloseAudio();
+}
+
+int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished )
+{
+ if ( !soundOn )
+ return -1;
+
+ if ( soundNr >= NR_SOUNDS )
+ return -1;
+
+ Mix_Chunk *chunk = sounds[soundNr];
+ if( channel == -1 || !Mix_Playing( channel ) )
+ channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops);
+
+ Mix_Volume( channel, MIX_MAX_VOLUME );
+ return channel;
+}
+
+void SoundHandler :: stopSound( int channel, bool fadeOut, int nrMilliSecs )
+{
+ if ( !soundOn )
+ return;
+
+ if ( !fadeOut )
+ Mix_HaltChannel( channel );
+ else
+ {
+ Mix_FadeOutChannel( channel, nrMilliSecs );
+ }
+}
+
+void SoundHandler :: playMusic( string musicFile )
+{
+ if ( !soundOn )
+ return;
+
+ // If music already exists - stop it playing if necessary and free it up
+ if ( music )
+ {
+ stopMusic();
+ Mix_FreeMusic( music );
+ }
+
+ // Load music
+ music = Mix_LoadMUS( musicFile.c_str() );
+ if(!music)
+ {
+ printf("Mix_LoadMUS(%s): %s\n", musicFile.c_str(), Mix_GetError());
+ // this might be a critical error...
+ }
+
+ playMusic();
+}
+
+void SoundHandler :: playMusic( bool fade )
+{
+ if ( !soundOn )
+ return;
+
+ if ( music )
+ {
+ Mix_VolumeMusic( MIX_MAX_VOLUME );
+ Mix_RewindMusic();
+
+ if ( fade )
+ Mix_FadeInMusic( music, -1, 1000 );
+ else
+ Mix_PlayMusic( music, -1 );
+
+ }
+}
+
+void SoundHandler :: stopMusic( bool fadeOut )
+{
+ if ( !music || !Mix_PlayingMusic() )
+ return;
+
+ if ( fadeOut && Mix_FadingMusic() == MIX_NO_FADING )
+ {
+ Mix_FadeOutMusic( 1000 );
+ }
+ else
+ {
+ Mix_HaltMusic();
+ }
+
+}
+
+void SoundHandler :: setMusicVolume( int vol )
+{
+ Mix_VolumeMusic( vol );
+}
+
+void SoundHandler :: setSoundsOn( bool val )
+{
+ soundOn = val;
+}
+
+void SoundHandler :: setMusicOn( bool val )
+{
+ musicOn = val;
+
+ if ( !musicOn )
+ stopMusic();
+ else
+ playMusic( true );
+}