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) (unidiff)
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 @@
1#include "constants.h"
2#include "sound.h"
3
4Mix_Chunk *SoundHandler :: sounds[NR_SOUNDS];
5Mix_Music *SoundHandler :: music;
6int SoundHandler :: soundChannels[NR_SOUNDS];
7bool SoundHandler :: soundOn;
8bool SoundHandler :: musicOn;
9
10bool SoundHandler :: init( )
11{
12 // We're going to be requesting certain things from our audio
13 // device, so we set them up beforehand
14 int audio_rate = 22050;
15 Uint16 audio_format = AUDIO_S16; //AUDIO_S16; /* 16-bit stereo */
16 int audio_channels = 2;
17 int audio_buffers = 1024;//4096;
18
19 // This is where we open up our audio device. Mix_OpenAudio takes
20 // as its parameters the audio format we'd /like/ to have.
21 if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
22 {
23 printf("Unable to open audio!\n");
24 return false;
25 }
26
27 // We're going to pre-load the sound effects that we need right here
28 sounds[SND_EXPLOSION] = Mix_LoadWAV( SOUND_PATH "explosion.wav");
29 sounds[SND_THRUST] = Mix_LoadWAV( SOUND_PATH "thrust.wav");
30
31 music = 0;
32
33 soundOn = true;
34
35 return true;
36}
37
38void SoundHandler :: cleanUp()
39{
40 // Free audio sounds
41 Mix_FreeChunk( sounds[SND_EXPLOSION] );
42 Mix_FreeChunk( sounds[SND_THRUST] );
43
44 if ( music )
45 Mix_FreeMusic( music );
46
47 Mix_CloseAudio();
48}
49
50int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished )
51{
52 if ( !soundOn )
53 return -1;
54
55 if ( soundNr >= NR_SOUNDS )
56 return -1;
57
58 Mix_Chunk *chunk = sounds[soundNr];
59 if( channel == -1 || !Mix_Playing( channel ) )
60 channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops);
61
62 Mix_Volume( channel, MIX_MAX_VOLUME );
63 return channel;
64}
65
66void SoundHandler :: stopSound( int channel, bool fadeOut, int nrMilliSecs )
67{
68 if ( !soundOn )
69 return;
70
71 if ( !fadeOut )
72 Mix_HaltChannel( channel );
73 else
74 {
75 Mix_FadeOutChannel( channel, nrMilliSecs );
76 }
77}
78
79void SoundHandler :: playMusic( string musicFile )
80{
81 if ( !soundOn )
82 return;
83
84 // If music already exists - stop it playing if necessary and free it up
85 if ( music )
86 {
87 stopMusic();
88 Mix_FreeMusic( music );
89 }
90
91 // Load music
92 music = Mix_LoadMUS( musicFile.c_str() );
93 if(!music)
94 {
95 printf("Mix_LoadMUS(%s): %s\n", musicFile.c_str(), Mix_GetError());
96 // this might be a critical error...
97 }
98
99 playMusic();
100}
101
102void SoundHandler :: playMusic( bool fade )
103{
104 if ( !soundOn )
105 return;
106
107 if ( music )
108 {
109 Mix_VolumeMusic( MIX_MAX_VOLUME );
110 Mix_RewindMusic();
111
112 if ( fade )
113 Mix_FadeInMusic( music, -1, 1000 );
114 else
115 Mix_PlayMusic( music, -1 );
116
117 }
118}
119
120void SoundHandler :: stopMusic( bool fadeOut )
121{
122 if ( !music || !Mix_PlayingMusic() )
123 return;
124
125 if ( fadeOut && Mix_FadingMusic() == MIX_NO_FADING )
126 {
127 Mix_FadeOutMusic( 1000 );
128 }
129 else
130 {
131 Mix_HaltMusic();
132 }
133
134}
135
136void SoundHandler :: setMusicVolume( int vol )
137{
138 Mix_VolumeMusic( vol );
139}
140
141void SoundHandler :: setSoundsOn( bool val )
142{
143 soundOn = val;
144}
145
146void SoundHandler :: setMusicOn( bool val )
147{
148 musicOn = val;
149
150 if ( !musicOn )
151 stopMusic();
152 else
153 playMusic( true );
154}