c++cocos2d-xcocos2d-x-3.0

Save sprite content as .png file with cocos2d-x


I need to create an empty sprite as a container and add there in different positions different sprites created by using different images. At the end I need to save the result which in added as child into the container sprite as an image - .png for example.

How I can do that?


Solution

  • You can use RenderTexture and call visit method of Sprite. Here is a sample code:

    RenderTexture* renderTexture = RenderTexture::create(width, height, Texture2D::PixelFormat::RGBA8888);
    renderTexture->begin();
    sprite->visit();
    renderTexture->end();
    renderTexture->saveToFile("snapshot.png", Image::Format::PNG);