From 2d1fab374f3d7e9765fe1e83b23dfb5b7f80f012 Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Mon, 13 Apr 2015 10:03:24 +0200 Subject: [PATCH] Added SoundSource::operator= and called it from Sound::operator= Signed-off-by: Jan Haller --- include/SFML/Audio/SoundSource.hpp | 10 ++++++++++ src/SFML/Audio/Sound.cpp | 14 ++++++-------- src/SFML/Audio/SoundSource.cpp | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/include/SFML/Audio/SoundSource.hpp b/include/SFML/Audio/SoundSource.hpp index cde0a3bd3..02445775a 100644 --- a/include/SFML/Audio/SoundSource.hpp +++ b/include/SFML/Audio/SoundSource.hpp @@ -240,6 +240,16 @@ public: //////////////////////////////////////////////////////////// float getAttenuation() const; + //////////////////////////////////////////////////////////// + /// \brief Overload of assignment operator + /// + /// \param right Instance to assign + /// + /// \return Reference to self + /// + //////////////////////////////////////////////////////////// + SoundSource& operator =(const SoundSource& right); + protected: //////////////////////////////////////////////////////////// diff --git a/src/SFML/Audio/Sound.cpp b/src/SFML/Audio/Sound.cpp index 82c1cca00..52a1c63ae 100644 --- a/src/SFML/Audio/Sound.cpp +++ b/src/SFML/Audio/Sound.cpp @@ -157,7 +157,11 @@ Sound::Status Sound::getStatus() const Sound& Sound::operator =(const Sound& right) { // Here we don't use the copy-and-swap idiom, because it would mess up - // the list of sound instances contained in the buffers + // the list of sound instances contained in the buffers and unnecessarily + // destroy/create OpenAL sound sources + + // Delegate to base class, which copies all the sound attributes + SoundSource::operator=(right); // Detach the sound instance from the previous buffer (if any) if (m_buffer) @@ -167,16 +171,10 @@ Sound& Sound::operator =(const Sound& right) m_buffer = NULL; } - // Copy the sound attributes + // Copy the remaining sound attributes if (right.m_buffer) setBuffer(*right.m_buffer); setLoop(right.getLoop()); - setPitch(right.getPitch()); - setVolume(right.getVolume()); - setPosition(right.getPosition()); - setRelativeToListener(right.isRelativeToListener()); - setMinDistance(right.getMinDistance()); - setAttenuation(right.getAttenuation()); return *this; } diff --git a/src/SFML/Audio/SoundSource.cpp b/src/SFML/Audio/SoundSource.cpp index 977769ebf..45498ec18 100644 --- a/src/SFML/Audio/SoundSource.cpp +++ b/src/SFML/Audio/SoundSource.cpp @@ -75,6 +75,7 @@ void SoundSource::setVolume(float volume) alCheck(alSourcef(m_source, AL_GAIN, volume * 0.01f)); } + //////////////////////////////////////////////////////////// void SoundSource::setPosition(float x, float y, float z) { @@ -170,6 +171,24 @@ float SoundSource::getAttenuation() const } +//////////////////////////////////////////////////////////// +SoundSource& SoundSource::operator =(const SoundSource& right) +{ + // Leave m_source untouched -- it's not necessary to destroy and + // recreate the OpenAL sound source, hence no copy-and-swap idiom + + // Assign the sound attributes + setPitch(right.getPitch()); + setVolume(right.getVolume()); + setPosition(right.getPosition()); + setRelativeToListener(right.isRelativeToListener()); + setMinDistance(right.getMinDistance()); + setAttenuation(right.getAttenuation()); + + return *this; +} + + //////////////////////////////////////////////////////////// SoundSource::Status SoundSource::getStatus() const {