effective way to loop onDraw call

Environment: cocos2dx 3.17.2

I’ve been trying to decode a video via ffmpeg and render it in cocos2dx it has gone well although. The problem I have is some trouble with the onDraw call.

void onDraw() {

    if (first_run == false) {

        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vp.get_width(0), vp.get_height(0), GL_RGB, GL_UNSIGNED_BYTE,gl_frame->data[0]);

    } else {

            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, vp.get_width(0), vp.get_height(0), 0, GL_RGB, GL_UNSIGNED_BYTE,gl_frame->data[0]);

            first_run = false;
        }

}

onDraw gets called only once so instead of it showing each frame only the first is shown (like below image)

i cannot call onDraw outside of custom command via the draw function. as the onDraw command MUST be called from cocos2dx’s draw func. (shown below)

void HelloWorld::draw(cocos2d::Renderer * renderer,const cocos2d::Mat4 &transform, uint32_t flags){
    _customCommand.init(VideoTest->getGlobalZOrder());
    _customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw, this);
    renderer->addCommand(&_customCommand);
}

I was told the onDraw should ONLY have render related code inside of it, so that means my idea of adding a loop to call other functions from onDraw to decode or etc is not a good idea. How should i loop this? what is the recommended way to do so? or is there something else I am not aware of?

thank you for reading! :slight_smile:

You should do some reading on the best way to handle this, because it’s pretty much the same regardless of which game engine or framework you decide to use for this task. The decoding should happen in a separate thread, and then the output frames are displayed in the render cycle.

Did you even try to do a web search for something like “cocos2dx ffmpeg”? Plenty of info out there, even an implementation of this which you can learn from: https://github.com/luochong/Cocos2dxVideo

1 Like