cocos2d-x 3.3 RC 个人升级总结 Director的主线解析,cocos2d-xrc
/**
*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的图像处理阶段:
//入栈,设置OpenGL的状态机,跟2D、3D切换相关,悲剧的是我没看懂,求大家一起来
pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
// draw the scene
if (_runningScene)
{
//clear draw stats3.3才有几这句,3.2是没有的