diff --git a/src/sound.c b/src/sound.c index 2c598e0..5c43c97 100644 --- a/src/sound.c +++ b/src/sound.c @@ -60,6 +60,8 @@ static void sound_free(alSound* snd); static int voice_getSource(alVoice* voc); int sound_init(void) { + int ret = 0; + sound_lock = SDL_CreateMutex(); SDL_mutexP(sound_lock); @@ -69,14 +71,16 @@ int sound_init(void) { al_device = alcOpenDevice(NULL); if(al_device == NULL) { WARN("Unable to open default sound device"); - return -1; + ret = -1; + goto snderr_dev; } // Create the OpenAL context. al_context = alcCreateContext(al_device, NULL); if(al_context == NULL) { WARN("Unable to create OpenAL context"); - return -2; + ret = -2; + goto snderr_ctx; } // Clear the errors. @@ -85,7 +89,8 @@ int sound_init(void) { // Set active context. if(alcMakeContextCurrent(al_context)==AL_FALSE) { WARN("Failure to set default context"); - return -4; + ret = -4; + goto snderr_act; } // Set the master gain. @@ -104,6 +109,17 @@ int sound_init(void) { music_player = SDL_CreateThread(music_thread, NULL); return 0; + +snderr_act: + alcDestroyContext(al_context); +snderr_ctx: + al_context = NULL; + alcCloseDevice(al_device); +snderr_dev: + al_device = NULL; + SDL_mutexV(sound_lock); + SDL_DestroyMutex(sound_lock); + return ret; }