【升级到Cocos2d-x v3.3-RC0总结】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,而不是经常跳转。

扫描二维码关注公众号,回复: 11694256 查看本文章

2、我终于明白引擎的运行完整路线了。

首先,通过director和scene的create和add分别完成资源创建和渲染树构造。

接着,每次循环中执行cocos2d-x的引擎主线和轮询OpenGL的事件:

director->mainLoop();

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)先清理旧场景,执行旧场景的清场

2)执行新场景的入场和入场后动作。我再次瞄了一下Cocos2d-x v3.3、v3.2、v2.2.3和是一样的,我曾经因为听别人说先进入新场景进场再执行旧场景的退场(这可能是之前某个版本的bug,现在不再去考究了)。大家记住,一定要自己看源码,要不然就不选开源引擎啦。

void Director::setNextScene()

{

bool runningIsTransition = dynamic_cast(_runningScene) != nullptr;

bool newIsTransition = dynamic_cast(_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_STAC

// 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();

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

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

立即模式下是直接在引擎的每个渲染节点中使用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();

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

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

绘制完之后:

_eventDispatcher->dispatchEvent(_eventAfterDraw);//派发绘制结束的事件,在OpenGL绘制之后主线程回到cocos引擎中

popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); //回复原先的OpenGL状态

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

// swap buffers

if (_openGLView)

{

_openGLView->swapBuffers();

}

if (_displayStats)

{

calculateMPF();

}

猜你喜欢

转载自blog.csdn.net/qq_21743659/article/details/108528876
今日推荐