Browse Source

Added Source:setRelativePosition and Source:hasRelativePosition, if enabled the Source's position will always be relative to the listener instead of absolute.

Alex Szpakowski 12 years ago
parent
commit
7f3cd7f044

+ 3 - 0
src/modules/audio/Source.h

@@ -80,6 +80,9 @@ public:
 	virtual void setDirection(float *v) = 0;
 	virtual void getDirection(float *v) const = 0;
 
+	virtual void setRelativePosition(bool relative) = 0;
+	virtual bool hasRelativePosition() const = 0;
+
 	virtual void setLooping(bool looping) = 0;
 	virtual bool isLooping() const = 0;
 	virtual bool isStatic() const = 0;

+ 10 - 0
src/modules/audio/null/Source.cpp

@@ -135,6 +135,16 @@ void Source::getDirection(float *) const
 {
 }
 
+void Source::setRelativePosition(bool relative)
+{
+	relativePosition = relative;
+}
+
+bool Source::hasRelativePosition() const
+{
+	return relativePosition;
+}
+
 void Source::setLooping(bool looping)
 {
 	this->looping = looping;

+ 3 - 0
src/modules/audio/null/Source.h

@@ -60,6 +60,8 @@ public:
 	virtual void getVelocity(float *v) const;
 	virtual void setDirection(float *v);
 	virtual void getDirection(float *v) const;
+	virtual void setRelativePosition(bool relative);
+	virtual bool hasRelativePosition() const;
 	void setLooping(bool looping);
 	bool isLooping() const;
 	bool isStatic() const;
@@ -78,6 +80,7 @@ private:
 
 	float pitch;
 	float volume;
+	bool relativePosition;
 	bool looping;
 	float minVolume;
 	float maxVolume;

+ 16 - 0
src/modules/audio/openal/Source.cpp

@@ -39,6 +39,7 @@ Source::Source(Pool *pool, love::sound::SoundData *soundData)
 	, valid(false)
 	, pitch(1.0f)
 	, volume(1.0f)
+	, relativePosition(false)
 	, looping(false)
 	, paused(false)
 	, minVolume(0.0f)
@@ -68,6 +69,7 @@ Source::Source(Pool *pool, love::sound::Decoder *decoder)
 	, valid(false)
 	, pitch(1.0f)
 	, volume(1.0f)
+	, relativePosition(false)
 	, looping(false)
 	, paused(false)
 	, minVolume(0.0f)
@@ -402,6 +404,19 @@ void Source::getDirection(float *v) const
 		setFloatv(v, direction);
 }
 
+void Source::setRelativePosition(bool relative)
+{
+	if (valid)
+		alSourcei(source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
+
+	relativePosition = relative;
+}
+
+bool Source::hasRelativePosition() const
+{
+	return relativePosition;
+}
+
 void Source::setLooping(bool looping)
 {
 	if (valid && type == TYPE_STATIC)
@@ -535,6 +550,7 @@ void Source::reset()
 	alSourcef(source, AL_ROLLOFF_FACTOR, rolloffFactor);
 	alSourcef(source, AL_MAX_DISTANCE, maxDistance);
 	alSourcei(source, AL_LOOPING, isStatic() && isLooping() ? AL_TRUE : AL_FALSE);
+	alSourcei(source, AL_SOURCE_RELATIVE, relativePosition ? AL_TRUE : AL_FALSE);
 }
 
 void Source::setFloatv(float *dst, const float *src) const

+ 3 - 0
src/modules/audio/openal/Source.h

@@ -78,6 +78,8 @@ public:
 	virtual void getVelocity(float *v) const;
 	virtual void setDirection(float *v);
 	virtual void getDirection(float *v) const;
+	virtual void setRelativePosition(bool relative);
+	virtual bool hasRelativePosition() const;
 	void setLooping(bool looping);
 	bool isLooping() const;
 	bool isStatic() const;
@@ -126,6 +128,7 @@ private:
 	float position[3];
 	float velocity[3];
 	float direction[3];
+	bool relativePosition;
 	bool looping;
 	bool paused;
 	float minVolume;

+ 17 - 0
src/modules/audio/wrap_Source.cpp

@@ -190,6 +190,20 @@ int w_Source_getDirection(lua_State *L)
 	return 3;
 }
 
+int w_Source_setRelativePosition(lua_State *L)
+{
+	Source *t = luax_checksource(L, 1);
+	t->setRelativePosition(luax_toboolean(L, 2));
+	return 0;
+}
+
+int w_Source_hasRelativePosition(lua_State *L)
+{
+	Source *t = luax_checksource(L, 1);
+	luax_pushboolean(L, t->hasRelativePosition());
+	return 1;
+}
+
 int w_Source_setLooping(lua_State *L)
 {
 	Source *t = luax_checksource(L, 1);
@@ -323,6 +337,9 @@ static const luaL_Reg functions[] =
 	{ "setDirection", w_Source_setDirection },
 	{ "getDirection", w_Source_getDirection },
 
+	{ "setRelativePosition", w_Source_setRelativePosition },
+	{ "hasRelativePosition", w_Source_hasRelativePosition },
+
 	{ "setLooping", w_Source_setLooping },
 	{ "isLooping", w_Source_isLooping },
 	{ "isStopped", w_Source_isStopped },

+ 2 - 0
src/modules/audio/wrap_Source.h

@@ -47,6 +47,8 @@ int w_Source_setVelocity(lua_State *L);
 int w_Source_getVelocity(lua_State *L);
 int w_Source_setDirection(lua_State *L);
 int w_Source_getDirection(lua_State *L);
+int w_Source_setRelativePosition(lua_State *L);
+int w_Source_hasRelativePosition(lua_State *L);
 int w_Source_setLooping(lua_State *L);
 int w_Source_isLooping(lua_State *L);
 int w_Source_isStopped(lua_State *L);