美文网首页
SpriteBatchNode

SpriteBatchNode

作者: 许彦峰 | 来源:发表于2022-03-31 20:27 被阅读0次

上层业务代码:

    CCSpriteBatchNode* batch = CCSpriteBatchNode::create("./test.png");
    this->addChild(batch);
    {
        auto sp = Sprite::createWithTexture(batch->getTexture());
        batch->addChild(sp);
    }
    {
        auto spr = Sprite::create();
        spr->initWithFile("./vx.png");
        batch->appendChild(spr);
    }

CCSpriteBatchNode* batch = CCSpriteBatchNode::create("./test.png");

SpriteBatchNode* SpriteBatchNode::create(const std::string& fileImage, ssize_t capacity/* = DEFAULT_CAPACITY*/)
{
    SpriteBatchNode *batchNode = new (std::nothrow) SpriteBatchNode();
    batchNode->initWithFile(fileImage, capacity)
}
bool SpriteBatchNode::initWithFile(const std::string& fileImage, ssize_t capacity/* = DEFAULT_CAPACITY*/)
{
    Texture2D *texture2D = Director::getInstance()->getTextureCache()->addImage(fileImage);
    return initWithTexture(texture2D, capacity);
}
bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity/* = DEFAULT_CAPACITY*/)
{
    // 注意这里_textureAtlas
     _textureAtlas->initWithTexture(tex, capacity);
}

bool TextureAtlas::initWithTexture(Texture2D *texture, ssize_t capacity){
    this->_texture = texture;
}

batch->appendChild(spr);

void SpriteBatchNode::appendChild(Sprite* sprite)
{
 
    sprite->setBatchNode(this);
 

    if(_textureAtlas->getTotalQuads() == _textureAtlas->getCapacity()) {
        increaseAtlasCapacity();
    }

    _descendants.push_back(sprite);
    int index = static_cast<int>(_descendants.size()-1);

    sprite->setAtlasIndex(index);

    V3F_C4B_T2F_Quad quad = sprite->getQuad();
    _textureAtlas->insertQuad(&quad, index);

    // add children recursively
    auto& children = sprite->getChildren();
    for(const auto &child: children) {
        appendChild(static_cast<Sprite*>(child));
    }
}

SpriteBatchNode使用的是batchCommand:

void SpriteBatchNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{
     draw(renderer, _modelViewTransform, flags);
}
void SpriteBatchNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    // 注意第4个参数_textureAtlas
    _batchCommand.init(_globalZOrder, getGLProgram(), _blendFunc, _textureAtlas, transform, flags);
    renderer->addCommand(&_batchCommand);
}

相关文章

网友评论

      本文标题:SpriteBatchNode

      本文链接:https://www.haomeiwen.com/subject/hywnjrtx.html