From 5d2ac1fc0c9e6354a741e85e9926ee84dc3dd5ff Mon Sep 17 00:00:00 2001 From: natural-law Date: Thu, 21 Apr 2011 14:46:15 +0800 Subject: [PATCH] fixed #428, Use CCArray instead of CCMutableArray as the parameter type of CCNode::m_pChildren. --- .../project.pbxproj.REMOVED.git-id | 2 +- cocos2dx/Android.mk | 1 + cocos2dx/base_nodes/CCNode.cpp | 93 ++-- cocos2dx/include/CCArray.h | 82 ++++ cocos2dx/include/CCNode.h | 6 +- cocos2dx/include/CCSpriteBatchNode.h | 4 +- cocos2dx/label_nodes/CCLabelBMFont.cpp | 76 ++-- cocos2dx/menu_nodes/CCMenu.cpp | 415 +++++++++--------- cocos2dx/proj.win32/cocos2d-win32.vcproj | 8 + cocos2dx/proj.wophone/Makefile.ARM | 4 + cocos2dx/proj.wophone/cocos2d-wophone.vcproj | 8 + cocos2dx/sprite_nodes/CCSprite.cpp | 34 +- cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp | 236 +++++----- cocos2dx/support/CCArray.cpp | 202 +++++++++ cocos2dx/support/data_support/ccCArray.h | 24 +- .../tileMap_parallax_nodes/CCTMXLayer.cpp | 54 ++- .../project.pbxproj.REMOVED.git-id | 2 +- .../project.pbxproj.REMOVED.git-id | 2 +- .../project.pbxproj.REMOVED.git-id | 2 +- .../project.pbxproj.REMOVED.git-id | 2 +- tests/tests/MenuTest/MenuTest.cpp | 11 +- .../PerformanceNodeChildrenTest.cpp | 41 +- .../PerformanceTest/PerformanceSpriteTest.cpp | 2 +- tests/tests/SchedulerTest/SchedulerTest.cpp | 9 +- .../SpriteTest/SpriteTest.cpp.REMOVED.git-id | 2 +- tests/tests/TileMapTest/TileMapTest.cpp | 51 ++- 26 files changed, 830 insertions(+), 543 deletions(-) create mode 100644 cocos2dx/include/CCArray.h create mode 100644 cocos2dx/support/CCArray.cpp diff --git a/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id b/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id index 651d71b20b..e1458e55c7 100644 --- a/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -564b1b0ff02bf8c6185be8446b8ea8e156db7f6a \ No newline at end of file +4b1a0a89eeffc4ac07d3adecd95b38b431f8cd5c \ No newline at end of file diff --git a/cocos2dx/Android.mk b/cocos2dx/Android.mk index af06f10e43..20040a2a4d 100644 --- a/cocos2dx/Android.mk +++ b/cocos2dx/Android.mk @@ -73,6 +73,7 @@ sprite_nodes/CCSpriteBatchNode.cpp \ sprite_nodes/CCSpriteFrame.cpp \ sprite_nodes/CCSpriteFrameCache.cpp \ sprite_nodes/CCSpriteSheet.cpp \ +support/CCArray.cpp \ support/CCProfiling.cpp \ support/CCPointExtension.cpp \ support/TransformUtils.cpp \ diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index f7d458ae17..86a30ca723 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -84,17 +84,17 @@ CCNode::~CCNode() CC_SAFE_RELEASE(m_pGrid); - if(m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for( it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (*it) - { - (*it)->m_pParent = NULL; - } - } + CCObject* child; + CCARRAY_FOREACH(m_pChildren, child) + { + CCNode* pChild = (CCNode*) child; + if (pChild) + { + pChild->m_pParent = NULL; + } + } } // children @@ -102,21 +102,19 @@ CCNode::~CCNode() } -void CCNode::arrayMakeObjectsPerformSelector(CCMutableArray * pArray, callbackFunc func) +void CCNode::arrayMakeObjectsPerformSelector(CCArray* pArray, callbackFunc func) { if(pArray && pArray->count() > 0) { - CCNode* pNode; - CCMutableArray::CCMutableArrayIterator it; - for( it = pArray->begin(); it != pArray->end(); it++) - { - pNode = (*it); - - if(pNode && func) - { - (pNode->*func)(); - } - } + CCObject* child; + CCARRAY_FOREACH(m_pChildren, child) + { + CCNode* pNode = (CCNode*) child; + if(pNode && func) + { + (pNode->*func)(); + } + } } } @@ -264,7 +262,7 @@ CCPoint CCNode::getPositionInPixels() } /// children getter -CCMutableArray * CCNode::getChildren() +CCArray* CCNode::getChildren() { return m_pChildren; } @@ -493,7 +491,8 @@ char * CCNode::description() // lazy allocs void CCNode::childrenAlloc(void) { - m_pChildren = new CCMutableArray(4); + m_pChildren = CCArray::arrayWithCapacity(4); + m_pChildren->retain(); } CCNode* CCNode::getChildByTag(int aTag) @@ -502,11 +501,10 @@ CCNode* CCNode::getChildByTag(int aTag) if(m_pChildren && m_pChildren->count() > 0) { - CCNode* pNode; - CCMutableArray::CCMutableArrayIterator it; - for( it = m_pChildren->begin(); it != m_pChildren->end(); it++) - { - pNode = (*it); + CCObject* child; + CCARRAY_FOREACH(m_pChildren, child) + { + CCNode* pNode = (CCNode*) child; if(pNode && pNode->m_nTag == aTag) return pNode; } @@ -597,11 +595,10 @@ void CCNode::removeAllChildrenWithCleanup(bool cleanup) // not using detachChild improves speed here if ( m_pChildren && m_pChildren->count() > 0 ) { - CCNode * pNode; - CCMutableArray::CCMutableArrayIterator it; - for ( it = m_pChildren->begin(); it!= m_pChildren->end(); it++ ) + CCObject* child; + CCARRAY_FOREACH(m_pChildren, child) { - pNode = *it; + CCNode* pNode = (CCNode*) child; if (pNode) { // IMPORTANT: @@ -654,22 +651,20 @@ void CCNode::detachChild(CCNode *child, bool doCleanup) void CCNode::insertChild(CCNode* child, int z) { unsigned int index = 0; - CCNode* a = m_pChildren->getLastObject(); + CCNode* a = (CCNode*) m_pChildren->lastObject(); if (!a || a->getZOrder() <= z) { m_pChildren->addObject(child); } else { - CCNode* pNode; - CCMutableArray::CCMutableArrayIterator it; - for( it = m_pChildren->begin(); it != m_pChildren->end(); it++) + CCObject* pObject; + CCARRAY_FOREACH(m_pChildren, pObject) { - pNode = (*it); - - if ( pNode && pNode->m_nZOrder > z ) + CCNode* pNode = (CCNode*) pObject; + if ( pNode && (pNode->m_nZOrder > z )) { - m_pChildren->insertObjectAtIndex(child, index); + m_pChildren->insertObject(child, index); break; } index++; @@ -715,15 +710,16 @@ void CCNode::visit() this->transform(); - CCNode* pNode; - CCMutableArray::CCMutableArrayIterator it; + CCNode* pNode = NULL; + unsigned int i = 0; if(m_pChildren && m_pChildren->count() > 0) { // draw children zOrder < 0 - for( it = m_pChildren->begin(); it != m_pChildren->end(); it++) - { - pNode = (*it); + ccArray *arrayData = m_pChildren->data; + for( ; i < arrayData->num; i++ ) + { + pNode = (CCNode*) arrayData->arr[i]; if ( pNode && pNode->m_nZOrder < 0 ) { @@ -742,9 +738,10 @@ void CCNode::visit() // draw children zOrder >= 0 if (m_pChildren && m_pChildren->count() > 0) { - for ( ; it!=m_pChildren->end(); it++ ) - { - pNode = (*it); + ccArray *arrayData = m_pChildren->data; + for( ; i < arrayData->num; i++ ) + { + pNode = (CCNode*) arrayData->arr[i]; if (pNode) { pNode->visit(); diff --git a/cocos2dx/include/CCArray.h b/cocos2dx/include/CCArray.h new file mode 100644 index 0000000000..e951feca77 --- /dev/null +++ b/cocos2dx/include/CCArray.h @@ -0,0 +1,82 @@ +/**************************************************************************** +Copyright (c) 2010 Abstraction Works. http://www.abstractionworks.com +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#ifndef __CCARRAY_H__ +#define __CCARRAY_H__ + +#include "support/data_support/ccCArray.h" + +/** @def CCARRAY_FOREACH +A convience macro to iterate over a CCArray using. It is faster than the "fast enumeration" interface. +@since v0.99.4 +*/ + +#define CCARRAY_FOREACH(__array__, __object__) \ + if (__array__ && __array__->data->num > 0) \ + for(CCObject** arr = __array__->data->arr, **end = __array__->data->arr + __array__->data->num-1; \ + arr <= end && ((__object__ = *arr) != NULL/* || true*/); \ + arr++) + +namespace cocos2d +{ + +class CC_DLL CCArray : public CCObject +{ +public: + ~CCArray(); + static CCArray* array(); + static CCArray* arrayWithCapacity(unsigned int capacity); + static CCArray* arrayWithArray(CCArray* otherArray); + + bool init(); + bool initWithCapacity(unsigned int capacity); + bool initWithArray(CCArray* otherArray); + + unsigned int count(); + unsigned int capacity(); + unsigned int indexOfObject(CCObject* object); + CCObject* objectAtIndex(unsigned int index); + CCObject* lastObject(); + CCObject* randomObject(); + bool containsObject(CCObject* object); + + void addObject(CCObject* object); + void addObjectsFromArray(CCArray* otherArray); + void insertObject(CCObject* object, unsigned int index); + + void removeLastObject(); + void removeObject(CCObject* object); + void removeObjectAtIndex(unsigned int index); + void removeObjectsInArray(CCArray* otherArray); + void removeAllObjects(); + void fastRemoveObject(CCObject* object); + void fastRemoveObjectAtIndex(unsigned int index); + +public: + ccArray* data; +}; + +} + +#endif // __CCARRAY_H__ diff --git a/cocos2dx/include/CCNode.h b/cocos2dx/include/CCNode.h index 5ce345d7c6..ace5d56fc7 100644 --- a/cocos2dx/include/CCNode.h +++ b/cocos2dx/include/CCNode.h @@ -29,7 +29,7 @@ THE SOFTWARE. #include "ccMacros.h" #include "CCAffineTransform.h" -#include "CCMutableArray.h" +#include "CCArray.h" #include "selector_protocol.h" #include "CCGL.h" @@ -140,7 +140,7 @@ namespace cocos2d { CC_PROPERTY(CCPoint, m_tPosition, Position) CC_PROPERTY(CCPoint, m_tPositionInPixels, PositionInPixels) - CC_PROPERTY_READONLY(CCMutableArray *, m_pChildren, Children) + CC_PROPERTY_READONLY(CCArray*, m_pChildren, Children) /** A CCCamera object that lets you move the node using a gluLookAt */ @@ -229,7 +229,7 @@ namespace cocos2d { typedef void (CCNode::*callbackFunc)(void); - void arrayMakeObjectsPerformSelector(CCMutableArray * pArray, callbackFunc func); + void arrayMakeObjectsPerformSelector(CCArray* pArray, callbackFunc func); CCPoint convertToWindowSpace(CCPoint nodePoint); diff --git a/cocos2dx/include/CCSpriteBatchNode.h b/cocos2dx/include/CCSpriteBatchNode.h index 2adbe2378b..052079d944 100644 --- a/cocos2dx/include/CCSpriteBatchNode.h +++ b/cocos2dx/include/CCSpriteBatchNode.h @@ -71,7 +71,7 @@ namespace cocos2d } } - inline CCMutableArray* getDescendants(void) { return m_pobDescendants; } + inline CCArray* getDescendants(void) { return m_pobDescendants; } /** creates a CCSpriteBatchNode with a texture2d and a default capacity of 29 children. The capacity will be increased in 33% in runtime if it run out of space. @@ -182,7 +182,7 @@ namespace cocos2d ccBlendFunc m_blendFunc; // all descendants: chlidren, gran children, etc... - CCMutableArray* m_pobDescendants; + CCArray* m_pobDescendants; }; } diff --git a/cocos2dx/label_nodes/CCLabelBMFont.cpp b/cocos2dx/label_nodes/CCLabelBMFont.cpp index 76b1d61d53..e87e541fc5 100644 --- a/cocos2dx/label_nodes/CCLabelBMFont.cpp +++ b/cocos2dx/label_nodes/CCLabelBMFont.cpp @@ -542,12 +542,15 @@ namespace cocos2d{ if (m_pChildren && m_pChildren->count() != 0) { - CCMutableArray::CCMutableArrayIterator it; - for(it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - (*it)->setIsVisible(false); - } - } + CCObject* child; + CCARRAY_FOREACH(m_pChildren, child) + { + CCNode* pNode = (CCNode*) child; + if (pNode) + { + pNode->setIsVisible(false); + } + } } this->createFontChars(); } @@ -567,12 +570,15 @@ namespace cocos2d{ m_tColor = var; if (m_pChildren && m_pChildren->count() != 0) { - CCMutableArray::CCMutableArrayIterator it; - for(it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - ((CCSprite*)(*it))->setColor(m_tColor); - } - } + CCObject* child; + CCARRAY_FOREACH(m_pChildren, child) + { + CCSprite* pNode = (CCSprite*) child; + if (pNode) + { + pNode->setColor(m_tColor); + } + } } } ccColor3B CCLabelBMFont::getColor() { @@ -584,16 +590,19 @@ namespace cocos2d{ if (m_pChildren && m_pChildren->count() != 0) { - CCMutableArray::CCMutableArrayIterator it; - for(it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - CCRGBAProtocol *pRGBAProtocol = (*it)->convertToRGBAProtocol(); - if (pRGBAProtocol) - { - pRGBAProtocol->setOpacity(m_cOpacity); - } - } - } + CCObject* child; + CCARRAY_FOREACH(m_pChildren, child) + { + CCNode* pNode = (CCNode*) child; + if (pNode) + { + CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol(); + if (pRGBAProtocol) + { + pRGBAProtocol->setOpacity(m_cOpacity); + } + } + } } } GLubyte CCLabelBMFont::getOpacity() { @@ -604,16 +613,19 @@ namespace cocos2d{ m_bIsOpacityModifyRGB = var; if (m_pChildren && m_pChildren->count() != 0) { - CCMutableArray::CCMutableArrayIterator it; - for(it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - CCRGBAProtocol *pRGBAProtocol = (*it)->convertToRGBAProtocol(); - if (pRGBAProtocol) - { - pRGBAProtocol->setIsOpacityModifyRGB(m_bIsOpacityModifyRGB); - } - } - } + CCObject* child; + CCARRAY_FOREACH(m_pChildren, child) + { + CCNode* pNode = (CCNode*) child; + if (pNode) + { + CCRGBAProtocol *pRGBAProtocol = pNode->convertToRGBAProtocol(); + if (pRGBAProtocol) + { + pRGBAProtocol->setIsOpacityModifyRGB(m_bIsOpacityModifyRGB); + } + } + } } } bool CCLabelBMFont::getIsOpacityModifyRGB() { diff --git a/cocos2dx/menu_nodes/CCMenu.cpp b/cocos2dx/menu_nodes/CCMenu.cpp index db152c5f69..58ffb9c19f 100644 --- a/cocos2dx/menu_nodes/CCMenu.cpp +++ b/cocos2dx/menu_nodes/CCMenu.cpp @@ -220,32 +220,30 @@ namespace cocos2d{ float height = -padding; if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (!(*it)) + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) { - break; + height += pChild->getContentSize().height * pChild->getScaleY() + padding; } - - height += (*it)->getContentSize().height * (*it)->getScaleY() + padding; - } + } } float y = height / 2.0f; if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (!(*it)) - { - break; - } - - (*it)->setPosition(ccp(0, y - (*it)->getContentSize().height * (*it)->getScaleY() / 2.0f)); - y -= (*it)->getContentSize().height * (*it)->getScaleY() + padding; - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) + { + pChild->setPosition(ccp(0, y - pChild->getContentSize().height * pChild->getScaleY() / 2.0f)); + y -= pChild->getContentSize().height * pChild->getScaleY() + padding; + } + } } } @@ -260,32 +258,30 @@ namespace cocos2d{ float width = -padding; if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (!(*it)) - { - break; + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) + { + width += pChild->getContentSize().width * pChild->getScaleX() + padding; } - - width += (*it)->getContentSize().width * (*it)->getScaleX() + padding; - } + } } float x = -width / 2.0f; if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (!(*it)) - { - break; + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) + { + pChild->setPosition(ccp(x + pChild->getContentSize().width * pChild->getScaleX() / 2.0f, 0)); + x += pChild->getContentSize().width * pChild->getScaleX() + padding; } - - (*it)->setPosition(ccp(x + (*it)->getContentSize().width * (*it)->getScaleX() / 2.0f, 0)); - x += (*it)->getContentSize().width * (*it)->getScaleX() + padding; - } + } } } @@ -316,34 +312,32 @@ namespace cocos2d{ if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - // if it has no value, break - if (! *it) - { - break; - } - - assert(row < rows.size()); - - rowColumns = rows[row]; - // can not have zero columns on a row - assert(rowColumns); - - float tmp = (*it)->getContentSize().height; - rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp); - - ++columnsOccupied; - if (columnsOccupied >= rowColumns) - { - height += rowHeight + 5; - - columnsOccupied = 0; - rowHeight = 0; - ++row; - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) + { + assert(row < rows.size()); + + rowColumns = rows[row]; + // can not have zero columns on a row + assert(rowColumns); + + float tmp = pChild->getContentSize().height; + rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp); + + ++columnsOccupied; + if (columnsOccupied >= rowColumns) + { + height += rowHeight + 5; + + columnsOccupied = 0; + rowHeight = 0; + ++row; + } + } + } } // check if too many rows/columns for available menu items @@ -360,40 +354,39 @@ namespace cocos2d{ if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (! *it) - { - break; - } - - if (rowColumns == 0) - { - rowColumns = rows[row]; - w = winSize.width / (1 + rowColumns); - x = w; - } - - float tmp = (*it)->getContentSize().height; - rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp); - - (*it)->setPosition(ccp(x - winSize.width / 2, - y - (*it)->getContentSize().height / 2)); - - x += w + 10; - ++columnsOccupied; - - if (columnsOccupied >= rowColumns) - { - y -= rowHeight + 5; - - columnsOccupied = 0; - rowColumns = 0; - rowHeight = 0; - ++row; - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) + { + if (rowColumns == 0) + { + rowColumns = rows[row]; + w = winSize.width / (1 + rowColumns); + x = w; + } + + float tmp = pChild->getContentSize().height; + rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp); + + pChild->setPosition(ccp(x - winSize.width / 2, + y - pChild->getContentSize().height / 2)); + + x += w + 10; + ++columnsOccupied; + + if (columnsOccupied >= rowColumns) + { + y -= rowHeight + 5; + + columnsOccupied = 0; + rowColumns = 0; + rowHeight = 0; + ++row; + } + } + } } } @@ -428,40 +421,39 @@ namespace cocos2d{ if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (! *it) - { - break; - } - - // check if too many menu items for the amount of rows/columns - assert(column < columns.size()); - - columnRows = columns[column]; - // can't have zero rows on a column - assert(columnRows); - - // columnWidth = fmaxf(columnWidth, [item contentSize].width); - float tmp = (*it)->getContentSize().width; - columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp); - - columnHeight += (int)((*it)->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; - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) + { + // check if too many menu items for the amount of rows/columns + assert(column < columns.size()); + + columnRows = columns[column]; + // can't have zero rows on a column + assert(columnRows); + + // columnWidth = fmaxf(columnWidth, [item contentSize].width); + float tmp = pChild->getContentSize().width; + columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp); + + columnHeight += (int)(pChild->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; + } + } + } } // check if too many rows/columns for available menu items. @@ -477,39 +469,38 @@ namespace cocos2d{ if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (! *it) - { - break; - } - - if (columnRows == 0) - { - columnRows = columns[column]; - y = (float) columnHeights[column]; - } - - // columnWidth = fmaxf(columnWidth, [item contentSize].width); - float tmp = (*it)->getContentSize().width; - columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp); - - (*it)->setPosition(ccp(x + columnWidths[column] / 2, - y - winSize.height / 2)); - - y -= (*it)->getContentSize().height + 10; - ++rowsOccupied; - - if (rowsOccupied >= columnRows) - { - x += columnWidth + 5; - rowsOccupied = 0; - columnRows = 0; - columnWidth = 0; - ++column; - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) + { + if (columnRows == 0) + { + columnRows = columns[column]; + y = (float) columnHeights[column]; + } + + // columnWidth = fmaxf(columnWidth, [item contentSize].width); + float tmp = pChild->getContentSize().width; + columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp); + + pChild->setPosition(ccp(x + columnWidths[column] / 2, + y - winSize.height / 2)); + + y -= pChild->getContentSize().height + 10; + ++rowsOccupied; + + if (rowsOccupied >= columnRows) + { + x += columnWidth + 5; + rowsOccupied = 0; + columnRows = 0; + columnWidth = 0; + ++column; + } + } + } } } @@ -522,20 +513,19 @@ namespace cocos2d{ if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (! *it) - { - break; - } - - CCRGBAProtocol *pRGBAProtocol = (*it)->convertToRGBAProtocol(); - if (pRGBAProtocol) - { - pRGBAProtocol->setOpacity(m_cOpacity); - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) + { + CCRGBAProtocol *pRGBAProtocol = pChild->convertToRGBAProtocol(); + if (pRGBAProtocol) + { + pRGBAProtocol->setOpacity(m_cOpacity); + } + } + } } } @@ -550,20 +540,19 @@ namespace cocos2d{ if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (! *it) - { - break; - } - - CCRGBAProtocol *pRGBAProtocol = (*it)->convertToRGBAProtocol(); - if (pRGBAProtocol) - { - pRGBAProtocol->setColor(m_tColor); - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild) + { + CCRGBAProtocol *pRGBAProtocol = pChild->convertToRGBAProtocol(); + if (pRGBAProtocol) + { + pRGBAProtocol->setColor(m_tColor); + } + } + } } } @@ -579,28 +568,22 @@ namespace cocos2d{ if (m_pChildren && m_pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator it; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - if (! *it) - { - break; - } - - // ignore invisible and disabled items: issue #779, #866 - if ((*it)->getIsVisible() && ((CCMenuItem*)(*it))->getIsEnabled()) - { - CCPoint local = (*it)->convertToNodeSpace(touchLocation); - - CCRect r = ((CCMenuItem*)(*it))->rect(); - r.origin = CCPointZero; - - if (CCRect::CCRectContainsPoint(r, local)) - { - return (CCMenuItem*)(*it); + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCNode* pChild = (CCNode*) pObject; + if (pChild && pChild->getIsVisible() && ((CCMenuItem*)pChild)->getIsEnabled()) + { + CCPoint local = pChild->convertToNodeSpace(touchLocation); + CCRect r = ((CCMenuItem*)pChild)->rect(); + r.origin = CCPointZero; + + if (CCRect::CCRectContainsPoint(r, local)) + { + return (CCMenuItem*)pChild; } - } - } + } + } } diff --git a/cocos2dx/proj.win32/cocos2d-win32.vcproj b/cocos2dx/proj.win32/cocos2d-win32.vcproj index 611fd6c093..265f241892 100644 --- a/cocos2dx/proj.win32/cocos2d-win32.vcproj +++ b/cocos2dx/proj.win32/cocos2d-win32.vcproj @@ -375,6 +375,10 @@ RelativePath="..\include\CCApplication.h" > + + @@ -879,6 +883,10 @@ RelativePath="..\support\base64.h" > + + diff --git a/cocos2dx/proj.wophone/Makefile.ARM b/cocos2dx/proj.wophone/Makefile.ARM index 118a910fe1..2e56558f83 100644 --- a/cocos2dx/proj.wophone/Makefile.ARM +++ b/cocos2dx/proj.wophone/Makefile.ARM @@ -101,6 +101,7 @@ OBJECTS = \ $(OBJECTS_DIR)/CCSpriteFrameCache.o \ $(OBJECTS_DIR)/CCSpriteSheet.o \ $(OBJECTS_DIR)/base64.o \ + $(OBJECTS_DIR)/CCArray.o \ $(OBJECTS_DIR)/CCPointExtension.o \ $(OBJECTS_DIR)/CCProfiling.o \ $(OBJECTS_DIR)/ccUtils.o \ @@ -345,6 +346,9 @@ $(OBJECTS_DIR)/CCSpriteSheet.o : ../sprite_nodes/CCSpriteSheet.cpp $(OBJECTS_DIR)/base64.o : ../support/base64.cpp $(CXX) -c $(CXX_FLAGS) $(INCLUDE_PATH) $(LAST_INCLUDE_PATH) -o $(OBJECTS_DIR)/base64.o ../support/base64.cpp +$(OBJECTS_DIR)/CCArray.o : ../support/CCArray.cpp + $(CXX) -c $(CXX_FLAGS) $(INCLUDE_PATH) $(LAST_INCLUDE_PATH) -o $(OBJECTS_DIR)/CCArray.o ../support/CCArray.cpp + $(OBJECTS_DIR)/CCPointExtension.o : ../support/CCPointExtension.cpp $(CXX) -c $(CXX_FLAGS) $(INCLUDE_PATH) $(LAST_INCLUDE_PATH) -o $(OBJECTS_DIR)/CCPointExtension.o ../support/CCPointExtension.cpp diff --git a/cocos2dx/proj.wophone/cocos2d-wophone.vcproj b/cocos2dx/proj.wophone/cocos2d-wophone.vcproj index 8262891f44..994e22c90b 100644 --- a/cocos2dx/proj.wophone/cocos2d-wophone.vcproj +++ b/cocos2dx/proj.wophone/cocos2d-wophone.vcproj @@ -336,6 +336,10 @@ RelativePath="..\include\CCApplication.h" > + + @@ -720,6 +724,10 @@ RelativePath="..\support\base64.h" > + + diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index b1e85bf1b2..d876617c1a 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -766,14 +766,15 @@ void CCSprite::removeAllChildrenWithCleanup(bool bCleanup) { if (m_bUsesBatchNode) { - CCSprite *pChild; - CCMutableArray::CCMutableArrayIterator iter; - for (iter = m_pChildren->begin(); iter != m_pChildren->end(); ++iter) - { - pChild = (CCSprite*)(*iter); - CC_BREAK_IF(! pChild); - m_pobBatchNode->removeSpriteFromAtlas(pChild); - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild) + { + m_pobBatchNode->removeSpriteFromAtlas(pChild); + } + } } CCNode::removeAllChildrenWithCleanup(bCleanup); @@ -792,14 +793,15 @@ void CCSprite::setDirtyRecursively(bool bValue) // recursively set dirty if (m_bHasChildren) { - CCSprite *pChild; - CCMutableArray::CCMutableArrayIterator iter; - for (iter = m_pChildren->begin(); iter != m_pChildren->end(); ++iter) - { - pChild = (CCSprite*)(*iter); - CC_BREAK_IF(! pChild); - pChild->setDirtyRecursively(true); - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild) + { + pChild->setDirtyRecursively(true); + } + } } } diff --git a/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp b/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp index b47afb80cc..816905f50a 100644 --- a/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp +++ b/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp @@ -110,8 +110,10 @@ namespace cocos2d updateBlendFunc(); // no lazy alloc in this node - m_pChildren = new CCMutableArray(); - m_pobDescendants = new CCMutableArray(); + m_pChildren = CCArray::array(); + m_pobDescendants = CCArray::array(); + m_pChildren->retain(); + m_pobDescendants->retain(); return true; } @@ -249,7 +251,7 @@ namespace cocos2d void CCSpriteBatchNode::removeChildAtIndex(unsigned int uIndex, bool bDoCleanup) { - removeChild((CCSprite*)(m_pChildren->getObjectAtIndex(uIndex)), bDoCleanup); + removeChild((CCSprite*)(m_pChildren->objectAtIndex(uIndex)), bDoCleanup); } void CCSpriteBatchNode::removeAllChildrenWithCleanup(bool bCleanup) @@ -257,19 +259,15 @@ namespace cocos2d // Invalidate atlas index. issue #569 if (m_pChildren && m_pChildren->count() > 0) { - CCSprite *pSprite; - CCMutableArray::CCMutableArrayIterator iter; - for (iter = m_pChildren->begin(); iter != m_pChildren->end(); ++iter) - { - pSprite = (CCSprite*)(*iter); - - if (! pSprite) - { - break; - } - - pSprite->useSelfRender(); - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild) + { + pChild->useSelfRender(); + } + } } CCNode::removeAllChildrenWithCleanup(bCleanup); @@ -289,33 +287,29 @@ namespace cocos2d if (m_pobDescendants && m_pobDescendants->count() > 0) { - CCSprite *pSprite; - CCMutableArray::CCMutableArrayIterator iter; - for (iter = m_pobDescendants->begin(); iter != m_pobDescendants->end(); ++iter) - { - pSprite = *iter; - - if (! pSprite) - { - break; - } - - // fast dispatch - pSprite->updateTransform(); + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild) + { + // fast dispatch + pChild->updateTransform(); #if CC_SPRITEBATCHNODE_DEBUG_DRAW - // issue #528 - CCRect rect = pSprite->boundingBox(); - CCPoint vertices[4]={ - ccp(rect.origin.x,rect.origin.y), - ccp(rect.origin.x+rect.size.width,rect.origin.y), - ccp(rect.origin.x+rect.size.width,rect.origin.y+rect.size.height), - ccp(rect.origin.x,rect.origin.y+rect.size.height), - }; - ccDrawPoly(vertices, 4, true); + // issue #528 + CCRect rect = pChild->boundingBox(); + CCPoint vertices[4]={ + ccp(rect.origin.x,rect.origin.y), + ccp(rect.origin.x+rect.size.width,rect.origin.y), + ccp(rect.origin.x+rect.size.width,rect.origin.y+rect.size.height), + ccp(rect.origin.x,rect.origin.y+rect.size.height), + }; + ccDrawPoly(vertices, 4, true); #endif // CC_SPRITEBATCHNODE_DEBUG_DRAW - } + } + } } // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY @@ -354,26 +348,19 @@ namespace cocos2d unsigned int CCSpriteBatchNode::rebuildIndexInOrder(CCSprite *pobParent, unsigned int uIndex) { - CCMutableArray *pChildren = pobParent->getChildren(); + CCArray *pChildren = pobParent->getChildren(); if (pChildren && pChildren->count() > 0) { - CCSprite *pSprite; - CCMutableArray::CCMutableArrayIterator iter; - for (iter = pChildren->begin(); iter != pChildren->end(); ++iter) - { - pSprite = (CCSprite*)(*iter); - - if (! pSprite) - { - break; - } - - if (pSprite->getZOrder() < 0) - { - uIndex = rebuildIndexInOrder(pSprite, uIndex); - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild && (pChild->getZOrder() < 0)) + { + uIndex = rebuildIndexInOrder(pChild, uIndex); + } + } } // ignore self (batch node) @@ -385,22 +372,15 @@ namespace cocos2d if (pChildren && pChildren->count() > 0) { - CCSprite *pSprite; - CCMutableArray::CCMutableArrayIterator iter; - for (iter = pChildren->begin(); iter != pChildren->end(); ++iter) - { - pSprite = (CCSprite*)(*iter); - - if (! pSprite) - { - break; - } - - if (pSprite->getZOrder() >= 0) - { - uIndex = rebuildIndexInOrder(pSprite, uIndex); - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild && (pChild->getZOrder() >= 0)) + { + uIndex = rebuildIndexInOrder(pChild, uIndex); + } + } } return uIndex; @@ -408,7 +388,7 @@ namespace cocos2d unsigned int CCSpriteBatchNode::highestAtlasIndexInChild(CCSprite *pSprite) { - CCMutableArray *pChildren = pSprite->getChildren(); + CCArray *pChildren = pSprite->getChildren(); if (! pChildren || pChildren->count() == 0) { @@ -416,13 +396,13 @@ namespace cocos2d } else { - return highestAtlasIndexInChild((CCSprite*)(pChildren->getLastObject())); + return highestAtlasIndexInChild((CCSprite*)(pChildren->lastObject())); } } unsigned int CCSpriteBatchNode::lowestAtlasIndexInChild(CCSprite *pSprite) { - CCMutableArray *pChildren = pSprite->getChildren(); + CCArray *pChildren = pSprite->getChildren(); if (! pChildren || pChildren->count() == 0) { @@ -430,21 +410,21 @@ namespace cocos2d } else { - return lowestAtlasIndexInChild((CCSprite*)(pChildren->getObjectAtIndex(0))); + return lowestAtlasIndexInChild((CCSprite*)(pChildren->objectAtIndex(0))); } } unsigned int CCSpriteBatchNode::atlasIndexForChild(CCSprite *pobSprite, int nZ) { - CCMutableArray *pBrothers = pobSprite->getParent()->getChildren(); - unsigned int uChildIndex = pBrothers->getIndexOfObject(pobSprite); + CCArray *pBrothers = pobSprite->getParent()->getChildren(); + unsigned int uChildIndex = pBrothers->indexOfObject(pobSprite); // ignore parent Z if parent is spriteSheet bool bIgnoreParent = (CCSpriteBatchNode*)(pobSprite->getParent()) == this; CCSprite *pPrevious = NULL; if (uChildIndex > 0) { - pPrevious = (CCSprite*)(pBrothers->getObjectAtIndex(uChildIndex - 1)); + pPrevious = (CCSprite*)(pBrothers->objectAtIndex(uChildIndex - 1)); } // first child of the sprite sheet @@ -509,47 +489,42 @@ namespace cocos2d ccV3F_C4B_T2F_Quad quad = pobSprite->getQuad(); m_pobTextureAtlas->insertQuad(&quad, uIndex); - m_pobDescendants->insertObjectAtIndex(pobSprite, uIndex); + m_pobDescendants->insertObject(pobSprite, uIndex); // update indices unsigned int i = 0; if (m_pobDescendants && m_pobDescendants->count() > 0) { - CCMutableArray::CCMutableArrayIterator iter; - for (iter = m_pobDescendants->begin(); iter != m_pobDescendants->end(); ++iter) - { - if (! *iter) - { - break; - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild) + { + if (i > uIndex) + { + pChild->setAtlasIndex(pChild->getAtlasIndex() + 1); + } - if (i > uIndex) - { - (*iter)->setAtlasIndex((*iter)->getAtlasIndex() + 1); - } - - ++i; - } + ++i; + } + } } // add children recursively - CCMutableArray *pChildren = pobSprite->getChildren(); + CCArray *pChildren = pobSprite->getChildren(); if (pChildren && pChildren->count() > 0) { - CCMutableArray::CCMutableArrayIterator iterNode; - CCSprite *pSprite; - for (iterNode = pChildren->begin(); iterNode != pChildren->end(); ++iterNode) - { - pSprite = (CCSprite*)(*iterNode); - - if (! pSprite) - { - break; - } - - unsigned int uIndex = atlasIndexForChild(pSprite, pSprite->getZOrder()); - insertChild(pSprite, uIndex); - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild) + { + unsigned int uIndex = atlasIndexForChild(pChild, pChild->getZOrder()); + insertChild(pChild, uIndex); + } + } } } @@ -561,7 +536,7 @@ namespace cocos2d // Cleanup sprite. It might be reused (issue #569) pobSprite->useSelfRender(); - unsigned int uIndex = m_pobDescendants->getIndexOfObject(pobSprite); + unsigned int uIndex = m_pobDescendants->indexOfObject(pobSprite); if (uIndex != -1) { m_pobDescendants->removeObjectAtIndex(uIndex); @@ -571,28 +546,24 @@ namespace cocos2d for(; uIndex < count; ++uIndex) { - CCSprite* s = (CCSprite*)(m_pobDescendants->getObjectAtIndex(uIndex)); + CCSprite* s = (CCSprite*)(m_pobDescendants->objectAtIndex(uIndex)); s->setAtlasIndex( s->getAtlasIndex() - 1 ); } } // remove children recursively - CCMutableArray *pChildren = pobSprite->getChildren(); + CCArray *pChildren = pobSprite->getChildren(); if (pChildren && pChildren->count() > 0) { - CCSprite *pSprite; - CCMutableArray::CCMutableArrayIterator iter; - for (iter = pChildren->begin(); iter != pChildren->end(); ++iter) - { - pSprite = (CCSprite*)(*iter); - - if (! pSprite) - { - break; - } - - removeSpriteFromAtlas(pSprite); - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild) + { + removeSpriteFromAtlas(pChild); + } + } } } @@ -667,18 +638,17 @@ namespace cocos2d int i=0; if (m_pobDescendants && m_pobDescendants->count() > 0) { - CCMutableArray::CCMutableArrayIterator iter; - for (iter = m_pobDescendants->begin(); iter != m_pobDescendants->end(); ++iter) - { - // fast dispatch - if (!(*iter) || (*iter)->getAtlasIndex() >=z) + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild && (pChild->getAtlasIndex() >= z)) { - break; + ++i; } - ++i; } } - m_pobDescendants->insertObjectAtIndex(child, i); + m_pobDescendants->insertObject(child, i); // IMPORTANT: Call super, and not self. Avoid adding it to the texture atlas array CCNode::addChild(child, z, aTag); diff --git a/cocos2dx/support/CCArray.cpp b/cocos2dx/support/CCArray.cpp new file mode 100644 index 0000000000..e57cf6c2ba --- /dev/null +++ b/cocos2dx/support/CCArray.cpp @@ -0,0 +1,202 @@ +/**************************************************************************** +Copyright (c) 2010 Abstraction Works. http://www.abstractionworks.com +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ + +#include "CCArray.h" + +namespace cocos2d +{ + +CCArray* CCArray::array() +{ + CCArray* pArray = new CCArray(); + + if (pArray && pArray->init()) + { + pArray->autorelease(); + } + else + { + CC_SAFE_DELETE(pArray); + } + + return pArray; +} + +CCArray* CCArray::arrayWithCapacity(unsigned int capacity) +{ + CCArray* pArray = new CCArray(); + + if (pArray && pArray->initWithCapacity(capacity)) + { + pArray->autorelease(); + } + else + { + CC_SAFE_DELETE(pArray); + } + + return pArray; +} + +CCArray* CCArray::arrayWithArray(CCArray* otherArray) +{ + CCArray* pArray = new CCArray(); + + if (pArray && pArray->initWithArray(otherArray)) + { + pArray->autorelease(); + } + else + { + CC_SAFE_DELETE(pArray); + } + + return pArray; +} + +bool CCArray::init() +{ + return initWithCapacity(1); +} + +bool CCArray::initWithCapacity(unsigned int capacity) +{ + data = ccArrayNew(capacity); + return true; +} + +bool CCArray::initWithArray(CCArray* otherArray) +{ + bool bRet = false; + do + { + CC_BREAK_IF(! initWithCapacity(otherArray->data->num)); + + addObjectsFromArray(otherArray); + bRet = true; + } while (0); + + return bRet; +} + +unsigned int CCArray::count() +{ + return data->num; +} + +unsigned int CCArray::capacity() +{ + return data->max; +} + +unsigned int CCArray::indexOfObject(CCObject* object) +{ + return ccArrayGetIndexOfObject(data, object); +} + +CCObject* CCArray::objectAtIndex(unsigned int index) +{ + CCAssert(index < data->num, "index out of range in objectAtIndex()"); + + return data->arr[index]; +} + +CCObject* CCArray::lastObject() +{ + if( data->num > 0 ) + return data->arr[data->num-1]; + + return NULL; +} + +CCObject* CCArray::randomObject() +{ + if(data->num==0) return NULL; + + return data->arr[(int)(data->num*CCRANDOM_0_1())]; +} + +bool CCArray::containsObject(CCObject* object) +{ + return ccArrayContainsObject(data, object); +} + +void CCArray::addObject(CCObject* object) +{ + ccArrayAppendObjectWithResize(data, object); +} + +void CCArray::addObjectsFromArray(CCArray* otherArray) +{ + ccArrayAppendArrayWithResize(data, otherArray->data); +} + +void CCArray::insertObject(CCObject* object, unsigned int index) +{ + ccArrayInsertObjectAtIndex(data, object, index); +} + +void CCArray::removeLastObject() +{ + CCAssert(data->num, "no objects added"); + ccArrayRemoveObjectAtIndex(data, data->num-1); +} + +void CCArray::removeObject(CCObject* object) +{ + ccArrayRemoveObject(data, object); +} + +void CCArray::removeObjectAtIndex(unsigned int index) +{ + ccArrayRemoveObjectAtIndex(data, index); +} + +void CCArray::removeObjectsInArray(CCArray* otherArray) +{ + ccArrayRemoveArray(data, otherArray->data); +} + +void CCArray::removeAllObjects() +{ + ccArrayRemoveAllObjects(data); +} + +void CCArray::fastRemoveObjectAtIndex(unsigned int index) +{ + ccArrayFastRemoveObjectAtIndex(data, index); +} + +void CCArray::fastRemoveObject(CCObject* object) +{ + ccArrayFastRemoveObject(data, object); +} + +CCArray::~CCArray() +{ + ccArrayFree(data); +} + +} diff --git a/cocos2dx/support/data_support/ccCArray.h b/cocos2dx/support/data_support/ccCArray.h index 58c5ed07cb..493a069b0e 100644 --- a/cocos2dx/support/data_support/ccCArray.h +++ b/cocos2dx/support/data_support/ccCArray.h @@ -41,8 +41,8 @@ THE SOFTWARE. #include #include -#include "CCMutableArray.h" #include "CCObject.h" +#include "ccMacros.h" namespace cocos2d { @@ -158,6 +158,21 @@ static inline void ccArrayAppendArrayWithResize(ccArray *arr, ccArray *plusArr) ccArrayAppendArray(arr, plusArr); } +static inline void ccArrayInsertObjectAtIndex(ccArray *arr, CCObject* object, unsigned int index) +{ + CCAssert(index<=arr->num, "Invalid index. Out of bounds"); + + ccArrayEnsureExtraCapacity(arr, 1); + + int remaining = arr->num - index; + if( remaining > 0) + memmove(&arr->arr[index+1], &arr->arr[index], sizeof(CCObject*) * remaining ); + + object->retain(); + arr->arr[index] = object; + arr->num++; +} + /** Removes all objects from arr */ static inline void ccArrayRemoveAllObjects(ccArray *arr) { @@ -192,6 +207,13 @@ static inline void ccArrayFastRemoveObjectAtIndex(ccArray *arr, unsigned int ind arr->arr[index] = arr->arr[last]; } +static inline void ccArrayFastRemoveObject(ccArray *arr, CCObject* object) +{ + unsigned int index = ccArrayGetIndexOfObject(arr, object); + if (index != -1) + ccArrayFastRemoveObjectAtIndex(arr, index); +} + /** Searches for the first occurance of object and removes it. If object is not found the function has no effect. */ static inline void ccArrayRemoveObject(ccArray *arr, CCObject* object) diff --git a/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp b/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp index e5ad23dd51..2643e45c89 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp @@ -291,20 +291,19 @@ namespace cocos2d { // update possible children if (m_pChildren && m_pChildren->count()>0) { - CCMutableArray::CCMutableArrayIterator it; - CCSprite *pSprite = NULL; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - pSprite = (CCSprite*)(*it); - if (pSprite) - { - unsigned int ai = pSprite->getAtlasIndex(); - if ( ai >= indexForZ ) - { - pSprite->setAtlasIndex(ai+1); - } - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild) + { + unsigned int ai = pChild->getAtlasIndex(); + if ( ai >= indexForZ ) + { + pChild->setAtlasIndex(ai+1); + } + } + } } m_pTiles[z] = gid; return m_pReusedTile; @@ -495,20 +494,19 @@ namespace cocos2d { // update possible children if (m_pChildren && m_pChildren->count()>0) { - CCMutableArray::CCMutableArrayIterator it; - CCSprite *pSprite = NULL; - for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it) - { - pSprite = (CCSprite*)(*it); - if (pSprite) - { - unsigned int ai = pSprite->getAtlasIndex(); - if ( ai >= atlasIndex ) - { - pSprite->setAtlasIndex(ai-1); - } - } - } + CCObject* pObject = NULL; + CCARRAY_FOREACH(m_pChildren, pObject) + { + CCSprite* pChild = (CCSprite*) pObject; + if (pChild) + { + unsigned int ai = pChild->getAtlasIndex(); + if ( ai >= atlasIndex ) + { + pChild->setAtlasIndex(ai-1); + } + } + } } } } diff --git a/template/xcode3/cocos2d-x_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id b/template/xcode3/cocos2d-x_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id index c6dc467df8..04c6ddc4d7 100644 --- a/template/xcode3/cocos2d-x_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/template/xcode3/cocos2d-x_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -dc7539c2ee19df2be260115613d46d73e062fa72 \ No newline at end of file +b69284f836d8fbe8a1494cda6f723f02f0fc67ea \ No newline at end of file diff --git a/template/xcode3/cocos2d-x_box2d_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id b/template/xcode3/cocos2d-x_box2d_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id index 6813a26270..cb4806ee2a 100644 --- a/template/xcode3/cocos2d-x_box2d_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/template/xcode3/cocos2d-x_box2d_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -55f9260dfa6b4d5f66ed3a42697a1896c779c8d5 \ No newline at end of file +db9df4d8057e9b2d6debaa04e51ba2dacc74b35b \ No newline at end of file diff --git a/template/xcode3/cocos2d-x_chipmunk_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id b/template/xcode3/cocos2d-x_chipmunk_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id index 5a59e34683..c74cbb5a06 100644 --- a/template/xcode3/cocos2d-x_chipmunk_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/template/xcode3/cocos2d-x_chipmunk_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -e61a9910ad0fd7301987ddd8664ed44b731a837d \ No newline at end of file +89cf804b6d8b2ea5f52b32c4b454b034b2251797 \ No newline at end of file diff --git a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id index 59f8b8c6fd..4f2b779ba5 100644 --- a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -38bdb35f5d331e4f326831460ce303bfed7e29bd \ No newline at end of file +e7dda511c80c56d286471fa6602e12ac60e02863 \ No newline at end of file diff --git a/tests/tests/MenuTest/MenuTest.cpp b/tests/tests/MenuTest/MenuTest.cpp index dee188c1ec..615d1edca4 100644 --- a/tests/tests/MenuTest/MenuTest.cpp +++ b/tests/tests/MenuTest/MenuTest.cpp @@ -75,15 +75,14 @@ MenuLayer1::MenuLayer1() int i=0; CCNode* child; - CCMutableArray * pArray = menu->getChildren(); - CCMutableArray::CCMutableArrayIterator it; - - for(it = pArray->begin(); it != pArray->end(); it++) + CCArray * pArray = menu->getChildren(); + CCObject* pObject = NULL; + CCARRAY_FOREACH(pArray, pObject) { - if(*it == NULL) + if(pObject == NULL) break; - child = (CCNode*)(*it); + child = (CCNode*)pObject; CCPoint dstPoint = child->getPosition(); int offset = (int) (s.width/2 + 50); diff --git a/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.cpp b/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.cpp index b93a88714e..f5643bca15 100644 --- a/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.cpp +++ b/tests/tests/PerformanceTest/PerformanceNodeChildrenTest.cpp @@ -7,7 +7,7 @@ enum { kTagBase = 20000, - TEST_COUNT = 5, + TEST_COUNT = 4, }; enum { @@ -210,11 +210,11 @@ void IterateSpriteSheetFastEnum::update(ccTime dt) //CCProfilingBeginTimingBlock(_profilingTimer); // iterate using fast enumeration protocol - CCMutableArray* pChildren = batchNode->getChildren(); - CCMutableArray::CCMutableArrayIterator iter; - for(iter = pChildren->begin(); iter != pChildren->end(); ++iter) + CCArray* pChildren = batchNode->getChildren(); + CCObject* pObject = NULL; + CCARRAY_FOREACH(pChildren, pObject) { - CCSprite* pSprite = (CCSprite*)(*iter); + CCSprite* pSprite = (CCSprite*) pObject; pSprite->setIsVisible(false); } @@ -239,11 +239,11 @@ std::string IterateSpriteSheetFastEnum::subtitle() void IterateSpriteSheetCArray::update(ccTime dt) { // iterate using fast enumeration protocol - CCMutableArray* pChildren = batchNode->getChildren(); - CCMutableArray::CCMutableArrayIterator iter; - for(iter = pChildren->begin(); iter != pChildren->end(); ++iter) + CCArray* pChildren = batchNode->getChildren(); + CCObject* pObject = NULL; + CCARRAY_FOREACH(pChildren, pObject) { - CCSprite* pSprite = (CCSprite*)(*iter); + CCSprite* pSprite = (CCSprite*)pObject; pSprite->setIsVisible(false); } } @@ -314,14 +314,14 @@ void AddSpriteSheet::update(ccTime dt) if( totalToAdd > 0 ) { - std::vector sprites; + CCArray* sprites = CCArray::arrayWithCapacity(totalToAdd); int *zs = new int[totalToAdd]; // Don't include the sprite creation time and random as part of the profiling for(int i=0; igetTexture(), CCRectMake(0,0,32,32)); - sprites.push_back(pSprite); + sprites->addObject(pSprite); zs[i] = CCRANDOM_MINUS1_1() * 50; } @@ -329,7 +329,7 @@ void AddSpriteSheet::update(ccTime dt) // CCProfilingBeginTimingBlock(_profilingTimer); for( int i=0; i < totalToAdd;i++ ) { - batchNode->addChild((CCSprite *)(sprites[i]), zs[i], kTagBase+i); + batchNode->addChild((CCNode*) (sprites->objectAtIndex(i)), zs[i], kTagBase+i); } // [batchNode sortAllChildren]; // CCProfilingEndTimingBlock(_profilingTimer); @@ -369,19 +369,19 @@ void RemoveSpriteSheet::update(ccTime dt) if( totalToAdd > 0 ) { - std::vector sprites; + CCArray* sprites = CCArray::arrayWithCapacity(totalToAdd); // Don't include the sprite creation time as part of the profiling for(int i=0;igetTexture(), CCRectMake(0,0,32,32)); - sprites.push_back(pSprite); + sprites->addObject(pSprite); } // add them with random Z (very important!) for( int i=0; i < totalToAdd;i++ ) { - batchNode->addChild((CCSprite *)(sprites[i]), CCRANDOM_MINUS1_1() * 50, kTagBase+i); + batchNode->addChild((CCNode*) (sprites->objectAtIndex(i)), CCRANDOM_MINUS1_1() * 50, kTagBase+i); } // remove them @@ -418,19 +418,19 @@ void ReorderSpriteSheet::update(ccTime dt) if( totalToAdd > 0 ) { - std::vector sprites; + CCArray* sprites = CCArray::arrayWithCapacity(totalToAdd); // Don't include the sprite creation time as part of the profiling for(int i=0;igetTexture(), CCRectMake(0,0,32,32)); - sprites.push_back(pSprite); + sprites->addObject(pSprite); } // add them with random Z (very important!) for( int i=0; i < totalToAdd;i++ ) { - batchNode->addChild((CCSprite *)(sprites[i]), CCRANDOM_MINUS1_1() * 50, kTagBase+i); + batchNode->addChild((CCNode*) (sprites->objectAtIndex(i)), CCRANDOM_MINUS1_1() * 50, kTagBase+i); } // [batchNode sortAllChildren]; @@ -439,7 +439,8 @@ void ReorderSpriteSheet::update(ccTime dt) //CCProfilingBeginTimingBlock(_profilingTimer); for( int i=0;i < totalToAdd;i++) { - batchNode->reorderChild(batchNode->getChildren()->getObjectAtIndex(i), CCRANDOM_MINUS1_1() * 50); + CCNode* pNode = (CCNode*) (batchNode->getChildren()->objectAtIndex(i)); + batchNode->reorderChild(pNode, CCRANDOM_MINUS1_1() * 50); } // [batchNode sortAllChildren]; //CCProfilingEndTimingBlock(_profilingTimer); @@ -464,7 +465,7 @@ std::string ReorderSpriteSheet::subtitle() void runNodeChildrenTest() { - IterateSpriteSheet* pScene = new IterateSpriteSheetFastEnum(); + IterateSpriteSheet* pScene = new IterateSpriteSheetCArray(); pScene->initWithQuantityOfNodes(kNodesIncrease); CCDirector::sharedDirector()->replaceScene(pScene); diff --git a/tests/tests/PerformanceTest/PerformanceSpriteTest.cpp b/tests/tests/PerformanceTest/PerformanceSpriteTest.cpp index 0f2ae18e38..cacab7ed36 100644 --- a/tests/tests/PerformanceTest/PerformanceSpriteTest.cpp +++ b/tests/tests/PerformanceTest/PerformanceSpriteTest.cpp @@ -10,7 +10,7 @@ enum { enum { kTagInfoLayer = 1, kTagMainLayer = 2, - kTagMenuLayer = 1000, + kTagMenuLayer = (kMaxNodes + 1000), }; static int s_nSpriteCurCase = 0; diff --git a/tests/tests/SchedulerTest/SchedulerTest.cpp b/tests/tests/SchedulerTest/SchedulerTest.cpp index 34a345542a..49a04af47d 100644 --- a/tests/tests/SchedulerTest/SchedulerTest.cpp +++ b/tests/tests/SchedulerTest/SchedulerTest.cpp @@ -446,13 +446,12 @@ void SchedulerUpdate::onEnter() void SchedulerUpdate::removeUpdates(ccTime dt) { - CCMutableArray * children = getChildren(); - CCMutableArray::CCMutableArrayIterator it; - + CCArray* children = getChildren(); CCNode* pNode; - for (it = children->begin(); it != children->end(); it++) + CCObject* pObject; + CCARRAY_FOREACH(children, pObject) { - pNode = (CCNode*)(*it); + pNode = (CCNode*)pObject; if (! pNode) { diff --git a/tests/tests/SpriteTest/SpriteTest.cpp.REMOVED.git-id b/tests/tests/SpriteTest/SpriteTest.cpp.REMOVED.git-id index b20d81d4eb..85377e616c 100644 --- a/tests/tests/SpriteTest/SpriteTest.cpp.REMOVED.git-id +++ b/tests/tests/SpriteTest/SpriteTest.cpp.REMOVED.git-id @@ -1 +1 @@ -9e16647d99e1705b6436f4764cfacc6a478d3715 \ No newline at end of file +225b2feefddaa0d61cec8c434664bc2738986082 \ No newline at end of file diff --git a/tests/tests/TileMapTest/TileMapTest.cpp b/tests/tests/TileMapTest/TileMapTest.cpp index 02bafd5529..029ffac9fe 100644 --- a/tests/tests/TileMapTest/TileMapTest.cpp +++ b/tests/tests/TileMapTest/TileMapTest.cpp @@ -124,12 +124,12 @@ TMXOrthoTest::TMXOrthoTest() CCSize s = map->getContentSize(); ////----UXLOG("ContentSize: %f, %f", s.width,s.height); - CCMutableArray * pChildrenArray = map->getChildren(); + CCArray * pChildrenArray = map->getChildren(); CCSpriteBatchNode* child = NULL; - CCMutableArray::CCMutableArrayIterator it; - for( it = pChildrenArray->begin(); it != pChildrenArray->end(); it++) + CCObject* pObject = NULL; + CCARRAY_FOREACH(pChildrenArray, pObject) { - child = (CCSpriteBatchNode*)(*it); + child = (CCSpriteBatchNode*)pObject; if(!child) break; @@ -173,12 +173,12 @@ TMXOrthoTest2::TMXOrthoTest2() CCSize s = map->getContentSize(); ////----UXLOG("ContentSize: %f, %f", s.width,s.height); - CCMutableArray * pChildrenArray = map->getChildren(); + CCArray* pChildrenArray = map->getChildren(); CCSpriteBatchNode* child = NULL; - CCMutableArray::CCMutableArrayIterator it; - for( it = pChildrenArray->begin(); it != pChildrenArray->end(); it++) + CCObject* pObject = NULL; + CCARRAY_FOREACH(pChildrenArray, pObject) { - child = (CCSpriteBatchNode*)(*it); + child = (CCSpriteBatchNode*)pObject; if(!child) break; @@ -207,12 +207,12 @@ TMXOrthoTest3::TMXOrthoTest3() CCSize s = map->getContentSize(); ////----UXLOG("ContentSize: %f, %f", s.width,s.height); - CCMutableArray * pChildrenArray = map->getChildren(); + CCArray* pChildrenArray = map->getChildren(); CCSpriteBatchNode* child = NULL; - CCMutableArray::CCMutableArrayIterator it; - for( it = pChildrenArray->begin(); it != pChildrenArray->end(); it++) + CCObject* pObject = NULL; + CCARRAY_FOREACH(pChildrenArray, pObject) { - child = (CCSpriteBatchNode*)(*it); + child = (CCSpriteBatchNode*)pObject; if(!child) break; @@ -242,12 +242,12 @@ TMXOrthoTest4::TMXOrthoTest4() CCSize s1 = map->getContentSize(); ////----UXLOG("ContentSize: %f, %f", s1.width,s1.height); - CCMutableArray * pChildrenArray = map->getChildren(); + CCArray* pChildrenArray = map->getChildren(); CCSpriteBatchNode* child = NULL; - CCMutableArray::CCMutableArrayIterator it; - for( it = pChildrenArray->begin(); it != pChildrenArray->end(); it++) + CCObject* pObject = NULL; + CCARRAY_FOREACH(pChildrenArray, pObject) { - child = (CCSpriteBatchNode*)(*it); + child = (CCSpriteBatchNode*)pObject; if(!child) break; @@ -547,12 +547,12 @@ TMXUncompressedTest::TMXUncompressedTest() map->runAction(CCMoveTo::actionWithDuration(1.0f, ccp( -ms.width * ts.width/2, -ms.height * ts.height/2 ) )); // testing release map - CCMutableArray * pChildrenArray = map->getChildren(); + CCArray* pChildrenArray = map->getChildren(); CCTMXLayer* layer; - CCMutableArray::CCMutableArrayIterator it; - for( it = pChildrenArray->begin(); it != pChildrenArray->end(); it++) + CCObject* pObject = NULL; + CCARRAY_FOREACH(pChildrenArray, pObject) { - layer= (CCTMXLayer*)(*it); + layer= (CCTMXLayer*)pObject; if(!layer) break; @@ -1122,13 +1122,12 @@ TMXBug987::TMXBug987() CCSize s1 = map->getContentSize(); CCLOG("ContentSize: %f, %f", s1.width,s1.height); - CCMutableArray* childs = map->getChildren(); - CCTMXLayer* pNode; - CCMutableArray::CCMutableArrayIterator it; - - for(it = childs->begin(); it != childs->end(); it++) + CCArray* childs = map->getChildren(); + CCTMXLayer* pNode; + CCObject* pObject = NULL; + CCARRAY_FOREACH(childs, pObject) { - pNode = (CCTMXLayer*)(*it); + pNode = (CCTMXLayer*) pObject; CC_BREAK_IF(!pNode); pNode->getTexture()->setAntiAliasTexParameters(); }