author | andyq <andyq> | 2003-01-21 20:37:00 (UTC) |
---|---|---|
committer | andyq <andyq> | 2003-01-21 20:37:00 (UTC) |
commit | 0a6563fcc2f49857c581d9def24407a3a4ef526c (patch) (unidiff) | |
tree | f1b82a4bd7582ef2cb722cffb87eecff1e1f96e6 /noncore/games/sfcave-sdl/sound.cpp | |
parent | 50b5915b48fc5cbacf23e4d2b75d7a266f141a4a (diff) | |
download | opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.zip opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.gz opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.bz2 |
Clean up of code - fixed memory leaks (most of them) and added new custom config menu
Diffstat (limited to 'noncore/games/sfcave-sdl/sound.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/sound.cpp | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/noncore/games/sfcave-sdl/sound.cpp b/noncore/games/sfcave-sdl/sound.cpp index 5fda859..855f2e6 100644 --- a/noncore/games/sfcave-sdl/sound.cpp +++ b/noncore/games/sfcave-sdl/sound.cpp | |||
@@ -10,119 +10,122 @@ bool SoundHandler :: musicOn; | |||
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 | 35 | ||
35 | return true; | 36 | return true; |
36 | } | 37 | } |
37 | 38 | ||
38 | void SoundHandler :: cleanUp() | 39 | void SoundHandler :: cleanUp() |
39 | { | 40 | { |
40 | // Free audio sounds | 41 | // Free audio sounds |
42 | if ( sounds[SND_EXPLOSION] ) | ||
41 | Mix_FreeChunk( sounds[SND_EXPLOSION] ); | 43 | Mix_FreeChunk( sounds[SND_EXPLOSION] ); |
44 | if ( sounds[SND_THRUST] ) | ||
42 | Mix_FreeChunk( sounds[SND_THRUST] ); | 45 | Mix_FreeChunk( sounds[SND_THRUST] ); |
43 | 46 | ||
44 | if ( music ) | 47 | if ( music ) |
45 | Mix_FreeMusic( music ); | 48 | Mix_FreeMusic( music ); |
46 | 49 | ||
47 | Mix_CloseAudio(); | 50 | Mix_CloseAudio(); |
48 | } | 51 | } |
49 | 52 | ||
50 | int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished ) | 53 | int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished ) |
51 | { | 54 | { |
52 | if ( !soundOn ) | 55 | if ( !soundOn ) |
53 | return -1; | 56 | return -1; |
54 | 57 | ||
55 | if ( soundNr >= NR_SOUNDS ) | 58 | if ( soundNr >= NR_SOUNDS || !sounds[soundNr] ) |
56 | return -1; | 59 | return -1; |
57 | 60 | ||
58 | Mix_Chunk *chunk = sounds[soundNr]; | 61 | Mix_Chunk *chunk = sounds[soundNr]; |
59 | if( channel == -1 || !Mix_Playing( channel ) ) | 62 | if( channel == -1 || !Mix_Playing( channel ) ) |
60 | channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops); | 63 | channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops); |
61 | 64 | ||
62 | Mix_Volume( channel, MIX_MAX_VOLUME ); | 65 | Mix_Volume( channel, MIX_MAX_VOLUME ); |
63 | return channel; | 66 | return channel; |
64 | } | 67 | } |
65 | 68 | ||
66 | void SoundHandler :: stopSound( int channel, bool fadeOut, int nrMilliSecs ) | 69 | void SoundHandler :: stopSound( int channel, bool fadeOut, int nrMilliSecs ) |
67 | { | 70 | { |
68 | if ( !soundOn ) | 71 | if ( !soundOn ) |
69 | return; | 72 | return; |
70 | 73 | ||
71 | if ( !fadeOut ) | 74 | if ( !fadeOut ) |
72 | Mix_HaltChannel( channel ); | 75 | Mix_HaltChannel( channel ); |
73 | else | 76 | else |
74 | { | 77 | { |
75 | Mix_FadeOutChannel( channel, nrMilliSecs ); | 78 | Mix_FadeOutChannel( channel, nrMilliSecs ); |
76 | } | 79 | } |
77 | } | 80 | } |
78 | 81 | ||
79 | void SoundHandler :: playMusic( string musicFile ) | 82 | void SoundHandler :: playMusic( string musicFile ) |
80 | { | 83 | { |
81 | if ( !soundOn ) | 84 | if ( !soundOn ) |
82 | return; | 85 | return; |
83 | 86 | ||
84 | // 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 |
85 | if ( music ) | 88 | if ( music ) |
86 | { | 89 | { |
87 | stopMusic(); | 90 | stopMusic(); |
88 | Mix_FreeMusic( music ); | 91 | Mix_FreeMusic( music ); |
89 | } | 92 | } |
90 | 93 | ||
91 | // Load music | 94 | // Load music |
92 | music = Mix_LoadMUS( musicFile.c_str() ); | 95 | music = Mix_LoadMUS( musicFile.c_str() ); |
93 | if(!music) | 96 | if(!music) |
94 | { | 97 | { |
95 | printf("Mix_LoadMUS(%s): %s\n", musicFile.c_str(), Mix_GetError()); | 98 | printf("Mix_LoadMUS(%s): %s\n", musicFile.c_str(), Mix_GetError()); |
96 | // this might be a critical error... | 99 | // this might be a critical error... |
97 | } | 100 | } |
98 | 101 | ||
99 | playMusic(); | 102 | playMusic(); |
100 | } | 103 | } |
101 | 104 | ||
102 | void SoundHandler :: playMusic( bool fade ) | 105 | void SoundHandler :: playMusic( bool fade ) |
103 | { | 106 | { |
104 | if ( !soundOn ) | 107 | if ( !musicOn ) |
105 | return; | 108 | return; |
106 | 109 | ||
107 | if ( music ) | 110 | if ( music ) |
108 | { | 111 | { |
109 | Mix_VolumeMusic( MIX_MAX_VOLUME ); | 112 | Mix_VolumeMusic( MIX_MAX_VOLUME ); |
110 | Mix_RewindMusic(); | 113 | Mix_RewindMusic(); |
111 | 114 | ||
112 | if ( fade ) | 115 | if ( fade ) |
113 | Mix_FadeInMusic( music, -1, 1000 ); | 116 | Mix_FadeInMusic( music, -1, 1000 ); |
114 | else | 117 | else |
115 | Mix_PlayMusic( music, -1 ); | 118 | Mix_PlayMusic( music, -1 ); |
116 | 119 | ||
117 | } | 120 | } |
118 | } | 121 | } |
119 | 122 | ||
120 | void SoundHandler :: stopMusic( bool fadeOut ) | 123 | void SoundHandler :: stopMusic( bool fadeOut ) |
121 | { | 124 | { |
122 | if ( !music || !Mix_PlayingMusic() ) | 125 | if ( !music || !Mix_PlayingMusic() ) |
123 | return; | 126 | return; |
124 | 127 | ||
125 | if ( fadeOut && Mix_FadingMusic() == MIX_NO_FADING ) | 128 | if ( fadeOut && Mix_FadingMusic() == MIX_NO_FADING ) |
126 | { | 129 | { |
127 | Mix_FadeOutMusic( 1000 ); | 130 | Mix_FadeOutMusic( 1000 ); |
128 | } | 131 | } |