mirror of
https://github.com/SFML/SFML.git
synced 2025-02-19 06:39:59 +08:00
Optimized sf::Listener with a cache
Two optimizations: - Get functions return locally stored value - Set functions call OpenAL only if value has changed
This commit is contained in:
parent
1fd6fae073
commit
d97e5244af
@ -29,83 +29,88 @@
|
|||||||
#include <SFML/Audio/ALCheck.hpp>
|
#include <SFML/Audio/ALCheck.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
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
|
namespace sf
|
||||||
{
|
{
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void Listener::setGlobalVolume(float volume)
|
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()
|
float Listener::getGlobalVolume()
|
||||||
{
|
{
|
||||||
priv::ensureALInit();
|
return listenerVolume;
|
||||||
|
|
||||||
float volume = 0.f;
|
|
||||||
alCheck(alGetListenerf(AL_GAIN, &volume));
|
|
||||||
|
|
||||||
return volume * 100;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void Listener::setPosition(float x, float y, float z)
|
void Listener::setPosition(float x, float y, float z)
|
||||||
{
|
{
|
||||||
priv::ensureALInit();
|
setPosition(sf::Vector3f(x, y, z));
|
||||||
|
|
||||||
alCheck(alListener3f(AL_POSITION, x, y, z));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void Listener::setPosition(const Vector3f& position)
|
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()
|
Vector3f Listener::getPosition()
|
||||||
{
|
{
|
||||||
priv::ensureALInit();
|
return listenerPosition;
|
||||||
|
|
||||||
Vector3f position;
|
|
||||||
alCheck(alGetListener3f(AL_POSITION, &position.x, &position.y, &position.z));
|
|
||||||
|
|
||||||
return position;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void Listener::setDirection(float x, float y, float z)
|
void Listener::setDirection(float x, float y, float z)
|
||||||
{
|
{
|
||||||
priv::ensureALInit();
|
setDirection(sf::Vector3f(x, y, z));
|
||||||
|
|
||||||
float orientation[] = {x, y, z, 0.f, 1.f, 0.f};
|
|
||||||
alCheck(alListenerfv(AL_ORIENTATION, orientation));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void Listener::setDirection(const Vector3f& direction)
|
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()
|
Vector3f Listener::getDirection()
|
||||||
{
|
{
|
||||||
priv::ensureALInit();
|
return listenerDirection;
|
||||||
|
|
||||||
float orientation[6];
|
|
||||||
alCheck(alGetListenerfv(AL_ORIENTATION, orientation));
|
|
||||||
|
|
||||||
return Vector3f(orientation[0], orientation[1], orientation[2]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sf
|
} // namespace sf
|
||||||
|
Loading…
x
Reference in New Issue
Block a user