cocos2d-x整体框架源码分析以及启动过程原理(win32)

前端之家收集整理的这篇文章主要介绍了cocos2d-x整体框架源码分析以及启动过程原理(win32)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
转载请注明,原文地址:http://blog.csdn.net/jinble03/article/details/20006407
引言:如果你想深入了解cocos2d-x的整个框架和运行流程,如果你想知道整个启动过程的细节,如果你想知道自己写的代码是在什么时候和在哪里被调用的,下面可以为你解答其中奥秘。 对象:适合刚刚入门了cocos2d-x的初学者,编写并运行过简单的demo,并且想仔细探究其中的原理机制。1、程序入口main.cpp
int@H_403_14@ APIENTRY _tWinMain(HINSTANCE hInstance,@H_403_14@
@H_403_14@ HINSTANCE hPrevInstance,@H_403_14@
@H_403_14@ LPTSTR lpCmdLine,@H_403_14@
@H_403_14@ int@H_403_14@ nCmdShow)@H_403_14@
{@H_403_14@
@H_403_14@ ...@H_403_14@
@H_403_14@ // create the application instance@H_403_14@
@H_403_14@ AppDelegate app;@H_403_14@//看这里@H_403_14@
@H_403_14@ ...@H_403_14@
@H_403_14@ return@H_403_14@CCApplication::sharedApplication()->run();@H_403_14@//看这里@H_403_14@
}@H_403_14@
以上两行是关键代码 (1)代码AppDelegate app;调用了AppDelegate的构造函数,而它的父类是CCApplication,所以CCApplication的构造函数也被调用
CCApplication::CCApplication()@H_403_14@
: m_hInstance(NULL)@H_403_14@
,m_hAccelTable(NULL)@H_403_14@
{@H_403_14@
@H_403_14@ m_hInstance = GetModuleHandle(NULL);@H_403_14@
@H_403_14@ m_nAnimationInterval.QuadPart =@H_403_14@ 0@H_403_14@ ;@H_403_14@
@H_403_14@ CC_ASSERT(! sm_pSharedApplication);@H_403_14@
@H_403_14@ sm_pSharedApplication =@H_403_14@ this@H_403_14@ ;@H_403_14@
}@H_403_14@
@H_403_14@
这里完成了AppDelegate和CCApplication之间的联系,由于AppDelegate是CCApplication的子类,故CCApplication里面的静态单例指针指的便是AppDelegate的实例。之后调用CCApplication::sharedApplication()的相关操作是基于AppDelegate的实现。 (2)代码CCApplication::sharedApplication()->run();调用了下面的代码
int@H_403_14@ CCApplication::run()@H_403_14@
{@H_403_14@
@H_403_14@ ...@H_403_14@
@H_403_14@ // Initialize instance and cocos2d.@H_403_14@
@H_403_14@ if@H_403_14@ (!applicationDidFinishLaunching())@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ return@H_403_14@ 0@H_403_14@ ;@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ ..@H_403_14@
@H_403_14@ while@H_403_14@ (@H_403_14@ 1@H_403_14@ )@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ if@H_403_14@ (! PeekMessage(&msg,NULL,@H_403_14@ 0@H_403_14@ ,PM_REMOVE))@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ // Get current time tick.@H_403_14@
@H_403_14@ QueryPerformanceCounter(&nNow);@H_403_14@
@H_403_14@ // If it's the time to draw next frame,draw it,else sleep a while.@H_403_14@
@H_403_14@ if@H_403_14@ (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ nLast.QuadPart = nNow.QuadPart;@H_403_14@
@H_403_14@ CCDirector::sharedDirector()->mainLoop();@H_403_14@//看这里@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ else@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ Sleep(@H_403_14@ 0@H_403_14@ );@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ continue@H_403_14@ ;@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ if@H_403_14@ (WM_QUIT == msg.message)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ // Quit message loop.@H_403_14@
@H_403_14@ break@H_403_14@ ;@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ // Deal with windows message.@H_403_14@
@H_403_14@ if@H_403_14@ (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd,m_hAccelTable,&msg))@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ TranslateMessage(&msg);@H_403_14@
@H_403_14@ DispatchMessage(&msg);@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ return@H_403_14@ (@H_403_14@ int@H_403_14@ ) msg.wParam;@H_403_14@
}@H_403_14@
在这里,run()函数里面是一个while死循环,不过里面有判断条件 if (WM_QUIT == msg.message)可以跳出循环,终止程序。循环里面每隔一段时间m_nAnimationInterval就调用CCDirector::sharedDirector()->mainLoop(),完成游戏的刷新。 (3)下面来看看mainLoop()都干了啥事:
void@H_403_14@ CCDisplayLinkDirector::mainLoop(@H_403_14@ void@H_403_14@ )@H_403_14@
{@H_403_14@
@H_403_14@ if@H_403_14@ (m_bPurgeDirecotorInNextLoop)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ m_bPurgeDirecotorInNextLoop =@H_403_14@ false@H_403_14@ ;@H_403_14@
@H_403_14@ purgeDirector();@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ else@H_403_14@ if@H_403_14@ (! m_bInvalid)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ drawScene();@H_403_14@//看这里@H_403_14@
@H_403_14@
@H_403_14@ // release the objects@H_403_14@
@H_403_14@ CCPoolManager::sharedPoolManager()->pop(); @H_403_14@ //看这里@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
首先解释一下为什么显示的是CCDisplayLinkDirector::mainLoop,而不是 CCDirector ::mainLoop,答案就在CCDirector.h头文件里面
class@H_403_14@ CC_DLL CCDirector : @H_403_14@ public@H_403_14@ CCObject,@H_403_14@ public@H_403_14@ TypeInfo@H_403_14@
{@H_403_14@
@H_403_14@ .......@H_403_14@
@H_403_14@ virtual@H_403_14@void@H_403_14@ mainLoop(@H_403_14@void@H_403_14@) =@H_403_14@0@H_403_14@;@H_403_14@//看这里@H_403_14@
@H_403_14@ .......@H_403_14@
}@H_403_14@
class@H_403_14@ CCDisplayLinkDirector : @H_403_14@ public@H_403_14@ CCDirector@H_403_14@
{@H_403_14@
@H_403_14@ ..........@H_403_14@
}@H_403_14@
@H_403_14@
在CCDirector类里面,mainLoop是纯虚函数,而CCDisplayLinkDirector是CCDirector的子类。 重新回到mainLoop函数的实现,我们发现drawScene()和CCPoolManager::sharedPoolManager()->pop()是其主要内容,也就是重绘和释放自动内存池管理器里面暂时存储的对象,这个和cocos2d-x的自动内存管理机制有关,所有静态创建对象函数::create()都采用了自动托管机制,比如:
CCSprite* CCSprite::create(@H_403_14@ const@H_403_14@ char@H_403_14@ *pszFileName)@H_403_14@
{@H_403_14@
@H_403_14@ CCSprite *pobSprite =@H_403_14@ new@H_403_14@ CCSprite();@H_403_14@
@H_403_14@ if@H_403_14@ (pobSprite && pobSprite->initWithFile(pszFileName))@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ pobSprite->autorelease();@H_403_14@//看这里@H_403_14@
@H_403_14@ return@H_403_14@ pobSprite;@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ CC_SAFE_DELETE(pobSprite);@H_403_14@
@H_403_14@ return@H_403_14@ NULL;@H_403_14@
}@H_403_14@
@H_403_14@
其中autorelease()函数实现如下
CCObject* CCObject::autorelease(@H_403_14@ void@H_403_14@ )@H_403_14@
{@H_403_14@
@H_403_14@ CCPoolManager::sharedPoolManager()->addObject(@H_403_14@ this@H_403_14@ );@H_403_14@
@H_403_14@ return@H_403_14@ this@H_403_14@ ;@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
由此可以看到自动内存管理对象在每帧绘制结束后都会被CCPoolManager::sharedPoolManager()->pop()释放,除非我们在create()对象后使用了retain(),retain()也很简单,实现如下
void@H_403_14@ CCObject::retain(@H_403_14@ void@H_403_14@ )@H_403_14@
{@H_403_14@
@H_403_14@ CCAssert(m_uReference >@H_403_14@ 0@H_403_14@ ,@H_403_14@ "reference count should greater than 0"@H_403_14@ );@H_403_14@
@H_403_14@ ++m_uReference;@H_403_14@//看这里@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
就是把对象的引用计数-1,这又对应于对象的release()函数
void@H_403_14@ CCObject::release(@H_403_14@ void@H_403_14@ )@H_403_14@
{@H_403_14@
@H_403_14@ CCAssert(m_uReference >@H_403_14@ 0@H_403_14@ ,@H_403_14@ "reference count should greater than 0"@H_403_14@ );@H_403_14@
@H_403_14@ --m_uReference;@H_403_14@ //看这里@H_403_14@
@H_403_14@ if@H_403_14@(m_uReference == @H_403_14@0@H_403_14@)@H_403_14@//看这里@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ delete@H_403_14@ this@H_403_14@ ;@H_403_14@
@H_403_14@ }@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
可以看到,对象只有当其引用计数m_uReference=0时,才会真正被删除。 (4)下面看看drawScene()重绘函数负责什么,看完你就全明白了整个框架了
// Draw the Scene@H_403_14@
void@H_403_14@ CCDirector::drawScene(@H_403_14@ void@H_403_14@ )@H_403_14@
{@H_403_14@
@H_403_14@ ..............@H_403_14@
@H_403_14@ //tick before glClear: issue #533@H_403_14@
@H_403_14@ if@H_403_14@ (! m_bPaused)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ m_pScheduler->update(m_fDeltaTime);@H_403_14@//看这里@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ .....................@H_403_14@
@H_403_14@ // draw the scene@H_403_14@
@H_403_14@ if@H_403_14@ (m_pRunningScene)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ m_pRunningScene->visit();@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ .....................@H_403_14@
@H_403_14@ // swap buffers@H_403_14@
@H_403_14@ if@H_403_14@ (m_pobOpenGLView)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ m_pobOpenGLView->swapBuffers();@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ ................@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
大家看到 m_pScheduler->update(m_fDeltaTime)会不会想到我们经常在HelloWorldScene.h写的void update(float dt),两者有什么关系呢? 首先回忆一下,我们在HelloWorldScene.h里面是怎样调用到update函数的this->schedule(schedule_selector( HelloWorld::update));(这只是调用方式的一种) 看看schedule函数的实现
void@H_403_14@ CCNode::schedule(SEL_SCHEDULE selector)@H_403_14@
{@H_403_14@
@H_403_14@ this@H_403_14@ ->schedule(selector,@H_403_14@ 0@H_403_14@ .0f,kCCRepeatForever,@H_403_14@ 0@H_403_14@ .0f);@H_403_14@
}@H_403_14@
void@H_403_14@ CCNode::schedule(SEL_SCHEDULE selector,@H_403_14@ float@H_403_14@ interval,unsigned @H_403_14@ int@H_403_14@ repeat,@H_403_14@ float@H_403_14@ delay)@H_403_14@
{@H_403_14@
@H_403_14@ CCAssert( selector,@H_403_14@ "Argument must be non-nil"@H_403_14@ );@H_403_14@
@H_403_14@ CCAssert( interval >=@H_403_14@ 0@H_403_14@ ,@H_403_14@ "Argument must be positive"@H_403_14@ );@H_403_14@
@H_403_14@ m_pScheduler->scheduleSelector(selector,@H_403_14@this@H_403_14@,interval,repeat,delay,!m_bRunning);@H_403_14@//看这里@H_403_14@
}@H_403_14@
class@H_403_14@ CC_DLL CCNode : @H_403_14@ public@H_403_14@ CCObject@H_403_14@
{ .........@H_403_14@
@H_403_14@ CCScheduler *m_pScheduler;@H_403_14@//看这里@H_403_14@
@H_403_14@ .......@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
再对比一下刚才drawScene()重绘函数
// Draw the Scene@H_403_14@
void@H_403_14@ CCDirector::drawScene(@H_403_14@ void@H_403_14@ )@H_403_14@
{@H_403_14@
@H_403_14@ ..............@H_403_14@
@H_403_14@ //tick before glClear: issue #533@H_403_14@
@H_403_14@ if@H_403_14@ (! m_bPaused)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ m_pScheduler->update(m_fDeltaTime);@H_403_14@//看这里@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ .....................@H_403_14@
}@H_403_14@
class@H_403_14@ CC_DLL CCDirector : @H_403_14@ public@H_403_14@ CCObject,@H_403_14@ public@H_403_14@ TypeInfo@H_403_14@
{ ...............@H_403_14@
@H_403_14@ CC_PROPERTY(CCScheduler*,m_pScheduler,Scheduler);@H_403_14@//看这里@H_403_14@
@H_403_14@ ..............@H_403_14@
}@H_403_14@
#define CC_PROPERTY(varType,varName,funName)\@H_403_14@//看这里@H_403_14@
protected@H_403_14@ : varType varName;\@H_403_14@
public@H_403_14@ : virtual varType get##funName(@H_403_14@ void@H_403_14@ );\@H_403_14@
public@H_403_14@ : virtual@H_403_14@ void@H_403_14@ set##funName(varType var);@H_403_14@
@H_403_14@
@H_403_14@
我们发现CCNode和CCDirector类都申明了CCScheduler *m_pScheduler,都不是同一个滴。 且慢,不一定哦,看看CCNode的构造函数
CCNode::CCNode(@H_403_14@ void@H_403_14@ )@H_403_14@
: ...........@H_403_14@
{@H_403_14@
@H_403_14@ CCDirector *director = CCDirector::sharedDirector();@H_403_14@
@H_403_14@ ...............@H_403_14@
@H_403_14@ m_pScheduler = director->getScheduler();@H_403_14@//看这里@H_403_14@
@H_403_14@ ................@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
原来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()有关吧。继续看看呗。
// main loop@H_403_14@
void@H_403_14@ CCScheduler::update(@H_403_14@ float@H_403_14@ dt)@H_403_14@
{@H_403_14@
@H_403_14@ ................@H_403_14@
@H_403_14@ // Iterate over all the custom selectors@H_403_14@
@H_403_14@ for@H_403_14@ (tHashTimerEntry *elt = m_pHashForTimers; elt != NULL; )@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ m_pCurrentTarget = elt;@H_403_14@
@H_403_14@ m_bCurrentTargetSalvaged =@H_403_14@ false@H_403_14@ ;@H_403_14@
@H_403_14@ if@H_403_14@ (! m_pCurrentTarget->paused)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ // The 'timers' array may change while inside this loop@H_403_14@
@H_403_14@ for@H_403_14@ (elt->timerIndex =@H_403_14@ 0@H_403_14@ ; elt->timerIndex < elt->timers->num; ++(elt->timerIndex))@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ elt->currentTimer = (CCTimer*)(elt->timers->arr[elt->timerIndex]);@H_403_14@
@H_403_14@ elt->currentTimerSalvaged = @H_403_14@ false@H_403_14@ ;@H_403_14@
@H_403_14@ elt->currentTimer->update(dt);@H_403_14@//看这里@H_403_14@
@H_403_14@ ......................@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ ..................@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ ...............@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
重点看elt->currentTimer->update(dt); elt->currentTimer是CCTimer类,看看里面的update函数是神马情况先
void@H_403_14@ CCTimer::update(@H_403_14@ float@H_403_14@ dt)@H_403_14@
{@H_403_14@
@H_403_14@ if@H_403_14@ (m_fElapsed == -@H_403_14@ 1@H_403_14@ )@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ ....................@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ else@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ if@H_403_14@ (m_bRunForever && !m_bUseDelay)@H_403_14@
@H_403_14@ {@H_403_14@ //standard timer usage@H_403_14@
@H_403_14@ ...................@H_403_14@
@H_403_14@ if@H_403_14@ (m_fElapsed >= m_fInterval)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ if@H_403_14@ (m_pTarget && m_pfnSelector)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ (m_pTarget->*m_pfnSelector)(m_fElapsed);@H_403_14@//看这里@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ ...................@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ } @H_403_14@
@H_403_14@ else@H_403_14@
@H_403_14@ {@H_403_14@ //advanced usage@H_403_14@
@H_403_14@ ...............@H_403_14@
@H_403_14@ if@H_403_14@ (m_bUseDelay)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ if@H_403_14@ ( m_fElapsed >= m_fDelay )@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ if@H_403_14@ (m_pTarget && m_pfnSelector)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ (m_pTarget->*m_pfnSelector)(m_fElapsed);@H_403_14@//看这里@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ ...............@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ else@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ if@H_403_14@ (m_fElapsed >= m_fInterval)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ if@H_403_14@ (m_pTarget && m_pfnSelector)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ (m_pTarget->*m_pfnSelector)(m_fElapsed);@H_403_14@//看这里@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ .......................@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ ..............@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ }@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
你可能还不知道(m_pTarget->*m_pfnSelector)(m_fElapsed);这个什么意思,没事,重新看看HelloWorldScene.h调用update函数时的代码:this->schedule(schedule_selector( HelloWorld::update));下面是schedule的实现(上面已提过)
void@H_403_14@ CCNode::schedule(SEL_SCHEDULE selector)@H_403_14@
{@H_403_14@
@H_403_14@ this@H_403_14@ ->schedule(selector,!m_bRunning);@H_403_14@//看这里@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
最后一步,就是看m_pScheduler->scheduleSelector(selector,this,!m_bRunning)和(m_pTarget->*m_pfnSelector)(m_fElapsed)有什么关系,其中selector是函数指针,指向HelloWorld::update,不懂的可以看schedule_selector的定义。 下面是scheduleSelector函数的实现
void@H_403_14@ CCScheduler::scheduleSelector(SEL_SCHEDULE pfnSelector,CCObject *pTarget,@H_403_14@ float@H_403_14@ fInterval,unsigned@H_403_14@ int@H_403_14@ repeat,@H_403_14@ float@H_403_14@ delay,bool bPaused)@H_403_14@
{@H_403_14@
@H_403_14@ ................@H_403_14@
@H_403_14@ tHashTimerEntry *pElement = NULL;@H_403_14@
@H_403_14@ HASH_FIND_INT(m_pHashForTimers,&pTarget,pElement);@H_403_14@
@H_403_14@ if@H_403_14@ (! pElement)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ ...........................@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ else@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ CCAssert(pElement->paused == bPaused,@H_403_14@ ""@H_403_14@ );@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ if@H_403_14@ (pElement->timers == NULL)@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ pElement->timers = ccArrayNew(@H_403_14@ 10@H_403_14@ );@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ else@H_403_14@
@H_403_14@ {@H_403_14@
@H_403_14@ ....................@H_403_14@
@H_403_14@ }@H_403_14@
@H_403_14@ CCTimer *pTimer =@H_403_14@ new@H_403_14@ CCTimer();@H_403_14@
@H_403_14@ pTimer->initWithTarget(pTarget,pfnSelector,fInterval,delay);@H_403_14@//看这里@H_403_14@
@H_403_14@ ccArrayAppendObject(pElement->timers,pTimer);@H_403_14@
@H_403_14@ pTimer->release();@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
重点看pTimer->initWithTarget(pTarget,delay),其中pTarget就是CCNode(对应HelloWorld类)对象本身,而pfnSelector就是刚才说的函数指针,指向HelloWorld::update,下面是initWithTarget的函数实现
bool CCTimer::initWithTarget(CCObject *pTarget,SEL_SCHEDULE pfnSelector,@H_403_14@ float@H_403_14@ fSeconds,unsigned@H_403_14@ int@H_403_14@ nRepeat,@H_403_14@ float@H_403_14@ fDelay)@H_403_14@
{@H_403_14@
@H_403_14@ m_pTarget = pTarget;@H_403_14@
@H_403_14@ m_pfnSelector = pfnSelector;@H_403_14@
@H_403_14@ .....................@H_403_14@
@H_403_14@ return@H_403_14@ true@H_403_14@ ;@H_403_14@
}@H_403_14@
@H_403_14@
@H_403_14@
由此,我们解决刚才提出的问题: 看看m_pScheduler->scheduleSelector(selector,!m_bRunning)和(m_pTarget->*m_pfnSelector)(m_fElapsed)有什么关系,其中selector是函数指针,指向HelloWorld::update。 答案就在上面,再次啰嗦说明一下,pTarget就是CCNode(对应HelloWorld类)对象本身,而pfnSelector就是刚才说的函数指针,指向HelloWorld::update,也就是说,HelloWorld类定义的void update(float dt)被调用了,而且是在整个主循环里面CCDirector::sharedDirector()->mainLoop()。 (5)结束语: 整个cocos2d-x的框架 源码就是这样,具体一些细节还未涉及,但我们已经理解程序一开始从main函数,然后怎么样一步步调用到我们自己是HelloWorld.h里面自己定义实现的函数,特别是update函数,同时也了解了cocos2d-x的一些内存管理机制和回调机制,也在这个框架代码里面,还有整个框架是怎么实现帧数控制的。 用《一代宗师》里面的一句话来概括这次框架源码研读之旅吧——”念念不忘,必有回响“

猜你在找的Cocos2d-x相关文章