前端之家收集整理的这篇文章主要介绍了
cocos2d-x-3.x snapshot,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
cocos2d-x-3.x snapshot
cocos2d::Image* snapshot(float scale)
{
cocos2d::Size visibleSize = Director::getInstance()->getVisibleSize();
cocos2d::Point visibleOrigin = Director::getInstance()->getVisibleOrigin();
Director::getInstance()->setNextDeltaTimeZero(true);
RenderTexture* finalRtx = nullptr;
RenderTexture* rtx = RenderTexture::create(visibleSize.width,visibleSize.height,Texture2D::PixelFormat::RGBA8888,GL_DEPTH24_STENCIL8);
cocos2d::Scene* scene = Director::getInstance()->getRunningScene();
cocos2d::Point savedPos = scene->getPosition();
scene->setPosition(-visibleOrigin);
rtx->begin();
scene->visit();
rtx->end();
scene->setPosition(savedPos);
if (std::abs(scale - 1.0f) < 1e-6)
finalRtx = rtx;
else {
cocos2d::Rect finalRect = cocos2d::Rect(0,0,visibleSize.width,visibleSize.height);
Sprite *sprite = Sprite::createWithTexture(rtx->getSprite()->getTexture(),finalRect);
sprite->setAnchorPoint(cocos2d::Point(0,0));
sprite->setFlipY(true);
finalRtx = RenderTexture::create(visibleSize.width * scale,visibleSize.height * scale,GL_DEPTH24_STENCIL8);
sprite->setScale(scale);
finalRtx->begin();
sprite->visit();
finalRtx->end();
}
Director::getInstance()->getRenderer()->render();
return finalRtx->newImage();
}
cocos2d::Image* snapshot(Node* startNode,float scale)
{
auto& size = startNode->getContentSize();
Director::getInstance()->setNextDeltaTimeZero(true);
RenderTexture* finalRtx = nullptr;
RenderTexture* rtx = RenderTexture::create(size.width,size.height,GL_DEPTH24_STENCIL8);
cocos2d::Point savedPos = startNode->getPosition();
Point anchor;
if (!startNode->isIgnoreAnchorPointForPosition()) {
anchor = startNode->getAnchorPoint();
}
startNode->setPosition(ccp(size.width * anchor.x,size.height * anchor.y));
rtx->begin();
startNode->visit();
rtx->end();
startNode->setPosition(savedPos);
if (std::abs(scale - 1.0f) < 1e-6)
finalRtx = rtx;
else {
cocos2d::Rect finalRect = cocos2d::Rect(0,size.width,size.height);
Sprite *sprite = Sprite::createWithTexture(rtx->getSprite()->getTexture(),0));
sprite->setFlipY(true);
finalRtx = RenderTexture::create(size.width * scale,size.height * scale,0);
sprite->visit();
finalRtx->end();
}
Director::getInstance()->getRenderer()->render();
return finalRtx->newImage();
}
原文链接:https://www.f2er.com/cocos2dx/339765.html