Cocos2d-x Functions
Cocos2d-x 2.x
Cocos2d-x 2. x does not provide functionality, but it can be achieved with CCRenderTexture:
Void CTestLayer: SaveScreenShot () {// obtain the screen size CCSize size = ccctor ctor: shareddire()-> getWinSize (); // use the screen size to initialize an empty rendered texture object: CCRenderTexture * texture = CCRenderTexture: create (int) size. width, (int) size. height); // set the position texture-> setPosition (ccp (size. width/2, size. height/2); // start to get texture-> begin (); // traverse the scene Node object and fill the texture into the texure CCDirector: sharedDirector ()-> getRunningScene () -> visit (); // obtain the texture end-> end (); // save it as a PNG image. In the Win32/Debug directory, texture-> saveToFile ("screenshot.png ", kCCImageFormatPNG );}
(2) Cocos2d-x 3.x
Before Cocos2d-x 3.2, the engine did not provide functionality and can also be implemented using RenderTexture:
Void Director: saveScreenshot (const std: string & fileName, const std: function
& 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 {log (" cocos2d: the image can only be saved as JPG or PNG format "); return ;}} else {log ("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 ");}
(3) Since Cocos2d-x 3.2, the engine provides the captureScreen function to achieve the function:
void Util::captureScreen(const std::function
& afterCaptured, const std::string& filename);