加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 20:47:59 所属栏目:百科 来源:网络整理
导读:(1)Cocos2d-x 2.x Cocos2d-x 2.x没有提供截图功能,但是可以用CCRenderTexture来实现这个功能: [cpp] view plain copy void CTestLayer::SaveScreenShot() { //获取屏幕尺寸 CCSizesize=CCDirector::sharedDirector()-getWinSize(); //使用屏幕尺寸初始化

(1)Cocos2d-x 2.x

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

[cpp] view plain copy
  1. voidCTestLayer::SaveScreenShot()
  2. {
  3. //获取屏幕尺寸
  4. CCSizesize=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实现:

[cpp] view plain copy
  1. voidDirector::saveScreenshot(conststd::string&fileName,conststd::function<void(conststd::string&)>&callback)
  2. {
  3. Image::Formatformat;
  4. //进行后缀判断
  5. if(std::string::npos!=fileName.find_last_of(".")){
  6. autoextension=fileName.substr(fileName.find_last_of("."),fileName.length());
  7. if(!extension.compare(".png")){
  8. format=Image::Format::PNG;
  9. }elseif(!extension.compare(".jpg")){
  10. format=Image::Format::JPG;
  11. }else{
  12. log("cocos2d:theimagecanonlybesavedasJPGorPNGformat");
  13. return;
  14. }
  15. }else{
  16. log("cocos2d:theimagecanonlybesavedasJPGorPNGformat");
  17. return;
  18. }
  19. //获取屏幕尺寸,初始化一个空的渲染纹理对象
  20. autorenderTexture=RenderTexture::create(getWinSize().width,getWinSize().height,Texture2D::PixelFormat::RGBA8888);
  21. //清空并开始获取
  22. renderTexture->beginWithClear(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. autofullPath=FileUtils::getInstance()->getWritablePath()+fileName;
  31. autoscheduleCallback=[&,fullPath,callback](floatdt){
  32. callback(fullPath);
  33. };
  34. auto_schedule=getRunningScene()->getScheduler();
  35. _schedule->schedule(scheduleCallback,this,false,"screenshot");
  36. }
(3)从Cocos2d-x 3.2之后开始,引擎提供了captureScreen函数来实现截图功能:

[cpp] view plain copy
  1. voidUtil::captureScreen(conststd::function<void(bool,conststd::string&)>&afterCaptured,conststd::string&filename);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读