[Cocos2d-x] cocos2dx screenshot save, cocos2d-xcocos2dx

Source: Internet
Author: User

[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.