#include "VertexBuffer.hpp"

TSE::uint TSE::OpenGL::VertexBuffer::boundBuffer = 0;

TSE::OpenGL::VertexBuffer::VertexBuffer()
{
    glGenBuffers(1, &bufferID);
}

void TSE::OpenGL::VertexBuffer::SetData(int size, void *buffer, GLenum usage)
{
    glBufferData(GL_ARRAY_BUFFER, size, buffer, usage);
}

void TSE::OpenGL::VertexBuffer::Bind()
{
    glBindBuffer(GL_ARRAY_BUFFER, bufferID);
    boundBuffer = bufferID;
}

void TSE::OpenGL::VertexBuffer::Unbind()
{
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    boundBuffer = 0;
}

bool TSE::OpenGL::VertexBuffer::IsBound()
{
    return boundBuffer == bufferID;
}

TSE::OpenGL::VertexBuffer::~VertexBuffer()
{
    if(bufferID != 0)
    {
        if(IsBound()) Unbind();
        glDeleteBuffers(1, &bufferID);
    }
}
