【玩转cocos2d-x之二十四】截图保存功能的实现
2013-11-13 08:41
363 查看
[b]原创作品,转载请标明:[/b]http://blog.csdn.net/jackystudio/article/details/15498083
官方TestCpp有这个demo了,这里还是把它单独拖出来写一下,游戏推广的一个很重要组成就是玩家分享,所以游戏截图就起到很大作用了。截图功能通过CCRenderTexture实现。
官方TestCpp有这个demo了,这里还是把它单独拖出来写一下,游戏推广的一个很重要组成就是玩家分享,所以游戏截图就起到很大作用了。截图功能通过CCRenderTexture实现。
1.CCRenderTexture
CCRenderTexture是一个通用渲染对象,可以通过构建一个CCRenderTexture对象,进而把要渲染的东西填充进去,在渲染开始前调用call函数,调用cocos的场景的visit函数对其进行渲染,渲染结束后调用end函数。CCRenderTexture继承于CCNode,所以可以简单地把渲染纹理添加到你的场景中,就像处理其它cocos中的节点一样,当然它还提供了保存功能,可以把渲染纹理保存为PNG或JPG格式。2.API
//创建和初始化函数 static CCRenderTexture * create(int w ,int h, CCTexture2DPixelFormat eFormat, GLuint uDepthStencilFormat); static CCRenderTexture * create(int w, int h, CCTexture2DPixelFormat eFormat); static CCRenderTexture * create(int w, int h); bool initWithWidthAndHeight(int w, int h, CCTexture2DPixelFormat eFormat); bool initWithWidthAndHeight(int w, int h, CCTexture2DPixelFormat eFormat, GLuint uDepthStencilFormat); //开始获取 void begin(); //开始渲染时清除之前渲染的内容 void beginWithClear(float r, float g, float b, float a); void beginWithClear(float r, float g, float b, float a, float depthValue); void beginWithClear(float r, float g, float b, float a, float depthValue, int stencilValue); //结束获取 void end(); //清除纹理 void clear(float r, float g, float b, float a); void clearDepth(float depthValue); void clearStencil(int stencilValue); //保存纹理为图片文件,可以选择JPG/PNG格式,默认是JPEG格式,成功返回真 bool saveToFile(const char *szFilePath); bool saveToFile(const char *name, tCCImageFormat format);
3.示例
修改HelloWorld中结束菜单的回调函数如下:void CTestLayer::menuCloseCallback(CCObject* pSender) { SaveScreenShot(); } //截图功能 void CTestLayer::SaveScreenShot() { //获取屏幕尺寸 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); }
4.源码下载
下载地址:http://download.csdn.net/detail/jackyvincefu/6538305相关文章推荐
- 【玩转cocos2d-x之二十四】截图保存功能的实现
- C# 实现完整功能的截图控件(1)-实现绘图工具栏控件 控件实现了截图、绘制矩形、圆形、箭头、线条、文字,还需要可以撤销绘制步骤、保存图形等功能,基本实现了跟QQ2008截图一样的功能,还可以更
- 截图保存功能的实现
- Web开发之用canvas2image.js将canvas保存为图片(实现页面截图下载功能)
- 前台JS 实现截图保存功能
- MFC实现截图保存功能
- Silverlight/WPF 截图保存功能的实现
- UE4中实现截图功能并保存到指定路径
- cocos2d-x实现简易屏幕截图功能
- java实现屏幕截图功能并保存截图
- COCOS2D中实现截图功能
- js实现截图保存图片功能的代码示例
- 调用系统相机 图库 截图功能。实现大图片裁剪保存
- 【Cocos2d-x】音量滑动设定以及自动保存功能实现(完整模块)
- Silverlight/WPF 截图保存功能的实现---我得用这个方式试下,把项目里的功能修改下。
- C#实现网页截图功能
- js实现web套打功能,支持拖动布局,支持按键移动支持打印预览,结果可保存至数据库中
- cocos2d-x嵌入android的camera 实现前后摄像头的预览和保存
- Cocos2d-x实现Android的Toast功能
- 一行代码完成 调用系统相册 和 视频,语音库,选择图片,视频,和音频文件。在选择界面实现了录制视频和录制音频功能。 截图