int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
...
// create the application instance
AppDelegate app;//看这里
...
return CCApplication::sharedApplication()->run();//看这里
}CCApplication::CCApplication()
: m_hInstance(NULL)
, m_hAccelTable(NULL)
{
m_hInstance = GetModuleHandle(NULL);
m_nAnimationInterval.QuadPart = 0;
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}int CCApplication::run()
{
...
// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{
return 0;
}
..
while (1)
{
if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Get current time tick.
QueryPerformanceCounter(&nNow);
// If it‘s the time to draw next frame, draw it, else sleep a while.
if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart;
CCDirector::sharedDirector()->mainLoop();//看这里
}
else
{
Sleep(0);
}
continue;
}
if (WM_QUIT == msg.message)
{
// Quit message loop.
break;
}
// Deal with windows message.
if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}void CCDisplayLinkDirector::mainLoop(void)
{
if (m_bPurgeDirecotorInNextLoop)
{
m_bPurgeDirecotorInNextLoop = false;
purgeDirector();
}
else if (! m_bInvalid)
{
drawScene();//看这里
// release the objects
CCPoolManager::sharedPoolManager()->pop(); //看这里
}
}class CC_DLL CCDirector : public CCObject, public TypeInfo
{
.......
virtual void mainLoop(void) = 0;//看这里
.......
}
class CCDisplayLinkDirector : public CCDirector
{
..........
}CCSprite* CCSprite::create(const char *pszFileName)
{
CCSprite *pobSprite = new CCSprite();
if (pobSprite && pobSprite->initWithFile(pszFileName))
{
pobSprite->autorelease();//看这里
return pobSprite;
}
CC_SAFE_DELETE(pobSprite);
return NULL;
}CCObject* CCObject::autorelease(void)
{
CCPoolManager::sharedPoolManager()->addObject(this);
return this;
}void CCObject::retain(void)
{
CCAssert(m_uReference > 0, "reference count should greater than 0");
++m_uReference;//看这里
}void CCObject::release(void)
{
CCAssert(m_uReference > 0, "reference count should greater than 0");
--m_uReference;//看这里
if (m_uReference == 0)//看这里
{
delete this;
}
}// Draw the Scene
void CCDirector::drawScene(void)
{
..............
//tick before glClear: issue #533
if (! m_bPaused)
{
m_pScheduler->update(m_fDeltaTime);//看这里
}
.....................
// draw the scene
if (m_pRunningScene)
{
m_pRunningScene->visit();
}
.....................
// swap buffers
if (m_pobOpenGLView)
{
m_pobOpenGLView->swapBuffers();
}
................
}void CCNode::schedule(SEL_SCHEDULE selector)
{
this->schedule(selector, 0.0f, kCCRepeatForever, 0.0f);
}
void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
{
CCAssert( selector, "Argument must be non-nil");
CCAssert( interval >=0, "Argument must be positive");
m_pScheduler->scheduleSelector(selector, this, interval , repeat, delay, !m_bRunning);//看这里
}
class CC_DLL CCNode : public CCObject
{ .........
CCScheduler *m_pScheduler; //看这里
.......
}// Draw the Scene
void CCDirector::drawScene(void)
{
..............
//tick before glClear: issue #533
if (! m_bPaused)
{
m_pScheduler->update(m_fDeltaTime);//看这里
}
.....................
}
class CC_DLL CCDirector : public CCObject, public TypeInfo
{ ...............
CC_PROPERTY(CCScheduler*, m_pScheduler, Scheduler);//看这里
..............
}
#define CC_PROPERTY(varType, varName, funName)\//看这里
protected: varType varName;public: virtual varType get##funName(void);public: virtual void set##funName(varType var);CCNode::CCNode(void)
: ...........
{
CCDirector *director = CCDirector::sharedDirector();
...............
m_pScheduler = director->getScheduler();//看这里
................
}// main loop
void CCScheduler::update(float dt)
{
................
// Iterate over all the custom selectors
for (tHashTimerEntry *elt = m_pHashForTimers; elt != NULL; )
{
m_pCurrentTarget = elt;
m_bCurrentTargetSalvaged = false;
if (! m_pCurrentTarget->paused)
{
// The ‘timers‘ array may change while inside this loop
for (elt->timerIndex = 0; elt->timerIndex < elt->timers->num; ++(elt->timerIndex))
{
elt->currentTimer = (CCTimer*)(elt->timers->arr[elt->timerIndex]);
elt->currentTimerSalvaged = false;
elt->currentTimer->update(dt);//看这里
......................
}
}
..................
}
...............
}void CCTimer::update(float dt)
{
if (m_fElapsed == -1)
{
....................
}
else
{
if (m_bRunForever && !m_bUseDelay)
{//standard timer usage
...................
if (m_fElapsed >= m_fInterval)
{
if (m_pTarget && m_pfnSelector)
{
(m_pTarget->*m_pfnSelector)(m_fElapsed);//看这里
}
...................
}
}
else
{//advanced usage
...............
if (m_bUseDelay)
{
if( m_fElapsed >= m_fDelay )
{
if (m_pTarget && m_pfnSelector)
{
(m_pTarget->*m_pfnSelector)(m_fElapsed);//看这里
}
...............
}
}
else
{
if (m_fElapsed >= m_fInterval)
{
if (m_pTarget && m_pfnSelector)
{
(m_pTarget->*m_pfnSelector)(m_fElapsed);//看这里
}
.......................
}
}
..............
}
}
}void CCNode::schedule(SEL_SCHEDULE selector)
{
this->schedule(selector, 0.0f, kCCRepeatForever, 0.0f);
}
void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
{
CCAssert( selector, "Argument must be non-nil");
CCAssert( interval >=0, "Argument must be positive");
m_pScheduler->scheduleSelector(selector, this, interval , repeat, delay, !m_bRunning);//看这里
}void CCScheduler::scheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget, float fInterval, unsigned int repeat, float delay, bool bPaused)
{
................
tHashTimerEntry *pElement = NULL;
HASH_FIND_INT(m_pHashForTimers, &pTarget, pElement);
if (! pElement)
{
...........................
}
else
{
CCAssert(pElement->paused == bPaused, "");
}
if (pElement->timers == NULL)
{
pElement->timers = ccArrayNew(10);
}
else
{
....................
}
CCTimer *pTimer = new CCTimer();
pTimer->initWithTarget(pTarget, pfnSelector, fInterval, repeat, delay);//看这里
ccArrayAppendObject(pElement->timers, pTimer);
pTimer->release();
}bool CCTimer::initWithTarget(CCObject *pTarget, SEL_SCHEDULE pfnSelector, float fSeconds, unsigned int nRepeat, float fDelay)
{
m_pTarget = pTarget;
m_pfnSelector = pfnSelector;
.....................
return true;
}cocos2d-x整体框架源码分析以及启动过程原理(win32),布布扣,bubuko.com
cocos2d-x整体框架源码分析以及启动过程原理(win32)
原文:http://blog.csdn.net/jinble03/article/details/20006407