From 0d443cc5352e47f0e0267e2dd6b770aa01934291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timmy=20Sj=C3=B6stedt?= Date: Sun, 29 May 2016 23:32:18 +0200 Subject: [PATCH] Improved sound/music loading. No temporary objects --- rwengine/src/audio/SoundManager.cpp | 33 ++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/rwengine/src/audio/SoundManager.cpp b/rwengine/src/audio/SoundManager.cpp index 3f4fe348..d6781cb4 100644 --- a/rwengine/src/audio/SoundManager.cpp +++ b/rwengine/src/audio/SoundManager.cpp @@ -79,16 +79,20 @@ bool SoundManager::initializeOpenAL() bool SoundManager::loadSound(const std::string& name, const std::string& fileName) { - auto emplaced = sounds.emplace(name, Sound{}); - if ( ! emplaced.second) { - return false; + Sound* sound = nullptr; + auto sound_iter = sounds.find(name); + + if (sound_iter != sounds.end()) { + sound = &sound_iter->second; + } else { + auto emplaced = sounds.emplace(std::piecewise_construct, std::forward_as_tuple(name), std::forward_as_tuple()); + sound = &emplaced.first->second; + + sound->source.loadFromFile(fileName); + sound->isLoaded = sound->buffer.bufferData(sound->source); } - Sound& sound = emplaced.first->second; - sound.source.loadFromFile(fileName); - sound.isLoaded = sound.buffer.bufferData(sound.source); - - return sound.isLoaded; + return sound->isLoaded; } bool SoundManager::isLoaded(const std::string& name) { @@ -134,8 +138,17 @@ bool SoundManager::playBackground(const std::string& fileName) bool SoundManager::loadMusic(const std::string& name, const std::string& fileName) { - auto music = musics.emplace(std::piecewise_construct, std::forward_as_tuple(name), std::forward_as_tuple()); - return music.first->second.openFromFile(fileName); + MADStream* music = nullptr; + auto music_iter = musics.find(name); + + if (music_iter != musics.end()) { + music = &music_iter->second; + } else { + auto emplaced = musics.emplace(std::piecewise_construct, std::forward_as_tuple(name), std::forward_as_tuple()); + music = &emplaced.first->second; + } + + return music->openFromFile(fileName); } void SoundManager::playMusic(const std::string& name) {