cocos2dx 3.x 支持自定义的渲染命令

版权声明:本博客所有原创文章均可以转载 https://blog.csdn.net/ivanx_cc/article/details/49451621

头文件, 重写draw 与 onDraw函数

Test.h

public:

void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags);

private:

    void onDraw(const kmMat4 &transform, uint32_t flags);

    CustomCommand _customCommand;


源文件

Test.cpp

只需要添加这样的基本代码就可以实现自己定义的命令,自定义的命令在 onDraw中实现。

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

本测试只是画一个矩形。

void Test::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)

{

    log("on Draw");

        _customCommand.init(_globalZOrder);

        _customCommand.func = CC_CALLBACK_0(TowerPos::onDraw, this, transform, flags);

        renderer->addCommand(&_customCommand);

}

void Test::onDraw(const kmMat4 &transform, uint32_t flags)

{

    

    kmGLPushMatrix();

    kmGLLoadMatrix(&transform);

    

    glLineWidth(5.0f);

    

    Point srcPos = Point(m_pos.x - RADIUS(RADIUS32), m_pos.y + RADIUS(RADIUS32));

    Point destPos = Point(m_pos.x + RADIUS(RADIUS32), m_pos.y - RADIUS(RADIUS32));

    log("%f, %f, %f, %f", srcPos.x, srcPos.y, destPos.x, destPos.y);

    DrawPrimitives::drawRect(srcPos, destPos);

    

    glLineWidth(5);

    

    kmGLPopMatrix();

}

猜你喜欢

转载自blog.csdn.net/ivanx_cc/article/details/49451621
今日推荐