游戏开发论坛

 找回密码
 立即注册
搜索
查看: 10354|回复: 6

???? Cocos2d-x ????

[复制链接]

1万

主题

1万

帖子

2万

积分

管理员

????

Rank: 9Rank: 9Rank: 9

积分
20599
发表于 2013-6-23 12:16:28 | 显示全部楼层 |阅读模式
????[????]??????????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(); ??????????????????????????

  1. // ???????
  2. static CCObject* create()
  3. {
  4.     // new CCObject ??
  5.     CCObject *pRet = new CCObject();
  6.     if (pRet && pRet->init())
  7.     {
  8.         // ????????
  9.         pRet->autorelease();
  10.         return pRet;
  11.     }
  12.     else
  13.     {
  14.         delete pRet;
  15.         pRet = 0;
  16.         return 0;
  17.     }
  18. }

  19. // ?????????? ??? m_uReference = 1
  20. CCObject::CCObject(void)
  21. :m_uAutoReleaseCount(0)
  22. ,m_uReference(1) // when the object is created, the reference count of it is 1
  23. ,m_nLuaID(0)
  24. {
  25.     static unsigned int uObjectCount = 0;

  26.     m_uID = ++uObjectCount;
  27. }

  28. // ?????????
  29. CCObject* CCObject::autorelease(void)
  30. {
  31.     // ????????
  32.     CCPoolManager::sharedPoolManager()->addObject(this);
  33.     return this;
  34. }

  35. // ????
  36. void CCPoolManager::addObject(CCObject* pObject)
  37. {
  38.     getCurReleasePool()->addObject(pObject);
  39. }

  40. // ?????????????
  41. void CCAutoreleasePool::addObject(CCObject* pObject)
  42. {
  43.     // ?????? CCArray ?????????????? ???? + 1
  44.     m_pManagedObjectArray->addObject(pObject);

  45.     // ????? ??? 1??????????????? 2 ???????????
  46.     CCAssert(pObject->m_uReference > 1, "reference count should be greater than 1");
  47.     ++(pObject->m_uAutoReleaseCount);
  48.     // ???????????????????? 1
  49.     pObject->release(); // no ref count, in this case autorelease pool added.
  50. }
复制代码

???????? create() ?????????????????????????????????????????????????????????????????????????????????????????CCPoolManager::sharedPoolManager()->pop(); ?? ( ???????Cocos2d-x ????????????? ?????????????????? pop() ??)?????? ???? ???????????? pop() ??????

  1. void CCPoolManager::pop()
  2. {
  3.     if (! m_pCurReleasePool)
  4.     {
  5.         return;
  6.     }

  7.     // ????????pop ?????
  8.      int nCount = m_pReleasePoolStack->count();
  9.     // ?????????? ???? ????????????
  10.     m_pCurReleasePool->clear();

  11.     // ?????????????????? nCount ???? 1????????????
  12.       if(nCount > 1)
  13.       {
  14.         m_pReleasePoolStack->removeObjectAtIndex(nCount-1);

  15. //         if(nCount > 1)
  16. //         {
  17. //             m_pCurReleasePool = m_pReleasePoolStack->objectAtIndex(nCount - 2);
  18. //             return;
  19. //         }
  20.         m_pCurReleasePool = (CCAutoreleasePool*)m_pReleasePoolStack->objectAtIndex(nCount - 2);
  21.     }

  22.     /*m_pCurReleasePool = NULL;*/
  23. }

  24. // ?????????
  25. void CCAutoreleasePool::clear()
  26. {
  27.     // ??????? ???? ???
  28.     if(m_pManagedObjectArray->count() > 0)
  29.     {
  30.         //CCAutoreleasePool* pReleasePool;
  31. #ifdef _DEBUG
  32.         int nIndex = m_pManagedObjectArray->count() - 1;
  33. #endif

  34.         CCObject* pObj = NULL;
  35.         CCARRAY_FOREACH_REVERSE(m_pManagedObjectArray, pObj)
  36.         {
  37.             if(!pObj)
  38.                 break;

  39.             --(pObj->m_uAutoReleaseCount);
  40.             //(*it)->release();
  41.             //delete (*it);
  42. #ifdef _DEBUG
  43.             nIndex--;
  44. #endif
  45.         }
  46.         // ??????????????????????????????
  47.         m_pManagedObjectArray->removeAllObjects();
  48.     }
  49. }
复制代码


???????????????????? ??????????? (????????????) ???????? ???????????????????????? ???????????????????????



???"???"?????

????????????? ???? ???????????????????????????????????????????????????? cocos2d-x ???????????? ?? ?????????????????????

  1. // ???????
  2. class CC_DLL CCPoolManager
  3. {
  4.     // ???????
  5.     CCArray*    m_pReleasePoolStack;
  6.     // ???????
  7.     CCAutoreleasePool*                    m_pCurReleasePool;

  8.     // ???????
  9.     CCAutoreleasePool* getCurReleasePool();
  10. public:
  11.     CCPoolManager();
  12.     ~CCPoolManager();
  13.     void finalize();
  14.     void push();
  15.     void pop();

  16.     void removeObject(CCObject* pObject);
  17.     // ???? ???? ??
  18.     void addObject(CCObject* pObject);

  19.     static CCPoolManager* sharedPoolManager();
  20.     static void purgePoolManager();

  21.     friend class CCAutoreleasePool;
  22. };

  23. // ??? addObject ?????????? addObject ?? CCObject ? autorelease ?????
  24. void CCPoolManager::addObject(CCObject* pObject)
  25. {
  26.     getCurReleasePool()->addObject(pObject);
  27. }

  28. CCAutoreleasePool* CCPoolManager::getCurReleasePool()
  29. {
  30.     // ?????????
  31.     if(!m_pCurReleasePool)
  32.     {
  33.         // ????
  34.         push();
  35.     }

  36.     CCAssert(m_pCurReleasePool, "current auto release pool should not be null");

  37.     return m_pCurReleasePool;
  38. }

  39. void CCPoolManager::push()
  40. {
  41.     CCAutoreleasePool* pPool = new CCAutoreleasePool();       //ref = 1
  42.     m_pCurReleasePool = pPool;
  43.     // ????????????
  44.     m_pReleasePoolStack->addObject(pPool);                   //ref = 2

  45.     pPool->release();                                       //ref = 1
  46. }
复制代码

??? addObject ?????????? addObject ?????????????????????????????????????????????????????? addObject ?????????????????????????????? pop ???

  1. void CCPoolManager::pop()
  2. {
  3.     if (! m_pCurReleasePool)
  4.     {
  5.         return;
  6.     }

  7.      int nCount = m_pReleasePoolStack->count();
  8.     // ??? ???? ???
  9.     m_pCurReleasePool->clear();

  10.     // ???? 1??????????????????????????
  11.       if(nCount > 1)
  12.       {
  13.           // ????????
  14.         m_pReleasePoolStack->removeObjectAtIndex(nCount-1);

  15. //         if(nCount > 1)
  16. //         {
  17. //             m_pCurReleasePool = m_pReleasePoolStack->objectAtIndex(nCount - 2);
  18. //             return;
  19. //         }
  20.         // ??????????????????? ?????
  21.         m_pCurReleasePool = (CCAutoreleasePool*)m_pReleasePoolStack->objectAtIndex(nCount - 2);
  22.     }

  23.     /*m_pCurReleasePool = NULL;*/
  24. }
复制代码

?????? ???????????????????????????????????????????CCPoolManager::push() ??????????????????? CCLog("??????? **********"); ?????????????????????????push ??????????????? addObject ??????? push() ?????????????????????? push() ??????????????????????????? bool CCDirector::init(void) ??????????????????????

  1. bool CCDirector::init(void)
  2. {
  3.     CCLOG("cocos2d: %s", cocos2dVersion());

  4.     ...
  5.     ...
  6.     m_dOldAnimationInterval = m_dAnimationInterval = 1.0 / kDefaultFPS;   
  7.     m_pobScenesStack = new CCArray();
  8.     m_pobScenesStack->init();

  9.     ...
  10.     ...
  11.     m_fContentScaleFactor = 1.0f;

  12.     ...
  13.     ...
  14.     // touchDispatcher
  15.     m_pTouchDispatcher = new CCTouchDispatcher();
  16.     m_pTouchDispatcher->init();

  17.     // KeypadDispatcher
  18.     m_pKeypadDispatcher = new CCKeypadDispatcher();

  19.     // Accelerometer
  20.     m_pAccelerometer = new CCAccelerometer();


  21.     // ??????? push ?????????????????????? CCObject ? autorelease???????? push ??
  22.     CCPoolManager::sharedPoolManager()->push();

  23.     return true;
  24. }
复制代码

??????????? push ?????????????????????????????????????????CCPoolManager::sharedPoolManager()->push(); ???????????????????????????????????????????????? ???????????????????????????????????????????? push() ????????????????? pop ??????????? clear ?????????????? Cocos2d-x ?????? ??????????????????????

  1. // ??????
  2. CCLog("update index: %d", updateCount);

  3. // ???????????????
  4. if (updateCount == 1) {
  5.     // ??????????
  6.     layer = LSLayer::create();
  7.     // ???????????
  8.     CCPoolManager::sharedPoolManager()->push();
  9.     // ???????????
  10.     sprite = LSSprite::create();
  11. } else if (updateCount == 2) {

  12. } else if (updateCount == 3) {

  13. }

  14. CCLog("update index: %d end", updateCount);

  15. /// ??????
  16. cocos2d-x debug info [update index: 1]
  17. // ??????????????
  18. cocos2d-x debug info [LSLayer().()]
  19. cocos2d-x debug info [LSSprite().()]
  20. cocos2d-x debug info [update index: 1 end]
  21. // ?????????? sprite ??
  22. cocos2d-x debug info [LSSprite().~()]
  23. cocos2d-x debug info [update index: 2]
  24. cocos2d-x debug info [update index: 2 end]
  25. // ????????? layer ??
  26. cocos2d-x debug info [LSLayer().~()]
  27. cocos2d-x debug info [update index: 3]
  28. 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 ????????????????????? ???? ???????????????????????????????????????

  1. // ?????????????
  2. LUser* lu = LUser::create();
  3. lu->m_sSprite = CCSprite::create("a.png");
  4. // ????? retain  ????????
  5. lu->m_sSprite->retain();

  6. // ????????
  7. LUser* lu = LUser::create();
  8. lu->m_sUserName = "??";
  9. // ??? sprite ??? lu ???????????????
  10. 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 ????????????????????????? ???? ??????????

11

主题

1238

帖子

1782

积分

金牌会员

Rank: 6Rank: 6

积分
1782
发表于 2013-6-24 12:02:02 | 显示全部楼层
?????????????????

3

主题

24

帖子

124

积分

注册会员

Rank: 2

积分
124
QQ
发表于 2013-6-27 22:19:07 | 显示全部楼层
void CCObject::release(void)
{
    CCAssert(m_uReference > 0, "reference count should greater than 0");
    --m_uReference;

    if (m_uReference == 0)
    {
        delete this;
    }
}

void CCObject::retain(void)
{
    CCAssert(m_uReference > 0, "reference count should greater than 0");

    ++m_uReference;
}

new ? retain ???
autorelease?release???
?????????

a= new A;//a.b = b
b = new B;//b.a = a

a?b?m_uReference ?1??2 ???new???
m_uReference????????????new ? retain??

11

主题

1238

帖子

1782

积分

金牌会员

Rank: 6Rank: 6

积分
1782
发表于 2013-6-28 11:24:07 | 显示全部楼层
wpc_LK ??? 2013-6-27 22:19
void CCObject::release(void)
{
    CCAssert(m_uReference > 0, "reference count should greater than 0 ...

?.......??......??.....
retain ??????????

1

主题

51

帖子

72

积分

注册会员

Rank: 2

积分
72
发表于 2013-7-26 11:15:47 | 显示全部楼层
???? ??? 2013-6-24 12:02
?????????????????

????????

1

主题

51

帖子

72

积分

注册会员

Rank: 2

积分
72
发表于 2013-7-26 11:18:41 | 显示全部楼层
???auto_release????auto_release_ptr<>??????????auto_release???????????????????????????????????????????????????????????????????????????????????auto_release???????????return?return????

2

主题

17

帖子

57

积分

注册会员

Rank: 2

积分
57
发表于 2013-7-26 12:49:44 | 显示全部楼层
??3.0????????????autorelease????
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-10-9 10:50

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表