From 90062b656d2f8495437bbef1ef059c74ea1d80ae Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 28 Nov 2013 16:02:03 +0800 Subject: [PATCH] issue #2790: Node::_children is Vector now. --- cocos/2d/CCActionInterval.cpp | 46 ++- cocos/2d/CCAnimation.cpp | 65 ++--- cocos/2d/CCAnimation.h | 92 +++--- cocos/2d/CCAnimationCache.cpp | 30 +- cocos/2d/CCClippingNode.cpp | 9 +- cocos/2d/CCEventDispatcher.cpp | 11 +- cocos/2d/CCLabel.cpp | 55 ++-- cocos/2d/CCLabelBMFont.cpp | 68 ++--- cocos/2d/CCLayer.cpp | 12 +- cocos/2d/CCMenu.cpp | 268 +++++++----------- cocos/2d/CCMenu.h | 6 +- cocos/2d/CCNode.cpp | 139 ++++----- cocos/2d/CCNode.h | 22 +- cocos/2d/CCParticleBatchNode.cpp | 80 +++--- cocos/2d/CCParticleBatchNode.h | 8 +- cocos/2d/CCRenderTexture.cpp | 8 +- cocos/2d/CCScene.cpp | 24 +- cocos/2d/CCSprite.cpp | 34 ++- cocos/2d/CCSpriteBatchNode.cpp | 149 ++++------ cocos/2d/CCTMXLayer.cpp | 42 ++- cocos/2d/CCTMXTiledMap.cpp | 8 +- cocos/base/CCVector.h | 55 +++- .../editor-support/cocosbuilder/CCBReader.cpp | 12 +- .../editor-support/cocostudio/CCArmature.cpp | 14 +- .../editor-support/cocostudio/CCBatchNode.cpp | 4 +- cocos/editor-support/cocostudio/CCBone.cpp | 33 +-- 26 files changed, 585 insertions(+), 709 deletions(-) diff --git a/cocos/2d/CCActionInterval.cpp b/cocos/2d/CCActionInterval.cpp index 655922be15..649fa09d51 100644 --- a/cocos/2d/CCActionInterval.cpp +++ b/cocos/2d/CCActionInterval.cpp @@ -2003,31 +2003,28 @@ Animate::~Animate() CC_SAFE_DELETE(_splitTimes); } -bool Animate::initWithAnimation(Animation *pAnimation) +bool Animate::initWithAnimation(Animation* animation) { - CCASSERT( pAnimation!=NULL, "Animate: argument Animation must be non-NULL"); + CCASSERT( animation!=NULL, "Animate: argument Animation must be non-NULL"); - float singleDuration = pAnimation->getDuration(); + float singleDuration = animation->getDuration(); - if ( ActionInterval::initWithDuration(singleDuration * pAnimation->getLoops() ) ) + if ( ActionInterval::initWithDuration(singleDuration * animation->getLoops() ) ) { _nextFrame = 0; - setAnimation(pAnimation); + setAnimation(animation); _origFrame = NULL; _executedLoops = 0; - _splitTimes->reserve(pAnimation->getFrames()->count()); + _splitTimes->reserve(animation->getFrames().count()); float accumUnitsOfTime = 0; - float newUnitOfTimeValue = singleDuration / pAnimation->getTotalDelayUnits(); + float newUnitOfTimeValue = singleDuration / animation->getTotalDelayUnits(); - Array* pFrames = pAnimation->getFrames(); - CCARRAY_VERIFY_TYPE(pFrames, AnimationFrame*); + auto frames = animation->getFrames(); - Object* pObj = NULL; - CCARRAY_FOREACH(pFrames, pObj) + for (auto& frame : frames) { - AnimationFrame* frame = static_cast(pObj); float value = (accumUnitsOfTime * newUnitOfTimeValue) / singleDuration; accumUnitsOfTime += frame->getDelayUnits(); _splitTimes->push_back(value); @@ -2099,15 +2096,15 @@ void Animate::update(float t) t = fmodf(t, 1.0f); } - Array* frames = _animation->getFrames(); - long numberOfFrames = frames->count(); + auto frames = _animation->getFrames(); + long numberOfFrames = frames.count(); SpriteFrame *frameToDisplay = NULL; for( int i=_nextFrame; i < numberOfFrames; i++ ) { float splitTime = _splitTimes->at(i); if( splitTime <= t ) { - AnimationFrame* frame = static_cast(frames->getObjectAtIndex(i)); + AnimationFrame* frame = frames[i]; frameToDisplay = frame->getSpriteFrame(); static_cast(_target)->setDisplayFrame(frameToDisplay); @@ -2127,27 +2124,24 @@ void Animate::update(float t) Animate* Animate::reverse() const { - Array* pOldArray = _animation->getFrames(); - Array* pNewArray = Array::createWithCapacity(pOldArray->count()); + auto oldArray = _animation->getFrames(); + Vector newArray(oldArray.count()); - CCARRAY_VERIFY_TYPE(pOldArray, AnimationFrame*); - - if (pOldArray->count() > 0) + if (oldArray.count() > 0) { - Object* pObj = NULL; - CCARRAY_FOREACH_REVERSE(pOldArray, pObj) + for (auto iter = oldArray.rcbegin(); iter != oldArray.rcend(); ++iter) { - AnimationFrame* pElement = static_cast(pObj); - if (! pElement) + AnimationFrame* animFrame = *iter; + if (!animFrame) { break; } - pNewArray->addObject(pElement->clone()); + newArray.addObject(animFrame->clone()); } } - Animation *newAnim = Animation::create(pNewArray, _animation->getDelayPerUnit(), _animation->getLoops()); + Animation *newAnim = Animation::create(newArray, _animation->getDelayPerUnit(), _animation->getLoops()); newAnim->setRestoreOriginalFrame(_animation->getRestoreOriginalFrame()); return Animate::create(newAnim); } diff --git a/cocos/2d/CCAnimation.cpp b/cocos/2d/CCAnimation.cpp index fad60c6533..8a3df8d0e9 100644 --- a/cocos/2d/CCAnimation.cpp +++ b/cocos/2d/CCAnimation.cpp @@ -32,6 +32,20 @@ THE SOFTWARE. NS_CC_BEGIN +AnimationFrame* AnimationFrame::create(SpriteFrame* spriteFrame, float delayUnits, Dictionary* userInfo) +{ + auto ret = new AnimationFrame(); + if (ret && ret->initWithSpriteFrame(spriteFrame, delayUnits, userInfo)) + { + ret->autorelease(); + } + else + { + CC_SAFE_DELETE(ret); + } + return ret; +} + AnimationFrame::AnimationFrame() : _spriteFrame(NULL) , _delayUnits(0.0f) @@ -80,7 +94,7 @@ Animation* Animation::create(void) return pAnimation; } -Animation* Animation::createWithSpriteFrames(Array *frames, float delay/* = 0.0f*/) +Animation* Animation::createWithSpriteFrames(const Vector& frames, float delay/* = 0.0f*/) { Animation *pAnimation = new Animation(); pAnimation->initWithSpriteFrames(frames, delay); @@ -89,7 +103,7 @@ Animation* Animation::createWithSpriteFrames(Array *frames, float delay/* = 0.0f return pAnimation; } -Animation* Animation::create(Array* arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops /* = 1 */) +Animation* Animation::create(const Vector& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops /* = 1 */) { Animation *pAnimation = new Animation(); pAnimation->initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops); @@ -99,49 +113,36 @@ Animation* Animation::create(Array* arrayOfAnimationFrameNames, float delayPerUn bool Animation::init() { - return initWithSpriteFrames(NULL, 0.0f); + _loops = 1; + _delayPerUnit = 0.0f; + + return true; } -bool Animation::initWithSpriteFrames(Array *pFrames, float delay/* = 0.0f*/) +bool Animation::initWithSpriteFrames(const Vector& frames, float delay/* = 0.0f*/) { - CCARRAY_VERIFY_TYPE(pFrames, SpriteFrame*); - _loops = 1; _delayPerUnit = delay; - Array* pTmpFrames = Array::create(); - setFrames(pTmpFrames); - if (pFrames != NULL) + for (auto& spriteFrame : frames) { - Object* pObj = NULL; - CCARRAY_FOREACH(pFrames, pObj) - { - SpriteFrame* frame = static_cast(pObj); - AnimationFrame *animFrame = new AnimationFrame(); - animFrame->initWithSpriteFrame(frame, 1, NULL); - _frames->addObject(animFrame); - animFrame->release(); - - _totalDelayUnits++; - } + auto animFrame = AnimationFrame::create(spriteFrame, 1, nullptr); + _frames.addObject(animFrame); + _totalDelayUnits++; } return true; } -bool Animation::initWithAnimationFrames(Array* arrayOfAnimationFrames, float delayPerUnit, unsigned int loops) +bool Animation::initWithAnimationFrames(const Vector& arrayOfAnimationFrames, float delayPerUnit, unsigned int loops) { - CCARRAY_VERIFY_TYPE(arrayOfAnimationFrames, AnimationFrame*); - _delayPerUnit = delayPerUnit; _loops = loops; - setFrames(Array::createWithArray(arrayOfAnimationFrames)); + setFrames(arrayOfAnimationFrames); - Object* pObj = NULL; - CCARRAY_FOREACH(_frames, pObj) + for (auto& animFrame : _frames) { - AnimationFrame* animFrame = static_cast(pObj); _totalDelayUnits += animFrame->getDelayUnits(); } return true; @@ -151,7 +152,6 @@ Animation::Animation() : _totalDelayUnits(0.0f) , _delayPerUnit(0.0f) , _duration(0.0f) -, _frames(NULL) , _restoreOriginalFrame(false) , _loops(0) { @@ -161,15 +161,12 @@ Animation::Animation() Animation::~Animation(void) { CCLOGINFO("deallocing Animation: %p", this); - CC_SAFE_RELEASE(_frames); } -void Animation::addSpriteFrame(SpriteFrame *pFrame) +void Animation::addSpriteFrame(SpriteFrame* spriteFrame) { - AnimationFrame *animFrame = new AnimationFrame(); - animFrame->initWithSpriteFrame(pFrame, 1.0f, NULL); - _frames->addObject(animFrame); - animFrame->release(); + AnimationFrame *animFrame = AnimationFrame::create(spriteFrame, 1.0f, nullptr); + _frames.addObject(animFrame); // update duration _totalDelayUnits++; diff --git a/cocos/2d/CCAnimation.h b/cocos/2d/CCAnimation.h index 13cf3b1fdc..2725314004 100644 --- a/cocos/2d/CCAnimation.h +++ b/cocos/2d/CCAnimation.h @@ -32,6 +32,8 @@ THE SOFTWARE. #include "CCDictionary.h" #include "CCGeometry.h" #include "CCSpriteFrame.h" +#include "CCVector.h" + #include NS_CC_BEGIN @@ -56,17 +58,10 @@ class CC_DLL AnimationFrame : public Object, public Clonable { public: /** - * @js ctor + * Creates the animation frame with a spriteframe, number of delay units and a notification user info + * @since 3.0 */ - AnimationFrame(); - /** - * @js NA - * @lua NA - */ - virtual ~AnimationFrame(); - - /** initializes the animation frame with a spriteframe, number of delay units and a notification user info */ - bool initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, Dictionary* userInfo); + static AnimationFrame* create(SpriteFrame* spriteFrame, float delayUnits, Dictionary* userInfo); SpriteFrame* getSpriteFrame() const { return _spriteFrame; }; @@ -100,6 +95,20 @@ public: virtual AnimationFrame *clone() const override; protected: + + /** + * @js ctor + */ + AnimationFrame(); + /** + * @js NA + * @lua NA + */ + virtual ~AnimationFrame(); + + /** initializes the animation frame with a spriteframe, number of delay units and a notification user info */ + bool initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, Dictionary* userInfo); + /** SpriteFrameName to be used */ SpriteFrame* _spriteFrame; @@ -108,6 +117,9 @@ protected: /** A AnimationFrameDisplayedNotification notification will be broadcast when the frame is displayed with this dictionary as UserInfo. If UserInfo is nil, then no notification will be broadcast. */ Dictionary* _userInfo; + +private: + CC_DISALLOW_COPY_AND_ASSIGN(AnimationFrame); }; @@ -135,34 +147,13 @@ public: The frames will be added with one "delay unit". @since v0.99.5 */ - static Animation* createWithSpriteFrames(Array* arrayOfSpriteFrameNames, float delay = 0.0f); + static Animation* createWithSpriteFrames(const Vector& arrayOfSpriteFrameNames, float delay = 0.0f); /* Creates an animation with an array of AnimationFrame, the delay per units in seconds and and how many times it should be executed. @since v2.0 * @js NA */ - static Animation* create(Array *arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops = 1); - /** - * @js ctor - */ - Animation(); - /** - * @js NA - * @lua NA - */ - virtual ~Animation(void); - - bool init(); - - /** Initializes a Animation with frames and a delay between frames - @since v0.99.5 - */ - bool initWithSpriteFrames(Array *pFrames, float delay = 0.0f); - - /** Initializes a Animation with AnimationFrame - @since v2.0 - */ - bool initWithAnimationFrames(Array* arrayOfAnimationFrames, float delayPerUnit, unsigned int loops); + static Animation* create(const Vector& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops = 1); /** Adds a SpriteFrame to a Animation. The frame will be added with one "delay unit". @@ -199,13 +190,11 @@ public: float getDuration() const; /** Gets the array of AnimationFrames */ - Array* getFrames() const { return _frames; }; + const Vector& getFrames() const { return _frames; }; /** Sets the array of AnimationFrames */ - void setFrames(Array* frames) + void setFrames(const Vector& frames) { - CC_SAFE_RETAIN(frames); - CC_SAFE_RELEASE(_frames); _frames = frames; } @@ -225,6 +214,30 @@ public: virtual Animation *clone() const override; protected: + + /** + * @js ctor + */ + Animation(); + /** + * @js NA + * @lua NA + */ + virtual ~Animation(void); + + /** Initializes a Animation */ + bool init(); + + /** Initializes a Animation with frames and a delay between frames + @since v0.99.5 + */ + bool initWithSpriteFrames(const Vector& arrayOfSpriteFrameNames, float delay = 0.0f); + + /** Initializes a Animation with AnimationFrame + @since v2.0 + */ + bool initWithAnimationFrames(const Vector& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops); + /** total Delay units of the Animation. */ float _totalDelayUnits; @@ -235,13 +248,16 @@ protected: float _duration; /** array of AnimationFrames */ - Array* _frames; + Vector _frames; /** whether or not it shall restore the original frame when the animation finishes */ bool _restoreOriginalFrame; /** how many times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ... */ unsigned int _loops; + +private: + CC_DISALLOW_COPY_AND_ASSIGN(Animation); }; // end of sprite_nodes group diff --git a/cocos/2d/CCAnimationCache.cpp b/cocos/2d/CCAnimationCache.cpp index a2c0fa9303..e4cb5dc6b2 100644 --- a/cocos/2d/CCAnimationCache.cpp +++ b/cocos/2d/CCAnimationCache.cpp @@ -107,8 +107,7 @@ void AnimationCache::parseVersion1(Dictionary* animations) continue; } - Array* frames = Array::createWithCapacity(frameNames->count()); - frames->retain(); + Vector frames(frameNames->count()); Object* pObj = NULL; CCARRAY_FOREACH(frameNames, pObj) @@ -122,24 +121,21 @@ void AnimationCache::parseVersion1(Dictionary* animations) continue; } - AnimationFrame* animFrame = new AnimationFrame(); - animFrame->initWithSpriteFrame(spriteFrame, 1, NULL); - frames->addObject(animFrame); - animFrame->release(); + AnimationFrame* animFrame = AnimationFrame::create(spriteFrame, 1, nullptr); + frames.addObject(animFrame); } - if ( frames->count() == 0 ) { + if ( frames.count() == 0 ) { CCLOG("cocos2d: AnimationCache: None of the frames for animation '%s' were found in the SpriteFrameCache. Animation is not being added to the Animation Cache.", element->getStrKey()); continue; - } else if ( frames->count() != frameNames->count() ) { + } else if ( frames.count() != frameNames->count() ) { CCLOG("cocos2d: AnimationCache: An animation in your dictionary refers to a frame which is not in the SpriteFrameCache. Some or all of the frames for the animation '%s' may be missing.", element->getStrKey()); } animation = Animation::create(frames, delay, 1); AnimationCache::getInstance()->addAnimation(animation, element->getStrKey()); - frames->release(); - } + } } void AnimationCache::parseVersion2(Dictionary* animations) @@ -163,8 +159,7 @@ void AnimationCache::parseVersion2(Dictionary* animations) } // Array of AnimationFrames - Array* array = Array::createWithCapacity(frameArray->count()); - array->retain(); + Vector array(frameArray->count()); Object* pObj = NULL; CCARRAY_FOREACH(frameArray, pObj) @@ -183,22 +178,17 @@ void AnimationCache::parseVersion2(Dictionary* animations) float delayUnits = entry->valueForKey("delayUnits")->floatValue(); Dictionary* userInfo = (Dictionary*)entry->objectForKey("notification"); - AnimationFrame *animFrame = new AnimationFrame(); - animFrame->initWithSpriteFrame(spriteFrame, delayUnits, userInfo); + AnimationFrame *animFrame = AnimationFrame::create(spriteFrame, delayUnits, userInfo); - array->addObject(animFrame); - animFrame->release(); + array.addObject(animFrame); } float delayPerUnit = animationDict->valueForKey("delayPerUnit")->floatValue(); - Animation *animation = new Animation(); - animation->initWithAnimationFrames(array, delayPerUnit, 0 != loops->length() ? loops->intValue() : 1); - array->release(); + Animation *animation = Animation::create(array, delayPerUnit, 0 != loops->length() ? loops->intValue() : 1); animation->setRestoreOriginalFrame(restoreOriginalFrame); AnimationCache::getInstance()->addAnimation(animation, name); - animation->release(); } } diff --git a/cocos/2d/CCClippingNode.cpp b/cocos/2d/CCClippingNode.cpp index d51fcc5002..a940cc1b6a 100644 --- a/cocos/2d/CCClippingNode.cpp +++ b/cocos/2d/CCClippingNode.cpp @@ -39,13 +39,10 @@ static GLint g_sStencilBits = -1; static void setProgram(Node *n, GLProgram *p) { n->setShaderProgram(p); - if (!n->getChildren()) return; - Object* pObj = NULL; - CCARRAY_FOREACH(n->getChildren(), pObj) - { - setProgram(static_cast(pObj), p); - } + n->getChildren().makeObjectsPerformCallback([p](Node* child){ + setProgram(child, p); + }); } ClippingNode::ClippingNode() diff --git a/cocos/2d/CCEventDispatcher.cpp b/cocos/2d/CCEventDispatcher.cpp index 3387e8b3bf..ade48d38f3 100644 --- a/cocos/2d/CCEventDispatcher.cpp +++ b/cocos/2d/CCEventDispatcher.cpp @@ -189,18 +189,17 @@ EventDispatcher::~EventDispatcher() void EventDispatcher::visitTarget(Node* node) { - int i = 0; - Array* children = node->getChildren(); - int childrenCount = children ? children->count() : 0; + long i = 0; + auto children = node->getChildren(); + long childrenCount = children.count(); if(childrenCount > 0) { - Node* child = nullptr; // visit children zOrder < 0 for( ; i < childrenCount; i++ ) { - child = static_cast( children->getObjectAtIndex(i) ); + child = children[i]; if ( child && child->getZOrder() < 0 ) visitTarget(child); @@ -212,7 +211,7 @@ void EventDispatcher::visitTarget(Node* node) for( ; i < childrenCount; i++ ) { - child = static_cast( children->getObjectAtIndex(i) ); + child = children[i]; if (child) visitTarget(child); } diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 07b652a897..7f9a4e2fa1 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -248,20 +248,17 @@ void Label::alignText() LabelTextFormatter::alignText(this); - int strLen = cc_wcslen(_currentUTF16String); - if (_children && _children->count() != 0) - { - for (auto child: *_children) + int strLen = cc_wcslen(_currentUTF16String); + + _children.makeObjectsPerformCallback([this, &strLen](Node* child){ + if (child) { - Node* pNode = static_cast( child ); - if (pNode) - { - int tag = pNode->getTag(); - if(tag < 0 || tag >= strLen) - SpriteBatchNode::removeChild(pNode, true); - } + int tag = child->getTag(); + if(tag < 0 || tag >= strLen) + SpriteBatchNode::removeChild(child, true); } - } + }); + _reusedLetter->setBatchNode(nullptr); int vaildIndex = 0; @@ -594,21 +591,18 @@ bool Label::isOpacityModifyRGB() const void Label::setOpacityModifyRGB(bool isOpacityModifyRGB) { _isOpacityModifyRGB = isOpacityModifyRGB; - if (_children && _children->count() != 0) - { - for (auto child: *_children) + + _children.makeObjectsPerformCallback([this](Node* child){ + if (child) { - Node* pNode = static_cast( child ); - if (pNode) + RGBAProtocol *pRGBAProtocol = dynamic_cast(child); + if (pRGBAProtocol) { - RGBAProtocol *pRGBAProtocol = dynamic_cast(pNode); - if (pRGBAProtocol) - { - pRGBAProtocol->setOpacityModifyRGB(_isOpacityModifyRGB); - } + pRGBAProtocol->setOpacityModifyRGB(_isOpacityModifyRGB); } } - } + }); + _reusedLetter->setOpacityModifyRGB(true); } @@ -640,13 +634,13 @@ void Label::updateDisplayedOpacity(GLubyte parentOpacity) { _displayedOpacity = _realOpacity * parentOpacity/255.0; - for (auto child: *_children) - { + _children.makeObjectsPerformCallback([this](Node* child){ Sprite *item = static_cast( child ); item->updateDisplayedOpacity(_displayedOpacity); - } + }); + V3F_C4B_T2F_Quad *quads = _textureAtlas->getQuads(); - int count = _textureAtlas->getTotalQuads(); + long count = _textureAtlas->getTotalQuads(); Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity ); if (_isOpacityModifyRGB) { @@ -706,14 +700,13 @@ void Label::updateDisplayedColor(const Color3B& parentColor) _displayedColor.g = _realColor.g * parentColor.g/255.0; _displayedColor.b = _realColor.b * parentColor.b/255.0; - for (auto child: *_children) - { + _children.makeObjectsPerformCallback([this](Node* child){ Sprite *item = static_cast( child ); item->updateDisplayedColor(_displayedColor); - } + }); V3F_C4B_T2F_Quad *quads = _textureAtlas->getQuads(); - int count = _textureAtlas->getTotalQuads(); + long count = _textureAtlas->getTotalQuads(); Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity ); // special opacity for premultiplied textures diff --git a/cocos/2d/CCLabelBMFont.cpp b/cocos/2d/CCLabelBMFont.cpp index 88fb154770..7129d3f58e 100644 --- a/cocos/2d/CCLabelBMFont.cpp +++ b/cocos/2d/CCLabelBMFont.cpp @@ -746,18 +746,10 @@ void LabelBMFont::setString(unsigned short *newString, bool needUpdateLabel) CC_SAFE_DELETE_ARRAY(tmp); } - if (_children && _children->count() != 0) - { - Object* child; - CCARRAY_FOREACH(_children, child) - { - Node* pNode = static_cast( child ); - if (pNode) - { - pNode->setVisible(false); - } - } - } + _children.makeObjectsPerformCallback([](Node* child){ + child->setVisible(false); + }); + this->createFontChars(); if (needUpdateLabel) { @@ -830,22 +822,16 @@ void LabelBMFont::setOpacity(GLubyte opacity) void LabelBMFont::setOpacityModifyRGB(bool var) { _isOpacityModifyRGB = var; - if (_children && _children->count() != 0) - { - Object* child; - CCARRAY_FOREACH(_children, child) + _children.makeObjectsPerformCallback([this](Node* child){ + if (child) { - Node* pNode = static_cast( child ); - if (pNode) + RGBAProtocol *pRGBAProtocol = dynamic_cast(child); + if (pRGBAProtocol) { - RGBAProtocol *pRGBAProtocol = dynamic_cast(pNode); - if (pRGBAProtocol) - { - pRGBAProtocol->setOpacityModifyRGB(_isOpacityModifyRGB); - } + pRGBAProtocol->setOpacityModifyRGB(_isOpacityModifyRGB); } } - } + }); } bool LabelBMFont::isOpacityModifyRGB() const { @@ -854,28 +840,24 @@ bool LabelBMFont::isOpacityModifyRGB() const void LabelBMFont::updateDisplayedOpacity(GLubyte parentOpacity) { - _displayedOpacity = _realOpacity * parentOpacity/255.0; + _displayedOpacity = _realOpacity * parentOpacity/255.0f; - Object* pObj; - CCARRAY_FOREACH(_children, pObj) - { - Sprite *item = static_cast( pObj ); + _children.makeObjectsPerformCallback([this](Node* child){ + Sprite *item = static_cast( child ); item->updateDisplayedOpacity(_displayedOpacity); - } + }); } void LabelBMFont::updateDisplayedColor(const Color3B& parentColor) { - _displayedColor.r = _realColor.r * parentColor.r/255.0; - _displayedColor.g = _realColor.g * parentColor.g/255.0; - _displayedColor.b = _realColor.b * parentColor.b/255.0; + _displayedColor.r = _realColor.r * parentColor.r/255.0f; + _displayedColor.g = _realColor.g * parentColor.g/255.0f; + _displayedColor.b = _realColor.b * parentColor.b/255.0f; - Object* pObj; - CCARRAY_FOREACH(_children, pObj) - { - Sprite *item = static_cast( pObj ); + _children.makeObjectsPerformCallback([this](Node* child){ + Sprite *item = static_cast( child ); item->updateDisplayedColor(_displayedColor); - } + }); } bool LabelBMFont::isCascadeColorEnabled() const @@ -917,7 +899,7 @@ void LabelBMFont::updateLabel() { // Step 1: Make multiline vector str_whole = cc_utf16_vec_from_utf16_str(_string); - unsigned int stringLength = str_whole.size(); + size_t stringLength = str_whole.size(); vector multiline_string; multiline_string.reserve( stringLength ); vector last_word; @@ -928,8 +910,8 @@ void LabelBMFont::updateLabel() float startOfLine = -1, startOfWord = -1; int skip = 0; - Array* children = getChildren(); - for (int j = 0; j < children->count(); j++) + auto children = getChildren(); + for (int j = 0; j < children.count(); j++) { Sprite* characterSprite; unsigned int justSkipped = 0; @@ -1068,7 +1050,7 @@ void LabelBMFont::updateLabel() multiline_string.insert(multiline_string.end(), last_word.begin(), last_word.end()); - int size = multiline_string.size(); + size_t size = multiline_string.size(); unsigned short* str_new = new unsigned short[size + 1]; for (int j = 0; j < size; ++j) @@ -1096,7 +1078,7 @@ void LabelBMFont::updateLabel() if (_string[ctr] == '\n' || _string[ctr] == 0) { float lineWidth = 0.0f; - unsigned int line_length = last_line.size(); + size_t line_length = last_line.size(); // if last line is empty we must just increase lineNumber and work with next line if (line_length == 0) { diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index e7fe6f5342..2d9d263a15 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -499,15 +499,13 @@ void LayerRGBA::updateDisplayedOpacity(GLubyte parentOpacity) if (_cascadeOpacityEnabled) { - Object *obj = NULL; - CCARRAY_FOREACH(_children, obj) - { + _children.makeObjectsPerformCallback([this](Node* obj){ RGBAProtocol *item = dynamic_cast(obj); if (item) { item->updateDisplayedOpacity(_displayedOpacity); } - } + }); } } @@ -519,15 +517,13 @@ void LayerRGBA::updateDisplayedColor(const Color3B& parentColor) if (_cascadeColorEnabled) { - Object *obj = NULL; - CCARRAY_FOREACH(_children, obj) - { + _children.makeObjectsPerformCallback([this](Node* obj){ RGBAProtocol *item = dynamic_cast(obj); if (item) { item->updateDisplayedColor(_displayedColor); } - } + }); } } diff --git a/cocos/2d/CCMenu.cpp b/cocos/2d/CCMenu.cpp index 6fef9d78a6..775ba5aaac 100644 --- a/cocos/2d/CCMenu.cpp +++ b/cocos/2d/CCMenu.cpp @@ -81,36 +81,36 @@ Menu * Menu::create(MenuItem* item, ...) return pRet; } -Menu* Menu::createWithArray(Array* pArrayOfItems) +Menu* Menu::createWithArray(const Vector& arrayOfItems) { - Menu *pRet = new Menu(); - if (pRet && pRet->initWithArray(pArrayOfItems)) + auto ret = new Menu(); + if (ret && ret->initWithArray(arrayOfItems)) { - pRet->autorelease(); + ret->autorelease(); } else { - CC_SAFE_DELETE(pRet); + CC_SAFE_DELETE(ret); } - return pRet; + return ret; } Menu* Menu::createWithItems(MenuItem* item, va_list args) { - Array* pArray = NULL; + Vector items; if( item ) { - pArray = Array::create(item, NULL); + items.addObject(item); MenuItem *i = va_arg(args, MenuItem*); while(i) { - pArray->addObject(i); + items.addObject(i); i = va_arg(args, MenuItem*); } } - return Menu::createWithArray(pArray); + return Menu::createWithArray(items); } Menu* Menu::createWithItem(MenuItem* item) @@ -120,10 +120,10 @@ Menu* Menu::createWithItem(MenuItem* item) bool Menu::init() { - return initWithArray(NULL); + return initWithArray(Vector()); } -bool Menu::initWithArray(Array* pArrayOfItems) +bool Menu::initWithArray(const Vector& arrayOfItems) { if (Layer::init()) { @@ -137,16 +137,12 @@ bool Menu::initWithArray(Array* pArrayOfItems) setPosition(Point(s.width/2, s.height/2)); - if (pArrayOfItems != NULL) + int z=0; + + for (auto& item : arrayOfItems) { - int z=0; - Object* pObj = NULL; - CCARRAY_FOREACH(pArrayOfItems, pObj) - { - MenuItem* item = static_cast(pObj); - this->addChild(item, z); - z++; - } + this->addChild(item, z); + z++; } _selectedItem = NULL; @@ -306,33 +302,23 @@ void Menu::alignItemsVertically() void Menu::alignItemsVerticallyWithPadding(float padding) { float height = -padding; - if (_children && _children->count() > 0) - { - Object* pObject = NULL; - CCARRAY_FOREACH(_children, pObject) + + _children.makeObjectsPerformCallback([&](Node* child){ + if (child) { - Node* child = dynamic_cast(pObject); - if (child) - { - height += child->getContentSize().height * child->getScaleY() + padding; - } + height += child->getContentSize().height * child->getScaleY() + padding; } - } + }); float y = height / 2.0f; - if (_children && _children->count() > 0) - { - Object* pObject = NULL; - CCARRAY_FOREACH(_children, pObject) + + _children.makeObjectsPerformCallback([&](Node* child){ + if (child) { - Node* child = dynamic_cast(pObject); - if (child) - { - child->setPosition(Point(0, y - child->getContentSize().height * child->getScaleY() / 2.0f)); - y -= child->getContentSize().height * child->getScaleY() + padding; - } + child->setPosition(Point(0, y - child->getContentSize().height * child->getScaleY() / 2.0f)); + y -= child->getContentSize().height * child->getScaleY() + padding; } - } + }); } void Menu::alignItemsHorizontally(void) @@ -342,35 +328,23 @@ void Menu::alignItemsHorizontally(void) void Menu::alignItemsHorizontallyWithPadding(float padding) { - float width = -padding; - if (_children && _children->count() > 0) - { - Object* pObject = NULL; - CCARRAY_FOREACH(_children, pObject) + _children.makeObjectsPerformCallback([&](Node* child){ + if (child) { - Node* child = dynamic_cast(pObject); - if (child) - { - width += child->getContentSize().width * child->getScaleX() + padding; - } + width += child->getContentSize().width * child->getScaleX() + padding; } - } + }); float x = -width / 2.0f; - if (_children && _children->count() > 0) - { - Object* pObject = NULL; - CCARRAY_FOREACH(_children, pObject) + + _children.makeObjectsPerformCallback([&](Node* child){ + if (child) { - Node* child = dynamic_cast(pObject); - if (child) - { - child->setPosition(Point(x + child->getContentSize().width * child->getScaleX() / 2.0f, 0)); - x += child->getContentSize().width * child->getScaleX() + padding; - } + child->setPosition(Point(x + child->getContentSize().width * child->getScaleX() / 2.0f, 0)); + x += child->getContentSize().width * child->getScaleX() + padding; } - } + }); } void Menu::alignItemsInColumns(int columns, ...) @@ -405,35 +379,29 @@ void Menu::alignItemsInColumnsWithArray(Array* rowsArray) unsigned int columnsOccupied = 0; unsigned int rowColumns; - if (_children && _children->count() > 0) - { - Object* pObject = NULL; - CCARRAY_FOREACH(_children, pObject) + _children.makeObjectsPerformCallback([&](Node* child){ + if (child) { - Node* child = dynamic_cast(pObject); - if (child) + CCASSERT(row < rows.size(), ""); + + rowColumns = rows[row]; + // can not have zero columns on a row + CCASSERT(rowColumns, ""); + + float tmp = child->getContentSize().height; + rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp); + + ++columnsOccupied; + if (columnsOccupied >= rowColumns) { - CCASSERT(row < rows.size(), ""); - - rowColumns = rows[row]; - // can not have zero columns on a row - CCASSERT(rowColumns, ""); - - float tmp = child->getContentSize().height; - rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp); - - ++columnsOccupied; - if (columnsOccupied >= rowColumns) - { - height += rowHeight + 5; - - columnsOccupied = 0; - rowHeight = 0; - ++row; - } + height += rowHeight + 5; + + columnsOccupied = 0; + rowHeight = 0; + ++row; } } - } + }); // check if too many rows/columns for available menu items CCASSERT(! columnsOccupied, ""); @@ -447,12 +415,9 @@ void Menu::alignItemsInColumnsWithArray(Array* rowsArray) float x = 0.0; float y = (float)(height / 2); - if (_children && _children->count() > 0) - { - Object* pObject = NULL; - CCARRAY_FOREACH(_children, pObject) + _children.makeObjectsPerformCallback([&](Node* child){ + if (child) { - Node* child = dynamic_cast(pObject); if (child) { if (rowColumns == 0) @@ -482,7 +447,7 @@ void Menu::alignItemsInColumnsWithArray(Array* rowsArray) } } } - } + }); } void Menu::alignItemsInRows(int rows, ...) @@ -520,42 +485,36 @@ void Menu::alignItemsInRowsWithArray(Array* columnArray) unsigned int rowsOccupied = 0; unsigned int columnRows; - if (_children && _children->count() > 0) - { - Object* pObject = NULL; - CCARRAY_FOREACH(_children, pObject) + _children.makeObjectsPerformCallback([&](Node* child){ + if (child) { - Node* child = dynamic_cast(pObject); - if (child) + // check if too many menu items for the amount of rows/columns + CCASSERT(column < columns.size(), ""); + + columnRows = columns[column]; + // can't have zero rows on a column + CCASSERT(columnRows, ""); + + // columnWidth = fmaxf(columnWidth, [item contentSize].width); + float tmp = child->getContentSize().width; + columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp); + + columnHeight += (int)(child->getContentSize().height + 5); + ++rowsOccupied; + + if (rowsOccupied >= columnRows) { - // check if too many menu items for the amount of rows/columns - CCASSERT(column < columns.size(), ""); + columnWidths.push_back(columnWidth); + columnHeights.push_back(columnHeight); + width += columnWidth + 10; - columnRows = columns[column]; - // can't have zero rows on a column - CCASSERT(columnRows, ""); - - // columnWidth = fmaxf(columnWidth, [item contentSize].width); - float tmp = child->getContentSize().width; - columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp); - - columnHeight += (int)(child->getContentSize().height + 5); - ++rowsOccupied; - - if (rowsOccupied >= columnRows) - { - columnWidths.push_back(columnWidth); - columnHeights.push_back(columnHeight); - width += columnWidth + 10; - - rowsOccupied = 0; - columnWidth = 0; - columnHeight = -5; - ++column; - } + rowsOccupied = 0; + columnWidth = 0; + columnHeight = -5; + ++column; } } - } + }); // check if too many rows/columns for available menu items. CCASSERT(! rowsOccupied, ""); @@ -568,59 +527,52 @@ void Menu::alignItemsInRowsWithArray(Array* columnArray) float x = (float)(-width / 2); float y = 0.0; - if (_children && _children->count() > 0) - { - Object* pObject = NULL; - CCARRAY_FOREACH(_children, pObject) + _children.makeObjectsPerformCallback([&](Node* child){ + if (child) { - Node* child = dynamic_cast(pObject); - if (child) + if (columnRows == 0) { - if (columnRows == 0) - { - columnRows = columns[column]; - y = (float) columnHeights[column]; - } + columnRows = columns[column]; + y = (float) columnHeights[column]; + } - // columnWidth = fmaxf(columnWidth, [item contentSize].width); - float tmp = child->getContentSize().width; - columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp); + // columnWidth = fmaxf(columnWidth, [item contentSize].width); + float tmp = child->getContentSize().width; + columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp); - child->setPosition(Point(x + columnWidths[column] / 2, - y - winSize.height / 2)); + child->setPosition(Point(x + columnWidths[column] / 2, + y - winSize.height / 2)); - y -= child->getContentSize().height + 10; - ++rowsOccupied; + y -= child->getContentSize().height + 10; + ++rowsOccupied; - if (rowsOccupied >= columnRows) - { - x += columnWidth + 5; - rowsOccupied = 0; - columnRows = 0; - columnWidth = 0; - ++column; - } + if (rowsOccupied >= columnRows) + { + x += columnWidth + 5; + rowsOccupied = 0; + columnRows = 0; + columnWidth = 0; + ++column; } } - } + }); } MenuItem* Menu::itemForTouch(Touch *touch) { Point touchLocation = touch->getLocation(); - if (_children && _children->count() > 0) + if (_children.count() > 0) { - Object* pObject = NULL; - CCARRAY_FOREACH_REVERSE(_children, pObject) + for (auto iter = _children.rcbegin(); iter != _children.rcend(); ++iter) { - MenuItem* child = dynamic_cast(pObject); + MenuItem* child = dynamic_cast(*iter); if (child && child->isVisible() && child->isEnabled()) { Point local = child->convertToNodeSpace(touchLocation); Rect r = child->rect(); r.origin = Point::ZERO; - + if (r.containsPoint(local)) { return child; diff --git a/cocos/2d/CCMenu.h b/cocos/2d/CCMenu.h index 04aeb831a4..ad285fbadb 100644 --- a/cocos/2d/CCMenu.h +++ b/cocos/2d/CCMenu.h @@ -27,7 +27,7 @@ THE SOFTWARE. #include "CCMenuItem.h" #include "CCLayer.h" - +#include "CCVector.h" #include "CCEventTouch.h" NS_CC_BEGIN @@ -63,7 +63,7 @@ public: static Menu* create(MenuItem* item, ...) CC_REQUIRES_NULL_TERMINATION; /** creates a Menu with a Array of MenuItem objects */ - static Menu* createWithArray(Array* pArrayOfItems); + static Menu* createWithArray(const Vector& arrayOfItems); /** creates a Menu with it's item, then use addChild() to add * other items. It is used for script, it can't init with undetermined @@ -129,7 +129,7 @@ protected: bool init(); /** initializes a Menu with a NSArray of MenuItem objects */ - bool initWithArray(Array* pArrayOfItems); + bool initWithArray(const Vector& arrayOfItems); /** whether or not the menu will receive events */ bool _enabled; diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index bdff745edf..641a743ed4 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -114,7 +114,6 @@ Node::Node(void) // lazy alloc , _grid(NULL) , _ZOrder(0) -, _children(NULL) , _parent(NULL) // "whole screen" objects. like Scenes and Layers, should set _ignoreAnchorPointForPosition to true , _tag(Node::INVALID_TAG) @@ -169,22 +168,14 @@ Node::~Node() CC_SAFE_RELEASE(_shaderProgram); CC_SAFE_RELEASE(_userObject); - if(_children && _children->count() > 0) + for (auto& child : _children) { - Object* child; - CCARRAY_FOREACH(_children, child) + if (child) { - Node* node = static_cast(child); - if (node) - { - node->_parent = NULL; - } + child->_parent = NULL; } } - // children - CC_SAFE_RELEASE(_children); - removeAllComponents(); CC_SAFE_DELETE(_componentContainer); @@ -406,7 +397,7 @@ void Node::setPositionY(float y) long Node::getChildrenCount() const { - return _children ? _children->count() : 0; + return _children.count(); } /// camera getter: lazy alloc @@ -584,7 +575,9 @@ void Node::cleanup() } // timers - arrayMakeObjectsPerformSelector(_children, cleanup, Node*); + _children.makeObjectsPerformCallback([](Node* child){ + child->cleanup(); + }); } @@ -593,28 +586,16 @@ const char* Node::description() const return String::createWithFormat("", _tag)->getCString(); } -// lazy allocs -void Node::childrenAlloc(void) -{ - _children = Array::createWithCapacity(4); - _children->retain(); -} - Node* Node::getChildByTag(int aTag) { CCASSERT( aTag != Node::INVALID_TAG, "Invalid tag"); - if(_children && _children->count() > 0) + for (auto& child : _children) { - Object* child; - CCARRAY_FOREACH(_children, child) - { - Node* pNode = static_cast(child); - if(pNode && pNode->_tag == aTag) - return pNode; - } + if(child && child->_tag == aTag) + return child; } - return NULL; + return nullptr; } /* "add" logic MUST only be on this method @@ -626,11 +607,6 @@ void Node::addChild(Node *child, int zOrder, int tag) CCASSERT( child != NULL, "Argument must be non-nil"); CCASSERT( child->_parent == NULL, "child already added. It can't be added again"); - if( ! _children ) - { - this->childrenAlloc(); - } - this->insertChild(child, zOrder); #ifdef CC_USE_PHYSICS @@ -691,12 +667,12 @@ void Node::removeFromParentAndCleanup(bool cleanup) void Node::removeChild(Node* child, bool cleanup /* = true */) { // explicit nil handling - if (_children == NULL) + if (_children.count() == 0) { return; } - long index = _children->getIndexOfObject(child); + long index = _children.getIndexOfObject(child); if( index != CC_INVALID_INDEX ) this->detachChild( child, index, cleanup ); } @@ -725,33 +701,31 @@ void Node::removeAllChildren() void Node::removeAllChildrenWithCleanup(bool cleanup) { // not using detachChild improves speed here - if ( _children && _children->count() > 0 ) + if ( _children.count() > 0 ) { - Object* child; - CCARRAY_FOREACH(_children, child) + for (auto& child : _children) { - Node* pNode = static_cast(child); - if (pNode) + if (child) { // IMPORTANT: // -1st do onExit // -2nd cleanup if(_running) { - pNode->onExitTransitionDidStart(); - pNode->onExit(); + child->onExitTransitionDidStart(); + child->onExit(); } if (cleanup) { - pNode->cleanup(); + child->cleanup(); } // set parent nil at the end - pNode->setParent(NULL); + child->setParent(nullptr); } } - _children->removeAllObjects(); + _children.removeAllObjects(); } } @@ -783,9 +757,9 @@ void Node::detachChild(Node *child, long childIndex, bool doCleanup) } // set parent nil at the end - child->setParent(NULL); + child->setParent(nullptr); - _children->removeObjectAtIndex(childIndex); + _children.removeObjectAtIndex(childIndex); } @@ -793,7 +767,7 @@ void Node::detachChild(Node *child, long childIndex, bool doCleanup) void Node::insertChild(Node* child, int z) { _reorderChildDirty = true; - _children->addObject(child); + _children.addObject(child); child->_setZOrder(z); } @@ -810,24 +784,24 @@ void Node::sortAllChildren() #if 0 if (_reorderChildDirty) { - int i,j,length = _children->count(); + int i,j,length = _children.count(); // insertion sort for(i=1; i( _children->getObjectAtIndex(i) ); - auto tempJ = static_cast( _children->getObjectAtIndex(j) ); + auto tempI = static_cast( _children.getObjectAtIndex(i) ); + auto tempJ = static_cast( _children.getObjectAtIndex(j) ); //continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller while(j>=0 && ( tempI->_ZOrder < tempJ->_ZOrder || ( tempI->_ZOrder == tempJ->_ZOrder && tempI->_orderOfArrival < tempJ->_orderOfArrival ) ) ) { - _children->fastSetObject( tempJ, j+1 ); + _children.fastSetObject( tempJ, j+1 ); j = j-1; if(j>=0) - tempJ = static_cast( _children->getObjectAtIndex(j) ); + tempJ = static_cast( _children.getObjectAtIndex(j) ); } - _children->fastSetObject(tempI, j+1); + _children.fastSetObject(tempI, j+1); } //don't need to check children recursively, that's done in visit of each child @@ -836,7 +810,7 @@ void Node::sortAllChildren() } #else if( _reorderChildDirty ) { - std::sort( std::begin(*_children), std::end(*_children), nodeComparisonLess ); + std::sort( std::begin(_children), std::end(_children), nodeComparisonLess ); _reorderChildDirty = false; } #endif @@ -869,13 +843,13 @@ void Node::visit() this->transform(); int i = 0; - if(_children && _children->count() > 0) + if(_children.count() > 0) { sortAllChildren(); // draw children zOrder < 0 - for( ; i < _children->count(); i++ ) + for( ; i < _children.count(); i++ ) { - auto node = static_cast( _children->getObjectAtIndex(i) ); + auto node = _children[i]; if ( node && node->_ZOrder < 0 ) node->visit(); @@ -885,9 +859,9 @@ void Node::visit() // self draw this->draw(); - for( ; i < _children->count(); i++ ) + for( ; i < _children.count(); i++ ) { - auto node = static_cast( _children->getObjectAtIndex(i) ); + auto node = _children[i]; if (node) node->visit(); } @@ -955,7 +929,9 @@ void Node::onEnter() { _isTransitionFinished = false; - arrayMakeObjectsPerformSelector(_children, onEnter, Node*); + _children.makeObjectsPerformCallback([](Node* child){ + child->onEnter(); + }); this->resume(); @@ -974,8 +950,10 @@ void Node::onEnterTransitionDidFinish() { _isTransitionFinished = true; - arrayMakeObjectsPerformSelector(_children, onEnterTransitionDidFinish, Node*); - + _children.makeObjectsPerformCallback([](Node* child){ + child->onEnterTransitionDidFinish(); + }); + if (_scriptType != kScriptTypeNone) { int action = kNodeOnEnterTransitionDidFinish; @@ -987,7 +965,10 @@ void Node::onEnterTransitionDidFinish() void Node::onExitTransitionDidStart() { - arrayMakeObjectsPerformSelector(_children, onExitTransitionDidStart, Node*); + _children.makeObjectsPerformCallback([](Node* child){ + child->onExitTransitionDidStart(); + }); + if (_scriptType != kScriptTypeNone) { int action = kNodeOnExitTransitionDidStart; @@ -1010,7 +991,9 @@ void Node::onExit() ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent); } - arrayMakeObjectsPerformSelector(_children, onExit, Node*); + _children.makeObjectsPerformCallback([](Node* child){ + child->onExit(); + }); } void Node::setEventDispatcher(EventDispatcher* dispatcher) @@ -1063,7 +1046,7 @@ Action * Node::getActionByTag(int tag) return _actionManager->getActionByTag(tag, this); } -unsigned int Node::getNumberOfRunningActions() const +long Node::getNumberOfRunningActions() const { return _actionManager->getNumberOfRunningActionsInTarget(this); } @@ -1358,7 +1341,9 @@ bool Node::updatePhysicsTransform() void Node::updateTransform() { // Recursively iterate over children - arrayMakeObjectsPerformSelector(_children, updateTransform, Node*); + _children.makeObjectsPerformCallback([](Node* child){ + child->updateTransform(); + }); } Component* Node::getComponent(const char *pName) @@ -1467,15 +1452,13 @@ void NodeRGBA::updateDisplayedOpacity(GLubyte parentOpacity) if (_cascadeOpacityEnabled) { - Object* pObj; - CCARRAY_FOREACH(_children, pObj) - { - RGBAProtocol* item = dynamic_cast(pObj); + _children.makeObjectsPerformCallback([this](Node* child){ + RGBAProtocol* item = dynamic_cast(child); if (item) { item->updateDisplayedOpacity(_displayedOpacity); } - } + }); } } @@ -1524,15 +1507,13 @@ void NodeRGBA::updateDisplayedColor(const Color3B& parentColor) if (_cascadeColorEnabled) { - Object *obj = NULL; - CCARRAY_FOREACH(_children, obj) - { - RGBAProtocol *item = dynamic_cast(obj); + _children.makeObjectsPerformCallback([this](Node* child){ + RGBAProtocol *item = dynamic_cast(child); if (item) { item->updateDisplayedColor(_displayedColor); } - } + }); } } diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 617244a40b..37d28b0555 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -38,8 +38,7 @@ #include "CCScriptSupport.h" #include "CCProtocols.h" #include "CCEventDispatcher.h" - -#include +#include "CCVector.h" NS_CC_BEGIN @@ -614,10 +613,10 @@ public: * * @return An array of children */ - virtual Array* getChildren() { return _children; } - virtual const Array *getChildren() const { return _children; } - - /** + virtual Vector& getChildren() { return _children; } + virtual const Vector& getChildren() const { return _children; } + + /** * Get the amount of children. * * @return The amount of children. @@ -1043,7 +1042,7 @@ public: * * @return The number of actions that are running plus the ones that are schedule to run */ - unsigned int getNumberOfRunningActions() const; + long getNumberOfRunningActions() const; /** @deprecated Use getNumberOfRunningActions() instead */ CC_DEPRECATED_ATTRIBUTE unsigned int numberOfRunningActions() const { return getNumberOfRunningActions(); }; @@ -1395,10 +1394,7 @@ protected: Node(); virtual ~Node(); virtual bool init(); - - /// lazy allocs - void childrenAlloc(void); - + /// helper that reorder a child void insertChild(Node* child, int z); @@ -1440,8 +1436,8 @@ protected: GridBase *_grid; ///< a grid int _ZOrder; ///< z-order value that affects the draw order - - Array *_children; ///< array of children nodes + + Vector _children; ///< array of children nodes Node *_parent; ///< weak reference to parent node int _tag; ///< a tag. Can be any number you assigned just to identify this node diff --git a/cocos/2d/CCParticleBatchNode.cpp b/cocos/2d/CCParticleBatchNode.cpp index a6d1fff5cb..d396f29024 100644 --- a/cocos/2d/CCParticleBatchNode.cpp +++ b/cocos/2d/CCParticleBatchNode.cpp @@ -96,9 +96,11 @@ bool ParticleBatchNode::initWithTexture(Texture2D *tex, int capacity) _textureAtlas->initWithTexture(tex, capacity); // no lazy alloc in this node - _children = new Array(); - _children->initWithCapacity(capacity); - + if (_children.count() == 0) + { + _children.init(capacity); + } + _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); @@ -171,7 +173,7 @@ void ParticleBatchNode::addChild(Node * aChild, int zOrder, int tag) ParticleSystem* child = static_cast(aChild); CCASSERT( child->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCParticleSystem is not using the same texture id"); // If this is the 1st children, then copy blending function - if( _children->count() == 0 ) + if( _children.count() == 0 ) { setBlendFunc(child->getBlendFunc()); } @@ -186,9 +188,8 @@ void ParticleBatchNode::addChild(Node * aChild, int zOrder, int tag) if (pos != 0) { - ParticleSystem* p = (ParticleSystem*)_children->getObjectAtIndex(pos-1); + ParticleSystem* p = static_cast(_children[pos-1]); atlasIndex = p->getAtlasIndex() + p->getTotalParticles(); - } else { @@ -205,21 +206,20 @@ void ParticleBatchNode::addChild(Node * aChild, int zOrder, int tag) // XXX research whether lazy sorting + freeing current quads and calloc a new block with size of capacity would be faster // XXX or possibly using vertexZ for reordering, that would be fastest // this helper is almost equivalent to Node's addChild, but doesn't make use of the lazy sorting -unsigned int ParticleBatchNode::addChildHelper(ParticleSystem* child, int z, int aTag) +long ParticleBatchNode::addChildHelper(ParticleSystem* child, int z, int aTag) { CCASSERT( child != NULL, "Argument must be non-nil"); CCASSERT( child->getParent() == NULL, "child already added. It can't be added again"); - if( ! _children ) + if (_children.count() == 0 ) { - _children = new Array(); - _children->initWithCapacity(4); + _children.init(4); } //don't use a lazy insert - unsigned int pos = searchNewPositionInChildrenForZ(z); + long pos = searchNewPositionInChildrenForZ(z); - _children->insertObject(child, pos); + _children.insertObject(child, pos); child->setTag(aTag); child->_setZOrder(z); @@ -239,7 +239,7 @@ void ParticleBatchNode::reorderChild(Node * aChild, int zOrder) { CCASSERT( aChild != NULL, "Child must be non-NULL"); CCASSERT( dynamic_cast(aChild) != NULL, "CCParticleBatchNode only supports QuadParticleSystems as children"); - CCASSERT( _children->containsObject(aChild), "Child doesn't belong to batch" ); + CCASSERT( _children.containsObject(aChild), "Child doesn't belong to batch" ); ParticleSystem* child = static_cast(aChild); @@ -249,9 +249,9 @@ void ParticleBatchNode::reorderChild(Node * aChild, int zOrder) } // no reordering if only 1 child - if( _children->count() > 1) + if( _children.count() > 1) { - unsigned int newIndex = 0, oldIndex = 0; + long newIndex = 0, oldIndex = 0; getCurrentIndex(&oldIndex, &newIndex, child, zOrder); @@ -260,8 +260,8 @@ void ParticleBatchNode::reorderChild(Node * aChild, int zOrder) // reorder _children->array child->retain(); - _children->removeObjectAtIndex(oldIndex); - _children->insertObject(child, newIndex); + _children.removeObjectAtIndex(oldIndex); + _children.insertObject(child, newIndex); child->release(); // save old altasIndex @@ -272,10 +272,10 @@ void ParticleBatchNode::reorderChild(Node * aChild, int zOrder) // Find new AtlasIndex int newAtlasIndex = 0; - for( int i=0;i < _children->count();i++) + for( int i=0;i < _children.count();i++) { - ParticleSystem* pNode = (ParticleSystem*)_children->getObjectAtIndex(i); - if( pNode == child ) + ParticleSystem* node = static_cast(_children[i]); + if( node == child ) { newAtlasIndex = child->getAtlasIndex(); break; @@ -292,17 +292,17 @@ void ParticleBatchNode::reorderChild(Node * aChild, int zOrder) child->_setZOrder(zOrder); } -void ParticleBatchNode::getCurrentIndex(unsigned int* oldIndex, unsigned int* newIndex, Node* child, int z) +void ParticleBatchNode::getCurrentIndex(long* oldIndex, long* newIndex, Node* child, int z) { bool foundCurrentIdx = false; bool foundNewIdx = false; int minusOne = 0; - unsigned int count = _children->count(); + long count = _children.count(); - for( unsigned int i=0; i < count; i++ ) + for( long i=0; i < count; i++ ) { - Node* pNode = (Node *)_children->getObjectAtIndex(i); + Node* pNode = _children[i]; // new index if( pNode->getZOrder() > z && ! foundNewIdx ) @@ -343,13 +343,13 @@ void ParticleBatchNode::getCurrentIndex(unsigned int* oldIndex, unsigned int* ne *newIndex += minusOne; } -unsigned int ParticleBatchNode::searchNewPositionInChildrenForZ(int z) +long ParticleBatchNode::searchNewPositionInChildrenForZ(int z) { - unsigned int count = _children->count(); + long count = _children.count(); - for( unsigned int i=0; i < count; i++ ) + for( long i=0; i < count; i++ ) { - Node *child = (Node *)_children->getObjectAtIndex(i); + Node *child = _children[i]; if (child->getZOrder() > z) { return i; @@ -366,7 +366,7 @@ void ParticleBatchNode::removeChild(Node* aChild, bool cleanup) return; CCASSERT( dynamic_cast(aChild) != NULL, "CCParticleBatchNode only supports QuadParticleSystems as children"); - CCASSERT(_children->containsObject(aChild), "CCParticleBatchNode doesn't contain the sprite. Can't remove it"); + CCASSERT(_children.containsObject(aChild), "CCParticleBatchNode doesn't contain the sprite. Can't remove it"); ParticleSystem* child = static_cast(aChild); Node::removeChild(child, cleanup); @@ -385,12 +385,14 @@ void ParticleBatchNode::removeChild(Node* aChild, bool cleanup) void ParticleBatchNode::removeChildAtIndex(unsigned int index, bool doCleanup) { - removeChild((ParticleSystem *)_children->getObjectAtIndex(index),doCleanup); + removeChild(_children[index],doCleanup); } void ParticleBatchNode::removeAllChildrenWithCleanup(bool doCleanup) { - arrayMakeObjectsPerformSelectorWithObject(_children, setBatchNode, NULL, ParticleSystem*); + _children.makeObjectsPerformCallback([](Node* child){ + static_cast(child)->setBatchNode(nullptr); + }); Node::removeAllChildrenWithCleanup(doCleanup); @@ -417,7 +419,7 @@ void ParticleBatchNode::draw(void) -void ParticleBatchNode::increaseAtlasCapacityTo(unsigned int quantity) +void ParticleBatchNode::increaseAtlasCapacityTo(long quantity) { CCLOG("cocos2d: ParticleBatchNode: resizing TextureAtlas capacity from [%lu] to [%lu].", (long)_textureAtlas->getCapacity(), @@ -467,15 +469,13 @@ void ParticleBatchNode::insertChild(ParticleSystem* system, int index) //rebuild atlas indexes void ParticleBatchNode::updateAllAtlasIndexes() { - Object *pObj = NULL; unsigned int index = 0; - - CCARRAY_FOREACH(_children,pObj) - { - ParticleSystem* child = static_cast(pObj); - child->setAtlasIndex(index); - index += child->getTotalParticles(); - } + + _children.makeObjectsPerformCallback([&index](Node* child){ + ParticleSystem* partiSys = static_cast(child); + partiSys->setAtlasIndex(index); + index += partiSys->getTotalParticles(); + }); } // ParticleBatchNode - CocosNodeTexture protocol diff --git a/cocos/2d/CCParticleBatchNode.h b/cocos/2d/CCParticleBatchNode.h index 937c348500..d7b6b93e42 100644 --- a/cocos/2d/CCParticleBatchNode.h +++ b/cocos/2d/CCParticleBatchNode.h @@ -129,10 +129,10 @@ public: private: void updateAllAtlasIndexes(); - void increaseAtlasCapacityTo(unsigned int quantity); - unsigned int searchNewPositionInChildrenForZ(int z); - void getCurrentIndex(unsigned int* oldIndex, unsigned int* newIndex, Node* child, int z); - unsigned int addChildHelper(ParticleSystem* child, int z, int aTag); + void increaseAtlasCapacityTo(long quantity); + long searchNewPositionInChildrenForZ(int z); + void getCurrentIndex(long* oldIndex, long* newIndex, Node* child, int z); + long addChildHelper(ParticleSystem* child, int z, int aTag); void updateBlendFunc(void); /** the texture atlas used for drawing the quads */ TextureAtlas* _textureAtlas; diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index a35a564f85..fb87756191 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -528,16 +528,12 @@ void RenderTexture::draw() //! make sure all children are drawn sortAllChildren(); - Object *pElement; - CCARRAY_FOREACH(_children, pElement) - { - Node *child = static_cast(pElement); - + _children.makeObjectsPerformCallback([this](Node* child){ if (child != _sprite) { child->visit(); } - } + }); end(); } diff --git a/cocos/2d/CCScene.cpp b/cocos/2d/CCScene.cpp index 97d4fbec65..0711839c0a 100644 --- a/cocos/2d/CCScene.cpp +++ b/cocos/2d/CCScene.cpp @@ -132,25 +132,17 @@ void Scene::addChildToPhysicsWorld(Node* child) { if (_physicsWorld) { - std::function addToPhysicsWorldFunc = nullptr; - addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Object* obj) -> void + std::function addToPhysicsWorldFunc = nullptr; + addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void { - - if (dynamic_cast(obj) != nullptr) + if (node->getPhysicsBody()) { - Node* node = dynamic_cast(obj); - - if (node->getPhysicsBody()) - { - _physicsWorld->addBody(node->getPhysicsBody()); - } - - Object* subChild = nullptr; - CCARRAY_FOREACH(node->getChildren(), subChild) - { - addToPhysicsWorldFunc(subChild); - } + _physicsWorld->addBody(node->getPhysicsBody()); } + + node->getChildren().makeObjectsPerformCallback([addToPhysicsWorldFunc](Node* n){ + addToPhysicsWorldFunc(n); + }); }; addToPhysicsWorldFunc(child); diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 36577238b0..3a03a5d39c 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -696,7 +696,7 @@ void Sprite::addChild(Node *child, int zOrder, int tag) void Sprite::reorderChild(Node *child, int zOrder) { CCASSERT(child != NULL, ""); - CCASSERT(_children->containsObject(child), ""); + CCASSERT(_children.containsObject(child), ""); if (zOrder == child->getZOrder()) { @@ -726,15 +726,13 @@ void Sprite::removeAllChildrenWithCleanup(bool cleanup) { if (_batchNode) { - Object* object = NULL; - CCARRAY_FOREACH(_children, object) - { - Sprite* child = dynamic_cast(object); - if (child) + _children.makeObjectsPerformCallback([this](Node* child){ + Sprite* sprite = dynamic_cast(child); + if (sprite) { - _batchNode->removeSpriteFromAtlas(child); + _batchNode->removeSpriteFromAtlas(sprite); } - } + }); } Node::removeAllChildrenWithCleanup(cleanup); @@ -769,12 +767,14 @@ void Sprite::sortAllChildren() _children->fastSetObject(tempI, j+1); } #else - std::sort(std::begin(*_children), std::end(*_children), nodeComparisonLess); + std::sort(std::begin(_children), std::end(_children), nodeComparisonLess); #endif if ( _batchNode) { - arrayMakeObjectsPerformSelector(_children, sortAllChildren, Sprite*); + _children.makeObjectsPerformCallback([](Node* child){ + child->sortAllChildren(); + }); } _reorderChildDirty = false; @@ -809,15 +809,13 @@ void Sprite::setDirtyRecursively(bool bValue) // recursively set dirty if (_hasChildren) { - Object* object = NULL; - CCARRAY_FOREACH(_children, object) - { - Sprite* child = dynamic_cast(object); - if (child) + _children.makeObjectsPerformCallback([](Node* child){ + Sprite* sp = dynamic_cast(child); + if (sp) { - child->setDirtyRecursively(true); + sp->setDirtyRecursively(true); } - } + }); } } @@ -1062,7 +1060,7 @@ void Sprite::setDisplayFrameWithAnimationName(const std::string& animationName, CCASSERT(a, "CCSprite#setDisplayFrameWithAnimationName: Frame not found"); - AnimationFrame* frame = static_cast( a->getFrames()->getObjectAtIndex(frameIndex) ); + AnimationFrame* frame = a->getFrames()[frameIndex]; CCASSERT(frame, "CCSprite#setDisplayFrame. Invalid frame"); diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index 03a71abf1c..05c3e2a415 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -93,9 +93,10 @@ bool SpriteBatchNode::initWithTexture(Texture2D *tex, long capacity) updateBlendFunc(); // no lazy alloc in this node - _children = new Array(); - _children->initWithCapacity(capacity); - + if (_children.count() == 0) + { + _children.init(capacity); + } _descendants.reserve(capacity); setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); @@ -187,7 +188,7 @@ void SpriteBatchNode::addChild(Node *child, int zOrder, int tag) void SpriteBatchNode::reorderChild(Node *child, int zOrder) { CCASSERT(child != NULL, "the child should not be null"); - CCASSERT(_children->containsObject(child), "Child doesn't belong to Sprite"); + CCASSERT(_children.containsObject(child), "Child doesn't belong to Sprite"); if (zOrder == child->getZOrder()) { @@ -209,7 +210,7 @@ void SpriteBatchNode::removeChild(Node *child, bool cleanup) return; } - CCASSERT(_children->containsObject(sprite), "sprite batch node should contain the child"); + CCASSERT(_children.containsObject(sprite), "sprite batch node should contain the child"); // cleanup before removing removeSpriteFromAtlas(sprite); @@ -219,8 +220,8 @@ void SpriteBatchNode::removeChild(Node *child, bool cleanup) void SpriteBatchNode::removeChildAtIndex(int index, bool doCleanup) { - CCASSERT(index>=0 && index < _children->count(), "Invalid index"); - removeChild( static_cast(_children->getObjectAtIndex(index)), doCleanup); + CCASSERT(index>=0 && index < _children.count(), "Invalid index"); + removeChild(_children[index], doCleanup); } void SpriteBatchNode::removeAllChildrenWithCleanup(bool doCleanup) @@ -265,25 +266,25 @@ void SpriteBatchNode::sortAllChildren() _children->fastSetObject(tempI, j+1); } #else - std::sort(std::begin(*_children), std::end(*_children), nodeComparisonLess); + std::sort(std::begin(_children), std::end(_children), nodeComparisonLess); #endif //sorted now check all children - if (_children->count() > 0) + if (_children.count() > 0) { //first sort all children recursively based on zOrder - arrayMakeObjectsPerformSelector(_children, sortAllChildren, Sprite*); + _children.makeObjectsPerformCallback([](Node* child){ + child->sortAllChildren(); + }); int index=0; - Object* obj = NULL; //fast dispatch, give every child a new atlasIndex based on their relative zOrder (keep parent -> child relations intact) // and at the same time reorder descendants and the quads to the right index - CCARRAY_FOREACH(_children, obj) - { - Sprite* child = static_cast(obj); - updateAtlasIndex(child, &index); - } + _children.makeObjectsPerformCallback([this, &index](Node* child){ + Sprite* sp = static_cast(child); + updateAtlasIndex(sp, &index); + }); } _reorderChildDirty=false; @@ -292,12 +293,8 @@ void SpriteBatchNode::sortAllChildren() void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, int* curIndex) { - int count = 0; - Array* array = sprite->getChildren(); - if (array != NULL) - { - count = array->count(); - } + auto array = sprite->getChildren(); + long count = array.count(); int oldIndex = 0; @@ -315,7 +312,7 @@ void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, int* curIndex) { bool needNewIndex=true; - if (static_cast(array->getObjectAtIndex(0) )->getZOrder() >= 0) + if (array[0]->getZOrder() >= 0) { //all children are in front of the parent oldIndex = sprite->getAtlasIndex(); @@ -330,11 +327,9 @@ void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, int* curIndex) needNewIndex = false; } - Object* obj = NULL; - CCARRAY_FOREACH(array,obj) - { - Sprite* child = static_cast(obj); - if (needNewIndex && child->getZOrder() >= 0) + array.makeObjectsPerformCallback([&](Node* child){ + Sprite* sp = static_cast(child); + if (needNewIndex && sp->getZOrder() >= 0) { oldIndex = sprite->getAtlasIndex(); sprite->setAtlasIndex(*curIndex); @@ -344,11 +339,10 @@ void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, int* curIndex) } (*curIndex)++; needNewIndex = false; - } - - updateAtlasIndex(child, curIndex); - } + + updateAtlasIndex(sp, curIndex); + }); if (needNewIndex) {//all children have a zOrder < 0) @@ -399,7 +393,9 @@ void SpriteBatchNode::draw(void) CC_NODE_DRAW_SETUP(); - arrayMakeObjectsPerformSelector(_children, updateTransform, Sprite*); + _children.makeObjectsPerformCallback([](Node* child){ + child->updateTransform(); + }); GL::blendFunc( _blendFunc.src, _blendFunc.dst ); @@ -413,11 +409,11 @@ void SpriteBatchNode::increaseAtlasCapacity(void) // if we're going beyond the current TextureAtlas's capacity, // all the previously initialized sprites will need to redo their texture coords // this is likely computationally expensive - int quantity = (_textureAtlas->getCapacity() + 1) * 4 / 3; + long quantity = (_textureAtlas->getCapacity() + 1) * 4 / 3; CCLOG("cocos2d: SpriteBatchNode: resizing TextureAtlas capacity from [%lu] to [%lu].", - (long)_textureAtlas->getCapacity(), - (long)quantity); + _textureAtlas->getCapacity(), + quantity); if (! _textureAtlas->resizeCapacity(quantity)) { @@ -429,22 +425,17 @@ void SpriteBatchNode::increaseAtlasCapacity(void) int SpriteBatchNode::rebuildIndexInOrder(Sprite *parent, int index) { - CCASSERT(index>=0 && index < _children->count(), "Invalid index"); + CCASSERT(index>=0 && index < _children.count(), "Invalid index"); - Array *children = parent->getChildren(); + auto children = parent->getChildren(); - if (children && children->count() > 0) - { - Object* object = NULL; - CCARRAY_FOREACH(children, object) + children.makeObjectsPerformCallback([this, &index](Node* child){ + Sprite* sp = static_cast(child); + if (sp && (sp->getZOrder() < 0)) { - Sprite* child = static_cast(object); - if (child && (child->getZOrder() < 0)) - { - index = rebuildIndexInOrder(child, index); - } + index = rebuildIndexInOrder(sp, index); } - } + }); // ignore self (batch node) if (! parent->isEqual(this)) @@ -453,61 +444,56 @@ int SpriteBatchNode::rebuildIndexInOrder(Sprite *parent, int index) index++; } - if (children && children->count() > 0) - { - Object* object = NULL; - CCARRAY_FOREACH(children, object) + children.makeObjectsPerformCallback([this, &index](Node* child){ + Sprite* sp = static_cast(child); + if (sp && (sp->getZOrder() >= 0)) { - Sprite* child = static_cast(object); - if (child && (child->getZOrder() >= 0)) - { - index = rebuildIndexInOrder(child, index); - } + index = rebuildIndexInOrder(sp, index); } - } + }); return index; } int SpriteBatchNode::highestAtlasIndexInChild(Sprite *sprite) { - Array *children = sprite->getChildren(); + auto children = sprite->getChildren(); - if (! children || children->count() == 0) + if (children.count() == 0) { return sprite->getAtlasIndex(); } else { - return highestAtlasIndexInChild( static_cast(children->getLastObject())); + return highestAtlasIndexInChild( static_cast(children.getLastObject())); } } int SpriteBatchNode::lowestAtlasIndexInChild(Sprite *sprite) { - Array *children = sprite->getChildren(); + auto children = sprite->getChildren(); - if (! children || children->count() == 0) + if (children.count() == 0) { return sprite->getAtlasIndex(); } else { - return lowestAtlasIndexInChild(static_cast(children->getObjectAtIndex(0))); + return lowestAtlasIndexInChild(static_cast(children[0])); } } int SpriteBatchNode::atlasIndexForChild(Sprite *sprite, int nZ) { - Array *siblings = sprite->getParent()->getChildren(); - int childIndex = siblings->getIndexOfObject(sprite); + auto siblings = sprite->getParent()->getChildren(); + long childIndex = siblings.getIndexOfObject(sprite); // ignore parent Z if parent is spriteSheet bool ignoreParent = (SpriteBatchNode*)(sprite->getParent()) == this; Sprite *prev = NULL; if (childIndex > 0 && childIndex != -1) { - prev = static_cast(siblings->getObjectAtIndex(childIndex - 1)); + prev = static_cast(siblings[childIndex - 1]); } // first child of the sprite sheet @@ -568,7 +554,7 @@ void SpriteBatchNode::appendChild(Sprite* sprite) } _descendants.push_back(sprite); - int index = _descendants.size()-1; + long index = _descendants.size()-1; sprite->setAtlasIndex(index); @@ -576,13 +562,9 @@ void SpriteBatchNode::appendChild(Sprite* sprite) _textureAtlas->insertQuad(&quad, index); // add children recursively - - Object* obj = nullptr; - CCARRAY_FOREACH(sprite->getChildren(), obj) - { - Sprite* child = static_cast(obj); - appendChild(child); - } + sprite->getChildren().makeObjectsPerformCallback([this](Node* child){ + appendChild(static_cast(child)); + }); } void SpriteBatchNode::removeSpriteFromAtlas(Sprite *sprite) @@ -606,19 +588,14 @@ void SpriteBatchNode::removeSpriteFromAtlas(Sprite *sprite) } // remove children recursively - Array *children = sprite->getChildren(); - if (children && children->count() > 0) - { - Object* object = NULL; - CCARRAY_FOREACH(children, object) + auto children = sprite->getChildren(); + children.makeObjectsPerformCallback([this](Node* obj){ + Sprite* child = static_cast(obj); + if (child) { - Sprite* child = static_cast(object); - if (child) - { - removeSpriteFromAtlas(child); - } + removeSpriteFromAtlas(child); } - } + }); } void SpriteBatchNode::updateBlendFunc(void) diff --git a/cocos/2d/CCTMXLayer.cpp b/cocos/2d/CCTMXLayer.cpp index 87a9893126..41d8cd961d 100644 --- a/cocos/2d/CCTMXLayer.cpp +++ b/cocos/2d/CCTMXLayer.cpp @@ -390,22 +390,19 @@ Sprite * TMXLayer::insertTileForGID(unsigned int gid, const Point& pos) ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ); // update possible children - if (_children && _children->count()>0) - { - Object* pObject = nullptr; - CCARRAY_FOREACH(_children, pObject) + + _children.makeObjectsPerformCallback([&indexForZ](Node* child){ + Sprite* sp = static_cast(child); + if (child) { - Sprite* child = static_cast(pObject); - if (child) + unsigned int ai = sp->getAtlasIndex(); + if ( ai >= indexForZ ) { - unsigned int ai = child->getAtlasIndex(); - if ( ai >= indexForZ ) - { - child->setAtlasIndex(ai+1); - } + sp->setAtlasIndex(ai+1); } } - } + }); + _tiles[z] = gid; return tile; } @@ -558,7 +555,7 @@ void TMXLayer::removeChild(Node* node, bool cleanup) return; } - CCASSERT(_children->containsObject(sprite), "Tile does not belong to TMXLayer"); + CCASSERT(_children.containsObject(sprite), "Tile does not belong to TMXLayer"); unsigned int atlasIndex = sprite->getAtlasIndex(); unsigned int zz = (size_t)_atlasIndexArray->arr[atlasIndex]; @@ -596,22 +593,17 @@ void TMXLayer::removeTileAt(const Point& pos) _textureAtlas->removeQuadAtIndex(atlasIndex); // update possible children - if (_children && _children->count()>0) - { - Object* pObject = nullptr; - CCARRAY_FOREACH(_children, pObject) + _children.makeObjectsPerformCallback([&atlasIndex](Node* obj){ + Sprite* child = static_cast(obj); + if (child) { - Sprite* child = static_cast(pObject); - if (child) + unsigned int ai = child->getAtlasIndex(); + if ( ai >= atlasIndex ) { - unsigned int ai = child->getAtlasIndex(); - if ( ai >= atlasIndex ) - { - child->setAtlasIndex(ai-1); - } + child->setAtlasIndex(ai-1); } } - } + }); } } } diff --git a/cocos/2d/CCTMXTiledMap.cpp b/cocos/2d/CCTMXTiledMap.cpp index 7ab6da363b..c21fc8666c 100644 --- a/cocos/2d/CCTMXTiledMap.cpp +++ b/cocos/2d/CCTMXTiledMap.cpp @@ -210,10 +210,10 @@ void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo) TMXLayer * TMXTiledMap::getLayer(const std::string& layerName) const { CCASSERT(layerName.size() > 0, "Invalid layer name!"); - Object* pObj = NULL; - CCARRAY_FOREACH(_children, pObj) + + for (auto& child : _children) { - TMXLayer* layer = dynamic_cast(pObj); + TMXLayer* layer = dynamic_cast(child); if(layer) { if(layerName.compare( layer->getLayerName()) == 0) @@ -224,7 +224,7 @@ TMXLayer * TMXTiledMap::getLayer(const std::string& layerName) const } // layer not found - return NULL; + return nullptr; } TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 8ffadcb4c5..9b9c307b96 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -35,11 +35,14 @@ template class CC_DLL Vector { public: + + static const long DEFAULT_CAPACTIY = 7; + /** creates an emptry Vector */ - Vector(long capacity=7) + explicit Vector(long capacity=DEFAULT_CAPACTIY) : _data() { - CCLOG("In the constructor of Vector."); + CCLOG("In the default constructor of Vector."); init(capacity); } @@ -54,13 +57,27 @@ public: copy(other); } - const Vector& operator=(const Vector& other) + /** Move constructor */ + Vector(Vector&& other) { - CCLOG("In the assignment operator!"); + CCLOG("In the move constructor of Vector!"); + _data = std::move(other._data); + } + + Vector& operator=(const Vector& other) + { + CCLOG("In the copy assignment operator!"); copy(other); return *this; } + Vector& operator=(Vector&& other) + { + CCLOG("In the move assignment operator!"); + _data = std::move(other._data); + return *this; + } + T operator[](long index) const { return getObjectAtIndex(index); @@ -171,7 +188,8 @@ public: /** Insert a certain object at a certain index */ void insertObject(T object, long index) { - _data.insert( std::next( std::begin(_data, index), object ) ); + CCASSERT(index >= 0 && index < count(), "Invalid index!"); + _data.insert((std::begin(_data) + index), object); object->retain(); } @@ -220,6 +238,7 @@ public: (*it)->release(); } _data.clear(); + _data.reserve(DEFAULT_CAPACTIY); } /** Fast way to remove a certain object */ @@ -275,6 +294,16 @@ public: { _data.shrink_to_fit(); } + + void makeObjectsPerformCallback(std::function callback) + { + if (count() <= 0) + return; + + std::for_each(_data.begin(), _data.end(), [&callback](T obj){ + callback(obj); + }); + } // ------------------------------------------ // Iterators @@ -282,18 +311,26 @@ public: typedef typename std::vector::iterator iterator; typedef typename std::vector::const_iterator const_iterator; + typedef typename std::vector::reverse_iterator reverse_iterator; + typedef typename std::vector::const_reverse_iterator const_reverse_iterator; + iterator begin() { return _data.begin(); } const_iterator begin() const { return _data.begin(); } iterator end() { return _data.end(); } 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(); } - + + reverse_iterator rbegin() { return _data.rbegin(); } + const_reverse_iterator rbegin() const { return _data.rbegin(); } + + reverse_iterator rend() { return _data.rend(); } + const_reverse_iterator rend() const { return _data.rend(); } + + const_reverse_iterator rcbegin() const { return _data.crbegin(); } + const_reverse_iterator rcend() const { return _data.crend(); } protected: std::vector _data; diff --git a/cocos/editor-support/cocosbuilder/CCBReader.cpp b/cocos/editor-support/cocosbuilder/CCBReader.cpp index bf566770fe..5ee2b8a229 100644 --- a/cocos/editor-support/cocosbuilder/CCBReader.cpp +++ b/cocos/editor-support/cocosbuilder/CCBReader.cpp @@ -324,15 +324,13 @@ Scene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName, Obj return pScene; } -void CCBReader::cleanUpNodeGraph(Node *pNode) +void CCBReader::cleanUpNodeGraph(Node *node) { - pNode->setUserObject(NULL); + node->setUserObject(nullptr); - Object *pChild = NULL; - CCARRAY_FOREACH(pNode->getChildren(), pChild) - { - cleanUpNodeGraph(static_cast(pChild)); - } + node->getChildren().makeObjectsPerformCallback([this](Node* obj){ + cleanUpNodeGraph(obj); + }); } Node* CCBReader::readFileWithCleanUp(bool bCleanUp, Dictionary* am) diff --git a/cocos/editor-support/cocostudio/CCArmature.cpp b/cocos/editor-support/cocostudio/CCArmature.cpp index b09922ff70..65c965276f 100644 --- a/cocos/editor-support/cocostudio/CCArmature.cpp +++ b/cocos/editor-support/cocostudio/CCArmature.cpp @@ -312,7 +312,7 @@ void Armature::changeBoneParent(Bone *bone, const char *parentName) if(bone->getParentBone()) { - bone->getParentBone()->getChildren()->removeObject(bone); + bone->getParentBone()->getChildren().removeObject(bone); bone->setParentBone(nullptr); } @@ -467,7 +467,7 @@ void Armature::draw() GL::blendFunc(_blendFunc.src, _blendFunc.dst); } - for (auto object : *_children) + for (auto object : _children) { if (Bone *bone = dynamic_cast(object)) { @@ -640,7 +640,7 @@ Rect Armature::getBoundingBox() const Rect boundingBox = Rect(0, 0, 0, 0); - for(auto object : *_children) + for(auto object : _children) { if (Bone *bone = dynamic_cast(object)) { @@ -672,12 +672,12 @@ Rect Armature::getBoundingBox() const Bone *Armature::getBoneAtPoint(float x, float y) const { - int length = _children->count(); + long length = _children.count(); Bone *bs; - for(int i = length - 1; i >= 0; i--) + for(long i = length - 1; i >= 0; i--) { - bs = static_cast( _children->getObjectAtIndex(i) ); + bs = static_cast( _children[i] ); if(bs->getDisplayManager()->containPoint(x, y)) { return bs; @@ -797,7 +797,7 @@ void Armature::setBody(cpBody *body) _body = body; _body->data = this; - for(auto object: *_children) + for(auto object: _children) { if (Bone *bone = dynamic_cast(object)) { diff --git a/cocos/editor-support/cocostudio/CCBatchNode.cpp b/cocos/editor-support/cocostudio/CCBatchNode.cpp index 0f00332265..a18a9022c1 100644 --- a/cocos/editor-support/cocostudio/CCBatchNode.cpp +++ b/cocos/editor-support/cocostudio/CCBatchNode.cpp @@ -163,14 +163,14 @@ void BatchNode::visit() void BatchNode::draw() { - if (_children == nullptr) + if (_children.count() == 0) { return; } CC_NODE_DRAW_SETUP(); - for(auto object : *_children) + for(auto object : _children) { Armature *armature = dynamic_cast(object); if (armature) diff --git a/cocos/editor-support/cocostudio/CCBone.cpp b/cocos/editor-support/cocostudio/CCBone.cpp index e4f61a171f..1e0e87bc94 100644 --- a/cocos/editor-support/cocostudio/CCBone.cpp +++ b/cocos/editor-support/cocostudio/CCBone.cpp @@ -69,7 +69,6 @@ Bone::Bone() _boneData = nullptr; _tween = nullptr; _tween = nullptr; - _children = nullptr; _displayManager = nullptr; _ignoreMovementBoneData = false; _worldTransform = AffineTransformMake(1, 0, 0, 1, 0, 0); @@ -85,7 +84,6 @@ Bone::Bone() Bone::~Bone(void) { CC_SAFE_DELETE(_tweenData); - CC_SAFE_DELETE(_children); CC_SAFE_DELETE(_tween); CC_SAFE_DELETE(_displayManager); CC_SAFE_DELETE(_worldInfo); @@ -229,14 +227,10 @@ void Bone::update(float delta) DisplayFactory::updateDisplay(this, delta, _boneTransformDirty || _armature->getArmatureTransformDirty()); - if (_children) - { - for(auto object : *_children) - { - Bone *childBone = (Bone *)object; - childBone->update(delta); - } - } + _children.makeObjectsPerformCallback([&delta](Node* obj){ + Bone *childBone = static_cast(obj); + childBone->update(delta); + }); _boneTransformDirty = false; } @@ -309,30 +303,29 @@ void Bone::addChildBone(Bone *child) CCASSERT( nullptr != child, "Argument must be non-nil"); CCASSERT( nullptr == child->_parentBone, "child already added. It can't be added again"); - if(!_children) + if(_children.count() == 0) { - _children = Array::createWithCapacity(4); - _children->retain(); + _children.init(4); } - if (_children->getIndexOfObject(child) == CC_INVALID_INDEX) + if (_children.getIndexOfObject(child) == CC_INVALID_INDEX) { - _children->addObject(child); + _children.addObject(child); child->setParentBone(this); } } void Bone::removeChildBone(Bone *bone, bool recursion) { - if (_children && _children->getIndexOfObject(bone) != CC_INVALID_INDEX ) + if (_children.count() > 0 && _children.getIndexOfObject(bone) != CC_INVALID_INDEX ) { if(recursion) { - Array *ccbones = bone->_children; + auto ccbones = bone->_children; - for(auto object : *ccbones) + for(auto& object : ccbones) { - Bone *ccBone = (Bone *)object; + Bone *ccBone = static_cast(object); bone->removeChildBone(ccBone, recursion); } } @@ -341,7 +334,7 @@ void Bone::removeChildBone(Bone *bone, bool recursion) bone->getDisplayManager()->setCurrentDecorativeDisplay(nullptr); - _children->removeObject(bone); + _children.removeObject(bone); } }