加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x-3.x snapshot

发布时间:2020-12-14 17:02:18 所属栏目:百科 来源:网络整理
导读:cocos2d-x-3.x snapshot cocos2d ::Image * snapshot(float scale){ cocos2d ::Size visibleSize = Director ::getInstance () - getVisibleSize(); cocos2d ::Point visibleOrigin = Director ::getInstance () - getVisibleOrigin(); Director ::getInstan

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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读