Added init check

This commit is contained in:
andreja6
2018-04-27 00:04:04 -07:00
parent 6d49e266dd
commit e9c7607a4b

View File

@@ -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,6 +61,8 @@ void mixaudio(void *unused, Uint8 *stream, int len)
void AudioPlayer::PlaySound(std::string fileString) void AudioPlayer::PlaySound(std::string fileString)
{ {
if(initiated)
{
char *file = new char[fileString.length() + 1]; char *file = new char[fileString.length() + 1];
strcpy(file, fileString.c_str()); strcpy(file, fileString.c_str());
@@ -101,4 +105,9 @@ void AudioPlayer::PlaySound(std::string fileString)
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");
}
} }