coco2dx 3.0截屏

1.集成到Director
这里选择把截屏功能继承到Director中,让全局的导演来执行截屏功能是一个很好的主意。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void 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 ;
         }
     } else {
         CCLOG( "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" );
}

2.如何使用saveScreenshot
截屏功能使用起来也很简单, 直接调用saveSecreenshot,其中第一个参数为文件名(支持png和jpg格式,不带后缀名默认为png格式),第二个参数为回调函数,你可以在回调函数中处理这个文件。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void ScreenshotTest::saveImage(Ref *pSender){
     static int counter = 0;
     
     char png[20];
     sprintf (png, "image-%d.png" , counter);
     char jpg[20];
     sprintf (jpg, "image-%d.jpg" , counter);
     
    //截屏后的回调函数,这里显示在左下角
     auto callback = [&]( const std::string& fullPath){
         auto sprite = Sprite::create(fullPath);
         CCASSERT(sprite!= nullptr , "Failed to create sprite." );
         addChild(sprite);
         sprite->setScale(0.3f);
         sprite->setPosition(Point(40, 40));
         sprite->setRotation(counter * 3);
         CCLOG( "Image saved %s" , fullPath.c_str());
     };
     
    //调用Director的截屏功能
     Director::getInstance()->saveScreenshot(png, callback);
     counter++;
}

3.源码下载
该功能已提交pull request到Cocos2d-x Github上了,有需求的童鞋们可以自己集成了。源码具体可以参见: https://github.com/cocos2d/cocos2d-x/pull/5978
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值