#include <GL/glew.h>
#include "vertexArray.h"

namespace render{

vertexArray::vertexArray()
{
    obj = 0;
}

vertexArray::~vertexArray()
{
    free();
}

void vertexArray::gen()
{
    _alloc();
}

void vertexArray::_alloc()
{
    if(obj)
        return;
    glGenVertexArrays(1, &obj);
    _addToList();
}

void vertexArray::free()
{
    if(!obj)
        return;
    unbind();
    glDeleteVertexArrays(1, &obj);
    obj = 0;
    _removeFromList();
}

void vertexArray::bind()
{
    if(obj)
        glBindVertexArray(obj);
}

void vertexArray::unbind()
{
    glBindVertexArray(0);
}

void vertexArray::draw(unsigned mode, int first, unsigned count)
{
    bind();
    glDrawArrays(mode, first, count);
    unbind();
}

}
