本文主要是介绍cocos2dx 2.x 每帧渲染分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、java端
1、Cocos2dxRenderer类@Overridepublic void onDrawFrame(final GL10 gl) {/** No need to use algorithm in default(60 FPS) situation,* since onDrawFrame() was called by system 60 times per second by default.*/if (sAnimationInterval <= 1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND) {Cocos2dxRenderer.nativeRender();} else {final long now = System.nanoTime();final long remain = mLastTickInNanoSeconds + Cocos2dxRenderer.sAnimationInterval - now;if (remain > 0) {try {Thread.sleep(remain / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);} catch (final Exception e) {}}/** Render time MUST be counted in, or the FPS will slower than appointed.*/mLastTickInNanoSeconds = System.nanoTime();Cocos2dxRenderer.nativeRender(); //调用C++部分的函数}}
2、Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp C++端
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender(JNIEnv* env) {cocos2d::CCDirector::sharedDirector()->mainLoop();}-->> mainLoop函数void CCDisplayLinkDirector::mainLoop(void)
{if (m_bPurgeDirecotorInNextLoop){m_bPurgeDirecotorInNextLoop = false;purgeDirector();}else if (! m_bInvalid){drawScene();// release the objectsCCPoolManager::sharedPoolManager()->pop(); }
}-->>drawScene渲染
// Draw the Scene
void CCDirector::drawScene(void)
{// calculate "global" dtcalculateDeltaTime();//tick before glClear: issue #533if (! m_bPaused) //定时器相关,以后说{m_pScheduler->update(m_fDeltaTime);}glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);/* to avoid flickr, nextScene MUST be here: after tick and before draw.XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */if (m_pNextScene){setNextScene();}kmGLPushMatrix();// draw the scene 渲染if (m_pRunningScene){m_pRunningScene->visit();}// draw the notifications nodeif (m_pNotificationNode){m_pNotificationNode->visit();}if (m_bDisplayStats){showStats();}kmGLPopMatrix();m_uTotalFrames++;// swap buffersif (m_pobOpenGLView){m_pobOpenGLView->swapBuffers();}if (m_bDisplayStats){calculateMPF();}
}
-->>visit函数,遍历UI树,调用visit和draw进行每个node的渲染
void CCNode::visit()
{// quick return if not visible. children won't be drawn.if (!m_bVisible){return;}kmGLPushMatrix();if (m_pGrid && m_pGrid->isActive()){m_pGrid->beforeDraw();}this->transform();CCNode* pNode = NULL;unsigned int i = 0;if(m_pChildren && m_pChildren->count() > 0){sortAllChildren();// draw children zOrder < 0ccArray *arrayData = m_pChildren->data;for( ; i < arrayData->num; i++ ){pNode = (CCNode*) arrayData->arr[i];if ( pNode && pNode->m_nZOrder < 0 ) {pNode->visit();}else{break;}}// self drawthis->draw();for( ; i < arrayData->num; i++ ){pNode = (CCNode*) arrayData->arr[i];if (pNode){pNode->visit();}} }else{this->draw();}// reset for next framem_uOrderOfArrival = 0;if (m_pGrid && m_pGrid->isActive()){m_pGrid->afterDraw(this);}kmGLPopMatrix();
}
这篇关于cocos2dx 2.x 每帧渲染分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!