#include "OpenGlFontTexture.h"

#include "FontGlyph.h"
#include "Grid.h"
#include "Image.h"

#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>

OpenGlFontTexture::OpenGlFontTexture(FontGlyph* glyph)
    : mGlyph(glyph)
{
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glGenTextures(1, &mHandle);
    glBindTexture(GL_TEXTURE_2D, mHandle);

    auto buffer = glyph->getImage()->getGridT<unsigned char>()->getInternalData()->getDataPtr();

    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RED,
        mGlyph->getWidth(),
        mGlyph->getHeight(),
        0,
        GL_RED,
        GL_UNSIGNED_BYTE,
        buffer
    );

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

}

FontGlyph* OpenGlFontTexture::getGlyph() const
{
    return mGlyph;
}
