From 0375d7588fe6e11472963e17586b952bdcf8268d Mon Sep 17 00:00:00 2001 From: Joshua Adam Reisenauer Date: Thu, 1 May 2014 02:09:12 +0200 Subject: [PATCH] Fixed soundbuffer contents not being able to be updated when still attached to sounds (#354), sounds now detach from their buffer when it is reset. Signed-off-by: binary1248 --- src/SFML/Audio/Sound.cpp | 8 ++++++-- src/SFML/Audio/SoundBuffer.cpp | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/SFML/Audio/Sound.cpp b/src/SFML/Audio/Sound.cpp index 9aa75011..d7228ed1 100644 --- a/src/SFML/Audio/Sound.cpp +++ b/src/SFML/Audio/Sound.cpp @@ -189,8 +189,12 @@ void Sound::resetBuffer() stop(); // Detach the buffer - alCheck(alSourcei(m_source, AL_BUFFER, 0)); - m_buffer = NULL; + if (m_buffer) + { + alCheck(alSourcei(m_source, AL_BUFFER, 0)); + m_buffer->detachSound(this); + m_buffer = NULL; + } } } // namespace sf diff --git a/src/SFML/Audio/SoundBuffer.cpp b/src/SFML/Audio/SoundBuffer.cpp index 05e1c8f2..d17227cb 100644 --- a/src/SFML/Audio/SoundBuffer.cpp +++ b/src/SFML/Audio/SoundBuffer.cpp @@ -248,6 +248,13 @@ bool SoundBuffer::update(unsigned int channelCount, unsigned int sampleRate) return false; } + // First make a copy of the list of sounds so we can reattach later + SoundList sounds(m_sounds); + + // Detach the buffer from the sounds that use it (to avoid OpenAL errors) + for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) + (*it)->resetBuffer(); + // Fill the buffer ALsizei size = static_cast(m_samples.size()) * sizeof(Int16); alCheck(alBufferData(m_buffer, format, &m_samples[0], size, sampleRate)); @@ -255,6 +262,10 @@ bool SoundBuffer::update(unsigned int channelCount, unsigned int sampleRate) // Compute the duration m_duration = seconds(static_cast(m_samples.size()) / sampleRate / channelCount); + // Now reattach the buffer to the sounds that use it + for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it) + (*it)->setBuffer(*this); + return true; }