cocos2dx截图

void takeScreenShot(){
// 截取整屏
Size winSize = Director::sharedDirector()->getWinSize();
texture = RenderTexture::create(winSize.width, winSize.height);
texture->clear(0,0,0,0);
// 开始渲染
texture->begin();
// 访问当前运行屏幕 上的每个子结点
Director::sharedDirector()->getRunningScene()->visit();
// 结束绘制
texture->end();
// 保存抓取到的图片
CalendarTime curTime = MissionManager::getInstance()->getCurrentTime();
CCString* str = CCString::createWithFormat("%d%d%d%d%d%d",curTime.year,curTime.month,curTime.day,curTime.hour,curTime.min,curTime.second);
string timestr = str->getCString();
string filename = "screenshot"+timestr+".png";
CCLog("screenshot filename = %s",filename.c_str());
if (texture->saveToFile(filename,Image::Format::PNG))
{
screenShotName = filename;
CCLOG("screenshot success");
showScreenshot();  // 显示出截取的图片
}
else
{
CCLOG("screenshot failed");
}

}

void showScreenshot()
{
CCSprite *sprite = CCSprite::createWithTexture(texture->getSprite()->getTexture());
if(sprite)
{
sprite->setFlipY(90);
sprite->runAction(ScaleTo::create(0.2,0.9f));
Size winSize = Director::sharedDirector()->getWinSize();
sprite->setPosition(ccp(winSize.width * 0.5f, winSize.height * 0.5f));
Director::sharedDirector()->getRunningScene()->addChild(sprite, 800);
}
}

你可能感兴趣的:(Cocos2d-x)