#include "pch.h"
#include "Tesselator.h"

#include "BufferBuilder.h"
#include "BufferUploader.h" 

namespace Vivid2D::Render::Buffer {
    Tesselator::Tesselator(int bufferSize) {
        // 初始化 BufferBuilder，分配底层数组
        this->builder = std::make_unique<BufferBuilder>(bufferSize);
    }

    Tesselator::Tesselator() : Tesselator(DEFAULT_BUFFER_SIZE) {}

    Tesselator& Tesselator::getInstance() {
        RenderSystem::assertOnRenderThreadOrInit();
        static Tesselator INSTANCE;
        return INSTANCE;
    }

    void Tesselator::end() {
        RenderSystem::assertOnRenderThread();
        std::unique_ptr<BuiltBuffer> builtBuffer = this->builder->end();
        if (builtBuffer) {
            BufferUploader::drawWithShader(std::move(builtBuffer));
        }
    }

    BufferBuilder& Tesselator::getBuilder() {
        return *this->builder;
    }

} // namespace Buffer