Cocos2d-x
发布时间:2022-05-03 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了【玩转cocos2d-x之三十九】Cocos2d-x 3.0截屏功能集成,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
3.0的截屏和2.x的截屏基本上相同,都是
利用RenderTexture来处理,在渲染之前调用call函数,然后调用Cocos的场景visit函数对其进行渲染,渲染结束后调用end函数即可。只是3.0截屏需要在截完屏的下一帧才能处理RenderTexture,这点要注意。
关于2.x的RenderTexture的API和demo可以参见
http://www.voidcn.com/article/p-bomolgql-ep.html
。
本文的重点在于如何将截图功能继承到Cocos2d-x 3.0引擎。
1.集成到Director
这里选择把截屏功能继承到Director中,让全局的导演来执行截屏功能是一个很好的主意。
- voidDirector::saveScreenshot(conststd::string&fil@R_616_8371@,conststd::function<void(conststd::string&)>&callBACk)
- {
- Image::Formatformat;
-
- if(std::string::npos!=fil@R_616_8371@.find_last_of(".")){
- autoextension=fil@R_616_8371@.substr(fil@R_616_8371@.find_last_of("."),fil@R_616_8371@.length());
- if(!extension.compare(".png")){
- format=Image::Format::PNG;
- }elseif(!extension.compare(".jpg")){
- format=Image::Format::JPG;
- else{
- CCLOG("cocos2d:theimagecanonlybesavedasJPGorPNGformat");
- return;
- }
-
- autorenderTexture=RenderTexture::create(getWinSize().width,getWinSize().height,Texture2D::PixelFormat::rGBA8888);
- //清空并开始获取
- renderTexture->beginWithClear(0.0f,0.0f,0.0f);
- //遍历场景节点对象,填充纹理到RenderTexture中
- getRunningScene()->visit();
- //结束获取
- renderTexture->end();
- //保存文件
- renderTexture->saveToFile(fil@R_616_8371@,format);
- //使用schedule在下一帧中调用callBACk函数
- autofullPath=FileUtils::geTinstance()->getWritablePath()+fil@R_616_8371@;
- autoscheduleCallBACk=[&,fullPath,callBACk](floatdt){
- callBACk(fullPath);
- };
- auto_schedule=getRunningScene()->getscheduler();
- _schedule->schedule(scheduleCallBACk,this,153); font-weight:bold; BACkground-color:inherit">false,"screenshot");
- }