cocos2d-x 3.3 RC 个人升级总结 Director的主线解析

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.     *  Sets the Resource root path. 
  3.     *  @deprecated Please use FileUtils::getInstance()->setSearchPaths() instead. 
  4.     */  
  5.    CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir);  
  6.   
  7.    /**  
  8.     *  Gets the Resource root path. 
  9.     *  @deprecated Please use FileUtils::getInstance()->getSearchPaths() instead.  
  10.     */  
  11.    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的事件

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space: pre;">      </span>director->mainLoop();  
  2.           <span style="white-space: pre;">    </span>glview->pollEvents();  

然后,mainLoop中完成drawScene和清理当前垃圾(未处理的内存要求开发自己去管理),drawScene主要完成了:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // calculate "global" dt  计算时间切片  
  2.    calculateDeltaTime();  
  3.      
  4.    // skip one flame when _deltaTime equal to zero. 是否跳过当前帧的绘制  
  5.    if(_deltaTime < FLT_EPSILON)  
  6.    {  
  7.        return;  
  8.    }  
  9.   
  10.    if (_openGLView)  //轮询OpenGL的回调事件处理,可以看到目前是个空实现  
  11.    {  
  12.        _openGLView->pollEvents();  
  13.    }  

进入平时主线相关的定时器刷新、事件的派发。
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //tick before glClear: issue #533  
  2.     if (! _paused)  
  3.     {  
  4.         _scheduler->update(_deltaTime);  
  5.         _eventDispatcher->dispatchEvent(_eventAfterUpdate);  
  6.     }  


完了原本的数据处理之后开始进入图像渲染的工作流:

/* to avoid flickr, nextScene MUST be here: after tick and before draw.

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */  
  2. if (_nextScene)  
  3. {  
  4.     setNextScene();  
  5. }  

1、setNextScene的工作内容和代码:根据是否需要切换场景完成场景切换相关:添加节点:执行完init()方法进入replaceScene后,在渲染树中,新旧场景是同时存在的 ,1.1、先清理旧场景,执行旧场景的清场 1.2 执行新场景的入场和入场后动作。我再次瞄了一下3.3、3.2、2.2.3和是一样的,我曾经因为听别人说先进入新场景进场再执行旧场景的退场(这可能是之前某个版本的bug,现在不再去考究了)。大家记住,一定要自己看源码,要不然就不选开源引擎啦。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void Director::setNextScene()  
  2. {  
  3.     bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr;  
  4.     bool newIsTransition = dynamic_cast<TransitionScene*>(_nextScene) != nullptr;  
  5.   
  6.     // If it is not a transition, call onExit/cleanup  
  7.      if (! newIsTransition)  
  8.      {  
  9.          if (_runningScene)  
  10.          {  
  11.              _runningScene->onExitTransitionDidStart();  
  12.              _runningScene->onExit();  
  13.          }  
  14.    
  15.          // issue #709. the root node (scene) should receive the cleanup message too  
  16.          // otherwise it might be leaked.  
  17.          if (_sendCleanupToScene && _runningScene)  
  18.          {  
  19.              _runningScene->cleanup();  
  20.          }  
  21.      }  
  22.   
  23.     if (_runningScene)  
  24.     {  
  25.         _runningScene->release();  
  26.     }  
  27.     _runningScene = _nextScene;  
  28.     _nextScene->retain();  
  29.     _nextScene = nullptr;  
  30.   
  31.     if ((! runningIsTransition) && _runningScene)  
  32.     {  
  33.         _runningScene->onEnter();  
  34.         _runningScene->onEnterTransitionDidFinish();  
  35.     }  
  36. }  

设置完场景,即挂载了当前的渲染树,就开始进入OpenGL的图像处理阶段:

//入栈,设置OpenGL的状态机,跟2D、3D切换相关,悲剧的是我没看懂,求大家一起来

pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  // draw the scene  
  2.     if (_runningScene)  
  3.     {  
  4.         //clear draw stats  3.3才有几这句,3.2是没有的  
  5.         _renderer->clearDrawStats();  
  6.           
  7.         //render the scene  
  8.         _runningScene->render(_renderer);  
  9.           
  10.         _eventDispatcher->dispatchEvent(_eventAfterVisit);  
  11.     }  
绘制未变化或刚刚设置挂载的渲染树(在跟渲染相关时,我比较喜欢把scene成为渲染树,因为它就是rootNode,在游戏引擎为单窗口单线程中只有一个Scene)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //下面这段代码是完成弹窗通知,因为其绘制永远在主渲染树之后,所以永远会遮盖当前的游戏主图像  
  2. // draw the notifications node  
  3.     if (_notificationNode)  
  4.     {  
  5.         _notificationNode->visit(_renderer, Mat4::IDENTITY, false);  
  6.     }  
  7.   
  8. //设置debug状态下的OpenGL绘制信息,不影响游戏情节  
  9.     if (_displayStats)  
  10.     {  
  11.         showStats();  
  12.     }  

接下来又是一句新代码:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. _renderer->render();  

待续!!!绘制命令的执行。



首先得说一句:在2.x中OpenGL的绘制是使用立即模式绘制,所以才有SpriteBatchNode这个产物

立即模式下是直接在引擎的每个渲染节点中使用OpenGL的绘制命令,所以会有这样类似的代码:kmGLPushMatrix、kmGLPopMatrix 管理OpenGL的状态。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. kmGLPushMatrix();  
  2.   
  3.  // draw the scene  
  4.  if (m_pRunningScene)  
  5.  {  
  6.      m_pRunningScene->visit();  
  7.  }  
  8.   
  9.  // draw the notifications node  
  10.  if (m_pNotificationNode)  
  11.  {  
  12.      m_pNotificationNode->visit();  
  13.  }  
  14.    
  15.  if (m_bDisplayStats)  
  16.  {  
  17.      showStats();  
  18.  }  
  19.    
  20.  kmGLPopMatrix();  

但是这个是v2版本的代码。v3中使用的OpenGL 可以参考这个帖子: 点击打开链接

v3用的是显示列表模式(不再需要我们自己去使用OpenGL的命令,因为已经有封装好立即模式的DrawPrimitives了)


绘制完之后:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">    </span>_eventDispatcher->dispatchEvent(_eventAfterDraw);//派发绘制结束的事件,在OpenGL绘制之后主线程回到cocos引擎中  
  2.   
  3.     <span style="white-space:pre">    </span>popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); //回复原先的opengl状态  

双缓冲,切换缓冲区,准备进入下一张画布的绘制。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // swap buffers  
  2.     if (_openGLView)  
  3.     {  
  4.         _openGLView->swapBuffers();  
  5.     }  
  6.   
  7.     if (_displayStats)  
  8.     {  
  9.         calculateMPF();  
  10.     }  


来源:http://blog.csdn.net/jingzhewangzi/article/details/40753995
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值