cocos的节点类CCNode中有如下接口visit()和draw(),其中visit()是外部用来调用绘制显示的,而draw()是用来实现具体绘制过程的。下面我们来分析CCNode的绘制过程,这里我们使用两个方面的例子来说明:1.通过场景的绘制来分析。2.通过CCRenderTexture的绘制来分析。
1.CCDirector的绘制场景的方法,如下所示
// Draw the Scene
void CCDirector::drawScene(void)
{
// calculate "global" dt
calculateDeltaTime();
//tick before glClear: issue #533
if (! 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 node
if (m_pNotificationNode)
{
m_pNotificationNode->visit();
}
if (m_bDisplayStats)
{
showStats();
}
kmGLPopMatrix();
m_uTotalFrames++;
// swap buffers
if (m_pobOpenGLView)
{
m_pobOpenGLView->swapBuffers();
}
if (m_bDisplayStats)
{
calculateMPF();
}
}
从如下的代码中我们可以看到,场景的绘制使用的是visit()接口。
2.CCRenderTexture使用begin()和end()接口,中间部分调用可绘制对象(CCNode)的visit接口来进行绘制。