diff --git a/include/SFML/Audio/Sound.hpp b/include/SFML/Audio/Sound.hpp index 691d6338c..366d7acfe 100644 --- a/include/SFML/Audio/Sound.hpp +++ b/include/SFML/Audio/Sound.hpp @@ -288,6 +288,15 @@ public : //////////////////////////////////////////////////////////// Sound& operator =(const Sound& Other); + //////////////////////////////////////////////////////////// + /// Reset the internal buffer + /// + /// This function is for internal use only, you don't have + /// to use it. + /// + //////////////////////////////////////////////////////////// + void ResetBuffer(); + private : friend class SoundStream; diff --git a/include/SFML/Audio/SoundBuffer.hpp b/include/SFML/Audio/SoundBuffer.hpp index af459f41c..023e5aa05 100644 --- a/include/SFML/Audio/SoundBuffer.hpp +++ b/include/SFML/Audio/SoundBuffer.hpp @@ -32,10 +32,13 @@ #include #include #include +#include namespace sf { +class Sound; + //////////////////////////////////////////////////////////// /// SoundBuffer is the low-level for loading and manipulating /// sound buffers @@ -174,12 +177,34 @@ private : //////////////////////////////////////////////////////////// bool Update(unsigned int ChannelsCount, unsigned int SampleRate); + //////////////////////////////////////////////////////////// + /// Add a sound to the list of sounds that use this buffer + /// + /// \param Instance : Sound object to attach + /// + //////////////////////////////////////////////////////////// + void AttachSound(Sound* Instance) const; + + //////////////////////////////////////////////////////////// + /// Remove a sound from the list of sounds that use this buffer + /// + /// \param Instance : Sound object to detach + /// + //////////////////////////////////////////////////////////// + void DetachSound(Sound* Instance) const; + + //////////////////////////////////////////////////////////// + // Types + //////////////////////////////////////////////////////////// + typedef std::set SoundList; ///< Set of unique sound instances + //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// unsigned int myBuffer; ///< OpenAL buffer identifier std::vector mySamples; ///< Samples buffer float myDuration; ///< Sound duration, in seconds + mutable SoundList mySounds; ///< List of sounds that are using this buffer }; } // namespace sf diff --git a/src/SFML/Audio/Sound.cpp b/src/SFML/Audio/Sound.cpp index cd953281d..29cac745c 100644 --- a/src/SFML/Audio/Sound.cpp +++ b/src/SFML/Audio/Sound.cpp @@ -90,6 +90,7 @@ Sound::~Sound() { Stop(); ALCheck(alSourcei(mySource, AL_BUFFER, 0)); + myBuffer->DetachSound(this); } ALCheck(alDeleteSources(1, &mySource)); } @@ -129,7 +130,8 @@ void Sound::Stop() void Sound::SetBuffer(const SoundBuffer& Buffer) { myBuffer = &Buffer; - ALCheck(alSourcei(mySource, AL_BUFFER, myBuffer ? myBuffer->myBuffer : 0)); + myBuffer->AttachSound(this); + ALCheck(alSourcei(mySource, AL_BUFFER, myBuffer->myBuffer)); } @@ -360,4 +362,18 @@ Sound& Sound::operator =(const Sound& Other) return *this; } + +//////////////////////////////////////////////////////////// +/// Reset the internal buffer +//////////////////////////////////////////////////////////// +void Sound::ResetBuffer() +{ + // First stop the sound in case it is playing + Stop(); + + // Detach the buffer + ALCheck(alSourcei(mySource, AL_BUFFER, 0)); + myBuffer = 0; +} + } // namespace sf diff --git a/src/SFML/Audio/SoundBuffer.cpp b/src/SFML/Audio/SoundBuffer.cpp index 7636a01db..e726b13ad 100644 --- a/src/SFML/Audio/SoundBuffer.cpp +++ b/src/SFML/Audio/SoundBuffer.cpp @@ -27,6 +27,7 @@ //////////////////////////////////////////////////////////// #include #include +#include #include #include #include @@ -55,7 +56,8 @@ AudioResource (Copy), Resource(Copy), myBuffer (0), mySamples (Copy.mySamples), -myDuration (Copy.myDuration) +myDuration (Copy.myDuration), +mySounds () // don't copy the attached sounds { // Create the buffer ALCheck(alGenBuffers(1, &myBuffer)); @@ -70,6 +72,11 @@ myDuration (Copy.myDuration) //////////////////////////////////////////////////////////// SoundBuffer::~SoundBuffer() { + // First detach the buffer from the sounds that use it (to avoid OpenAL errors) + for (SoundList::const_iterator it = mySounds.begin(); it != mySounds.end(); ++it) + (*it)->ResetBuffer(); + + // Destroy the buffer if (myBuffer) ALCheck(alDeleteBuffers(1, &myBuffer)); } @@ -268,9 +275,10 @@ SoundBuffer& SoundBuffer::operator =(const SoundBuffer& Other) { SoundBuffer Temp(Other); - mySamples.swap(Temp.mySamples); + std::swap(mySamples, Temp.mySamples); std::swap(myBuffer, Temp.myBuffer); std::swap(myDuration, Temp.myDuration); + std::swap(mySounds, Temp.mySounds); // swap sounds too, so that they are detached when Temp is destroyed return *this; } @@ -305,4 +313,22 @@ bool SoundBuffer::Update(unsigned int ChannelsCount, unsigned int SampleRate) return true; } + +//////////////////////////////////////////////////////////// +/// Add a sound to the list of sounds that use this buffer +//////////////////////////////////////////////////////////// +void SoundBuffer::AttachSound(Sound* Instance) const +{ + mySounds.insert(Instance); +} + + +//////////////////////////////////////////////////////////// +/// Remove a sound from the list of sounds that use this buffer +//////////////////////////////////////////////////////////// +void SoundBuffer::DetachSound(Sound* Instance) const +{ + mySounds.erase(Instance); +} + } // namespace sf