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

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

sdlport: fix a crash in the SFX directory detection.

File size: 5.2 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au>
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]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 */
[2]20
[555]21#if defined HAVE_CONFIG_H
22#   include "config.h"
23#endif
[56]24
[551]25#include <cstring>
[131]26
[2]27#include <SDL.h>
[479]28#include <SDL/SDL_mixer.h>
[2]29
[481]30#include "sound.h"
[551]31#include "hmi.h"
[481]32#include "specs.h"
[2]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{
[124]45    char *sfxdir, *datadir;
[2]46
[124]47    // Disable sound if requested.
48    if( flags.nosound )
49    {
50        // User requested that sound be disabled
[536]51        printf( "Sound: Disabled (-nosound)\n" );
[124]52        return 0;
53    }
[2]54
[124]55    // Check for the sfx directory, disable sound if we can't find it.
56    datadir = get_filename_prefix();
[634]57    sfxdir = (char *)malloc(strlen(datadir) + 17 + 1);
[627]58    sprintf(sfxdir, "%s/sfx/ambcave1.wav", datadir);
59    FILE *f = fopen(sfxdir, "r");
60    if (!f)
[124]61    {
62        // Didn't find the directory, so disable sound.
[536]63        printf( "Sound: Disabled (couldn't find the sfx directory)\n" );
[124]64        return 0;
65    }
[627]66    fclose(f);
67    free(sfxdir);
[2]68
[617]69    if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 128) < 0)
[479]70    {
[536]71        printf( "Sound: Unable to open audio - %s\nSound: Disabled (error)\n", SDL_GetError() );
[479]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;
[536]82
83    printf( "Sound: Enabled\n" );
[2]84
[124]85    // It's all good
86    return sound_enabled;
[2]87}
88
89//
90// sound_uninit
91//
92// Shutdown audio and release any memory left over.
93//
94void sound_uninit()
95{
[552]96    if (!sound_enabled)
97        return;
98
99    Mix_CloseAudio();
[2]100}
101
102//
103// sound_effect constructor
104//
105// Read in the requested .wav file.
106//
[552]107sound_effect::sound_effect(char const *filename)
[2]108{
[552]109    if (!sound_enabled)
110        return;
[479]111
[552]112    jFILE fp(filename, "rb");
113    if (fp.open_failure())
114        return;
[479]115
[552]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);
[2]121}
122
123//
124// sound_effect destructor
125//
126// Release the audio data.
127//
128sound_effect::~sound_effect()
129{
[552]130    if(!sound_enabled)
131        return;
[479]132
[552]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);
[2]143}
144
145//
146// sound_effect::play
147//
[551]148// Add a new sample for playing.
[2]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//
[552]154void sound_effect::play(int volume, int pitch, int panpot)
[2]155{
[552]156    if (!sound_enabled)
157        return;
158
159    int channel = Mix_PlayChannel(-1, m_chunk, 0);
160    if (channel > -1)
[124]161    {
[552]162        Mix_Volume(channel, volume);
163        Mix_SetPanning(channel, panpot, 255 - panpot);
[124]164    }
[2]165}
166
167
[479]168// Play music using SDL_Mixer
[2]169
[536]170song::song(char const * filename)
[2]171{
[124]172    data = NULL;
[131]173    Name = strdup(filename);
[124]174    song_id = 0;
[494]175
[479]176    rw = NULL;
177    music = NULL;
178
179    char realname[255];
180    strcpy(realname, get_filename_prefix());
181    strcat(realname, filename);
182
[616]183    uint32_t data_size;
[479]184    data = load_hmi(realname, data_size);
185
[536]186    if (!data)
[479]187    {
[551]188        printf("Sound: ERROR - could not load %s\n", realname);
[536]189        return;
[479]190    }
[536]191
192    rw = SDL_RWFromMem(data, data_size);
193    music = Mix_LoadMUS_RW(rw);
194
195    if (!music)
196    {
[551]197        printf("Sound: ERROR - %s while loading %s\n",
[536]198               Mix_GetError(), realname);
199        return;
200    }
[2]201}
202
203song::~song()
204{
[536]205    if(playing())
[124]206        stop();
[536]207    free(data);
208    free(Name);
[479]209
[536]210    Mix_FreeMusic(music);
211    SDL_FreeRW(rw);
[2]212}
213
214void song::play( unsigned char volume )
215{
[124]216    song_id = 1;
[479]217
218    Mix_PlayMusic(this->music, 0);
219    Mix_VolumeMusic(volume);
[2]220}
221
222void song::stop( long fadeout_time )
223{
[124]224    song_id = 0;
[479]225
226    Mix_FadeOutMusic(100);
[2]227}
228
229int song::playing()
230{
[479]231    return Mix_PlayingMusic();
[2]232}
233
234void song::set_volume( int volume )
235{
[479]236    Mix_VolumeMusic(volume);
[2]237}
[551]238
Note: See TracBrowser for help on using the repository browser.