【升级到Cocos2d-x v3.3-RC0总结】Director的主线解析
一、API 变更处理
在Cocos2d-x的代码中,存在以下两个被弃用的API:
/**
* 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);
随着引擎的升级,越来越多的API发生了变化。为了方便处理这些变化,在每次升级引擎时,可以在全局中搜索 CC_DEPRECATED_ATTRIBUTE,这样就能看到哪些API进行了修改。如果是在Mac环境中,还可以在查找结果中匹配特定的字符,例如某个API,直接搜索查看其说明。通过这种方式,可以方便地切换到新的API,避免频繁跳转代码。
二、引擎运行完整路线解析
1. 资源创建与渲染树构造
首先,通过 director 和 scene 的 create 和 add 方法分别完成资源创建和渲染树构造。
2. 循环执行引擎主线和轮询 OpenGL 事件
在每次循环中,执行以下操作:
director->mainLoop();
glview->pollEvents();
3. mainLoop 中的具体操作
3.1 计算时间切片与帧跳过判断
// calculate “global” dt 计算时间切片
calculateDeltaTime();
// skip one flame when _deltaTime equal to zero. 是否跳过当前帧的绘制
if(_deltaTime < FLT_EPSILON)
{
return;
}
3.2 轮询 OpenGL 回调事件处理
if (_openGLView) //轮询OpenGL的回调事件处理,可以看到目前是个空实现
{
_openGLView->pollEvents();
}
3.3 定时器刷新与事件派发
进入平时主线相关的定时器刷新、事件的派发:
//tick before glClear: issue #533
if (! _paused)
{
_scheduler->update(_deltaTime);
_eventDispatcher->dispatchEvent(_eventAfterUpdate);
}
3.4 图像渲染工作流
3.4.1 场景切换
/* 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();
}
setNextScene 的工作内容和代码如下:
根据是否需要切换场景完成场景切换相关操作,添加节点,执行完 init() 方法进入 replaceScene 后,在渲染树中,新旧场景是同时存在的。具体步骤如下:
- 清理旧场景:执行旧场景的清场操作。
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(); } }
2. **执行新场景的入场和入场后动作**:
if (_runningScene) { _runningScene->release(); } _runningScene = _nextScene; _nextScene->retain(); _nextScene = nullptr;
if ((! runningIsTransition) && _runningScene) { _runningScene->onEnter(); _runningScene->onEnterTransitionDidFinish(); }
需要注意的是,曾经有人说先进入新场景进场再执行旧场景的退场,这可能是之前某个版本的bug。在实际开发中,一定要自己查看源码,充分利用开源引擎的优势。
##### 3.4.2 OpenGL 图像处理阶段
设置完场景,即挂载了当前的渲染树,就开始进入 OpenGL 的图像处理阶段:
//入栈,设置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();
这里涉及到绘制命令的执行,待后续继续深入分析。
### 4. OpenGL 绘制模式对比
#### 4.1 2.x 版本的立即模式绘制
在 2.x 中,OpenGL 的绘制是使用立即模式绘制,所以才有 `SpriteBatchNode` 这个产物。立即模式下是直接在引擎的每个渲染节点中使用 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();
#### 4.2 3.x 版本的显示列表模式
v3 中使用的是显示列表模式(不再需要我们自己去使用 OpenGL 的命令,因为已经有封装好立即模式的 `DrawPrimitives` 了)。v3 用的 OpenGL 可以参考这个帖子:[点击打开链接](此处应补充具体链接)
### 5. 绘制结束后的操作
//派发绘制结束的事件,在OpenGL绘制之后主线程回到cocos引擎中 _eventDispatcher->dispatchEvent(_eventAfterDraw);
//回复原先的OpenGL状态 popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
// 双缓冲,切换缓冲区,准备进入下一张画布的绘制 // swap buffers if (_openGLView) { _openGLView->swapBuffers(); }
if (_displayStats) { calculateMPF(); }
综上所述,Cocos2d-x 引擎的运行路线涉及资源创建、渲染树构造、循环执行引擎主线和 OpenGL 事件处理、场景切换、图像渲染等多个环节,每个环节都有其特定的功能和实现方式。在升级引擎时,需要关注 API 的变化,并深入理解引擎的运行机制,以确保项目的顺利开发。后续将继续深入分析绘制命令的执行等内容。