码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx截整屏、截部分屏

时间:2017-02-10 20:44:49      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:cocos2dx   alt   name   ems   position   cpp   form   flip   shared   

我的环境是cocos2dx 2.x的版本

 

【CCRenderTexture】

    CCRenderTexture这个动态纹理类,顾名思义就是可以动态创建纹理图片。

    屏幕截图主要步骤:

        > 开始截图:render->begin();

        > 遍历场景:scene->visit();

        > 结束截图:render->end();

        > 保存截图:render->saveToFile(string& filename, Image::Format format);

    其中Image::Format为图片格式,可以保存为PNG、或JPG两种格式。

    即:Image::Format::PNGImage::Format::JPG

 

首先是截整个屏幕(CCDirector.cpp中):

saveToDCIM函数,找个目录,然后找到一个没用过的名字开始saveScreenShot

 1 void CCDirector::saveToDCIM()
 2 {
 3     char bufferName[256];
 4 
 5     for (int i = 0;i<999999;i++)
 6     {
 7         std:string filedir = "";
 8 #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID        
 9         filedir = std::string(SDKInterface::getExternalStoragePath())  +"/Screenshots";
10 #else
11         filedir = CCFileUtils::sharedFileUtils()->getWritablePath() +"/Screenshots";
12 #endif
13         memset(bufferName,0,sizeof(bufferName));
14         sprintf(bufferName,"/SCREENSHOT_%d.png",i);
15         std::string filename = filedir + std::string(bufferName);
16         if(!CCFileUtils::sharedFileUtils()->isFileExist(filename))
17         {
18             CCLog("ScreenShoot");
19             createDirectory(filedir.c_str());
20             saveScreenshot(filename);
21             return;
22         }
23     }
24 }

这是实际干活的函数:

 1 void CCDirector::saveScreenshot( const std::string& fileName)
 2 {
 3     CCScene* temp = CCDirector::sharedDirector()->getRunningScene();
 4     if (temp)
 5     {
 6         CCSize winSize = CCDirector::sharedDirector()->getWinSize();
 7         CCRenderTexture* rt = CCRenderTexture::create(winSize.width, winSize.height);    
 8         rt->begin();
 9         temp->visit();    
10         rt->end();
11         rt->saveToFile(fileName.c_str());
12         CCLog(fileName.c_str());
13     }    
14 }

 

然后是截取屏幕的某一个部分,cocos2dx 3.x的版本里可以直接用这个函数:

技术分享

2.x版本的怎么办呢?用得到的全屏截图rt的一部分RECT创建Sprite,然后在visit该Sprite,获得第二张截图rt2(比较懒,肯定不是最好的方法,欢迎指点)

 1 void CCDirector::renderTexturePartImpl(int x, int y, int width, int height, const std::string& fileName)
 2 {
 3     CCScene* temp = CCDirector::sharedDirector()->getRunningScene();
 4     if (temp)
 5     {
 6         CCSize winSize = CCDirector::sharedDirector()->getWinSize();
 7         CCRenderTexture* rt = CCRenderTexture::create(winSize.width, winSize.height);    
 8         rt->beginWithClear(0,0,0,0,0);
 9         temp->visit();    
10         rt->endToLua();
11 
12         CCRenderTexture* rt2 = CCRenderTexture::create(width, height);
13         CCSprite* spr = CCSprite::createWithTexture(rt->getSprite()->getTexture(), CCRect(x, y, width, height));
14         spr->setAnchorPoint(CCPoint(0,0));
15         spr->setPosition(CCPoint(0,0));
16         spr->setFlipY(true);
17         rt2->beginWithClear(0,0,0,0,0);
18         spr->visit();
19         rt2->endToLua();
20         rt2->saveToFile(fileName.c_str());
21         CCLog(fileName.c_str());
22     }    
23 }

 

cocos2dx截整屏、截部分屏

标签:cocos2dx   alt   name   ems   position   cpp   form   flip   shared   

原文地址:http://www.cnblogs.com/pandawuwyj/p/6387489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!