Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d-x整体框架源码分析以及启动过程原理(win32)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

引言:如果你想深入了解cocos2d-x的整个框架和运行流程,如果你想知道整个启动过程的细节,如果你想知道自己写的代码是在什么时候和在哪里被调用的,下面可以为你解答其中奥秘。
对象:适合刚刚入门了cocos2d-x的初学者,编写并运行过简单的demo,并且想仔细探究其中的原理机制。
1、程序入口main.cpp
[cpp]@H_616_24@ view plain copy
  1. intAPIENTRY_tWinMain(HINSTANCEhInstance,
  2. hPrevInstance,
  3. LPTSTRlpCmdLine,51)">nCmdShow)
  4. {
  5. ...
  6. //createtheapplicationinstance
  7. AppDelegateapp;//看这里
  8. ...
  9. @H_616_77@returnCCApplication::sharedApplication()->run();}
以上两行是关键代码
(1)代码AppDelegate app;调用了AppDelegate的构造函数,而它的父类是CCApplication,所以CCApplication的构造函数也被调用了
CCApplication::CCApplication()
  • :m_hInstance(NULL)
  • ,m_hAccelTable(NULL)
  • {
  • @H_348_53@m_hInstance=GetModuleHandle(null); @H_763_56@m_nAnimationInterval.QuadPart=0;
  • CC_ASSERT(!sm_pSharedApplication);
  • sm_pSharedApplication=@H_616_77@this;
  • 这里完成了AppDelegate和CCApplication之间的联系,由于AppDelegate是CCApplication的子类,故CCApplication里面的静态单例指针指的便是AppDelegate的实例。之后调用CCApplication::sharedApplication()的相关操作是基于AppDelegate的实现。
  • (2)代码CCApplication::sharedApplication()->run();调用了下面的代码
    CCApplication::run()
  • //Initializeinstanceandcocos2d.if(!applicationDidFinishLaunching())
  • 0;
  • }
  • ..
  • while(1)
  • (!Peekmessage(&msg,NULL,PM_REMOVE))
  • //Getcurrenttimetick.QueryPerfoRMANceCounter(&nNow);
  • //Ifit'sthetimetodrawnextframe,drawit,elsesleepawhile.(nNow.QuadPart-nLast.QuadPart>m_nAnimationInterval.QuadPart)
  • nLast.QuadPart=nNow.QuadPart;
  • CCDirector::sharedDirector()->mainLoop();elseSleep(0);
  • conTinue;
  • (WM_QUIT==msg.messagE)
  • //Quitmessageloop.break//Dealwithwindowsmessage.(!m_hAccelTable||!TranslateAccelerator(msg.hwnd,m_hAccelTable,&msg))
  • Translatemessage(&msg);
  • Dispatchmessage(&msg);
  • }
  • ()msg.wParam;
  • }
  • 在这里,run()函数里面是一个while死循环,不过里面有判断条件if (WM_QUIT == msg.messagE)可以跳出循环,终止程序。循环里面每隔一段时间m_nAnimationInterval就调用CCDirector::sharedDirector()->mainLoop(),完成游戏的刷新。
    (3)下面来看看mainLoop()都干了啥事:
    voidCCDisplayLinkDirector::mainLoop()
  • (m_bPurgeDirecotorInNextLoop)
  • @H_763_56@m_bPurgeDirecotorInNextLoop=@H_616_77@falsepurgeDirector();
  • (!m_bInvalid)
  • drawScene();//releasetheobjectsCCPoolManager::sharedPoolManager()->pop();@H_88_262@ 首先解释一下为什么显示的是CCDisplayLinkDirector::mainLoop,而不是 CCDirector::mainLoop,答案就在CCDirector.h头文件里面
  • classCC_DLLCCDirector:@H_616_77@publicCCObject,TypeInfo
  • .......
  • virtual@H_481_34@mainLoop()=0;CCDisplayLinkDirector:CCDirector
  • ..........
  • 在CCDirector类里面,mainLoop是纯虚函数,而CCDisplayLinkDirector是CCDirector的子类。
  • 重新回到mainLoop函数的实现,我们发现drawScene()和CCPoolManager::sharedPoolManager()->pop()是其主要内容,也就是重绘和释放自动内存池管理器里面暂时存储的对象,这个和cocos2d-x的自动内存管理机制有关,所有静态创建对象函数::create()都采用了自动托管机制,比如:
    CCSprite*CCSprite::create(@H_616_77@constchar*pszFileName)
  • CCSprite*pobSprite=@H_616_77@newCCSprite();
  • (pobSprite&&pobSprite->initWithFile(pszFileName))
  • pobSprite->autorelease();pobSprite;
  • CC_SAFE_deletE(pobSpritE);
  • NULL;
  • 其中autorelease()函数实现如下
  • CCObject*CCObject::autorelease(CCPoolManager::sharedPoolManager()->addObject();
  • 由此可以看到自动内存管理对象在每帧绘制结束后都会被CCPoolManager::sharedPoolManager()->pop()释放,除非我们在create()对象后使用了retain(),retain()也很简单,实现如下
  • CCObject::retain(CCassert(m_uReference>0,"referencecountshouldgreaterthan0"++m_uReference;@H_88_262@ 就是把对象的引用计数-1,这又对应于对象的release()函数
    CCObject::release(--m_uReference;(m_uReference==0)delete 可以看到,对象只有当其引用计数m_uReference=0时,才会真正被删除。
    (4)下面看看drawScene()重绘函数负责什么,看完你就全明白了整个框架了
    //DrawtheSceneCCDirector::drawScene()
  • ..............
  • //tickbeforeglClear:issue#533(!m_bPaused)
  • @H_348_53@m_pscheduler->update(m_fDeltaTimE);.....................
  • //drawthescene(m_pRunningScenE)
  • @H_348_53@m_pRunningScene->visit();
  • //swapbuffers(m_pobOpenGLView)
  • @H_348_53@m_pobOpenGLView->swapBuffers();
  • ................
  • 大家看到m_pscheduler->update(m_fDeltaTimE)会不会想到我们经常在HelloWorldScene.h写的void update(float dt),两者有什么关系呢?
  • 首先回忆一下,我们在HelloWorldScene.h里面是怎样调用到update函数的
    this->schedule(schedule_SELEctor( HelloWorld::updatE));(这只是调用方式的一种)
    看看schedule函数的实现
    1. CCNode::schedule(SEL_scheDULESELEctor)
    2. {
    3. ->schedule(SELEctor,0.0f,kCCRepeatForever,0.0f);
    4. @H_515_450@}
    5. CCNode::schedule(SEL_scheDULESELEctor,floatinterval,unsignedrepeat,51)">delay)
    6. @H_515_450@{
    7. CCassert(SELEctor,51)">"Argumentmustbenon-nil");
    8. @H_515_450@CCassert(interval>=0,51)">"Argumentmustbepositive");
    9. @H_32_481@m_pscheduler->scheduleSELEctor(SELEctor,,interval,repeat,delay,!m_bRunning);
    10. }
    11. CC_DLLCCNode:CCObject
    12. {.........
    13. @H_515_450@CCscheduler*m_pscheduler;....... @H_515_450@}
    再对比一下刚才drawScene()重绘函数
    TypeInfo
  • {...............
  • CC_PROPERTY(CCscheduler*,m_pscheduler,scheduler);..............
  • #defineCC_PROPERTY(varType,varName,funName)\//看这里protected:varTypevarName;\
  • :varTypeget##funName();\
  • set##funName(varTypevar);
  • 我们发现CCNode和CCDirector类都申明了CCscheduler *m_pscheduler,都不是同一个滴。
    且慢,不一定哦,看看CCNode的构造函数先
    CCNode::CCNode(:...........
  • CCDirector*director=CCDirector::sharedDirector();
  • ...............
  • @H_763_56@m_pscheduler=director->getscheduler();................
  • 原来CCNode里面的m_pscheduler引用了CCDirector的m_pscheduler,感受到穷追不舍的魅力了吧。
  • 再回到之前的问题:
    CCDirector::drawScene里面的m_pscheduler->update(m_fDeltaTimE)和我们经常在HelloWorldScene.h写的void update(float dt),两者有什么关系呢?
    由上面代码可见,this->schedule(schedule_SELEctor(HelloWorld::updatE))实际上会把schedule_SELEctor(HelloWorld::updatE)保存到CCNode里面的m_pscheduler,也就是CCDirector里面的m_pscheduler,那么CCDirector::drawScene里面的m_pscheduler->update(m_fDeltaTimE)里面的代码应该就和HelloWorld::update()有关吧。继续看看呗。
    //mainloop
  • CCscheduler::update(dt)
  • {
  • @H_515_450@................
  • //IterateoverallthecustomSELEctors
  • for(tHashTimerEntry*elt=m_pHashForTimers;elt!=NULL;)
  • @H_576_466@m_pCurrentTarget=elt; @H_576_466@m_bCurrentTargetSalvaged=; @H_515_450@
  • (!m_pCurrentTarget->paused)
  • //The'timers'arraymaychangewhileinsidethisloop(elt->timerIndex=0;elt->timerIndex<elt->timers->num;++(elt->timerIndeX))
  • elt->currentTimer=(Cctimer*)(elt->timers->arr[elt->timerIndex]);
  • elt->currentTimerSalvaged=elt->currentTimer->update(dt);......................
  • @H_515_450@}
  • ..................
  • ...............
  • 重点看elt->currentTimer->update(dt);
  • elt->currentTimer是Cctimer类,看看里面的update函数是神马情况先
    Cctimer::update(dt)
  • (m_fElapsed==-1)
  • ....................
  • (m_bRunForever&&!m_bUseDelay)
  • {//standardtimerusage...................
  • (m_fElapsed>=m_fInterval)
  • (m_pTarget&&m_pfnSELEctor)
  • (m_pTarget->*m_pfnSELEctor)(m_fElapsed);...................
  • //advancedusage(m_bUseDelay)
  • (m_fElapsed>=m_fDelay)
  • ...............
  • (m_fElapsed>=m_fInterval)
  • (m_pTarget&&m_pfnSELEctor)
  • .......................
  • 你可能还不知道(m_pTarget->*m_pfnSELEctor)(m_fElapsed);这个什么意思,没事,重新看看HelloWorldScene.h调用update函数时的代码:
  • this->schedule(schedule_SELEctor(HelloWorld::updatE));
    下面是schedule的实现(上面已提过)
    CCNode::schedule(SEL_scheDULESELEctor)
  • :rgb(108,0.0f);
  • delay)
  • );
  • @H_348_53@m_pscheduler->scheduleSELEctor(SELEctor,51)!important"> 最后一步,就是看m_pscheduler->scheduleSELEctor(SELEctor,this,!m_bRunning)和 (m_pTarget->*m_pfnSELEctor)(m_fElapsed)有什么关系,其中SELEctor是函数指针,指向 HelloWorld::update,不懂的可以看 schedule_SELEctor的定义。
    下面是scheduleSELEctor函数的实现
    CCscheduler::scheduleSELEctor(SEL_scheDULEpfnSELEctor,CCObject*pTarget,51)">fInterval,51)">delay,51); font-weight:bold">boolbPaused)
  • tHashTimerEntry*pElement=NULL;
  • HASH_FIND_INT(m_pHashForTimers,&pTarget,pElement);
  • (!pElement)
  • ...........................
  • CCassert(pElement->paused==bPaused,51)">""(pElement->timers==NULL)
  • pElement->timers=ccArrayNew(10);
  • Cctimer*pTimer=Cctimer();
  • pTimer->initWithTarget(pTarget,pfnSELEctor,fInterval,delay);ccArrayAppendObject(pElement->timers,pTimer);
  • pTimer->release();
  • 重点看pTimer->initWithTarget(pTarget,delay),其中pTarget就是CCNode(对应 HelloWorld类)对象本身,而pfnSELEctor就是刚才说的函数指针,指向 HelloWorld::update,下面是initWithTarget的函数实现
  • Cctimer::initWithTarget(CCObject*pTarget,SEL_scheDULEpfnSELEctor,51)">fSeconds,51)">nRepeat,51)">fDelay) @H_763_56@m_pTarget=pTarget; @H_763_56@m_pfnSELEctor=pfnSELEctor;
  • .....................
  • true 由此,我们解决刚才提出的问题:
  • 看看m_pscheduler->scheduleSELEctor(SELEctor,51)!important">HelloWorld::update。
    答案就在上面,再次啰嗦说明一下,pTarget就是CCNode(对应 HelloWorld::update,也就是说, HelloWorld类定义的void update(float dt)被调用了,而且是在整个主循环里面 CCDirector::sharedDirector()->mainLoop()。
    (5)结束语:
    整个cocos2d-x的框架源码就是这样,具体一些细节还未涉及,但我们已经理解程序一开始从main函数,然后怎么样一步步调用到我们自己是HelloWorld.h里面自己定义实现的函数,特别是update函数,同时也了解了cocos2d-x的一些内存管理机制和回调机制,也在这个框架代码里面,还有整个框架是怎么实现帧数控制的。
    用《一代宗师》里面的一句话来概括这次框架源码研读之旅吧——”念念不忘,必有回响“。

    大佬总结

    以上是大佬教程为你收集整理的cocos2d-x整体框架源码分析以及启动过程原理(win32)全部内容,希望文章能够帮你解决cocos2d-x整体框架源码分析以及启动过程原理(win32)所遇到的程序开发问题。

    如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。