source: abuse/branches/lol/src/sdlport/sound.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 5.4 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au>
4 *  Copyright (c) 2005-2013 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software Foundation,
18 *  Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 */
20
21#if HAVE_CONFIG_H
22#   include "config.h"
23#endif
24
25#include <cstring>
26
27#include "common.h"
28
29#include "imlib/specs.h"
30
31#include "sound.h"
32#include "hmi.h"
33#include "setup.h"
34
35extern flags_struct flags;
36static int sound_enabled = 0;
37static SDL_AudioSpec audioObtained;
38
39//
40// sound_init()
41// Initialise audio
42//
43int sound_init( int argc, char **argv )
44{
45    char *sfxdir, *datadir;
46
47    // Disable sound if requested.
48    if( flags.nosound )
49    {
50        // User requested that sound be disabled
51        printf( "Sound: Disabled (-nosound)\n" );
52        return 0;
53    }
54
55    // Check for the sfx directory, disable sound if we can't find it.
56    datadir = get_filename_prefix();
57    sfxdir = (char *)malloc(strlen(datadir) + 17 + 1);
58    sprintf(sfxdir, "%s/sfx/ambcave1.wav", datadir);
59    FILE *f = fopen(sfxdir, "r");
60    if (!f)
61    {
62        // Didn't find the directory, so disable sound.
63        printf( "Sound: Disabled (couldn't find the sfx directory)\n" );
64        return 0;
65    }
66    fclose(f);
67    free(sfxdir);
68
69    if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 128) < 0)
70    {
71        printf( "Sound: Unable to open audio - %s\nSound: Disabled (error)\n", SDL_GetError() );
72        return 0;
73    }
74
75    Mix_AllocateChannels(50);
76
77    int tempChannels = 0;
78    Mix_QuerySpec(&audioObtained.freq, &audioObtained.format, &tempChannels);
79    audioObtained.channels = tempChannels & 0xFF;
80
81    sound_enabled = SFX_INITIALIZED | MUSIC_INITIALIZED;
82
83    printf( "Sound: Enabled\n" );
84
85    // It's all good
86    return sound_enabled;
87}
88
89//
90// sound_uninit
91//
92// Shutdown audio and release any memory left over.
93//
94void sound_uninit()
95{
96    if (!sound_enabled)
97        return;
98
99    Mix_CloseAudio();
100}
101
102//
103// sound_effect constructor
104//
105// Read in the requested .wav file.
106//
107sound_effect::sound_effect(char const *filename)
108{
109    if (!sound_enabled)
110        return;
111
112#if defined USE_SDL_MIXER
113    jFILE fp(filename, "rb");
114    if (fp.open_failure())
115        return;
116
117    void *temp_data = malloc(fp.file_size());
118    fp.read(temp_data, fp.file_size());
119    SDL_RWops *rw = SDL_RWFromMem(temp_data, fp.file_size());
120    m_chunk = Mix_LoadWAV_RW(rw, 1);
121    free(temp_data);
122#endif
123}
124
125//
126// sound_effect destructor
127//
128// Release the audio data.
129//
130sound_effect::~sound_effect()
131{
132    if(!sound_enabled)
133        return;
134
135    // Sound effect deletion only happens on level load, so there
136    // is no problem in stopping everything. But the original playing
137    // code handles the sound effects and the "playlist" differently.
138    // Therefore with SDL_mixer, a sound that has not finished playing
139    // on a level load will cut off in the middle. This is most noticable
140    // for the button sound of the load savegame dialog.
141#if defined USE_SDL_MIXER
142    Mix_FadeOutGroup(-1, 100);
143    while (Mix_Playing(-1))
144        SDL_Delay(10);
145    Mix_FreeChunk(m_chunk);
146#endif
147}
148
149//
150// sound_effect::play
151//
152// Add a new sample for playing.
153// panpot defines the pan position for the sound effect.
154//   0   - Completely to the right.
155//   128 - Centered.
156//   255 - Completely to the left.
157//
158void sound_effect::play(int volume, int pitch, int panpot)
159{
160    if (!sound_enabled)
161        return;
162
163#if defined USE_SDL_MIXER
164    int channel = Mix_PlayChannel(-1, m_chunk, 0);
165    if (channel > -1)
166    {
167        Mix_Volume(channel, volume);
168        Mix_SetPanning(channel, panpot, 255 - panpot);
169    }
170#endif
171}
172
173
174// Play music using SDL_Mixer
175
176song::song(String const &filename)
177{
178    data = NULL;
179    m_name = filename;
180    song_id = 0;
181
182    rw = NULL;
183    music = NULL;
184
185    String realname = String::Printf("%s%s", get_filename_prefix(), m_name.C());
186
187    uint32_t data_size;
188    data = load_hmi(realname.C(), data_size);
189
190    if (!data)
191    {
192        printf("Sound: ERROR - could not load %s\n", realname.C());
193        return;
194    }
195
196    rw = SDL_RWFromMem(data, data_size);
197#if SDL_VERSION_ATLEAST(2,0,0)
198    music = Mix_LoadMUS_RW(rw, SDL_FALSE);
199#else
200    music = Mix_LoadMUS_RW(rw);
201#endif
202
203    if (!music)
204    {
205        printf("Sound: ERROR - %s while loading %s\n",
206               Mix_GetError(), realname.C());
207        return;
208    }
209}
210
211song::~song()
212{
213    if(playing())
214        stop();
215    free(data);
216
217    Mix_FreeMusic(music);
218    SDL_FreeRW(rw);
219}
220
221void song::play( unsigned char volume )
222{
223    song_id = 1;
224
225    Mix_PlayMusic(this->music, 0);
226    Mix_VolumeMusic(volume);
227}
228
229void song::stop( long fadeout_time )
230{
231    song_id = 0;
232
233    Mix_FadeOutMusic(100);
234}
235
236int song::playing()
237{
238    return Mix_PlayingMusic();
239}
240
241void song::set_volume( int volume )
242{
243    Mix_VolumeMusic(volume);
244}
245
Note: See TracBrowser for help on using the repository browser.