标签:cocos2d-x 3.3 3.2 2.2.3 director
 /**
     *  Sets the Resource root path.
     *  @deprecated Please use FileUtils::getInstance()->setSearchPaths() instead.
     */
    CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir);
    /** 
     *  Gets the Resource root path.
     *  @deprecated Please use FileUtils::getInstance()->getSearchPaths() instead. 
     */
    CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath(void);
1、越来越多的api变化,怎么办?鉴于此,以后每次升级引擎的时候,在全局中搜索:CC_DEPRECATED_ATTRIBUTE,这样子就可以看到那些api进行了修改。如果是在mac环境中,则可以继续查找结果中匹配的字符,比如某个api,直接搜索看看说明。这样子就能方便的切换新api,而不是经常跳转。
2、我终于明白引擎的运行完整路线了。
首先,通过director和scene的create和add分别完成资源创建和渲染树构造,之前一直漏掉了这句,现在已经补上了。可以去相关博客重新阅读。
接着,每次循环中执行cocos2d-x的引擎主线和轮询OpenGL的事件
 <span style="white-space: pre;">		</span>director->mainLoop();
           <span style="white-space: pre;">	</span>glview->pollEvents();
然后,在mainLoop中完成drawScene和清理当前垃圾(未处理的内存要求开发自己去管理),drawScene主要完成了:
 // calculate "global" dt  计算时间切片
    calculateDeltaTime();
    
    // skip one flame when _deltaTime equal to zero. 是否跳过当前帧的绘制
    if(_deltaTime < FLT_EPSILON)
    {
        return;
    }
    if (_openGLView)  //轮询OpenGL的回调事件处理,可以看到目前是个空实现
    {
        _openGLView->pollEvents();
    }//tick before glClear: issue #533
    if (! _paused)
    {
        _scheduler->update(_deltaTime);
        _eventDispatcher->dispatchEvent(_eventAfterUpdate);
    }
完了原本的数据处理之后开始进入图像渲染的工作流:
/* 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();
    }
1、setNextScene的工作内容和代码:根据是否需要切换场景完成场景切换相关:添加节点:执行完init()方法进入replaceScene后,在渲染树中,新旧场景是同时存在的 ,1.1、先清理旧场景,执行旧场景的清场 1.2 执行新场景的入场和入场后动作。我再次瞄了一下3.3、3.2、2.2.3和是一样的,我曾经因为听别人说先进入新场景进场再执行旧场景的退场(这可能是之前某个版本的bug,现在不再去考究了)。大家记住,一定要自己看源码,要不然就不选开源引擎啦。
void Director::setNextScene()
{
    bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr;
    bool newIsTransition = dynamic_cast<TransitionScene*>(_nextScene) != nullptr;
    // If it is not a transition, call onExit/cleanup
     if (! newIsTransition)
     {
         if (_runningScene)
         {
             _runningScene->onExitTransitionDidStart();
             _runningScene->onExit();
         }
 
         // issue #709. the root node (scene) should receive the cleanup message too
         // otherwise it might be leaked.
         if (_sendCleanupToScene && _runningScene)
         {
             _runningScene->cleanup();
         }
     }
    if (_runningScene)
    {
        _runningScene->release();
    }
    _runningScene = _nextScene;
    _nextScene->retain();
    _nextScene = nullptr;
    if ((! runningIsTransition) && _runningScene)
    {
        _runningScene->onEnter();
        _runningScene->onEnterTransitionDidFinish();
    }
}//入栈,设置OpenGL的状态机,跟2D、3D切换相关,悲剧的是我没看懂,求大家一起来
pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
 // draw the scene
    if (_runningScene)
    {
        //clear draw stats  3.3才有几这句,3.2是没有的
        _renderer->clearDrawStats();
        
        //render the scene
        _runningScene->render(_renderer);
        
        _eventDispatcher->dispatchEvent(_eventAfterVisit);
    }绘制未变化或刚刚设置挂载的渲染树(在跟渲染相关时,我比较喜欢把scene成为渲染树,因为它就是rootNode,在游戏引擎为单窗口单线程中只有一个Scene)//下面这段代码是完成弹窗通知,因为其绘制永远在主渲染树之后,所以永远会遮盖当前的游戏主图像
// draw the notifications node
    if (_notificationNode)
    {
        _notificationNode->visit(_renderer, Mat4::IDENTITY, false);
    }
//设置debug状态下的OpenGL绘制信息,不影响游戏情节
    if (_displayStats)
    {
        showStats();
    }_renderer->render();
待续!!!绘制命令的执行。
立即模式下是直接在引擎的每个渲染节点中使用OpenGL的绘制命令,所以会有这样类似的代码:kmGLPushMatrix、kmGLPopMatrix 管理OpenGL的状态。
   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();v3用的是显示列表模式(不再需要我们自己去使用OpenGL的命令,因为已经有封装好立即模式的DrawPrimitives了)
绘制完之后:
<span style="white-space:pre">	</span>_eventDispatcher->dispatchEvent(_eventAfterDraw);//派发绘制结束的事件,在OpenGL绘制之后主线程回到cocos引擎中
    <span style="white-space:pre">	</span>popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); //回复原先的opengl状态// swap buffers
    if (_openGLView)
    {
        _openGLView->swapBuffers();
    }
    if (_displayStats)
    {
        calculateMPF();
    }cocos2d-x 3.3 RC 个人升级总结 Director的主线解析
标签:cocos2d-x 3.3 3.2 2.2.3 director
原文地址:http://blog.csdn.net/jingzhewangzi/article/details/40753995