[Cocos2d-x] cocos2dx storage, cocos2d-xcocos2dx
In the game process, occasionally need and save pictures, but the cocos2d-x itself to this support is relatively small, so can only use a special method to save ~
RenderTexture
RenderTexture is a common rendering object. You can build an RenderTexture object to fill in the content to be rendered and call the call function before rendering starts, call the visit function of the cocos scenario to render it, and call the end function after the rendering is complete. RenderTexture inherits from Node, so you can simply add the rendering texture to your scenario, just like processing nodes in other cocos. Of course, RenderTexture also provides the Save function, you can save the rendered texture as PNG or JPG.
(Reference from http://blog.csdn.net/jackystudio/article/details/15498083)
For example
Sprite *sprite = Sprite::create("HelloWorld.png");sprite -> setAnchorPoint(Point(0, 0));RenderTexture *renderTexture = RenderTexture::create(480, 320, Texture2D::PixelFormat::RGBA8888);renderTexture -> begin();sprite -> visit();renderTexture -> end();renderTexture -> saveToFile(StringUtils::format("demo.png"), Image::Format::PNG);
The authorization code is stored in helloworld.png as demo.png. Where can it be stored? You can use getWritablePath () to obtain
log("%s", FileUtils::getInstance() -> getWritablePath().c_str());
If you need to capture the entire screen, replace the sprite aboveDirector::getInstance() -> getRunningScene()
, Of course, there are simpler methods in 3.x.
utils::captureScreen(CC_CALLBACK_2(HelloWorld::capture, this), "screenshot.png");//capturevoid HelloWorld::capture(bool succeed, const std::string& outputFile){ if (succeed) { log("%s", outputFile); }}
You only need to add a callback function to a statement ~
Finally, paste a Demo that uses RenderTexture to take the small image in the animated big image and store it. The effect is as follows:
Before interception:
After the screenshot is taken: (the image is taken from PlantNanny)
Download images and code
Welcome to my blog: helkyle. tk