This commit is contained in:
Ming 2010-08-18 06:57:36 +00:00
parent acef33a591
commit 2f638dbe14
7 changed files with 56 additions and 64 deletions

View File

@ -48,33 +48,25 @@ using namespace cocos2d;
namespace cocos2d { namespace cocos2d {
// singleton stuff // singleton stuff
static CCDirector *pobSharedDirector = NULL; static CCDisplayLinkDirector s_sharedDirector;
static bool s_bFirstRun = true;
#define kDefaultFPS 60 // 60 frames per second #define kDefaultFPS 60 // 60 frames per second
extern string cocos2dVersion(void); extern string cocos2dVersion(void);
CCDirector* CCDirector::getSharedDirector(void) CCDirector* CCDirector::getSharedDirector(void)
{ {
if (! pobSharedDirector) if (s_bFirstRun)
{ {
// s_sharedDirector.init();
// Default Director is TimerDirector s_bFirstRun = false;
//
//pobSharedDirector = new CCTimerDirector();
//pobSharedDirector->init();
// we now only support CCDisplayLinkDirector
pobSharedDirector = new CCDisplayLinkDirector();
pobSharedDirector->init();
} }
return pobSharedDirector; return &s_sharedDirector;
} }
bool CCDirector::setDierectorType(ccDirectorType obDirectorType) bool CCDirector::setDierectorType(ccDirectorType obDirectorType)
{ {
assert(pobSharedDirector==NULL);
/* /*
switch (obDirectorType) switch (obDirectorType)
{ {
@ -152,14 +144,13 @@ CCDirector::~CCDirector(void)
// FPSLabel->release(); // FPSLabel->release();
#endif #endif
m_pRunningScene->release(); CCX_SAFE_RELEASE(m_pRunningScene);
m_pobScenesStack->release(); CCX_SAFE_RELEASE(m_pobScenesStack);
// pop the autorelease pool // pop the autorelease pool
NSPoolManager::getInstance()->pop(); NSPoolManager::getInstance()->pop();
pobSharedDirector = NULL;
// delete m_pLastUpdate // delete m_pLastUpdate
delete m_pLastUpdate; delete m_pLastUpdate;
m_pLastUpdate = NULL; m_pLastUpdate = NULL;
@ -613,8 +604,8 @@ void CCDirector::end(void)
// purge all managers // purge all managers
CCSpriteFrameCache::purgeSharedSpriteFrameCache(); CCSpriteFrameCache::purgeSharedSpriteFrameCache();
CCScheduler::purgeSharedScheduler();
CCActionManager::getSharedManager()->purgeSharedManager(); CCActionManager::getSharedManager()->purgeSharedManager();
CCScheduler::purgeSharedScheduler();
CCTextureCache::purgeSharedTextureCache(); CCTextureCache::purgeSharedTextureCache();
// OpenGL view // OpenGL view

View File

@ -25,7 +25,9 @@ THE SOFTWARE.
#include "CCScheduler.h" #include "CCScheduler.h"
#include "ccMacros.h" #include "ccMacros.h"
#include "support/data_support/utlist.h" #include "support/data_support/utlist.h"
#include "support/data_support/ccArray.h"
#include "NSMutableArray.h" #include "NSMutableArray.h"
#include "Cocos2dDefine.h"
#include <assert.h> #include <assert.h>
namespace cocos2d { namespace cocos2d {
@ -53,7 +55,7 @@ typedef struct _hashUpdateEntry
// Hash Element used for "selectors with interval" // Hash Element used for "selectors with interval"
typedef struct _hashSelectorEntry typedef struct _hashSelectorEntry
{ {
NSMutableArray<CCTimer*> *timers; ccArray *timers;
SelectorProtocol *target; // hash key (retained) SelectorProtocol *target; // hash key (retained)
unsigned int timerIndex; unsigned int timerIndex;
CCTimer *currentTimer; CCTimer *currentTimer;
@ -134,7 +136,6 @@ CCScheduler::~CCScheduler(void)
{ {
unscheduleAllSelectors(); unscheduleAllSelectors();
delete pSharedScheduler;
pSharedScheduler = NULL; pSharedScheduler = NULL;
} }
@ -173,7 +174,10 @@ CCScheduler* CCScheduler::init(void)
void CCScheduler::removeHashElement(_hashSelectorEntry *pElement) void CCScheduler::removeHashElement(_hashSelectorEntry *pElement)
{ {
delete pElement->timers; ccArrayFree(pElement->timers);
NSObject *pObj = dynamic_cast<NSObject*>(pElement->target);
CCX_SAFE_RELEASE(pObj);
pElement->target = NULL;
HASH_DEL(m_pHashForSelectors, pElement); HASH_DEL(m_pHashForSelectors, pElement);
free(pElement); free(pElement);
} }
@ -217,16 +221,16 @@ void CCScheduler::scheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol *p
if (pElement->timers == NULL) if (pElement->timers == NULL)
{ {
pElement->timers = new NSMutableArray<CCTimer*>(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(); CCTimer *pTimer = new CCTimer();
pTimer->initWithTarget(pTarget, pfnSelector, fInterval); pTimer->initWithTarget(pTarget, pfnSelector, fInterval);
pElement->timers->addObject(pTimer); ccArrayAppendObject(pElement->timers, pTimer);
pTimer->release(); pTimer->release();
} }
@ -246,11 +250,9 @@ void CCScheduler::unscheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol
if (pElement) if (pElement)
{ {
NSMutableArray<CCTimer*>::NSMutableArrayIterator iter; for (unsigned int i = 0; i < pElement->timers->num; ++i)
unsigned int i;
for (iter = pElement->timers->begin(), i = 0; iter != pElement->timers->end(); ++iter, ++i)
{ {
CCTimer *pTimer = *iter; CCTimer *pTimer = static_cast<CCTimer*>(pElement->timers->arr[i]);
if (pfnSelector == pTimer->m_pfnSelector) if (pfnSelector == pTimer->m_pfnSelector)
{ {
@ -260,7 +262,7 @@ void CCScheduler::unscheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol
pElement->currentTimerSalvaged = true; pElement->currentTimerSalvaged = true;
} }
pElement->timers->removeObjectAtIndex(i); ccArrayRemoveObjectAtIndex(pElement->timers, i );
// update timerIndex in case we are in tick:, looping over the actions // update timerIndex in case we are in tick:, looping over the actions
if (pElement->timerIndex >= i) if (pElement->timerIndex >= i)
@ -268,7 +270,7 @@ void CCScheduler::unscheduleSelector(SEL_SCHEDULE pfnSelector, SelectorProtocol
pElement->timerIndex--; pElement->timerIndex--;
} }
if (pElement->timers->count() == 0) if (pElement->timers->num == 0)
{ {
if (m_pCurrentTarget == pElement) if (m_pCurrentTarget == pElement)
{ {
@ -337,6 +339,7 @@ void CCScheduler::priorityIn(tListEntry **ppList, SelectorProtocol *pTarget, int
// update hash entry for quick access // update hash entry for quick access
tHashUpdateEntry *pHashElement = (tHashUpdateEntry *)calloc(sizeof(*pHashElement), 1); tHashUpdateEntry *pHashElement = (tHashUpdateEntry *)calloc(sizeof(*pHashElement), 1);
pHashElement->target = pTarget; pHashElement->target = pTarget;
dynamic_cast<NSObject*>(pTarget)->retain();
pHashElement->list = ppList; pHashElement->list = ppList;
pHashElement->entry = pListElement; pHashElement->entry = pListElement;
HASH_ADD_INT(m_pHashForUpdates, target, pHashElement); 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) void CCScheduler::appendIn(_listEntry **ppList, SelectorProtocol *pTarget, bool bPaused)
{ {
tListEntry *pListElement = (tListEntry *)malloc(sizeof(*pListElement)); tListEntry *pListElement = (tListEntry *)malloc(sizeof(*pListElement));
;
pListElement->target = pTarget; pListElement->target = pTarget;
pListElement->paused = bPaused; pListElement->paused = bPaused;
@ -355,7 +357,8 @@ void CCScheduler::appendIn(_listEntry **ppList, SelectorProtocol *pTarget, bool
// update hash entry for quicker access // update hash entry for quicker access
tHashUpdateEntry *pHashElement = (tHashUpdateEntry *)calloc(sizeof(*pHashElement), 1); tHashUpdateEntry *pHashElement = (tHashUpdateEntry *)calloc(sizeof(*pHashElement), 1);
// hashElement->target = [target retain]; pHashElement->target = pTarget;
dynamic_cast<NSObject*>(pTarget)->retain();
pHashElement->list = ppList; pHashElement->list = ppList;
pHashElement->entry = pListElement; pHashElement->entry = pListElement;
HASH_ADD_INT(m_pHashForUpdates, target, pHashElement); HASH_ADD_INT(m_pHashForUpdates, target, pHashElement);
@ -402,6 +405,9 @@ void CCScheduler::unscheduleUpdateForTarget(const SelectorProtocol *pTarget)
free(pElement->entry); free(pElement->entry);
// hash entry // hash entry
NSObject *pObj = dynamic_cast<NSObject*>(pElement->target);
CCX_SAFE_RELEASE(pObj);
pElement->target = NULL;
HASH_DEL(m_pHashForUpdates, pElement); HASH_DEL(m_pHashForUpdates, pElement);
free(pElement); free(pElement);
} }
@ -447,13 +453,13 @@ void CCScheduler::unscheduleAllSelectorsForTarget(SelectorProtocol *pTarget)
if (pElement) if (pElement)
{ {
if (pElement->timers->containsObject(pElement->currentTimer) if (ccArrayContainsObject(pElement->timers, pElement->currentTimer)
&& (! pElement->currentTimerSalvaged)) && (! pElement->currentTimerSalvaged))
{ {
pElement->currentTimer->retain(); pElement->currentTimer->retain();
pElement->currentTimerSalvaged = true; pElement->currentTimerSalvaged = true;
} }
pElement->timers->removeAllObjects(); ccArrayRemoveAllObjects(pElement->timers);
if (m_pCurrentTarget == pElement) if (m_pCurrentTarget == pElement)
{ {
@ -560,9 +566,9 @@ void CCScheduler::tick(ccTime dt)
if (! m_pCurrentTarget->paused) if (! m_pCurrentTarget->paused)
{ {
// The 'timers' array may change while inside this loop // 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<CCTimer*>(elt->timers->arr[elt->timerIndex]);
elt->currentTimerSalvaged = false; elt->currentTimerSalvaged = false;
elt->currentTimer->update(dt); elt->currentTimer->update(dt);
@ -584,7 +590,7 @@ void CCScheduler::tick(ccTime dt)
elt = (tHashSelectorEntry *)elt->hh.next; elt = (tHashSelectorEntry *)elt->hh.next;
// only delete currentTarget if no actions were scheduled during the cycle (issue #481) // 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); removeHashElement(m_pCurrentTarget);
} }

View File

@ -56,8 +56,7 @@ CCActionManager* CCActionManager::getSharedManager(void)
void CCActionManager::purgeSharedManager(void) void CCActionManager::purgeSharedManager(void)
{ {
CCScheduler::getSharedScheduler()->unscheduleUpdateForTarget(this); CCScheduler::getSharedScheduler()->unscheduleUpdateForTarget(this);
delete gSharedManager; CCX_SAFE_RELEASE(gSharedManager);
gSharedManager = NULL;
} }
CCActionManager::CCActionManager(void) CCActionManager::CCActionManager(void)

View File

@ -37,6 +37,7 @@ NSAutoreleasePool::NSAutoreleasePool(void)
NSAutoreleasePool::~NSAutoreleasePool(void) NSAutoreleasePool::~NSAutoreleasePool(void)
{ {
clear();
delete m_pManagedObjectArray; 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) NSPoolManager::NSPoolManager(void)
{ {
m_pReleasePoolStack = new stack<NSAutoreleasePool *>(); m_pReleasePoolStack = new stack<NSAutoreleasePool *>();
m_pCurReleasePool = NULL;
} }
NSPoolManager::~NSPoolManager(void) NSPoolManager::~NSPoolManager(void)
@ -104,7 +104,6 @@ void NSPoolManager::finalize(void)
NSAutoreleasePool *pTop; NSAutoreleasePool *pTop;
while (pTop = m_pReleasePoolStack->top()) while (pTop = m_pReleasePoolStack->top())
{ {
pTop->clear();
m_pReleasePoolStack->pop(); m_pReleasePoolStack->pop();
} }
} }
@ -113,17 +112,18 @@ void NSPoolManager::finalize(void)
void NSPoolManager::push(void) void NSPoolManager::push(void)
{ {
NSAutoreleasePool *pPool = new NSAutoreleasePool(); NSAutoreleasePool *pPool = new NSAutoreleasePool();
m_pCurReleasePool = pPool;
m_pReleasePoolStack->push(pPool); m_pReleasePoolStack->push(pPool);
} }
void NSPoolManager::pop(void) void NSPoolManager::pop(void)
{ {
if (! m_pReleasePoolStack->empty()) if (m_pReleasePoolStack->top())
{ {
delete m_pReleasePoolStack->top();
m_pReleasePoolStack->pop(); m_pReleasePoolStack->pop();
} }
} }
void NSPoolManager::addObject(NSObject *pObject) void NSPoolManager::addObject(NSObject *pObject)
@ -133,20 +133,18 @@ void NSPoolManager::addObject(NSObject *pObject)
void NSPoolManager::removeObject(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) NSAutoreleasePool* NSPoolManager::getCurReleasePool(void)
{ {
if (! m_pCurReleasePool) if (m_pReleasePoolStack->empty())
{ {
push(); push();
} }
assert(m_pCurReleasePool); return m_pReleasePoolStack->top();
return m_pCurReleasePool;
} }
}//namespace cocos2d { }//namespace cocos2d

View File

@ -69,7 +69,6 @@ private:
private: private:
std::stack<NSAutoreleasePool *> *m_pReleasePoolStack; std::stack<NSAutoreleasePool *> *m_pReleasePoolStack;
NSAutoreleasePool *m_pCurReleasePool;
}; };
}//namespace cocos2d }//namespace cocos2d

View File

@ -48,8 +48,7 @@ CCSpriteFrameCache* CCSpriteFrameCache::sharedSpriteFrameCache(void)
void CCSpriteFrameCache::purgeSharedSpriteFrameCache(void) void CCSpriteFrameCache::purgeSharedSpriteFrameCache(void)
{ {
pSharedSpriteFrameCache->release(); CCX_SAFE_RELEASE(pSharedSpriteFrameCache);
pSharedSpriteFrameCache = NULL;
} }
CCSpriteFrameCache* CCSpriteFrameCache::init(void) CCSpriteFrameCache* CCSpriteFrameCache::init(void)

View File

@ -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. */ /** Returns index of first occurence of object, UXNotFound if object not found. */
static inline unsigned int ccArrayGetIndexOfObject(ccArray *arr, NSObject* object) 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) 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. */ /** 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) if (arr->arr[i] == (NSObject*)value)
{ {