~ubuntu-branches/ubuntu/precise/viruskiller/precise

« back to all changes in this revision

Viewing changes to .pc/dont_crash_if_no_sound_files.patch/src/CAudio.cpp

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2011-09-12 17:21:21 UTC
  • Revision ID: package-import@ubuntu.com-20110912172121-rwy0vg0eo4e01x0e
Tags: 1.0-1.dfsg.1-1ubuntu1
Fix FTBFS with ld --as-needed. LP: #832825.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2004 Parallel Realities
 
3
 
 
4
This program is free software; you can redistribute it and/or
 
5
modify it under the terms of the GNU General Public License
 
6
as published by the Free Software Foundation; either version 2
 
7
of the License, or (at your option) any later version.
 
8
 
 
9
This program is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
12
 
 
13
See the GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 
 
19
*/
 
20
 
 
21
#include "headers.h"
 
22
 
 
23
Audio::Audio()
 
24
{
 
25
        useAudio = 1;
 
26
        useSound = true;
 
27
        useMusic = true;
 
28
 
 
29
        musicVolume = 100;
 
30
        soundVolume = 128;
 
31
 
 
32
        for (int i = 0 ; i < MAX_SOUNDS ; i++)
 
33
                sound[i] = NULL;
 
34
 
 
35
        music = NULL;
 
36
}
 
37
 
 
38
void Audio::setSoundVolume(int soundVolume)
 
39
{
 
40
        this->soundVolume = soundVolume;
 
41
        if (engine->useAudio)
 
42
                Mix_Volume(-1, soundVolume);
 
43
}
 
44
 
 
45
void Audio::setMusicVolume(int musicVolume)
 
46
{
 
47
        this->musicVolume = musicVolume;
 
48
        if (engine->useAudio)
 
49
                Mix_VolumeMusic(musicVolume);
 
50
}
 
51
 
 
52
void Audio::registerEngine(Engine *engine)
 
53
{
 
54
        this->engine = engine;
 
55
}
 
56
 
 
57
bool Audio::loadSound(int i, char *filename)
 
58
{
 
59
        if (!engine->useAudio)
 
60
                return true;
 
61
                
 
62
        if (i >= MAX_SOUNDS)
 
63
        {
 
64
                printf("ERROR: SOUND INDEX IS HIGHER THAN MAXIMUM ALLOWED %d >= %d\n", i, MAX_SOUNDS);
 
65
                exit(1);
 
66
        }
 
67
 
 
68
        if (sound[i] != NULL)
 
69
        {
 
70
                Mix_FreeChunk(sound[i]);
 
71
                sound[i] = NULL;
 
72
        }
 
73
 
 
74
        #if USEPAK
 
75
                engine->unpack(filename, PAK_SOUND);
 
76
                sound[i] = Mix_LoadWAV_RW(engine->sdlrw, 1);
 
77
        #else
 
78
                sound[i] = Mix_LoadWAV(filename);
 
79
        #endif
 
80
 
 
81
        if (!sound[i])
 
82
        {
 
83
                printf("Sound '%s' not loaded\n", filename);
 
84
                return false;
 
85
        }
 
86
 
 
87
        return true;
 
88
}
 
89
 
 
90
bool Audio::loadMusic(char *filename)
 
91
{
 
92
        char tempPath[PATH_MAX];
 
93
        
 
94
        sprintf(tempPath, "%smusic.mod", engine->userHomeDirectory);
 
95
        
 
96
        if (!engine->useAudio)
 
97
        {
 
98
                return true;
 
99
        }
 
100
 
 
101
        remove(tempPath);
 
102
        
 
103
        SDL_Delay(250); // wait a bit, just to be sure!
 
104
 
 
105
        if (music != NULL)
 
106
        {
 
107
                Mix_HaltMusic();
 
108
                SDL_Delay(5);
 
109
                Mix_FreeMusic(music);
 
110
                music = NULL;
 
111
        }
 
112
 
 
113
        #if USEPAK
 
114
                engine->unpack(filename, PAK_MUSIC);
 
115
                music = Mix_LoadMUS(tempPath);
 
116
        #else
 
117
                music = Mix_LoadMUS(filename);
 
118
        #endif
 
119
 
 
120
        if (!music)
 
121
        {
 
122
                debug(("WARNING - Failed to load %s\n", filename));
 
123
                return false;
 
124
        }
 
125
 
 
126
        return true;
 
127
}
 
128
 
 
129
void Audio::playSound(int snd, int channel)
 
130
{
 
131
        if ((!engine->useAudio) || (soundVolume == 0))
 
132
                return;
 
133
 
 
134
        Mix_Volume(channel, soundVolume);
 
135
 
 
136
        Mix_PlayChannel(channel, sound[snd], 0);
 
137
}
 
138
 
 
139
void Audio::playSound(int snd, int channel, int loops)
 
140
{
 
141
        if ((!engine->useAudio) || (soundVolume == 0))
 
142
                return;
 
143
 
 
144
        Mix_Volume(channel, soundVolume);
 
145
 
 
146
        Mix_PlayChannel(channel, sound[snd], loops);
 
147
}
 
148
 
 
149
void Audio::playMusic()
 
150
{
 
151
        if (!engine->useAudio)
 
152
                return;
 
153
 
 
154
        Mix_PlayMusic(music, -1);
 
155
 
 
156
        Mix_VolumeMusic(musicVolume);
 
157
}
 
158
 
 
159
int Audio::playMenuSound(int sound)
 
160
{
 
161
        if ((!engine->useAudio) || (soundVolume == 0))
 
162
                return sound;
 
163
 
 
164
        if ((sound == 0) || (sound == 3))
 
165
                return sound;
 
166
 
 
167
        //if (sound == 1)
 
168
                //playSound(SND_HIGHLIGHT, CH_ANY);
 
169
 
 
170
        //if (sound == 2)
 
171
                //playSound(SND_SELECT, CH_ANY);
 
172
                
 
173
        return sound;
 
174
}
 
175
 
 
176
void Audio::pause()
 
177
{
 
178
        if (!engine->useAudio)
 
179
                return;
 
180
 
 
181
        for (int i = 0 ; i < 8 ; i++)
 
182
                Mix_Pause(i);
 
183
 
 
184
        Mix_PauseMusic();
 
185
}
 
186
 
 
187
void Audio::resume()
 
188
{
 
189
        if (!engine->useAudio)
 
190
                return;
 
191
 
 
192
        for (int i = 0 ; i < 8 ; i++)
 
193
                Mix_Resume(i);
 
194
 
 
195
        Mix_ResumeMusic();
 
196
}
 
197
 
 
198
void Audio::stopMusic()
 
199
{
 
200
        if (!engine->useAudio)
 
201
                return;
 
202
 
 
203
        Mix_HaltMusic();
 
204
}
 
205
 
 
206
void Audio::fadeMusic()
 
207
{
 
208
        if (!engine->useAudio)
 
209
                return;
 
210
 
 
211
        Mix_FadeOutMusic(3500);
 
212
}
 
213
 
 
214
void Audio::free()
 
215
{
 
216
        debug(("Audio::free()\n"));
 
217
 
 
218
        for (int i = 0 ; i < MAX_SOUNDS - 3 ; i++)
 
219
        {
 
220
                if (sound[i] != NULL)
 
221
                {
 
222
                        Mix_FreeChunk(sound[i]);
 
223
                        sound[i] = NULL;
 
224
                }
 
225
        }
 
226
 
 
227
        if (music != NULL)
 
228
        {
 
229
                Mix_HaltMusic();
 
230
                SDL_Delay(5);
 
231
                Mix_FreeMusic(music);
 
232
        }
 
233
 
 
234
        music = NULL;
 
235
}
 
236
 
 
237
void Audio::destroy()
 
238
{
 
239
        debug(("Audio::destroy()\n"));
 
240
 
 
241
        free();
 
242
 
 
243
        for (int i = MAX_SOUNDS - 3 ; i < MAX_SOUNDS ; i++)
 
244
        {
 
245
                if (sound[i] != NULL)
 
246
                {
 
247
                        Mix_FreeChunk(sound[i]);
 
248
                        sound[i] = NULL;
 
249
                }
 
250
        }
 
251
}