Bläddra i källkod

Fixed bug in AudioSource related to cloning AudioSource.
Fixed memory leak where AudioListener was not being cleaned up. (Now it is a member variable of Game, just like the controllers).

Darryl Gough 13 år sedan
förälder
incheckning
4c19de919b

+ 3 - 8
gameplay/src/AudioListener.cpp

@@ -1,29 +1,24 @@
 #include "Base.h"
 #include "Node.h"
 #include "AudioListener.h"
+#include "Game.h"
 
 namespace gameplay
 {
 
-static AudioListener* __audioListenerInstance = NULL;
-
 AudioListener::AudioListener()
     : _gain(1.0f), _camera(NULL)
-
 {
-    assert(__audioListenerInstance == NULL);
-    __audioListenerInstance = this;
 }
 
 AudioListener::~AudioListener()
 {
+    SAFE_RELEASE(_camera);
 }
 
 AudioListener* AudioListener::getInstance()
 {
-    if (!__audioListenerInstance)
-        new AudioListener();
-    return __audioListenerInstance;
+    return Game::getInstance()->getAudioListener();
 }
 
 float AudioListener::getGain() const 

+ 1 - 0
gameplay/src/AudioListener.h

@@ -15,6 +15,7 @@ class Camera;
 class AudioListener : public Transform::Listener
 {
     friend class AudioController;
+    friend class Game;
 
 public:
 

+ 2 - 2
gameplay/src/AudioSource.cpp

@@ -497,12 +497,12 @@ AudioSource* AudioSource::clone(CloneContext &context) const
     AudioSource* audioClone = new AudioSource(AudioBuffer* buffer, const SLObjectItf& player);
 
 #endif
-
+    _buffer->addRef();
     audioClone->setLooped(isLooped());
     audioClone->setGain(getGain());
     audioClone->setPitch(getPitch());
     audioClone->setVelocity(getVelocity());
-    if (Node* node = audioClone->getNode())
+    if (Node* node = getNode())
     {
         Node* clonedNode = context.findClonedNode(node);
         if (clonedNode)

+ 12 - 1
gameplay/src/Game.cpp

@@ -17,7 +17,7 @@ Game::Game()
     : _initialized(false), _state(UNINITIALIZED), 
       _frameLastFPS(0), _frameCount(0), _frameRate(0), 
       _clearDepth(1.0f), _clearStencil(0),
-      _animationController(NULL), _audioController(NULL)
+      _animationController(NULL), _audioController(NULL), _physicsController(NULL), _audioListener(NULL)
 {
     assert(__gameInstance == NULL);
     __gameInstance = this;
@@ -124,6 +124,8 @@ void Game::shutdown()
         _physicsController->finalize();
         SAFE_DELETE(_physicsController);
 
+        SAFE_DELETE(_audioListener);
+
         RenderState::finalize();
     }
 
@@ -253,6 +255,15 @@ void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth,
     glClear(bits);
 }
 
+AudioListener* Game::getAudioListener()
+{
+    if (_audioListener == NULL)
+    {
+        _audioListener = new AudioListener();
+    }
+    return _audioListener;
+}
+
 void Game::menu()
 {
 }

+ 9 - 0
gameplay/src/Game.h

@@ -9,6 +9,7 @@
 #include "AudioController.h"
 #include "AnimationController.h"
 #include "PhysicsController.h"
+#include "AudioListener.h"
 #include "Vector4.h"
 #include "TimeListener.h"
 
@@ -184,6 +185,13 @@ public:
      */
     inline PhysicsController* getPhysicsController() const;
 
+    /**
+     * Gets the audio listener for 3D audio.
+     * 
+     * @return The audio listener for this game.
+     */
+    AudioListener* getAudioListener();
+
     /**
      * Menu callback on menu events.
      */
@@ -383,6 +391,7 @@ private:
     AnimationController* _animationController;  // Controls the scheduling and running of animations.
     AudioController* _audioController;          // Controls audio sources that are playing in the game.
     PhysicsController* _physicsController;      // Controls the simulation of a physics scene and entities.
+    AudioListener* _audioListener;              // The audio listener in 3D space.
     std::priority_queue<TimeEvent, std::vector<TimeEvent>, std::less<TimeEvent> > _timeEvents; // Contains the scheduled time events.
 
     friend class SplashDisplayer;