<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>风牛菜籽 &#187; Android</title>
	<atom:link href="http://www.caiyanpei.com/category/network-technology/android/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.caiyanpei.com</link>
	<description>拖延症重症患者</description>
	<lastBuildDate>Tue, 25 Mar 2025 01:51:19 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.6.1</generator>
		<item>
		<title>cocos2d-x 截图保存到本地</title>
		<link>http://www.caiyanpei.com/cocos2d-x-saveshotcut/</link>
		<comments>http://www.caiyanpei.com/cocos2d-x-saveshotcut/#comments</comments>
		<pubDate>Sun, 23 Mar 2014 12:22:45 +0000</pubDate>
		<dc:creator>tsai</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cocos2d-x]]></category>
		<category><![CDATA[IOS]]></category>
		<category><![CDATA[技术]]></category>

		<guid isPermaLink="false">http://www.xiuchezu.com/?p=532</guid>
		<description><![CDATA[游戏推广的一个很重要组成就是玩家分享，所以游戏截图就起到很大作用了。截图功能通过 &#8230; <a href="http://www.caiyanpei.com/cocos2d-x-saveshotcut/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>游戏推广的一个很重要组成就是玩家分享，所以游戏截图就起到很大作用了。截图功能通过CCRenderTexture实现。</p>
<h3>1.CCRenderTexture</h3>
<p>CCRenderTexture是一个通用渲染对象，可以通过构建一个CCRenderTexture对象，把要渲染的东西填充进去，在渲染开始前调用call函数，调用cocos的场景的visit函数对其进行渲染，渲染结束后调用end函数。</p>
<p>CCRenderTexture继承于CCNode，所以可以简单地把渲染纹理添加到你的场景中，就像处理其它cocos中的节点一样，当然它还提供了保存功能，可以把渲染纹理保存为PNG或JPG格式。</p>
<p>在实际应用中中，只是获取渲染的纹理还不够，还要考虑不同尺寸设备的截图范围，当然，这个是针对采用kResolutionNoBorder屏幕适配方案来说，下面的粒子就是针对kResolutionNoBorder。</p>
<h3>2.API</h3>
<pre class="brush: cpp; title: ; notranslate">
//创建和初始化函数
   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);
</pre>
<h3>3.示例</h3>
<p>修改HelloWorld中结束菜单的回调函数如下：</p>
<pre class="brush: cpp; title: ; notranslate">

void CTestLayer::menuCloseCallback(CCObject* pSender)
{
    SaveScreenShot();
}

//截图功能
void CTestLayer::SaveScreenShot()
{
 //获取屏幕尺寸
//CCDirector::sharedDirector()-&gt;getVisibleSize();
CCSize size =  CCDirector::sharedDirector()-&gt;getWinSize();
CCPoint origin = VisibleRect::getVisibleRect().origin;
CCSize visableSize = VisibleRect::getVisibleRect().size;

//使用屏幕尺寸初始化一个空的渲染纹理对象
CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height);

//设置位置
texture-&gt;setPosition(ccp(size.width/2, size.height/2));
CCScene* curScene = CCDirector::sharedDirector()-&gt;getRunningScene();

//开始获取
texture-&gt;begin();

//遍历场景节点对象，填充纹理到texure中
curScene-&gt;visit();

//结束获取
texture-&gt;end();

//保存为PNG图
if (origin.x == 0 &amp;&amp; origin.y == 0) {
texture-&gt;saveToFile(&quot;screenshot.png&quot;, kCCImageFormatPNG);
return;
}

// 适配不同尺寸设备，只截取屏幕大小的尺寸,没采用kResolutionNoBorder就不用下面的处理

CCRect finalRect = CCRectMake(origin.x, origin.y, visableSize.width, visableSize.height);
CCSprite *sprite = CCSprite::createWithTexture(texture-&gt;getSprite()-&gt;getTexture(), finalRect);
sprite-&gt;setAnchorPoint(CCPoint(0, 0));
sprite-&gt;setFlipY(true);
CCRenderTexture *finalRtx = CCRenderTexture::create(visableSize.width, visableSize.height);
finalRtx-&gt;begin();
sprite-&gt;visit();
finalRtx-&gt;end();
finalRtx-&gt;saveToFile(&quot;screenshot.png&quot;, kCCImageFormatPNG);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.caiyanpei.com/cocos2d-x-saveshotcut/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
