cocos2dx截取指定区域的屏幕

前端之家收集整理的这篇文章主要介绍了cocos2dx截取指定区域的屏幕前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

实现这个功能时候,在网上查了一些方法,http://www.cocoachina.com/bbs/read.PHP?tid-277669-page-1.html这个论坛里面有个人用renderTexture->setVirtualViewport()来实现,没看懂(后面慢慢研究),看了一些回复后,自己想了个笨方法,如下:

auto winSize = Director::getInstance()->getWinSize();
auto origin = Director::getInstance()->getVisibleOrigin();
RenderTexture *renderTexture = RenderTexture::create(winSize.width,winSize.height);
//这里截取整个屏幕
renderTexture->begin();
Director::getInstance()->getRunningScene()->visit();
renderTexture->end();

//截取的指定区域

Rect shootArea(winSize.width - 200,winSize.height - 200,200,200);

auto sp = renderTexture->getSprite();
sp->setTextureRect(shootArea);
//显示截取要的部分
//sp->removeFromParent();
//this->addChild(sp,222);
sp->setPosition(sp->getContentSize()/2);

RenderTexture *renderTexture2 = RenderTexture::create(shootArea.size.width,shootArea.size.height);
renderTexture2->begin();
sp->visit();
renderTexture2->end();
if (renderTexture2->saveToFile("screen_shot.png",Image::Format::PNG))
{
//保存在这个路径FileUtils::sharedFileUtils()->getWritablePath()
CCLOG("screenshot success");
}
else
{
CCLOG("screenshot Failed");
}

谁有其他更好的方法说一下

猜你在找的Cocos2d-x相关文章