From 3247f441f890dc73fc14985bccab668d41527479 Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Tue, 26 Jan 2010 10:31:09 +0000 Subject: [PATCH] Made the assignment operator in sf::Sound work with the last modification git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1371 4e206d99-4929-0410-ac5d-dfc041789085 --- src/SFML/Audio/Sound.cpp | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/SFML/Audio/Sound.cpp b/src/SFML/Audio/Sound.cpp index 29cac745..84136db1 100644 --- a/src/SFML/Audio/Sound.cpp +++ b/src/SFML/Audio/Sound.cpp @@ -129,6 +129,14 @@ void Sound::Stop() //////////////////////////////////////////////////////////// void Sound::SetBuffer(const SoundBuffer& Buffer) { + // First detach from the previous buffer + if (myBuffer) + { + Stop(); + myBuffer->DetachSound(this); + } + + // Assign and use the new buffer myBuffer = &Buffer; myBuffer->AttachSound(this); ALCheck(alSourcei(mySource, AL_BUFFER, myBuffer->myBuffer)); @@ -354,10 +362,27 @@ Sound::Status Sound::GetStatus() const //////////////////////////////////////////////////////////// Sound& Sound::operator =(const Sound& Other) { - Sound Temp(Other); + // Here we don't use the copy-and-swap idiom, because it would mess up + // the list of sound instances contained in the buffers - std::swap(mySource, Temp.mySource); - std::swap(myBuffer, Temp.myBuffer); + // Detach the sound instance from the previous buffer (if any) + if (myBuffer) + { + Stop(); + myBuffer->DetachSound(this); + myBuffer = NULL; + } + + // Copy the sound attributes + if (Other.myBuffer) + SetBuffer(*Other.myBuffer); + SetLoop(Other.GetLoop()); + SetPitch(Other.GetPitch()); + SetVolume(Other.GetVolume()); + SetPosition(Other.GetPosition()); + SetRelativeToListener(Other.IsRelativeToListener()); + SetMinDistance(Other.GetMinDistance()); + SetAttenuation(Other.GetAttenuation()); return *this; } @@ -373,7 +398,7 @@ void Sound::ResetBuffer() // Detach the buffer ALCheck(alSourcei(mySource, AL_BUFFER, 0)); - myBuffer = 0; + myBuffer = NULL; } } // namespace sf