diff --git a/rwengine/include/audio/SoundManager.hpp b/rwengine/include/audio/SoundManager.hpp index f7392c0b..399f2244 100644 --- a/rwengine/include/audio/SoundManager.hpp +++ b/rwengine/include/audio/SoundManager.hpp @@ -14,6 +14,7 @@ class SoundManager { public: SoundManager(); + ~SoundManager(); bool loadSound(const std::string& name, const std::string& fileName); bool isLoaded(const std::string& name); @@ -63,8 +64,8 @@ private: bool initializeOpenAL(); - ALCcontext* alContext; - ALCdevice* alDevice; + ALCcontext* alContext = nullptr; + ALCdevice* alDevice = nullptr; std::map sounds; std::map musics; diff --git a/rwengine/src/audio/SoundManager.cpp b/rwengine/src/audio/SoundManager.cpp index 76ca52a2..7a0ae809 100644 --- a/rwengine/src/audio/SoundManager.cpp +++ b/rwengine/src/audio/SoundManager.cpp @@ -7,6 +7,45 @@ #include #include +SoundManager::SoundManager() +{ + initializeOpenAL(); +} + +SoundManager::~SoundManager() +{ + // De-initialize OpenAL + if(alContext) { + alcMakeContextCurrent(NULL); + alcDestroyContext(alContext); + } + + if(alDevice) + alcCloseDevice(alDevice); +} + +bool SoundManager::initializeOpenAL() +{ + alDevice = alcOpenDevice(NULL); + if ( ! alDevice) { + std::cerr << "Could not find OpenAL device!" << std::endl; + return false; + } + + alContext = alcCreateContext(alDevice, NULL); + if ( ! alContext) { + std::cerr << "Could not create OpenAL context!" << std::endl; + return false; + } + + if ( ! alcMakeContextCurrent(alContext)) { + std::cerr << "Unable to make OpenAL context current!" << std::endl; + return false; + } + + return true; +} + void SoundManager::SoundSource::loadFromFile(const std::string& filename) { fileInfo.format = 0; @@ -50,33 +89,6 @@ bool SoundManager::SoundBuffer::bufferData(SoundSource& soundSource) return true; } -SoundManager::SoundManager() -{ - initializeOpenAL(); -} - -bool SoundManager::initializeOpenAL() -{ - alDevice = alcOpenDevice(NULL); - if ( ! alDevice) { - std::cerr << "Could not find OpenAL device!" << std::endl; - return false; - } - - alContext = alcCreateContext(alDevice, NULL); - if ( ! alContext) { - std::cerr << "Could not create OpenAL context!" << std::endl; - return false; - } - - if ( ! alcMakeContextCurrent(alContext)) { - std::cerr << "Unable to make OpenAL context current!" << std::endl; - return false; - } - - return true; -} - bool SoundManager::loadSound(const std::string& name, const std::string& fileName) { Sound* sound = nullptr;