首页 > 代码库 > Cocos2d-x 截图功能
Cocos2d-x 截图功能
2.x-3.x版本 //获取屏幕尺寸
CCSize size = CCDirector::sharedDirector()->getWinSize();
//使用屏幕尺寸初始化一个空的渲染纹理对象
CCRenderTexture* texture = CCRenderTexture::create((
int
)size.width, (
int
)size.height);
//设置位置
texture->setPosition(ccp(size.width/2, size.height/2));
//开始获取
texture->begin();
//遍历场景节点对象,填充纹理到texure中
CCDirector::sharedDirector()->getRunningScene()->visit();
//结束获取
texture->end();
//保存为PNG图,Win32/Debug目录下
texture->saveToFile(
"screenshot.png"
, kCCImageFormatPNG);
3.x-3.2x版本
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
{
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
;
}
//获取屏幕尺寸,初始化一个空的渲染纹理对象
auto renderTexture = RenderTexture::create(getWinSize().width, getWinSize().height, Texture2D::PixelFormat::RGBA8888);
//清空并开始获取
renderTexture->beginWithClear(0.0f, 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);
};
auto _schedule = getRunningScene()->getScheduler();
_schedule->schedule(scheduleCallback,
this
, 0.0f,0,0.0f,
false
,
"screenshot"
);
3.2x以后 void
Util::captureScreen(
const
std::function<
void
(
bool
,
const
std::string&)>& afterC
aptured, const
std::string& filename);
Cocos2d-x 截图功能
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。