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, 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, Texture2D::PixelFormat::RGBA8888, GL_DEPTH24_STENCIL8);

        sprite->setScale(scale); // or use finalRtx->setKeepMatrix(true);
        finalRtx->begin(); // finalRtx->beginWithClear(0, 0, 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, Texture2D::PixelFormat::RGBA8888, 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, 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, 0, size.width, size.height);
        Sprite *sprite = Sprite::createWithTexture(rtx->getSprite()->getTexture(), finalRect);
        sprite->setAnchorPoint(cocos2d::Point(0, 0));
        sprite->setFlipY(true);

        finalRtx = RenderTexture::create(size.width * scale, size.height * scale, Texture2D::PixelFormat::RGBA8888, GL_DEPTH24_STENCIL8);

        sprite->setScale(scale); // or use finalRtx->setKeepMatrix(true);
        finalRtx->begin(); // finalRtx->beginWithClear(0, 0, 0, 0); 
        sprite->visit();
        finalRtx->end();
    }

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

    return finalRtx->newImage();
}

你可能感兴趣的:(游戏引擎技术,cocos2d-x)