cocos2d-x-3.x snapshot

前端之家收集整理的这篇文章主要介绍了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);
    // rtx->setKeepMatrix(true);
    cocos2d::Scene* scene = Director::getInstance()->getRunningScene();
    cocos2d::Point savedPos = scene->getPosition();
    scene->setPosition(-visibleOrigin);
    rtx->begin(); // rtx->beginWithClear(0,0); 
    scene->visit();
    rtx->end();
    scene->setPosition(savedPos);

    if (std::abs(scale - 1.0f) < 1e-6/* no scale */)
        finalRtx = rtx;
    else {

        /* scale */
        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); // or use finalRtx->setKeepMatrix(true);
        finalRtx->begin(); // finalRtx->beginWithClear(0,0); 
        sprite->visit();
        finalRtx->end();
    }

    Director::getInstance()->getRenderer()->render();

    return finalRtx->newImage();
}


cocos2d::Image* snapshot(Node* startNode,float scale)
{ // The best snapshot API,support Scene and any Node
    auto& size = startNode->getContentSize();
    // cocos2d::Point visibleOrigin = Director::getInstance()->getVisibleOrigin();

    Director::getInstance()->setNextDeltaTimeZero(true);

    RenderTexture* finalRtx = nullptr;

    RenderTexture* rtx = RenderTexture::create(size.width,size.height,GL_DEPTH24_STENCIL8);
    // rtx->setKeepMatrix(true);
    // cocos2d::Scene* scene = Director::getInstance()->getRunningScene();
    cocos2d::Point savedPos = startNode->getPosition();
    Point anchor;
    if (!startNode->isIgnoreAnchorPointForPosition()) {
        anchor = startNode->getAnchorPoint();
    }
    startNode->setPosition(ccp(size.width * anchor.x,size.height * anchor.y));// startNode->setPosition(-visibleOrigin);
    rtx->begin(); // rtx->beginWithClear(0,0); 
    startNode->visit();
    rtx->end();
    startNode->setPosition(savedPos);

    if (std::abs(scale - 1.0f) < 1e-6/* no scale */)
        finalRtx = rtx;
    else {

        /* scale */
        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

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