|
????[????]??????????http://blog.leafsoar.com/archives/2013/06-04-10.html
???? Cocos2d-x ?????? ???????? Cocos2d-x ???????????????????????????? Cocos2d-x ??????????????????????? ~?????????????????????????????????????????????~~
????? ?????????????????????? ? ??????????????????????????????????????????????????????????????? ~
??????????????????????????????????????????????????????????????????? ????????????????? ??????? Cocos2d-x ??????????????????????? ~(??????????? ????????????????? cocos2d-x 2.0.4 ???)
???????
??cocos2d-x ?????? CCObject ?????????????????????????? CCObject ?????? ?????????????? CCObject????? cocos2d ? C++ ????????????????? ??????????????????????? CCNode ??????????????????????????C# ???????????????????????? OC ?Objective-C? ????????
??????????????CCObject ????????????????? 0 ????? ?????????? delete ????????????????????????????? "????" ???? retain ? release ????????????
?????????? ????
?????? cocos2d-x ???????????????????????????????????????????????????????????????????? ?? CCObject ????????????????????????????????
- ???????? ????????????????????????????????????1?
- ?????? ?????????????????????????
- ?????????????????
- ???????????????????
- ????????????????????????????????????????????? CCNode??
- ??????????????????????????????
???????????????????????????????????????? 1???? 0 ??????????????????? 0? ???????????????????????????????????????(????????)???? 1????????????????????????????
????????????(??????????????????????)?????????????????????????????????????????? ?????????????????????? ??????????????????????????????? ?????????????????????????????????????????? ???????????????
???????? create(); ??????????????????????????
- // ???????
- static CCObject* create()
- {
- // new CCObject ??
- CCObject *pRet = new CCObject();
- if (pRet && pRet->init())
- {
- // ????????
- pRet->autorelease();
- return pRet;
- }
- else
- {
- delete pRet;
- pRet = 0;
- return 0;
- }
- }
- // ?????????? ??? m_uReference = 1
- CCObject::CCObject(void)
- :m_uAutoReleaseCount(0)
- ,m_uReference(1) // when the object is created, the reference count of it is 1
- ,m_nLuaID(0)
- {
- static unsigned int uObjectCount = 0;
- m_uID = ++uObjectCount;
- }
- // ?????????
- CCObject* CCObject::autorelease(void)
- {
- // ????????
- CCPoolManager::sharedPoolManager()->addObject(this);
- return this;
- }
- // ????
- void CCPoolManager::addObject(CCObject* pObject)
- {
- getCurReleasePool()->addObject(pObject);
- }
- // ?????????????
- void CCAutoreleasePool::addObject(CCObject* pObject)
- {
- // ?????? CCArray ?????????????? ???? + 1
- m_pManagedObjectArray->addObject(pObject);
- // ????? ??? 1??????????????? 2 ???????????
- CCAssert(pObject->m_uReference > 1, "reference count should be greater than 1");
- ++(pObject->m_uAutoReleaseCount);
- // ???????????????????? 1
- pObject->release(); // no ref count, in this case autorelease pool added.
- }
复制代码
???????? create() ?????????????????????????????????????????????????????????????????????????????????????????CCPoolManager::sharedPoolManager()->pop(); ?? ( ???????Cocos2d-x ????????????? ?????????????????? pop() ??)?????? ???? ???????????? pop() ??????
- void CCPoolManager::pop()
- {
- if (! m_pCurReleasePool)
- {
- return;
- }
- // ????????pop ?????
- int nCount = m_pReleasePoolStack->count();
- // ?????????? ???? ????????????
- m_pCurReleasePool->clear();
- // ?????????????????? nCount ???? 1????????????
- if(nCount > 1)
- {
- m_pReleasePoolStack->removeObjectAtIndex(nCount-1);
- // if(nCount > 1)
- // {
- // m_pCurReleasePool = m_pReleasePoolStack->objectAtIndex(nCount - 2);
- // return;
- // }
- m_pCurReleasePool = (CCAutoreleasePool*)m_pReleasePoolStack->objectAtIndex(nCount - 2);
- }
- /*m_pCurReleasePool = NULL;*/
- }
- // ?????????
- void CCAutoreleasePool::clear()
- {
- // ??????? ???? ???
- if(m_pManagedObjectArray->count() > 0)
- {
- //CCAutoreleasePool* pReleasePool;
- #ifdef _DEBUG
- int nIndex = m_pManagedObjectArray->count() - 1;
- #endif
- CCObject* pObj = NULL;
- CCARRAY_FOREACH_REVERSE(m_pManagedObjectArray, pObj)
- {
- if(!pObj)
- break;
- --(pObj->m_uAutoReleaseCount);
- //(*it)->release();
- //delete (*it);
- #ifdef _DEBUG
- nIndex--;
- #endif
- }
- // ??????????????????????????????
- m_pManagedObjectArray->removeAllObjects();
- }
- }
复制代码
???????????????????? ??????????? (????????????) ???????? ???????????????????????? ???????????????????????
???"???"?????
????????????? ???? ???????????????????????????????????????????????????? cocos2d-x ???????????? ?? ?????????????????????
- // ???????
- class CC_DLL CCPoolManager
- {
- // ???????
- CCArray* m_pReleasePoolStack;
- // ???????
- CCAutoreleasePool* m_pCurReleasePool;
- // ???????
- CCAutoreleasePool* getCurReleasePool();
- public:
- CCPoolManager();
- ~CCPoolManager();
- void finalize();
- void push();
- void pop();
- void removeObject(CCObject* pObject);
- // ???? ???? ??
- void addObject(CCObject* pObject);
- static CCPoolManager* sharedPoolManager();
- static void purgePoolManager();
- friend class CCAutoreleasePool;
- };
- // ??? addObject ?????????? addObject ?? CCObject ? autorelease ?????
- void CCPoolManager::addObject(CCObject* pObject)
- {
- getCurReleasePool()->addObject(pObject);
- }
- CCAutoreleasePool* CCPoolManager::getCurReleasePool()
- {
- // ?????????
- if(!m_pCurReleasePool)
- {
- // ????
- push();
- }
- CCAssert(m_pCurReleasePool, "current auto release pool should not be null");
- return m_pCurReleasePool;
- }
- void CCPoolManager::push()
- {
- CCAutoreleasePool* pPool = new CCAutoreleasePool(); //ref = 1
- m_pCurReleasePool = pPool;
- // ????????????
- m_pReleasePoolStack->addObject(pPool); //ref = 2
- pPool->release(); //ref = 1
- }
复制代码
??? addObject ?????????? addObject ?????????????????????????????????????????????????????? addObject ?????????????????????????????? pop ???
- void CCPoolManager::pop()
- {
- if (! m_pCurReleasePool)
- {
- return;
- }
- int nCount = m_pReleasePoolStack->count();
- // ??? ???? ???
- m_pCurReleasePool->clear();
- // ???? 1??????????????????????????
- if(nCount > 1)
- {
- // ????????
- m_pReleasePoolStack->removeObjectAtIndex(nCount-1);
- // if(nCount > 1)
- // {
- // m_pCurReleasePool = m_pReleasePoolStack->objectAtIndex(nCount - 2);
- // return;
- // }
- // ??????????????????? ?????
- m_pCurReleasePool = (CCAutoreleasePool*)m_pReleasePoolStack->objectAtIndex(nCount - 2);
- }
- /*m_pCurReleasePool = NULL;*/
- }
复制代码
?????? ???????????????????????????????????????????CCPoolManager::push() ??????????????????? CCLog("??????? **********"); ?????????????????????????push ??????????????? addObject ??????? push() ?????????????????????? push() ??????????????????????????? bool CCDirector::init(void) ??????????????????????
- bool CCDirector::init(void)
- {
- CCLOG("cocos2d: %s", cocos2dVersion());
- ...
- ...
- m_dOldAnimationInterval = m_dAnimationInterval = 1.0 / kDefaultFPS;
- m_pobScenesStack = new CCArray();
- m_pobScenesStack->init();
- ...
- ...
- m_fContentScaleFactor = 1.0f;
- ...
- ...
- // touchDispatcher
- m_pTouchDispatcher = new CCTouchDispatcher();
- m_pTouchDispatcher->init();
- // KeypadDispatcher
- m_pKeypadDispatcher = new CCKeypadDispatcher();
- // Accelerometer
- m_pAccelerometer = new CCAccelerometer();
- // ??????? push ?????????????????????? CCObject ? autorelease???????? push ??
- CCPoolManager::sharedPoolManager()->push();
- return true;
- }
复制代码
??????????? push ?????????????????????????????????????????CCPoolManager::sharedPoolManager()->push(); ???????????????????????????????????????????????? ???????????????????????????????????????????? push() ????????????????? pop ??????????? clear ?????????????? Cocos2d-x ?????? ??????????????????????
- // ??????
- CCLog("update index: %d", updateCount);
- // ???????????????
- if (updateCount == 1) {
- // ??????????
- layer = LSLayer::create();
- // ???????????
- CCPoolManager::sharedPoolManager()->push();
- // ???????????
- sprite = LSSprite::create();
- } else if (updateCount == 2) {
- } else if (updateCount == 3) {
- }
- CCLog("update index: %d end", updateCount);
- /// ??????
- cocos2d-x debug info [update index: 1]
- // ??????????????
- cocos2d-x debug info [LSLayer().()]
- cocos2d-x debug info [LSSprite().()]
- cocos2d-x debug info [update index: 1 end]
- // ?????????? sprite ??
- cocos2d-x debug info [LSSprite().~()]
- cocos2d-x debug info [update index: 2]
- cocos2d-x debug info [update index: 2 end]
- // ????????? layer ??
- cocos2d-x debug info [LSLayer().~()]
- cocos2d-x debug info [update index: 3]
- cocos2d-x debug info [update index: 3 end]
复制代码
?????? sprite ? layer ????????????????????????? ???? push() ???????????????????????????????????????????????????? ??????????????cocos2d-x 2.0.4 ????????? push() ?????????????????????????????????????????? : P???????????????? ???????????????
?????????? ???? ????????????????????????????????????? ?????? ???? ??????
???????????
????????????????????????? ???? ?????????????????????? cocos2d-x ?????????????????????????????????????????????????????????????????? ? ???? ????????????????????????????? ?P?
????????????????????????????????????????????? CCNode ??????????????????????????????????????? ?? (???? retain) ?????????????????????? ??????????? ?????????????????????(???? release)?????????
??Cocos2d-x ????????????????????? ???? ???????????????????????????????????????
- // ?????????????
- LUser* lu = LUser::create();
- lu->m_sSprite = CCSprite::create("a.png");
- // ????? retain ????????
- lu->m_sSprite->retain();
- // ????????
- LUser* lu = LUser::create();
- lu->m_sUserName = "??";
- // ??? sprite ??? lu ???????????????
- lu->setSprite(CCSprite::create("a.png"));
复制代码
????????????????????? setSprite ??? sprite ?? retain????????????????? lu->m_sSprite->retain();??????????????? LUser??????LUser ?? sprite ?????????????? cocos2d-x ????????????? ~
?????????????????????? retain ? release ?? ~
??????
???? cocos2d-x ????????????????????????????? = =!? ????????????????? ?? ??? cocos2d-x ????????????????????????????????????????????????????????????????????????? cocos2d-x ????????? CCObject ?????????????????????????????????? CCArray ??????????? CCArray ??????????????????????????????????????????? CCArray ??????????????????????????????? ?? ?????????????? ~
???? ?????????????????????????????????????????????? CCPoolManager? CCAutoreleasePool ????????????????????????? ???? ??????????
|
|