The reason to implement with private inheritance is to hide some interface details of CCDirector.
*/
class AppDelegate : private cocos2d::CCApplication
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
[displayLink setFrameInterval: self.interval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
// release the objects
CCPoolManager::sharedPoolManager()->pop(); //每次回调都会清楚pool池(还记得之前的内存管理么)
}
}
//tick before glClear: issue #533
if (! m_bPaused) //暂停
{
m_pScheduler->update(m_fDeltaTime); //待会会解释这里的内容
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //函数的作用是用当前缓冲区清除值,也就是glClearColor或者glClearDepth、 glClearIndex、glClearStencil、glClearAccum等函数所指定的值来清除指定的缓冲区,也可以使用glDrawBuffer一次清除多个颜色缓存。比如:glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);第一条语句表示清除颜色设为黑色,第二条语句表示实际完成了把整个窗口清除为黑色的任务,glClear()的唯一参数表示需要被清除的缓冲区
kmGLPushMatrix(); //矩阵压栈
// draw the scene
if (m_pRunningScene)
{
m_pRunningScene->visit(); //遍历正在运行的Scene,先遍历的是CCNode的visit,因为CCScene并没有实现这个方法。
}
// draw the notifications node
if (m_pNotificationNode)
{
m_pNotificationNode->visit(); //遍历m_pNotificationNode,可以通过该m_pNotificationNode绘制特殊的节点
}
if (m_bDisplayStats)
{
showStats(); //左下角的fps(每秒传输的帧数)
}
kmGLPopMatrix(); //从矩阵栈中弹出
m_uTotalFrames++;
// swap buffers
if (m_pobOpenGLView)
{
m_pobOpenGLView->swapBuffers();
}
if
(m_bDisplayStats)return s_SharedDirector;
}
………………...
// scheduler 初始化 CCScheduler定时调度器 第二篇中会看到哟
m_pScheduler = new CCScheduler();
………………….
// create autorelease pool 这里CCPoolManager管理多个CCAutoreleasePool,将CCAutoreleasePool放到CCPoolManager中的m_pReleasePoolStack
return true;
}
if (m_pGrid && m_pGrid->isActive()) //处理CCGridBase
{
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++ ) //遍历zOrder<0的子类
{
pNode = (CCNode*) arrayData->arr[i];
if ( pNode && pNode->m_nZOrder < 0 )
{
pNode->visit();
}
else
{
break;
}
}
// self draw
this->draw();
for( ; i < arrayData->num; i++ )
{
pNode = (CCNode*) arrayData->arr[i];
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
(); //出矩阵// Convert 3x3 into 4x4 matrix
CCAffineTransform tmpAffine = this->nodeToParentTransform(); //获取节点相对于父节点的变换矩阵
CGAffineToGL(&tmpAffine, transfrom4x4.mat); //转换
// Update Z vertex manually
transfrom4x4.mat[14] = m_fVertexZ; //设置z
kmGLMultMatrix( &transfrom4x4 ); //右乘一个矩阵 参数里面那个
// XXX: Expensive calls. Camera should be integrated into the cached affine matrix
if ( m_pCamera != NULL && !(m_pGrid != NULL && m_pGrid->isActive()) )
{
bool translate = (m_obAnchorPointInPoints.x != 0.0f || m_obAnchorPointInPoints.y != 0.0f);
if( translate )
kmGLTranslatef(RENDER_IN_SUBPIXEL(m_obAnchorPointInPoints.x), RENDER_IN_SUBPIXEL(m_obAnchorPointInPoints.y), 0 );
m_pCamera->locate();
if( translate )
kmGLTranslatef(RENDER_IN_SUBPIXEL(-m_obAnchorPointInPoints.x), RENDER_IN_SUBPIXEL(-m_obAnchorPointInPoints.y), 0 );
}
}
struct CCAffineTransform { float a, b, c, d; float tx, ty; }; typedef struct CGAffineTransform CGAffineTransform;
| m11 m21 m31 m41 | | a c 0 tx | | m12 m22 m32 m42 | | b d 0 ty | | m13 m23 m33 m43 | <=> | 0 0 1 0 | | m14 m24 m34 m44 | | 0 0 0 1 |