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