比较Cocos2d-x v2.x与v3.x的截图功能

本文详细介绍了Cocos2d-x2.x版本中如何使用CCRenderTexture实现截图功能,并阐述了Cocos2d-x3.x版本中截图功能的改进与使用方法。从Cocos2d-x3.2版本开始,引擎提供了更便捷的captureScreen函数来实现截图操作。

(1)Cocos2d-x 2.x

Cocos2d-x 2.x没有提供截图功能,但是可以用CCRenderTexture来实现这个功能:

  1. void CTestLayer::SaveScreenShot()  
  2.   {  
  3.       //获取屏幕尺寸    
  4.       CCSize size = CCDirector::sharedDirector()->getWinSize();  
  5.       //使用屏幕尺寸初始化一个空的渲染纹理对象    
  6.       CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height);  
  7.       //设置位置        
  8.       texture->setPosition(ccp(size.width/2, size.height/2));  
  9.       //开始获取        
  10.       texture->begin();  
  11.       //遍历场景节点对象,填充纹理到texure中    
  12.       CCDirector::sharedDirector()->getRunningScene()->visit();  
  13.       //结束获取    
  14.       texture->end();  
  15.       //保存为PNG图,Win32/Debug目录下    
  16.       texture->saveToFile("screenshot.png", kCCImageFormatPNG);  
  17.   }  

(2)Cocos2d-x 3.x

在Cocos2d-x 3.2之前,引擎也没有提供截图功能,同样可以使用RenderTexture实现:

  1. void Director::saveScreenshot(const std::string& fileName,const std::function<void(const std::string&)>& callback)  
  2.   {  
  3.       Image::Format format;  
  4.       //进行后缀判断    
  5.       if(std::string::npos != fileName.find_last_of(".")){  
  6.         auto extension = fileName.substr(fileName.find_last_of("."),fileName.length());  
  7.         if (!extension.compare(".png")) {  
  8.             format = Image::Format::PNG;  
  9.         } else if(!extension.compare(".jpg")) {  
  10.             format = Image::Format::JPG;  
  11.         } else{  
  12.             log("cocos2d: the image can only be saved as JPG or PNG format");  
  13.             return;  
  14.         }  
  15.     } else {  
  16.         log("cocos2d: the image can only be saved as JPG or PNG format");  
  17.         return ;  
  18.     }  
  19.     //获取屏幕尺寸,初始化一个空的渲染纹理对象    
  20.       auto renderTexture = RenderTexture::create(getWinSize().width, getWinSize().height, Texture2D::PixelFormat::RGBA8888);  
  21.       //清空并开始获取    
  22.       renderTexture->beginWithClear(0.0f, 0.0f, 0.0f, 0.0f);  
  23.       //遍历场景节点对象,填充纹理到RenderTexture中    
  24.       getRunningScene()->visit();  
  25.       //结束获取    
  26.       renderTexture->end();  
  27.       //保存文件    
  28.       renderTexture->saveToFile(fileName , format);  
  29.       //使用schedule在下一帧中调用callback函数    
  30.       auto fullPath = FileUtils::getInstance()->getWritablePath() + fileName;  
  31.       auto scheduleCallback = [&,fullPath,callback](float dt){  
  32.           callback(fullPath);  
  33.       };  
  34.       auto _schedule = getRunningScene()->getScheduler();  
  35.       _schedule->schedule(scheduleCallback, this, 0.0f,0,0.0f, false"screenshot");  
  36.   }  
(3)从Cocos2d-x 3.2之后开始,引擎提供了captureScreen函数来实现截图功能:

  1. void Util::captureScreen(const std::function<void(boolconst std::string&)>& afterCaptured, const std::string& filename); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值