From d97e5244af0138aa5de6076ea13fb5ce1b6ed309 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sun, 23 Mar 2014 18:03:32 +0100 Subject: [PATCH] Optimized sf::Listener with a cache Two optimizations: - Get functions return locally stored value - Set functions call OpenAL only if value has changed --- src/SFML/Audio/Listener.cpp | 63 ++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/src/SFML/Audio/Listener.cpp b/src/SFML/Audio/Listener.cpp index 7c64ea3d..f41abe53 100644 --- a/src/SFML/Audio/Listener.cpp +++ b/src/SFML/Audio/Listener.cpp @@ -29,83 +29,88 @@ #include +namespace +{ + float listenerVolume = 100.f; + sf::Vector3f listenerPosition(0.f, 0.f, 0.f); + sf::Vector3f listenerDirection(0.f, 0.f, -1.f); +} + + namespace sf { //////////////////////////////////////////////////////////// void Listener::setGlobalVolume(float volume) { - priv::ensureALInit(); + if (listenerVolume != volume) + { + priv::ensureALInit(); - alCheck(alListenerf(AL_GAIN, volume * 0.01f)); + alCheck(alListenerf(AL_GAIN, volume * 0.01f)); + listenerVolume = volume; + } } //////////////////////////////////////////////////////////// float Listener::getGlobalVolume() { - priv::ensureALInit(); - - float volume = 0.f; - alCheck(alGetListenerf(AL_GAIN, &volume)); - - return volume * 100; + return listenerVolume; } //////////////////////////////////////////////////////////// void Listener::setPosition(float x, float y, float z) { - priv::ensureALInit(); - - alCheck(alListener3f(AL_POSITION, x, y, z)); + setPosition(sf::Vector3f(x, y, z)); } //////////////////////////////////////////////////////////// void Listener::setPosition(const Vector3f& position) { - setPosition(position.x, position.y, position.z); + if (position != listenerPosition) + { + priv::ensureALInit(); + + alCheck(alListener3f(AL_POSITION, position.x, position.y, position.z)); + listenerPosition = position; + } } //////////////////////////////////////////////////////////// Vector3f Listener::getPosition() { - priv::ensureALInit(); - - Vector3f position; - alCheck(alGetListener3f(AL_POSITION, &position.x, &position.y, &position.z)); - - return position; + return listenerPosition; } //////////////////////////////////////////////////////////// void Listener::setDirection(float x, float y, float z) { - priv::ensureALInit(); - - float orientation[] = {x, y, z, 0.f, 1.f, 0.f}; - alCheck(alListenerfv(AL_ORIENTATION, orientation)); + setDirection(sf::Vector3f(x, y, z)); } //////////////////////////////////////////////////////////// void Listener::setDirection(const Vector3f& direction) { - setDirection(direction.x, direction.y, direction.z); + if (direction != listenerDirection) + { + priv::ensureALInit(); + + float orientation[] = {direction.x, direction.y, direction.z, 0.f, 1.f, 0.f}; + alCheck(alListenerfv(AL_ORIENTATION, orientation)); + listenerDirection = direction; + } } //////////////////////////////////////////////////////////// Vector3f Listener::getDirection() { - priv::ensureALInit(); - - float orientation[6]; - alCheck(alGetListenerfv(AL_ORIENTATION, orientation)); - - return Vector3f(orientation[0], orientation[1], orientation[2]); + return listenerDirection; } } // namespace sf