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

cocos2d-x截屏功能clippingnode也能使用——白白

发布时间:2020-12-14 19:14:51 所属栏目:百科 来源:网络整理
导读:更多精彩分享:http://blog.csdn.net/u010229677 3.1版本: 在Director里面增加一个函数: bool Director::saveScreenshot(const std::string fileName,const std::functionvoid(const std::string) callback) {Image::Format format; //进行后缀判断 if(std

更多精彩分享:http://blog.csdn.net/u010229677

3.1版本:

在Director里面增加一个函数:

bool Director::saveScreenshot(const std::string& fileName,const std::function<void(const std::string&)>& callback) 
{
	Image::Format format;  
	//进行后缀判断  
	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 false;  
		}  
	} else {  
		CCLOG("cocos2d: the image can only be saved as JPG or PNG format");  
		return false ;  
	}  
	//获取屏幕尺寸,初始化一个空的渲染纹理对象  
	auto renderTexture = RenderTexture::create(getWinSize().width,getWinSize().height,Texture2D::PixelFormat::RGBA8888,GL_DEPTH24_STENCIL8); //最后一个参数省略的话,对clippingnode截图是无效的
	//清空并开始获取  
	renderTexture->beginWithClear(0.0f,0.0f,0.0f);  
	//遍历场景节点对象,填充纹理到RenderTexture中  
	getRunningScene()->visit();  
	//结束获取  
	renderTexture->end();  
	//保存文件  
	renderTexture->saveToFile(fileName,format);  
	//使用schedule在下一帧中调用callback函数  
	auto fullPath = FileUtils::getInstance()->getWritablePath() + fileName;  
	auto scheduleCallback = [&,fullPath,callback](float dt){  
		callback(fullPath); 
	};  
	return true;
}

调用的话:
Director::getInstance()->saveScreenshot("sss.png", nullptr);

3.2版本:
#include "base/ccUtils.h"
utils::captureScreen([](){log("ok");},"dd.png");

(编辑:李大同)

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

    推荐文章
      热点阅读