cocos2dx渲染指令CustomCommand的使用

cocos2dx中CustomCommand用于用户自定义的渲染指令,使用CustomCommand分4步:
1. 实现一个从Node继承的类
2. 重写draw虚函数,设置渲染回调函数
3. 实现渲染回调函数
4. 添加这个类的实例到创建中

完整代码如下:

class CustomCommandTest : public Node { public: CREATE_FUNC(CustomCommandTest); void CustomCommandTest::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) { _customCommand.init(_globalZOrder); /*设置指令回调函数*/ _customCommand.func = CC_CALLBACK_0(CustomCommandTest::onDraw, this, transform, flags); /*添加渲染指令到渲染队列中*/ renderer->addCommand(&_customCommand); } /*实际的渲染操作*/ void CustomCommandTest::onDraw(const Mat4 &transform, uint32_t flags) { Director* director = Director::getInstance(); director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); //draw CHECK_GL_ERROR_DEBUG(); /*画一个对角线*/ auto visibleSize = Director::getInstance()->getVisibleSize(); DrawPrimitives::drawLine(Vec2(0,0), Vec2(visibleSize.width, visibleSize.height)); CHECK_GL_ERROR_DEBUG(); } private: cocos2d::CustomCommand _customCommand; };

效果图:
cocos2dx渲染指令CustomCommand的使用_第1张图片

你可能感兴趣的:(cocos2d-x)