diff --git a/rwengine/src/audio/SoundBuffer.cpp b/rwengine/src/audio/SoundBuffer.cpp index ba6f831b..2961d4d6 100644 --- a/rwengine/src/audio/SoundBuffer.cpp +++ b/rwengine/src/audio/SoundBuffer.cpp @@ -15,6 +15,11 @@ SoundBuffer::SoundBuffer() { alCheck(alSourcei(source, AL_LOOPING, AL_FALSE)); } +SoundBuffer::~SoundBuffer() { + alCheck(alDeleteSources(1, &source)); + alCheck(alDeleteBuffers(1, &buffer)); +} + bool SoundBuffer::bufferData(SoundSource& soundSource) { alCheck(alBufferData( buffer, diff --git a/rwengine/src/audio/SoundBuffer.hpp b/rwengine/src/audio/SoundBuffer.hpp index a1660785..0b06a385 100644 --- a/rwengine/src/audio/SoundBuffer.hpp +++ b/rwengine/src/audio/SoundBuffer.hpp @@ -11,6 +11,7 @@ /// sound instance. struct SoundBuffer { SoundBuffer(); + ~SoundBuffer(); bool bufferData(SoundSource& soundSource); bool isPlaying() const; diff --git a/rwengine/src/audio/SoundManager.cpp b/rwengine/src/audio/SoundManager.cpp index 229b33f1..d0966093 100644 --- a/rwengine/src/audio/SoundManager.cpp +++ b/rwengine/src/audio/SoundManager.cpp @@ -43,13 +43,7 @@ SoundManager::SoundManager(GameWorld* engine) : _engine(engine) { } SoundManager::~SoundManager() { - // De-initialize OpenAL - if (alContext) { - alcMakeContextCurrent(nullptr); - alcDestroyContext(alContext); - } - - if (alDevice) alcCloseDevice(alDevice); + deinitializeOpenAL(); } bool SoundManager::initializeOpenAL() { @@ -76,7 +70,7 @@ bool SoundManager::initializeOpenAL() { return true; } -bool SoundManager::initializeAVCodec() { +void SoundManager::initializeAVCodec() { #if defined(RW_DEBUG) && defined(RW_VERBOSE_DEBUG_MESSAGES) av_log_set_level(AV_LOG_WARNING); #else @@ -88,8 +82,24 @@ bool SoundManager::initializeAVCodec() { #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100) av_register_all(); #endif +} - return true; +void SoundManager::deinitializeOpenAL() { + // Buffers have to been removed before openAL is deinitialized + sounds.clear(); + buffers.clear(); + + // De-initialize OpenAL + if (alContext) { + alcMakeContextCurrent(nullptr); + alcDestroyContext(alContext); + } + alContext = nullptr; + + if (alDevice) { + alcCloseDevice(alDevice); + } + alDevice = nullptr; } bool SoundManager::loadSound(const std::string& name, diff --git a/rwengine/src/audio/SoundManager.hpp b/rwengine/src/audio/SoundManager.hpp index f76f443e..867110ef 100644 --- a/rwengine/src/audio/SoundManager.hpp +++ b/rwengine/src/audio/SoundManager.hpp @@ -87,7 +87,9 @@ public: private: bool initializeOpenAL(); - bool initializeAVCodec(); + void initializeAVCodec(); + + void deinitializeOpenAL(); ALCcontext* alContext = nullptr; ALCdevice* alDevice = nullptr;