在游戏中,我们经常需要分享到社交网络的功能。分享时,我们时常会需要用到截屏的功能。目前网上的文章虽然很多,但是都是截取的
设计分辨率(DesignResolutionSize)大小的屏幕,而这个并不是最佳选择,因为它不符合当前设备的宽高比例。
下面的这个函数,可以截取屏幕可见范围的内容,生成一个CCImage对象。
CCImage* screenshotWithStartNode(CCNode *startNode)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint visibleOrigin = CCDirector::sharedDirector()->getVisibleOrigin();
CCDirector::sharedDirector()->setNextDeltaTimeZero(true);
CCRenderTexture* rtx = CCRenderTexture::create(winSize.width, winSize.height);
rtx->begin();
startNode->visit();
rtx->end();
if (0 == visibleOrigin.x)
return rtx->newCCImage();
CCRect finalRect = CCRectMake(visibleOrigin.x, visibleOrigin.y, visibleSize.width, visibleSize.height);
CCSprite *sprite = CCSprite::createWithTexture(rtx->getSprite()->getTexture(), finalRect);
sprite->setAnchorPoint(CCPoint(0, 0));
sprite->setFlipY(true);
CCRenderTexture *finalRtx = CCRenderTexture::create(visibleSize.width, visibleSize.height);
finalRtx->begin();
sprite->visit();
finalRtx->end();
return finalRtx->newCCImage();
}