Cocos2d-x 3.0 screenshot function integration

Source: Internet
Author: User

3.0 screenshots and 2. the screenshots of x are basically the same. They are all processed using RenderTexture. call the call function before rendering, and then call the scenario visit function of Cocos to render it. After rendering, call the end function. Note that only the next frame of the 3.0 screenshot must be completed before RenderTexture can be processed. For the API and demo of RenderTexture of 2.x, see http://blog.csdn.net/jackystudio/article/details/15498083.

This article focuses on how to inherit functionality to the Cocos2d-x 3.0 Engine.



1. It is a good idea to integrate to ctor and choose to inherit the screenshot function from Director.

Void Director: saveScreenshot (const std: string & fileName, const std: function <void (const std: string &)> & callback) {Image: Format format; // determine the suffix if (std: string: npos! = FileName. find_last_of (".") {auto extension = fileName. substr (fileName. find_last_of ("."), fileName. length (); if (! Extension. compare (". png") {format = Image: Format: PNG;} else if (! Extension. compare (". jpg ") {format = Image: Format: JPG;} else {CCLOG (" cocos2d: the image can only be saved as JPG or PNG format "); return ;}} else {CCLOG ("cocos2d: the image can only be saved as JPG or PNG format"); return ;}// get the screen size, initialize an empty rendering texture object auto renderTexture = RenderTexture: create (getWinSize (). width, getWinSize (). height, Texture2D: PixelFormat: RGBA8888); // clear and start obtaining renderTexture-> beginWithClear (0.0f, 0.0f, 0.0f, 0.0f); // traverse scene node objects, fill in the texture to the RenderTexture getRunningScene ()-> visit (); // get the renderTexture-> end (); // save the file renderTexture-> saveToFile (fileName, format ); // call the callback function auto fullPath = FileUtils: getInstance ()-> getWritablePath () + fileName; auto scheduleCallback = [&, fullPath, callback] (float dt) {callback (fullPath) ;}; auto _ schedule = getRunningScene ()-> getScheduler (); _ schedule-> schedule (scheduleCallback, this, 0.0f, 0, 0.0f, false, "screenshot ");}

2. how to Use the saveScreenshot screenshot function is also easy to use. You can call saveSecreenshot directly. The first parameter is the file name (png and jpg formats are supported, and the default format is png without a suffix ), the second parameter is the callback function. You can process this file in the callback function.

Void ScreenshotTest: saveImage (Ref * pSender) {static int counter = 0; char png [20]; sprintf (png, "image-0000d.png", counter); char jpg [20]; sprintf (jpg, "image-0000d.jpg", counter); // callback function after screenshot, which is displayed in the lower left corner of auto callback = [&] (const std: string & fullPath) {auto sprite = Sprite: create (fullPath); CCASSERT (sprite! = Nullptr, "Failed to create sprite. "); addChild (sprite); sprite-> setScale (0.3f); sprite-> setPosition (Point (40, 40); sprite-> setRotation (counter * 3 ); CCLOG ("Image saved % s", fullPath. c_str () ;}; // call Director's screenshot function Director: getInstance ()-> saveScreenshot (png, callback); counter ++ ;}

3. Source Code download this function has submitted pull request to the Cocos2d-x Github, there is a need for children's shoes can be integrated by themselves. Specific source code can see: https://github.com/cocos2d/cocos2d-x/pull/5978

Related Article

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.