From c152652c5b5fa6a7def43ec1b62d2b76bd049489 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 28 Nov 2013 11:04:39 +0800 Subject: [PATCH] issue #2790: Vector for Sequence::create(arr) and Spawn::create(arr). --- cocos/2d/CCActionInterval.cpp | 20 +++---- cocos/2d/CCActionInterval.h | 5 +- cocos/2d/cocos2d.h | 1 + cocos/base/CCVector.h | 54 +++++++++++++++---- .../cocosbuilder/CCBAnimationManager.cpp | 32 +++++------ .../cocostudio/CCActionNode.cpp | 11 ++-- 6 files changed, 81 insertions(+), 42 deletions(-) diff --git a/cocos/2d/CCActionInterval.cpp b/cocos/2d/CCActionInterval.cpp index a2ae6b7824..655922be15 100644 --- a/cocos/2d/CCActionInterval.cpp +++ b/cocos/2d/CCActionInterval.cpp @@ -198,21 +198,21 @@ Sequence* Sequence::createWithVariableList(FiniteTimeAction *pAction1, va_list a return ((Sequence*)pPrev); } -Sequence* Sequence::create(Array* arrayOfActions) +Sequence* Sequence::create(const Vector& arrayOfActions) { Sequence* pRet = NULL; do { - long count = arrayOfActions->count(); + long count = arrayOfActions.count(); CC_BREAK_IF(count == 0); - FiniteTimeAction* prev = static_cast(arrayOfActions->getObjectAtIndex(0)); + auto prev = arrayOfActions[0]; if (count > 1) { for (long i = 1; i < count; ++i) { - prev = createWithTwoActions(prev, static_cast(arrayOfActions->getObjectAtIndex(i))); + prev = createWithTwoActions(prev, arrayOfActions[i]); } } else @@ -571,19 +571,19 @@ Spawn* Spawn::createWithVariableList(FiniteTimeAction *pAction1, va_list args) return ((Spawn*)pPrev); } -Spawn* Spawn::create(Array *arrayOfActions) +Spawn* Spawn::create(const Vector& arrayOfActions) { Spawn* pRet = NULL; do { - long count = arrayOfActions->count(); + long count = arrayOfActions.count(); CC_BREAK_IF(count == 0); - FiniteTimeAction* prev = static_cast(arrayOfActions->getObjectAtIndex(0)); + auto prev = arrayOfActions[0]; if (count > 1) { - for (int i = 1; i < arrayOfActions->count(); ++i) + for (int i = 1; i < arrayOfActions.count(); ++i) { - prev = createWithTwoActions(prev, static_cast(arrayOfActions->getObjectAtIndex(i))); + prev = createWithTwoActions(prev, arrayOfActions[i]); } } else @@ -591,7 +591,7 @@ Spawn* Spawn::create(Array *arrayOfActions) // If only one action is added to Spawn, make up a Spawn by adding a simplest finite time action. prev = createWithTwoActions(prev, ExtraAction::create()); } - pRet = (Spawn*)prev; + pRet = static_cast(prev); }while (0); return pRet; diff --git a/cocos/2d/CCActionInterval.h b/cocos/2d/CCActionInterval.h index a72327e997..36306e2939 100644 --- a/cocos/2d/CCActionInterval.h +++ b/cocos/2d/CCActionInterval.h @@ -32,6 +32,7 @@ THE SOFTWARE. #include "CCProtocols.h" #include "CCSpriteFrame.h" #include "CCAnimation.h" +#include #include NS_CC_BEGIN @@ -99,7 +100,7 @@ public: * in lua :local create(local object1,local object2, ...) * @endcode */ - static Sequence* create(Array *arrayOfActions); + static Sequence* create(const Vector& arrayOfActions); /** helper constructor to create an array of sequence-able actions */ static Sequence* createWithVariableList(FiniteTimeAction *pAction1, va_list args); /** creates the action */ @@ -246,7 +247,7 @@ public: static Spawn* createWithVariableList(FiniteTimeAction *pAction1, va_list args); /** helper constructor to create an array of spawned actions given an array */ - static Spawn* create(Array *arrayOfActions); + static Spawn* create(const Vector& arrayOfActions); /** creates the Spawn action */ static Spawn* createWithTwoActions(FiniteTimeAction *pAction1, FiniteTimeAction *pAction2); diff --git a/cocos/2d/cocos2d.h b/cocos/2d/cocos2d.h index b25bb7bee8..fa194c7fe0 100644 --- a/cocos/2d/cocos2d.h +++ b/cocos/2d/cocos2d.h @@ -60,6 +60,7 @@ THE SOFTWARE. #include "CCDictionary.h" #include "CCObject.h" #include "CCArray.h" +#include "CCVector.h" #include "CCGeometry.h" #include "CCSet.h" #include "CCAutoreleasePool.h" diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index cd75c94a26..8ffadcb4c5 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -39,21 +39,50 @@ public: Vector(long capacity=7) : _data() { + CCLOG("In the constructor of Vector."); init(capacity); } virtual ~Vector() { - for( auto it=std::begin(_data); it != std::end(_data); ++it ) - (*it)->release(); + CCLOG("In the destructor of Vector."); + removeAllObjects(); } + Vector(const Vector& other) + { + CCLOG("In the copy constructor!"); + copy(other); + } + + const Vector& operator=(const Vector& other) + { + CCLOG("In the assignment operator!"); + copy(other); + return *this; + } + + T operator[](long index) const + { + return getObjectAtIndex(index); + } + /** Initializes an array with capacity */ bool init(long capacity) { _data.reserve(capacity); return true; } - + + void copy(const Vector& other) + { + if (this == &other) + return; + + removeAllObjects(); + init(other.count()); + addObjectsFromArray(other); + } + // Querying an Array /** Returns element count of the array */ @@ -131,9 +160,9 @@ public: } /** Add all elements of an existing array */ - void addObjectsFromArray(T otherArray) + void addObjectsFromArray(const Vector& otherArray) { - for( auto it = std::begin(otherArray); it != std::end(otherArray); ++it ) { + for( auto it = otherArray.begin(); it != otherArray.end(); ++it ) { _data.push_back( *it ); (*it)->retain(); } @@ -180,17 +209,17 @@ public: void removeObjectAtIndex(long index) { auto it = std::next( begin(), index ); - _data.erase(it); (*it)->release(); + _data.erase(it); } /** Removes all objects */ void removeAllObjects() { for( auto it = std::begin(_data); it != std::end(_data); ++it ) { - _data.erase(it); (*it)->release(); } + _data.clear(); } /** Fast way to remove a certain object */ @@ -254,9 +283,16 @@ public: typedef typename std::vector::const_iterator const_iterator; iterator begin() { return _data.begin(); } + const_iterator begin() const { return _data.begin(); } + iterator end() { return _data.end(); } - const_iterator cbegin() { return _data.cbegin(); } - const_iterator cend() { return _data.cend(); } + const_iterator end() const { return _data.end(); } + + iterator cbegin() { return _data.cbegin(); } + const_iterator cbegin() const { return _data.cbegin(); } + + iterator cend() { return _data.cend(); } + const_iterator cend() const { return _data.cend(); } protected: std::vector _data; diff --git a/cocos/editor-support/cocosbuilder/CCBAnimationManager.cpp b/cocos/editor-support/cocosbuilder/CCBAnimationManager.cpp index df8721de0d..5fa169014f 100644 --- a/cocos/editor-support/cocosbuilder/CCBAnimationManager.cpp +++ b/cocos/editor-support/cocosbuilder/CCBAnimationManager.cpp @@ -620,7 +620,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann float lastKeyframeTime = 0; - Array *actions = Array::create(); + Vector actions; Array *keyframes = channel->getKeyframes(); long numKeyframes = keyframes->count(); @@ -631,7 +631,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime; lastKeyframeTime = keyframe->getTime(); if(timeSinceLastKeyframe > 0) { - actions->addObject(DelayTime::create(timeSinceLastKeyframe)); + actions.addObject(DelayTime::create(timeSinceLastKeyframe)); } Array* keyVal = static_cast(keyframe->getValue()); @@ -646,7 +646,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann CallFunc *callbackClone = (static_cast(callback))->clone(); if(callbackClone != NULL) { - actions->addObject(callbackClone); + actions.addObject(callbackClone); } } } @@ -680,7 +680,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann { // XXX: how to fix this warning? CallFuncN *callback = CallFuncN::create(target, selCallFunc); - actions->addObject(callback); + actions.addObject(callback); } } else @@ -690,7 +690,7 @@ Object* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* chann } } } - if(actions->count() < 1) return NULL; + if(actions.count() < 1) return NULL; return (Object *) Sequence::create(actions); } @@ -699,9 +699,9 @@ Object* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel) float lastKeyframeTime = 0; - Array *actions = Array::create(); + Vector actions; Array *keyframes = channel->getKeyframes(); - int numKeyframes = keyframes->count(); + long numKeyframes = keyframes->count(); for (int i = 0; i < numKeyframes; ++i) { @@ -709,7 +709,7 @@ Object* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel) float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime; lastKeyframeTime = keyframe->getTime(); if(timeSinceLastKeyframe > 0) { - actions->addObject(DelayTime::create(timeSinceLastKeyframe)); + actions.addObject(DelayTime::create(timeSinceLastKeyframe)); } stringstream ss (stringstream::in | stringstream::out); @@ -729,12 +729,12 @@ Object* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel) ss >> gain; ss.flush(); - actions->addObject(CCBSoundEffect::actionWithSoundFile(soundFile, pitch, pan, gain)); + actions.addObject(CCBSoundEffect::actionWithSoundFile(soundFile, pitch, pan, gain)); } - if(actions->count() < 1) return NULL; + if(actions.count() < 1) return NULL; - return (Object *) Sequence::create(actions); + return Sequence::create(actions); } @@ -742,19 +742,19 @@ Object* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel) void CCBAnimationManager::runAction(Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration) { Array *keyframes = pSeqProp->getKeyframes(); - int numKeyframes = keyframes->count(); + long numKeyframes = keyframes->count(); if (numKeyframes > 1) { // Make an animation! - Array *actions = Array::create(); + Vector actions; CCBKeyframe *keyframeFirst = (CCBKeyframe*)keyframes->getObjectAtIndex(0); float timeFirst = keyframeFirst->getTime() + fTweenDuration; if (timeFirst > 0) { - actions->addObject(DelayTime::create(timeFirst)); + actions.addObject(DelayTime::create(timeFirst)); } for (int i = 0; i < numKeyframes - 1; ++i) @@ -768,11 +768,11 @@ void CCBAnimationManager::runAction(Node *pNode, CCBSequenceProperty *pSeqProp, // Apply easing action = getEaseAction(action, kf0->getEasingType(), kf0->getEasingOpt()); - actions->addObject(action); + actions.addObject(action); } } - FiniteTimeAction *seq = Sequence::create(actions); + auto seq = Sequence::create(actions); pNode->runAction(seq); } } diff --git a/cocos/editor-support/cocostudio/CCActionNode.cpp b/cocos/editor-support/cocostudio/CCActionNode.cpp index 64a91744d0..f094c10038 100644 --- a/cocos/editor-support/cocostudio/CCActionNode.cpp +++ b/cocos/editor-support/cocostudio/CCActionNode.cpp @@ -276,7 +276,8 @@ Spawn * ActionNode::refreshActionProperty() { return NULL; } - Array* cSpawnArray = Array::create(); + Vector cSpawnArray; + for (int n = 0; n < _frameArrayNum; n++) { Array* cArray = (Array*)(_frameArray->getObjectAtIndex(n)); @@ -285,8 +286,8 @@ Spawn * ActionNode::refreshActionProperty() continue; } - Array* cSequenceArray = Array::create(); - int frameCount = cArray->count(); + Vector cSequenceArray; + long frameCount = cArray->count(); for (int i = 0; i < frameCount; i++) { ActionFrame* frame = (ActionFrame*)(cArray->getObjectAtIndex(i)); @@ -298,13 +299,13 @@ Spawn * ActionNode::refreshActionProperty() ActionFrame* srcFrame = (ActionFrame*)(cArray->getObjectAtIndex(i-1)); float duration = (frame->getFrameIndex() - srcFrame->getFrameIndex()) * getUnitTime(); Action* cAction = frame->getAction(duration); - cSequenceArray->addObject(cAction); + cSequenceArray.addObject(static_cast(cAction)); } } Sequence* cSequence = Sequence::create(cSequenceArray); if (cSequence != NULL) { - cSpawnArray->addObject(cSequence); + cSpawnArray.addObject(cSequence); } }