source: abuse/trunk/src/sdl2port/sound.cpp @ 731

Last change on this file since 731 was 731, checked in by jjsimpso, 8 years ago

sdlport: Add support for SDL2, inculding the game controller API. Configure
script will check for SDL2 and use it if present, otherwise it will fall
back to SDL v1. Some old SDL v1 features not implemented yet on SDL2, such
as saving screenshots.

core: Small change for SDL2 game controller API to disable the game
controller in the save/load game menu.

Summary of game controller API changes:

  • Enable with -gamepad
  • Tested with PS3 dualshock 3.
  • D-pad moves, right analog controls aiming
  • X: change weapon, []: jump, R1: fire, L1: special ability, /\:use/activate
  • All menu navigation requires the mouse, which is disabled during normal game play.
File size: 5.3 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 <SDL2/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    FILE *fd = NULL;
47
48    // Disable sound if requested.
49    if( flags.nosound )
50    {
51        // User requested that sound be disabled
52        printf( "Sound: Disabled (-nosound)\n" );
53        return 0;
54    }
55
56    // Check for the sfx directory, disable sound if we can't find it.
57    datadir = get_filename_prefix();
58    sfxdir = (char *)malloc( strlen( datadir ) + 5 + 1 );
59    sprintf( sfxdir, "%s/sfx/", datadir );
60    if( (fd = fopen( sfxdir,"r" )) == NULL )
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    free( sfxdir );
67
68    if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 128) < 0)
69    {
70        printf( "Sound: Unable to open audio - %s\nSound: Disabled (error)\n", SDL_GetError() );
71        return 0;
72    }
73
74    Mix_AllocateChannels(50);
75
76    int tempChannels = 0;
77    Mix_QuerySpec(&audioObtained.freq, &audioObtained.format, &tempChannels);
78    audioObtained.channels = tempChannels & 0xFF;
79
80    sound_enabled = SFX_INITIALIZED | MUSIC_INITIALIZED;
81
82    printf( "Sound: Enabled\n" );
83
84    // It's all good
85    return sound_enabled;
86}
87
88//
89// sound_uninit
90//
91// Shutdown audio and release any memory left over.
92//
93void sound_uninit()
94{
95    if (!sound_enabled)
96        return;
97
98    Mix_CloseAudio();
99}
100
101//
102// sound_effect constructor
103//
104// Read in the requested .wav file.
105//
106sound_effect::sound_effect(char const *filename)
107{
108    if (!sound_enabled)
109        return;
110
111    jFILE fp(filename, "rb");
112    if (fp.open_failure())
113        return;
114
115    void *temp_data = malloc(fp.file_size());
116    fp.read(temp_data, fp.file_size());
117    SDL_RWops *rw = SDL_RWFromMem(temp_data, fp.file_size());
118    m_chunk = Mix_LoadWAV_RW(rw, 1);
119    free(temp_data);
120}
121
122//
123// sound_effect destructor
124//
125// Release the audio data.
126//
127sound_effect::~sound_effect()
128{
129    if(!sound_enabled)
130        return;
131
132    // Sound effect deletion only happens on level load, so there
133    // is no problem in stopping everything. But the original playing
134    // code handles the sound effects and the "playlist" differently.
135    // Therefore with SDL_mixer, a sound that has not finished playing
136    // on a level load will cut off in the middle. This is most noticable
137    // for the button sound of the load savegame dialog.
138    Mix_FadeOutGroup(-1, 100);
139    while (Mix_Playing(-1))
140        SDL_Delay(10);
141    Mix_FreeChunk(m_chunk);
142}
143
144//
145// sound_effect::play
146//
147// Add a new sample for playing.
148// panpot defines the pan position for the sound effect.
149//   0   - Completely to the right.
150//   128 - Centered.
151//   255 - Completely to the left.
152//
153void sound_effect::play(int volume, int pitch, int panpot)
154{
155    if (!sound_enabled)
156        return;
157
158    int channel = Mix_PlayChannel(-1, m_chunk, 0);
159    if (channel > -1)
160    {
161        Mix_Volume(channel, volume);
162        Mix_SetPanning(channel, panpot, 255 - panpot);
163    }
164}
165
166
167// Play music using SDL_Mixer
168
169song::song(char const * filename)
170{
171    data = NULL;
172    Name = strdup(filename);
173    song_id = 0;
174
175    rw = NULL;
176    music = NULL;
177
178    char realname[255];
179    strcpy(realname, get_filename_prefix());
180    strcat(realname, filename);
181
182    uint32_t data_size;
183    data = load_hmi(realname, data_size);
184
185    if (!data)
186    {
187        printf("Sound: ERROR - could not load %s\n", realname);
188        return;
189    }
190
191    rw = SDL_RWFromMem(data, data_size);
192    if(!rw)
193    {
194        printf("Sound: ERROR - could not create RWops\n", realname);
195        return;
196    }
197
198    music = Mix_LoadMUS_RW(rw, 0);
199
200    if (!music)
201    {
202        printf("Sound: ERROR - %s while loading %s\n",
203               Mix_GetError(), realname);
204        return;
205    }
206}
207
208song::~song()
209{
210    if(playing())
211        stop();
212    free(data);
213    free(Name);
214
215    Mix_FreeMusic(music);
216    SDL_FreeRW(rw);
217}
218
219void song::play( unsigned char volume )
220{
221    song_id = 1;
222
223    Mix_PlayMusic(this->music, 0);
224    Mix_VolumeMusic(volume);
225}
226
227void song::stop( long fadeout_time )
228{
229    song_id = 0;
230
231    Mix_FadeOutMusic(100);
232}
233
234int song::playing()
235{
236    return Mix_PlayingMusic();
237}
238
239void song::set_volume( int volume )
240{
241    Mix_VolumeMusic(volume);
242}
243
Note: See TracBrowser for help on using the repository browser.