打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【玩转cocos2d-x之二十四】截图保存功能的实现

原创作品,转载请标明http://blog.csdn.net/jackystudio/article/details/15498083


官方TestCpp有这个demo了,这里还是把它单独拖出来写一下,游戏推广的一个很重要组成就是玩家分享,所以游戏截图就起到很大作用了。截图功能通过CCRenderTexture实现。


1.CCRenderTexture

CCRenderTexture是一个通用渲染对象,可以通过构建一个CCRenderTexture对象,进而把要渲染的东西填充进去,在渲染开始前调用call函数,调用cocos的场景的visit函数对其进行渲染,渲染结束后调用end函数。CCRenderTexture继承于CCNode,所以可以简单地把渲染纹理添加到你的场景中,就像处理其它cocos中的节点一样,当然它还提供了保存功能,可以把渲染纹理保存为PNG或JPG格式。


2.API

  1. //创建和初始化函数  
  2.    static CCRenderTexture * create(int w ,int h, CCTexture2DPixelFormat eFormat, GLuint uDepthStencilFormat);  
  3.    static CCRenderTexture * create(int w, int h, CCTexture2DPixelFormat eFormat);  
  4.    static CCRenderTexture * create(int w, int h);  
  5.    bool initWithWidthAndHeight(int w, int h, CCTexture2DPixelFormat eFormat);  
  6.    bool initWithWidthAndHeight(int w, int h, CCTexture2DPixelFormat eFormat, GLuint uDepthStencilFormat);  
  7.   
  8.    //开始获取  
  9.    void begin();  
  10.   
  11.    //开始渲染时清除之前渲染的内容  
  12.    void beginWithClear(float r, float g, float b, float a);  
  13.    void beginWithClear(float r, float g, float b, float a, float depthValue);  
  14.    void beginWithClear(float r, float g, float b, float a, float depthValue, int stencilValue);  
  15.   
  16.    //结束获取  
  17.    void end();  
  18.   
  19.    //清除纹理  
  20.    void clear(float r, float g, float b, float a);  
  21.    void clearDepth(float depthValue);  
  22.    void clearStencil(int stencilValue);  
  23.   
  24.    //保存纹理为图片文件,可以选择JPG/PNG格式,默认是JPEG格式,成功返回真  
  25.    bool saveToFile(const char *szFilePath);  
  26.    bool saveToFile(const char *name, tCCImageFormat format);  

3.示例

修改HelloWorld中结束菜单的回调函数如下:

  1. void CTestLayer::menuCloseCallback(CCObject* pSender)  
  2. {  
  3.     SaveScreenShot();  
  4. }  
  5.   
  6. //截图功能  
  7. void CTestLayer::SaveScreenShot()  
  8. {  
  9.     //获取屏幕尺寸  
  10.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  11.     //使用屏幕尺寸初始化一个空的渲染纹理对象  
  12.     CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height);  
  13.     //设置位置      
  14.     texture->setPosition(ccp(size.width/2, size.height/2));  
  15.     //开始获取      
  16.     texture->begin();  
  17.     //遍历场景节点对象,填充纹理到texure中  
  18.     CCDirector::sharedDirector()->getRunningScene()->visit();  
  19.     //结束获取  
  20.     texture->end();  
  21.     //保存为PNG图,Win32/Debug目录下  
  22.     texture->saveToFile("screenshot.png", kCCImageFormatPNG);  
  23. }  

4.源码下载

下载地址:http://download.csdn.net/detail/jackyvincefu/6538305

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
玩转cocos2d-x ---多线程和同步-图片异步加载
Cocos2d
cocos2dx截取屏幕 本地存取 图片微博分享
CC_UNUSED_PARAM
解读Texture2D.ReadPixels
(四)Unity纹理、贴图和材质
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服