Cocos2D-X Version 3.0+ RenderTexture saveToFile

Hello Friends,

I was trying to capture my game screen using RenderTexture and trying to save as PNG image. It was working nice on version Cocos2D-X 2.2+. But I was facing issue with version 3.6. Finally I got it working. Hope this may help to someone and may save someone’s precious time.

Here is the code snippet to capture screen and save as image. (I have usedCocos2D-X version 3.6 and  tested it on Simulator. Its working.)


Sprite *sprite = Sprite::create(“main_bg.png”);

RenderTexture* renderTexture = RenderTexture::create(sprite->getBoundingBox().size.width, sprite->getBoundingBox().size.height);

renderTexture->retain();

renderTexture->begin();

sprite->visit();

renderTexture->end();

renderTexture->saveToFile(“snapshot.png”, Image::Format::PNG, true, [&] (RenderTexture * rt, const std::string & path) {

        CCLOG(“Image finally saved…”);

       // Proceed further with saved image.

});


This callback function will wait till RenderTexture saves the image.

Leave a Reply

Your email address will not be published. Required fields are marked *