From 2f638dbe14a89b31a3f00f13250a7ff7126b1c26 Mon Sep 17 00:00:00 2001 From: Ming Date: Wed, 18 Aug 2010 06:57:36 +0000 Subject: [PATCH] issue #55 --- cocos2dx/CCDirector.cpp | 29 ++++------- cocos2dx/CCScheduler.cpp | 54 +++++++++++--------- cocos2dx/actions/CCActionManager.cpp | 3 +- cocos2dx/cocoa/NSAutoreleasePool.cpp | 24 ++++----- cocos2dx/include/NSAutoreleasePool.h | 1 - cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp | 3 +- cocos2dx/support/data_support/ccArray.h | 6 +-- 7 files changed, 56 insertions(+), 64 deletions(-) diff --git a/cocos2dx/CCDirector.cpp b/cocos2dx/CCDirector.cpp index 5af455f327..d5449c2a9e 100644 --- a/cocos2dx/CCDirector.cpp +++ b/cocos2dx/CCDirector.cpp @@ -48,33 +48,25 @@ using namespace cocos2d; namespace cocos2d { // singleton stuff -static CCDirector *pobSharedDirector = NULL; +static CCDisplayLinkDirector s_sharedDirector; +static bool s_bFirstRun = true; #define kDefaultFPS 60 // 60 frames per second extern string cocos2dVersion(void); CCDirector* CCDirector::getSharedDirector(void) { - if (! pobSharedDirector) + if (s_bFirstRun) { - // - // Default Director is TimerDirector - // - //pobSharedDirector = new CCTimerDirector(); - //pobSharedDirector->init(); - - // we now only support CCDisplayLinkDirector - pobSharedDirector = new CCDisplayLinkDirector(); - pobSharedDirector->init(); + s_sharedDirector.init(); + s_bFirstRun = false; } - return pobSharedDirector; + return &s_sharedDirector; } bool CCDirector::setDierectorType(ccDirectorType obDirectorType) { - assert(pobSharedDirector==NULL); - /* switch (obDirectorType) { @@ -152,14 +144,13 @@ CCDirector::~CCDirector(void) // FPSLabel->release(); #endif - m_pRunningScene->release(); - m_pobScenesStack->release(); + CCX_SAFE_RELEASE(m_pRunningScene); + CCX_SAFE_RELEASE(m_pobScenesStack); + // pop the autorelease pool NSPoolManager::getInstance()->pop(); - pobSharedDirector = NULL; - // delete m_pLastUpdate delete m_pLastUpdate; m_pLastUpdate = NULL; @@ -613,8 +604,8 @@ void CCDirector::end(void) // purge all managers CCSpriteFrameCache::purgeSharedSpriteFrameCache(); - CCScheduler::purgeSharedScheduler(); CCActionManager::getSharedManager()->purgeSharedManager(); + CCScheduler::purgeSharedScheduler(); CCTextureCache::purgeSharedTextureCache(); // OpenGL view diff --git a/cocos2dx/CCScheduler.cpp b/cocos2dx/CCScheduler.cpp index c20f911b99..d8b48b5947 100644 --- a/cocos2dx/CCScheduler.cpp +++ b/cocos2dx/CCScheduler.cpp @@ -25,7 +25,9 @@ THE SOFTWARE. #include "CCScheduler.h" #include "ccMacros.h" #include "support/data_support/utlist.h" +#include "support/data_support/ccArray.h" #include "NSMutableArray.h" +#include "Cocos2dDefine.h" #include namespace cocos2d { @@ -37,7 +39,7 @@ typedef struct _listEntry { struct _listEntry *prev, *next; SelectorProtocol *target; // not retained (retained by hashUpdateEntry) - int priority; + int priority; bool paused; } tListEntry; @@ -53,9 +55,9 @@ typedef struct _hashUpdateEntry // Hash Element used for "selectors with interval" typedef struct _hashSelectorEntry { - NSMutableArray *timers; + ccArray *timers; SelectorProtocol *target; // hash key (retained) - unsigned int timerIndex; + unsigned int timerIndex; CCTimer *currentTimer; bool currentTimerSalvaged; bool paused; @@ -134,7 +136,6 @@ CCScheduler::~CCScheduler(void) { unscheduleAllSelectors(); - delete pSharedScheduler; pSharedScheduler = NULL; } @@ -173,7 +174,10 @@ CCScheduler* CCScheduler::init(void) void CCScheduler::removeHashElement(_hashSelectorEntry *pElement) { - delete pElement->timers; + ccArrayFree(pElement->timers); + NSObject *pObj = dynamic_cast(pElement->target); + CCX_SAFE_RELEASE(pObj); + pElement->target = NULL; HASH_DEL(m_pHashForSelectors, pElement); free(pElement); } @@ -217,16 +221,16 @@ void CCScheduler::scheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol *p if (pElement->timers == NULL) { - pElement->timers = new NSMutableArray(10); + pElement->timers = ccArrayNew(10); + }else + if (pElement->timers->num == pElement->timers->max) + { + ccArrayDoubleCapacity(pElement->timers); } - - // NSMutableArray will increase it's capacity automatically - //else if( element->timers->num == element->timers->max ) - //ccArrayDoubleCapacity(element->timers); CCTimer *pTimer = new CCTimer(); pTimer->initWithTarget(pTarget, pfnSelector, fInterval); - pElement->timers->addObject(pTimer); + ccArrayAppendObject(pElement->timers, pTimer); pTimer->release(); } @@ -246,11 +250,9 @@ void CCScheduler::unscheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol if (pElement) { - NSMutableArray::NSMutableArrayIterator iter; - unsigned int i; - for (iter = pElement->timers->begin(), i = 0; iter != pElement->timers->end(); ++iter, ++i) + for (unsigned int i = 0; i < pElement->timers->num; ++i) { - CCTimer *pTimer = *iter; + CCTimer *pTimer = static_cast(pElement->timers->arr[i]); if (pfnSelector == pTimer->m_pfnSelector) { @@ -260,7 +262,7 @@ void CCScheduler::unscheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol pElement->currentTimerSalvaged = true; } - pElement->timers->removeObjectAtIndex(i); + ccArrayRemoveObjectAtIndex(pElement->timers, i ); // update timerIndex in case we are in tick:, looping over the actions if (pElement->timerIndex >= i) @@ -268,7 +270,7 @@ void CCScheduler::unscheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol pElement->timerIndex--; } - if (pElement->timers->count() == 0) + if (pElement->timers->num == 0) { if (m_pCurrentTarget == pElement) { @@ -337,6 +339,7 @@ void CCScheduler::priorityIn(tListEntry **ppList, SelectorProtocol *pTarget, int // update hash entry for quick access tHashUpdateEntry *pHashElement = (tHashUpdateEntry *)calloc(sizeof(*pHashElement), 1); pHashElement->target = pTarget; + dynamic_cast(pTarget)->retain(); pHashElement->list = ppList; pHashElement->entry = pListElement; HASH_ADD_INT(m_pHashForUpdates, target, pHashElement); @@ -345,7 +348,6 @@ void CCScheduler::priorityIn(tListEntry **ppList, SelectorProtocol *pTarget, int void CCScheduler::appendIn(_listEntry **ppList, SelectorProtocol *pTarget, bool bPaused) { tListEntry *pListElement = (tListEntry *)malloc(sizeof(*pListElement)); -; pListElement->target = pTarget; pListElement->paused = bPaused; @@ -355,7 +357,8 @@ void CCScheduler::appendIn(_listEntry **ppList, SelectorProtocol *pTarget, bool // update hash entry for quicker access tHashUpdateEntry *pHashElement = (tHashUpdateEntry *)calloc(sizeof(*pHashElement), 1); - // hashElement->target = [target retain]; + pHashElement->target = pTarget; + dynamic_cast(pTarget)->retain(); pHashElement->list = ppList; pHashElement->entry = pListElement; HASH_ADD_INT(m_pHashForUpdates, target, pHashElement); @@ -402,6 +405,9 @@ void CCScheduler::unscheduleUpdateForTarget(const SelectorProtocol *pTarget) free(pElement->entry); // hash entry + NSObject *pObj = dynamic_cast(pElement->target); + CCX_SAFE_RELEASE(pObj); + pElement->target = NULL; HASH_DEL(m_pHashForUpdates, pElement); free(pElement); } @@ -447,13 +453,13 @@ void CCScheduler::unscheduleAllSelectorsForTarget(SelectorProtocol *pTarget) if (pElement) { - if (pElement->timers->containsObject(pElement->currentTimer) + if (ccArrayContainsObject(pElement->timers, pElement->currentTimer) && (! pElement->currentTimerSalvaged)) { pElement->currentTimer->retain(); pElement->currentTimerSalvaged = true; } - pElement->timers->removeAllObjects(); + ccArrayRemoveAllObjects(pElement->timers); if (m_pCurrentTarget == pElement) { @@ -560,9 +566,9 @@ void CCScheduler::tick(ccTime dt) if (! m_pCurrentTarget->paused) { // The 'timers' array may change while inside this loop - for (elt->timerIndex = 0; elt->timerIndex < elt->timers->count(); ++(elt->timerIndex)) + for (elt->timerIndex = 0; elt->timerIndex < elt->timers->num; ++(elt->timerIndex)) { - elt->currentTimer = elt->timers->getObjectAtIndex(elt->timerIndex); + elt->currentTimer = static_cast(elt->timers->arr[elt->timerIndex]); elt->currentTimerSalvaged = false; elt->currentTimer->update(dt); @@ -584,7 +590,7 @@ void CCScheduler::tick(ccTime dt) elt = (tHashSelectorEntry *)elt->hh.next; // only delete currentTarget if no actions were scheduled during the cycle (issue #481) - if (m_bCurrentTargetSalvaged && m_pCurrentTarget->timers->count() == 0) + if (m_bCurrentTargetSalvaged && m_pCurrentTarget->timers->num == 0) { removeHashElement(m_pCurrentTarget); } diff --git a/cocos2dx/actions/CCActionManager.cpp b/cocos2dx/actions/CCActionManager.cpp index f2aa82a969..c928f05c32 100644 --- a/cocos2dx/actions/CCActionManager.cpp +++ b/cocos2dx/actions/CCActionManager.cpp @@ -56,8 +56,7 @@ CCActionManager* CCActionManager::getSharedManager(void) void CCActionManager::purgeSharedManager(void) { CCScheduler::getSharedScheduler()->unscheduleUpdateForTarget(this); - delete gSharedManager; - gSharedManager = NULL; + CCX_SAFE_RELEASE(gSharedManager); } CCActionManager::CCActionManager(void) diff --git a/cocos2dx/cocoa/NSAutoreleasePool.cpp b/cocos2dx/cocoa/NSAutoreleasePool.cpp index 964e43fb3f..76d49c159c 100644 --- a/cocos2dx/cocoa/NSAutoreleasePool.cpp +++ b/cocos2dx/cocoa/NSAutoreleasePool.cpp @@ -37,6 +37,7 @@ NSAutoreleasePool::NSAutoreleasePool(void) NSAutoreleasePool::~NSAutoreleasePool(void) { + clear(); delete m_pManagedObjectArray; } @@ -63,7 +64,7 @@ void NSAutoreleasePool::clear(void) } } - m_pManagedObjectArray->removeAllObjects(); +// m_pManagedObjectArray->removeAllObjects(); } } @@ -87,7 +88,6 @@ NSPoolManager* NSPoolManager::getInstance(void) NSPoolManager::NSPoolManager(void) { m_pReleasePoolStack = new stack(); - m_pCurReleasePool = NULL; } NSPoolManager::~NSPoolManager(void) @@ -104,7 +104,6 @@ void NSPoolManager::finalize(void) NSAutoreleasePool *pTop; while (pTop = m_pReleasePoolStack->top()) { - pTop->clear(); m_pReleasePoolStack->pop(); } } @@ -113,17 +112,18 @@ void NSPoolManager::finalize(void) void NSPoolManager::push(void) { NSAutoreleasePool *pPool = new NSAutoreleasePool(); - m_pCurReleasePool = pPool; m_pReleasePoolStack->push(pPool); } void NSPoolManager::pop(void) { - if (! m_pReleasePoolStack->empty()) + if (m_pReleasePoolStack->top()) { - m_pReleasePoolStack->pop(); + delete m_pReleasePoolStack->top(); + m_pReleasePoolStack->pop(); } + } void NSPoolManager::addObject(NSObject *pObject) @@ -133,20 +133,18 @@ void NSPoolManager::addObject(NSObject *pObject) void NSPoolManager::removeObject(NSObject *pObject) { - assert(m_pCurReleasePool); + assert(m_pReleasePoolStack->top()); - m_pCurReleasePool->removeObject(pObject); + m_pReleasePoolStack->top()->removeObject(pObject); } NSAutoreleasePool* NSPoolManager::getCurReleasePool(void) { - if (! m_pCurReleasePool) + if (m_pReleasePoolStack->empty()) { push(); } - assert(m_pCurReleasePool); - - return m_pCurReleasePool; + return m_pReleasePoolStack->top(); } -}//namespace cocos2d { +}//namespace cocos2d diff --git a/cocos2dx/include/NSAutoreleasePool.h b/cocos2dx/include/NSAutoreleasePool.h index 9ab9bd343b..2d42d3ae7e 100644 --- a/cocos2dx/include/NSAutoreleasePool.h +++ b/cocos2dx/include/NSAutoreleasePool.h @@ -69,7 +69,6 @@ private: private: std::stack *m_pReleasePoolStack; - NSAutoreleasePool *m_pCurReleasePool; }; }//namespace cocos2d diff --git a/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp b/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp index 500510c2bd..7e4513054d 100644 --- a/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp +++ b/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp @@ -48,8 +48,7 @@ CCSpriteFrameCache* CCSpriteFrameCache::sharedSpriteFrameCache(void) void CCSpriteFrameCache::purgeSharedSpriteFrameCache(void) { - pSharedSpriteFrameCache->release(); - pSharedSpriteFrameCache = NULL; + CCX_SAFE_RELEASE(pSharedSpriteFrameCache); } CCSpriteFrameCache* CCSpriteFrameCache::init(void) diff --git a/cocos2dx/support/data_support/ccArray.h b/cocos2dx/support/data_support/ccArray.h index 47687a6d60..1f577f29ff 100644 --- a/cocos2dx/support/data_support/ccArray.h +++ b/cocos2dx/support/data_support/ccArray.h @@ -105,7 +105,7 @@ static inline void ccArrayEnsureExtraCapacity(ccArray *arr, unsigned int extra) /** Returns index of first occurence of object, UXNotFound if object not found. */ static inline unsigned int ccArrayGetIndexOfObject(ccArray *arr, NSObject* object) { - for ( UINT i = 0; i < arr->num; i++) + for ( unsigned int i = 0; i < arr->num; i++) { if (arr->arr[i] == object) { @@ -276,9 +276,9 @@ static inline void ccCArrayEnsureExtraCapacity(ccCArray *arr, unsigned int extra } /** Returns index of first occurence of value, NSNotFound if value not found. */ -static inline UINT ccCArrayGetIndexOfValue(ccCArray *arr, void* value) +static inline unsigned int ccCArrayGetIndexOfValue(ccCArray *arr, void* value) { - for (UINT i = 0; i < arr->num; i++) + for (unsigned int i = 0; i < arr->num; i++) { if (arr->arr[i] == (NSObject*)value) {