Przeglądaj źródła

Merge pull request #281 from blackberry-gaming/next-dgough

Next dgough
Sean Paul Taylor 13 lat temu
rodzic
commit
289c26fb83

+ 15 - 1
gameplay/src/Animation.cpp

@@ -19,12 +19,18 @@ Animation::Animation(const char* id, AnimationTarget* target, int propertyId, un
     : _controller(Game::getInstance()->getAnimationController()), _id(id), _duration(0), _defaultClip(NULL), _clips(NULL)
 {
     createChannel(target, propertyId, keyCount, keyTimes, keyValues, type);
+    // Release the animation because a newly created animation has a ref count of 1 and the channels hold the ref to animation.
+    release();
+    assert(getRefCount() == 1);
 }
 
 Animation::Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type)
     : _controller(Game::getInstance()->getAnimationController()), _id(id), _duration(0), _defaultClip(NULL), _clips(NULL)
 {
     createChannel(target, propertyId, keyCount, keyTimes, keyValues, keyInValue, keyOutValue, type);
+    // Release the animation because a newly created animation has a ref count of 1 and the channels hold the ref to animation.
+    release();
+    assert(getRefCount() == 1);
 }
 
 Animation::Animation(const char* id)
@@ -67,6 +73,7 @@ Animation::Channel::Channel(Animation* animation, AnimationTarget* target, int p
     assert(_target->getAnimationPropertyComponentCount(propertyId));
     _curve->addRef();
     _target->addChannel(this);
+    _animation->addRef();
 }
 
 Animation::Channel::Channel(const Channel& copy, Animation* animation, AnimationTarget* target)
@@ -74,6 +81,7 @@ Animation::Channel::Channel(const Channel& copy, Animation* animation, Animation
 {
     _curve->addRef();
     _target->addChannel(this);
+    _animation->addRef();
 }
 
 Animation::Channel::~Channel()
@@ -388,9 +396,15 @@ void Animation::setTransformRotationOffset(Curve* curve, unsigned int propertyId
     return;
 }
 
-Animation* Animation::clone()
+Animation* Animation::clone(Channel* channel, AnimationTarget* target)
 {
     Animation* animation = new Animation(getId());
+
+    Animation::Channel* channelCopy = new Animation::Channel(*channel, animation, target);
+    animation->addChannel(channelCopy);
+    // Release the animation because a newly created animation has a ref count of 1 and the channels hold the ref to animation.
+    animation->release();
+    assert(animation->getRefCount() == 1);
     return animation;
 }
 

+ 4 - 1
gameplay/src/Animation.h

@@ -201,9 +201,12 @@ private:
     /**
      * Clones this animation.
      * 
+     * @param channel The channel to clone and add to the animation.
+     * @param target The target of the animation.
+     * 
      * @return The newly created animation.
      */
-    Animation* clone();
+    Animation* clone(Channel* channel, AnimationTarget* target);
     
     AnimationController* _controller;       // The AnimationController that this Animation will run on.
     std::string _id;                        // The Animation's ID.

+ 9 - 12
gameplay/src/AnimationTarget.cpp

@@ -397,21 +397,18 @@ void AnimationTarget::cloneInto(AnimationTarget* target, NodeCloneContext &conte
             Animation::Channel* channel = *it;
             assert(channel->_animation);
 
-            bool animationCloned = false;
-
-            // Don't clone the Animaton if it is already in the clone context.
             Animation* animation = context.findClonedAnimation(channel->_animation);
-            if (animation == NULL)
+            if (animation != NULL)
             {
-                animation = channel->_animation->clone();
-                animationCloned = true;
+                Animation::Channel* channelCopy = new Animation::Channel(*channel, animation, target);
+                animation->addChannel(channelCopy);
+            }
+            else
+            {
+                // Clone the animation and register it with the context so that it only gets cloned once.
+                animation = channel->_animation->clone(channel, target);
+                context.registerClonedAnimation(channel->_animation, animation);
             }
-            assert(animation);
-
-            context.registerClonedAnimation(channel->_animation, animation);
-            
-            Animation::Channel* channelCopy = new Animation::Channel(*channel, animation, target);
-            animation->addChannel(channelCopy);
         }
     }
 }

+ 0 - 1
gameplay/src/Package.cpp

@@ -969,7 +969,6 @@ Animation* Package::readAnimationChannel(Scene* scene, Animation* animation, con
         else
         {
             animation->createChannel(target, targetAttribute, keyTimesCount, &keyTimes[0], &values[0], Curve::LINEAR);
-            animation->addRef();
         }
     }
 

+ 1 - 0
gameplay/src/SceneLoader.cpp

@@ -309,6 +309,7 @@ void SceneLoader::applyNodeProperty(SceneNode& sceneNode, Node* node, const Prop
                         {
                             // Temporarily set rigidbody model on model so it's used during collision object creation.
                             Model* model = node->getModel();
+                            assert(model);
                         
                             // Up ref count to prevent node from releasing the model when we swap it.
                             model->addRef();