source: abuse/trunk/src/sdlport/sound.cpp @ 633

Last change on this file since 633 was 633, checked in by Sam Hocevar, 11 years ago

sdlport: fix a compilation error.

File size: 5.2 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-2011 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 defined HAVE_CONFIG_H
22#   include "config.h"
23#endif
24
25#include <cstring>
26
27#include <SDL.h>
28#include <SDL/SDL_mixer.h>
29
30#include "sound.h"
31#include "hmi.h"
32#include "specs.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) + 5 + 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    jFILE fp(filename, "rb");
113    if (fp.open_failure())
114        return;
115
116    void *temp_data = malloc(fp.file_size());
117    fp.read(temp_data, fp.file_size());
118    SDL_RWops *rw = SDL_RWFromMem(temp_data, fp.file_size());
119    m_chunk = Mix_LoadWAV_RW(rw, 1);
120    free(temp_data);
121}
122
123//
124// sound_effect destructor
125//
126// Release the audio data.
127//
128sound_effect::~sound_effect()
129{
130    if(!sound_enabled)
131        return;
132
133    // Sound effect deletion only happens on level load, so there
134    // is no problem in stopping everything. But the original playing
135    // code handles the sound effects and the "playlist" differently.
136    // Therefore with SDL_mixer, a sound that has not finished playing
137    // on a level load will cut off in the middle. This is most noticable
138    // for the button sound of the load savegame dialog.
139    Mix_FadeOutGroup(-1, 100);
140    while (Mix_Playing(-1))
141        SDL_Delay(10);
142    Mix_FreeChunk(m_chunk);
143}
144
145//
146// sound_effect::play
147//
148// Add a new sample for playing.
149// panpot defines the pan position for the sound effect.
150//   0   - Completely to the right.
151//   128 - Centered.
152//   255 - Completely to the left.
153//
154void sound_effect::play(int volume, int pitch, int panpot)
155{
156    if (!sound_enabled)
157        return;
158
159    int channel = Mix_PlayChannel(-1, m_chunk, 0);
160    if (channel > -1)
161    {
162        Mix_Volume(channel, volume);
163        Mix_SetPanning(channel, panpot, 255 - panpot);
164    }
165}
166
167
168// Play music using SDL_Mixer
169
170song::song(char const * filename)
171{
172    data = NULL;
173    Name = strdup(filename);
174    song_id = 0;
175
176    rw = NULL;
177    music = NULL;
178
179    char realname[255];
180    strcpy(realname, get_filename_prefix());
181    strcat(realname, filename);
182
183    uint32_t data_size;
184    data = load_hmi(realname, data_size);
185
186    if (!data)
187    {
188        printf("Sound: ERROR - could not load %s\n", realname);
189        return;
190    }
191
192    rw = SDL_RWFromMem(data, data_size);
193    music = Mix_LoadMUS_RW(rw);
194
195    if (!music)
196    {
197        printf("Sound: ERROR - %s while loading %s\n",
198               Mix_GetError(), realname);
199        return;
200    }
201}
202
203song::~song()
204{
205    if(playing())
206        stop();
207    free(data);
208    free(Name);
209
210    Mix_FreeMusic(music);
211    SDL_FreeRW(rw);
212}
213
214void song::play( unsigned char volume )
215{
216    song_id = 1;
217
218    Mix_PlayMusic(this->music, 0);
219    Mix_VolumeMusic(volume);
220}
221
222void song::stop( long fadeout_time )
223{
224    song_id = 0;
225
226    Mix_FadeOutMusic(100);
227}
228
229int song::playing()
230{
231    return Mix_PlayingMusic();
232}
233
234void song::set_volume( int volume )
235{
236    Mix_VolumeMusic(volume);
237}
238
Note: See TracBrowser for help on using the repository browser.