Преглед изворни кода

Merge pull request #1296 from seanpaultaylor/next

Fixes AudioListener::setCamera problems change to use a cameraListener t...
Sean Taylor пре 12 година
родитељ
комит
aea02549c6
2 измењених фајлова са 318 додато и 312 уклоњено
  1. 147 140
      gameplay/src/AudioListener.cpp
  2. 171 172
      gameplay/src/AudioListener.h

+ 147 - 140
gameplay/src/AudioListener.cpp

@@ -1,140 +1,147 @@
-#include "Base.h"
-#include "Node.h"
-#include "AudioListener.h"
-#include "Game.h"
-
-namespace gameplay
-{
-
-AudioListener::AudioListener()
-    : _gain(1.0f), _camera(NULL)
-{
-}
-
-AudioListener::~AudioListener()
-{
-	// Call setCamera() to release camera and cause transform listener
-	// to be removed.
-	setCamera(NULL);
-}
-
-AudioListener* AudioListener::getInstance()
-{
-    return Game::getInstance()->getAudioListener();
-}
-
-float AudioListener::getGain() const 
-{ 
-    return _gain; 
-}
-
-void AudioListener::setGain(float gain)
-{
-    _gain = gain;
-}
-
-const Vector3& AudioListener::getPosition() const 
-{ 
-    return _position; 
-}
-
-void AudioListener::setPosition(const Vector3& position)
-{
-    _position = position;
-}
-
-void AudioListener::setPosition(float x, float y, float z)
-{
-    _position.set(x, y, z);
-}
-
-const Vector3& AudioListener::getVelocity() const 
-{ 
-    return _velocity; 
-}
-
-void AudioListener::setVelocity(const Vector3& velocity)
-{
-    _velocity = velocity;
-}
-
-void AudioListener::setVelocity(float x, float y, float z)
-{
-    _velocity.set(x, y, z);
-}
-
-const float* AudioListener::getOrientation() const
-{
-    return (const float*)&_orientation[0];
-}
-
-const Vector3& AudioListener::getOrientationForward() const 
-{ 
-    return _orientation[0]; 
-}
-
-const Vector3& AudioListener::getOrientationUp() const 
-{ 
-    return _orientation[1]; 
-}
-
-void AudioListener::setOrientation(const Vector3& forward, const Vector3& up)
-{
-    _orientation[0].x = forward.x;
-    _orientation[0].y = forward.y;
-    _orientation[0].z = forward.z;
-
-    _orientation[1].x = up.x;
-    _orientation[1].y = up.y;
-    _orientation[1].z = up.z;
-}
-
-void AudioListener::setOrientation(float forwardX, float forwardY, float forwardZ, float upX, float upY, float upZ)
-{
-    _orientation[0].set(forwardX, forwardY, forwardZ);
-    _orientation[1].set(upX, upY, upZ);
-}
-
-Camera* AudioListener::getCamera() const
-{
-    return _camera;
-}
-
-void AudioListener::setCamera(Camera* c)
-{
-    if (_camera != c)
-    {
-        // Disconnect our current camera.
-        if (_camera)
-        {
-            GP_ASSERT(_camera->getNode());
-            _camera->getNode()->removeListener(this);
-            SAFE_RELEASE(_camera);
-        }
-
-        // Connect the new camera.
-        _camera = c;
-
-        if (_camera)
-        {
-            GP_ASSERT(_camera->getNode());
-            _camera->addRef();
-            _camera->getNode()->addListener(this);
-        }
-    }
-}
-
-void AudioListener::transformChanged(Transform* transform, long cookie)
-{
-    if (transform)
-    {
-        Node* node = static_cast<Node*>(transform);
-        setPosition(node->getTranslationWorld());
-        
-        Vector3 up;
-        node->getWorldMatrix().getUpVector(&up);
-        setOrientation(node->getForwardVectorWorld(), up);
-    }
-}
-
-}
+#include "Base.h"
+#include "Node.h"
+#include "AudioListener.h"
+#include "Game.h"
+
+namespace gameplay
+{
+
+AudioListener::AudioListener()
+    : _gain(1.0f), _camera(NULL)
+{
+}
+
+AudioListener::~AudioListener()
+{
+	// Call setCamera() to release camera and cause transform listener
+	// to be removed.
+	setCamera(NULL);
+}
+
+AudioListener* AudioListener::getInstance()
+{
+    return Game::getInstance()->getAudioListener();
+}
+
+float AudioListener::getGain() const 
+{ 
+    return _gain; 
+}
+
+void AudioListener::setGain(float gain)
+{
+    _gain = gain;
+}
+
+const Vector3& AudioListener::getPosition() const 
+{ 
+    return _position; 
+}
+
+void AudioListener::setPosition(const Vector3& position)
+{
+    _position = position;
+}
+
+void AudioListener::setPosition(float x, float y, float z)
+{
+    _position.set(x, y, z);
+}
+
+const Vector3& AudioListener::getVelocity() const 
+{ 
+    return _velocity; 
+}
+
+void AudioListener::setVelocity(const Vector3& velocity)
+{
+    _velocity = velocity;
+}
+
+void AudioListener::setVelocity(float x, float y, float z)
+{
+    _velocity.set(x, y, z);
+}
+
+const float* AudioListener::getOrientation() const
+{
+    return (const float*)&_orientation[0];
+}
+
+const Vector3& AudioListener::getOrientationForward() const 
+{ 
+    return _orientation[0]; 
+}
+
+const Vector3& AudioListener::getOrientationUp() const 
+{ 
+    return _orientation[1]; 
+}
+
+void AudioListener::setOrientation(const Vector3& forward, const Vector3& up)
+{
+    _orientation[0].x = forward.x;
+    _orientation[0].y = forward.y;
+    _orientation[0].z = forward.z;
+
+    _orientation[1].x = up.x;
+    _orientation[1].y = up.y;
+    _orientation[1].z = up.z;
+}
+
+void AudioListener::setOrientation(float forwardX, float forwardY, float forwardZ, float upX, float upY, float upZ)
+{
+    _orientation[0].set(forwardX, forwardY, forwardZ);
+    _orientation[1].set(upX, upY, upZ);
+}
+
+Camera* AudioListener::getCamera() const
+{
+    return _camera;
+}
+
+void AudioListener::setCamera(Camera* camera)
+{
+    if (!camera || _camera == camera)
+        return;
+
+    // Disconnect our current camera.
+    if (_camera)
+    {
+        _camera->removeListener(this);
+        SAFE_RELEASE(_camera);
+    }
+
+    // Connect the new camera.
+    _camera = camera;
+    if (_camera)
+    {
+        _camera->addRef();
+        _camera->addListener(this);
+    }
+}
+
+void AudioListener::cameraChanged(Camera* camera)
+{
+    if (_camera != camera)
+        setCamera(camera);
+   
+    if (_camera)
+    {
+        Node* node = camera->getNode();
+        if (node)
+        {
+            setPosition(node->getTranslationWorld());
+            Vector3 up;
+            node->getWorldMatrix().getUpVector(&up);
+            setOrientation(node->getForwardVectorWorld(), up);
+        }
+        else
+        {
+            setPosition(Vector3::zero());
+            setOrientation(Vector3::unitY(), -Vector3::unitZ());
+        }
+    }
+}
+
+}

+ 171 - 172
gameplay/src/AudioListener.h

@@ -1,172 +1,171 @@
-#ifndef AUDIOLISTENER_H_
-#define AUDIOLISTENER_H_
-
-#include "Vector3.h"
-#include "Transform.h"
-
-namespace gameplay
-{
-
-class Camera;
-
-/**
- * Defines an audio listener in 3D space.
- */
-class AudioListener : public Transform::Listener
-{
-    friend class AudioController;
-    friend class Game;
-
-public:
-
-    /**
-     * Gets the single instance of the audio listener.
-     *
-     * @return The single instance of the audio listener.
-     */
-    static AudioListener* getInstance();
-
-    /**
-     * Gets the current position of the audio listener.
-     *
-     * @return The position of the audio listener
-     */
-    const Vector3& getPosition() const;
-
-    /**
-     * Sets the position of the audio source.
-     *
-     * @param position The position to set the listener to.
-     */
-    void setPosition(const Vector3& position);
-
-    /**
-     * Sets the position of the audio source.
-     * 
-     * @param x The x coordinate of the position.
-     * @param y The y coordinate of the position.
-     * @param z The z coordinate of the position.
-     */
-    void setPosition(float x, float y, float z);
-
-    /**
-     * Returns the gain of the audio listener.
-     *
-     * @return The gain of the audio listener.
-     */
-    float getGain() const;
-
-    /**
-     * Sets the gain/volume of the audio listener.
-     *
-     * @param gain The gain/volume of the listener.
-     */
-    void setGain(float gain);
-
-    /**
-     * Gets the velocity of the audio source.
-     *
-     * @return The velocity as a vector.
-     */
-    const Vector3& getVelocity() const;
-
-    /**
-     * Sets the velocity of the audio source
-     *
-     * @param velocity A vector representing the velocity.
-     */
-    void setVelocity(const Vector3& velocity);
-
-    /**
-     * Sets the velocity of the audio source
-     * 
-     * @param x The x coordinate of the velocity.
-     * @param y The y coordinate of the velocity.
-     * @param z The z coordinate of the velocity.
-     */
-    void setVelocity(float x, float y, float z);
-
-    /**
-     * Gets the float pointer to the orientation of the audio listener.
-     * Orientation is represented as 6 floats. (forward.x, forward.y, forward.z, up.x, up.y, up.z).
-     * 
-     * @return Pointer to the 6 orientation float values.
-     * @script{ignore}
-     */
-    const float* getOrientation() const;
-
-    /**
-     * Gets the forward orientation vector of the audio listener.
-     *
-     * @return The forward vector.
-     */
-    const Vector3& getOrientationForward() const;
-
-    /**
-     * Gets the up orientation vector of the audio listener.
-     *
-     * @return The up vector.
-     */
-    const Vector3& getOrientationUp() const;
-
-    /**
-     * Sets the orientation of the audio listener.
-     *
-     * @param forward The forward vector.
-     * @param up The up vector.
-     */
-    void setOrientation(const Vector3& forward, const Vector3& up);
-
-    /**
-     * Sets the orientation of the audio listener.
-     * 
-     * @param forwardX The x coordinate of the forward vector.
-     * @param forwardY The y coordinate of the forward vector.
-     * @param forwardZ The z coordinate of the forward vector.
-     * @param upX The x coordinate of the up vector.
-     * @param upY The y coordinate of the up vector.
-     * @param upZ The z coordinate of the up vector.
-     */
-    void setOrientation(float forwardX, float forwardY, float forwardZ, float upX, float upY, float upZ);
-
-    /**
-     * Gets the camera currently associated with the audio listener.
-     *
-     * @return camera The camera currently associated with the audio listener.
-     */
-    Camera* getCamera() const;
-
-    /**
-     * Sets the camera that is associated with the audio listener. This should usually be the current camera.
-     *
-     * @param camera The camera that is associated with the audio listener
-     */
-    void setCamera(Camera* camera);
-
-private:
-
-    /**
-    * Constructor.
-    */
-    AudioListener();
-
-    /**
-    * Destructor.
-    */
-    ~AudioListener();
-
-    /**
-    * @see Transform::Listener::transformChanged
-    */
-    void transformChanged(Transform* transform, long cookie);
-
-    float _gain;
-    Vector3 _position;
-    Vector3 _velocity;
-    Vector3 _orientation[2];
-    Camera* _camera;
-};
-
-}
-
-#endif
+#ifndef AUDIOLISTENER_H_
+#define AUDIOLISTENER_H_
+
+#include "Vector3.h"
+#include "Transform.h"
+#include "Camera.h"
+
+namespace gameplay
+{
+
+/**
+ * Defines an audio listener in 3D space.
+ */
+class AudioListener : public Camera::Listener
+{
+    friend class AudioController;
+    friend class Game;
+
+public:
+
+    /**
+     * Gets the single instance of the audio listener.
+     *
+     * @return The single instance of the audio listener.
+     */
+    static AudioListener* getInstance();
+
+    /**
+     * Gets the current position of the audio listener.
+     *
+     * @return The position of the audio listener
+     */
+    const Vector3& getPosition() const;
+
+    /**
+     * Sets the position of the audio source.
+     *
+     * @param position The position to set the listener to.
+     */
+    void setPosition(const Vector3& position);
+
+    /**
+     * Sets the position of the audio source.
+     * 
+     * @param x The x coordinate of the position.
+     * @param y The y coordinate of the position.
+     * @param z The z coordinate of the position.
+     */
+    void setPosition(float x, float y, float z);
+
+    /**
+     * Returns the gain of the audio listener.
+     *
+     * @return The gain of the audio listener.
+     */
+    float getGain() const;
+
+    /**
+     * Sets the gain/volume of the audio listener.
+     *
+     * @param gain The gain/volume of the listener.
+     */
+    void setGain(float gain);
+
+    /**
+     * Gets the velocity of the audio source.
+     *
+     * @return The velocity as a vector.
+     */
+    const Vector3& getVelocity() const;
+
+    /**
+     * Sets the velocity of the audio source
+     *
+     * @param velocity A vector representing the velocity.
+     */
+    void setVelocity(const Vector3& velocity);
+
+    /**
+     * Sets the velocity of the audio source
+     * 
+     * @param x The x coordinate of the velocity.
+     * @param y The y coordinate of the velocity.
+     * @param z The z coordinate of the velocity.
+     */
+    void setVelocity(float x, float y, float z);
+
+    /**
+     * Gets the float pointer to the orientation of the audio listener.
+     * Orientation is represented as 6 floats. (forward.x, forward.y, forward.z, up.x, up.y, up.z).
+     * 
+     * @return Pointer to the 6 orientation float values.
+     * @script{ignore}
+     */
+    const float* getOrientation() const;
+
+    /**
+     * Gets the forward orientation vector of the audio listener.
+     *
+     * @return The forward vector.
+     */
+    const Vector3& getOrientationForward() const;
+
+    /**
+     * Gets the up orientation vector of the audio listener.
+     *
+     * @return The up vector.
+     */
+    const Vector3& getOrientationUp() const;
+
+    /**
+     * Sets the orientation of the audio listener.
+     *
+     * @param forward The forward vector.
+     * @param up The up vector.
+     */
+    void setOrientation(const Vector3& forward, const Vector3& up);
+
+    /**
+     * Sets the orientation of the audio listener.
+     * 
+     * @param forwardX The x coordinate of the forward vector.
+     * @param forwardY The y coordinate of the forward vector.
+     * @param forwardZ The z coordinate of the forward vector.
+     * @param upX The x coordinate of the up vector.
+     * @param upY The y coordinate of the up vector.
+     * @param upZ The z coordinate of the up vector.
+     */
+    void setOrientation(float forwardX, float forwardY, float forwardZ, float upX, float upY, float upZ);
+
+    /**
+     * Gets the camera currently associated with the audio listener.
+     *
+     * @return camera The camera currently associated with the audio listener.
+     */
+    Camera* getCamera() const;
+
+    /**
+     * Sets the camera that is associated with the audio listener. This should usually be the current camera.
+     *
+     * @param camera The camera that is associated with the audio listener
+     */
+    void setCamera(Camera* camera);
+
+private:
+
+    /**
+    * Constructor.
+    */
+    AudioListener();
+
+    /**
+    * Destructor.
+    */
+    ~AudioListener();
+
+    /**
+    * @see Camera::Listener::cameraChanged
+    */
+    void cameraChanged(Camera* camera);
+
+    float _gain;
+    Vector3 _position;
+    Vector3 _velocity;
+    Vector3 _orientation[2];
+    Camera* _camera;
+};
+
+}
+
+#endif