首页 > 移动平台 > 详细

cocos2d-x源码分析-----主循环(android)

时间:2014-02-25 10:52:52      阅读:359      评论:0      收藏:0      [点我收藏+]

在前面的engine_draw_frame的函数中,可以看到每一帧都调用了cocos2d::Director::getInstance()->mainLoop()方法,接下来,去看些循环中具体做了什么

bubuko.com,布布扣
void DisplayLinkDirector::mainLoop()
{
    if (_purgeDirectorInNextLoop)
    {
        _purgeDirectorInNextLoop = false;
        purgeDirector();
    }
    else if (! _invalid)
    {
        drawScene();
     
        // release the objects
        PoolManager::sharedPoolManager()->pop();        
    }
}
bubuko.com,布布扣

当中,还做了一些矩阵的计算和变换。

_purgeDirectorInNextLoop 注释已经写明了。// this flag will be set to true in end()  在结束时,会设为true,接下来是释放资源用的。

重点在drawScene() 这个函数.

 

bubuko.com,布布扣
// Draw the Scene
void Director::drawScene()
{
    //计算了每帧的时间差。
    calculateDeltaTime();

    if (_openGLView)
    {
        _openGLView->pollInputEvents();
    }

    //没有暂停的情况下,引用定时器
    if (! _paused)
    {
        _scheduler->update(_deltaTime);
        _eventDispatcher->dispatchEvent(_eventAfterUpdate);
    }

    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 (_nextScene)
    {
        setNextScene();
    }

    kmGLPushMatrix();
    
    //construct the frustum
    {
        kmMat4 view;
        kmMat4 projection;
        kmGLGetMatrix(KM_GL_PROJECTION, &projection);
        kmGLGetMatrix(KM_GL_MODELVIEW, &view);
        
        _cullingFrustum->setupFromMatrix(view, projection);
    }
    
    // 绘制当前的场景
    if (_runningScene)
    {
        _runningScene->visit();
        _eventDispatcher->dispatchEvent(_eventAfterVisit);
    }

    // draw the notifications node
    if (_notificationNode)
    {
        _notificationNode->visit();
    }
    
//是否显示帧数等一些信息
if (_displayStats) { showStats(); } _renderer->render(); _eventDispatcher->dispatchEvent(_eventAfterDraw); kmGLPopMatrix(); _totalFrames++; // swap buffers if (_openGLView) { _openGLView->swapBuffers(); } if (_displayStats) { calculateMPF(); } }
bubuko.com,布布扣

以上代码中,对_openGLView具体的作用不太了解,猜测是渲染使用的,接下来,要好好注意这个问题了。

cocos2d-x源码分析-----主循环(android)

原文:http://www.cnblogs.com/lihaibo19891007/p/3564331.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!