本文我们来学习cocos2dx渲染流程,我们所使用2.x版本,一个程序的开始:我们先从main函数开始看,
return CCApplication::sharedApplication()->run();
进入改函数中,改函数中代码片段 CCApplication类是跨平台文件,不同平台的实现方式不一样,ios的是*.mm文件,里面实现死循环的方式也是调用该平台的方式,不过意思都是这样只不过,实现不同。
//一个死循环
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();
}
这里会按条件一直调用CCDirector中的mainLoop函数,进入这个函数,函数一面调用
drawScene();
drawScene 代码片段
// draw the scene
if (m_pRunningScene)
{
m_pRunningScene->visit();
}
// draw the notifications node
if (m_pNotificationNode)
{
m_pNotificationNode->visit();
}
里面调用了visit()函数,cocos2dx所有node都继承于CCNode,我们看一下CCNode的中的visit函数
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 < 0
ccArray *arrayData = m_pChildren->data;
for( ; i < arrayData->num; i++ )
{
pNode = (CCNode*) arrayData->arr;
if ( pNode && pNode->m_nZOrder < 0 )
{
pNode->visit();
}
else
{
break;
}
}
// self draw
this->draw();
//获取到所有子节点,调用该子节点的onvisit方法
for( ; i < arrayData->num; i++ )
{
pNode = (CCNode*) arrayData->arr;
if (pNode)
{
pNode->visit();
}
}
}
else
{//调用自己的画方法,实现图像绘制
this->draw();
}
// reset for next frame
m_uOrderOfArrival = 0;
if (m_pGrid && m_pGrid->isActive())
{
m_pGrid->afterDraw(this);
}
kmGLPopMatrix();
}