Added init check
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#define NUM_SOUNDS 10
|
#define NUM_SOUNDS 10
|
||||||
static SDL_AudioSpec fmt;
|
static SDL_AudioSpec fmt;
|
||||||
|
static bool initiated = false;
|
||||||
|
|
||||||
AudioPlayer::AudioPlayer(void)
|
AudioPlayer::AudioPlayer(void)
|
||||||
{
|
{
|
||||||
@@ -19,6 +20,7 @@ AudioPlayer::~AudioPlayer(void)
|
|||||||
|
|
||||||
void AudioPlayer::init()
|
void AudioPlayer::init()
|
||||||
{
|
{
|
||||||
|
initiated = true;
|
||||||
extern void mixaudio(void *unused, Uint8 *stream, int len);
|
extern void mixaudio(void *unused, Uint8 *stream, int len);
|
||||||
fmt.freq = 22050;
|
fmt.freq = 22050;
|
||||||
fmt.format = AUDIO_S16;
|
fmt.format = AUDIO_S16;
|
||||||
@@ -59,46 +61,53 @@ void mixaudio(void *unused, Uint8 *stream, int len)
|
|||||||
void AudioPlayer::PlaySound(std::string fileString)
|
void AudioPlayer::PlaySound(std::string fileString)
|
||||||
{
|
{
|
||||||
|
|
||||||
char *file = new char[fileString.length() + 1];
|
if(initiated)
|
||||||
strcpy(file, fileString.c_str());
|
{
|
||||||
|
char *file = new char[fileString.length() + 1];
|
||||||
|
strcpy(file, fileString.c_str());
|
||||||
|
|
||||||
|
|
||||||
int index;
|
int index;
|
||||||
SDL_AudioSpec wave;
|
SDL_AudioSpec wave;
|
||||||
Uint8 *data;
|
Uint8 *data;
|
||||||
Uint32 dlen;
|
Uint32 dlen;
|
||||||
SDL_AudioCVT cvt;
|
SDL_AudioCVT cvt;
|
||||||
|
|
||||||
/* Look for an empty (or finished) sound slot */
|
/* Look for an empty (or finished) sound slot */
|
||||||
for ( index=0; index<NUM_SOUNDS; ++index ) {
|
for ( index=0; index<NUM_SOUNDS; ++index ) {
|
||||||
if ( sounds[index].dpos == sounds[index].dlen ) {
|
if ( sounds[index].dpos == sounds[index].dlen ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( index == NUM_SOUNDS )
|
if ( index == NUM_SOUNDS )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Load the sound file and convert it to 16-bit stereo at 22kHz */
|
/* Load the sound file and convert it to 16-bit stereo at 22kHz */
|
||||||
if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) {
|
if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) {
|
||||||
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
|
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq,
|
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq,
|
||||||
AUDIO_S16, 2, fmt.freq);
|
AUDIO_S16, 2, fmt.freq);
|
||||||
cvt.buf = (Uint8*)malloc(dlen*cvt.len_mult);
|
cvt.buf = (Uint8*)malloc(dlen*cvt.len_mult);
|
||||||
memcpy(cvt.buf, data, dlen);
|
memcpy(cvt.buf, data, dlen);
|
||||||
cvt.len = dlen;
|
cvt.len = dlen;
|
||||||
SDL_ConvertAudio(&cvt);
|
SDL_ConvertAudio(&cvt);
|
||||||
SDL_FreeWAV(data);
|
SDL_FreeWAV(data);
|
||||||
|
|
||||||
/* Put the sound data in the slot (it starts playing immediately) */
|
/* Put the sound data in the slot (it starts playing immediately) */
|
||||||
if ( sounds[index].data ) {
|
if ( sounds[index].data ) {
|
||||||
free(sounds[index].data);
|
free(sounds[index].data);
|
||||||
}
|
}
|
||||||
SDL_LockAudio();
|
SDL_LockAudio();
|
||||||
sounds[index].data = cvt.buf;
|
sounds[index].data = cvt.buf;
|
||||||
sounds[index].dlen = cvt.len_cvt;
|
sounds[index].dlen = cvt.len_cvt;
|
||||||
sounds[index].dpos = 0;
|
sounds[index].dpos = 0;
|
||||||
SDL_UnlockAudio();
|
SDL_UnlockAudio();
|
||||||
delete [] file;
|
delete [] file;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
OutputDebugString("Audio player not initialized, sound will not play\r\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user