引言:如果你想深入了解cocos2d-x的整个框架和运行流程,如果你想知道整个启动过程的细节,如果你想知道自己写的代码是在什么时候和在哪里被调用的,下面可以为你解答其中奥秘。 对象:适合刚刚入门了cocos2d-x的初学者,编写并运行过简单的demo,并且想仔细探究其中的原理机制。1、程序入口main.cpp
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_hAccel@R_419_5991@(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 ,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_hAccel@R_419_5991@ || ! TranslateAccelerator(msg.hwnd,m_hAccel@R_419_5991@,&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); //看这里 .............. } #@R_419_5552@ 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,!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,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)全部内容,希望文章能够帮你解决cocos2d-x整体框架源码分析以及启动过程原理(win32)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)