#include "GLTexture.h"
#include <iostream>

using namespace std;

namespace jes
{
    GLTexture::GLTexture()
    {
        glGenTextures(1, &m_id);
        m_width = 0u;
        m_height = 0u;
    }

    GLTexture::~GLTexture()
    {
        glDeleteTextures(1, &m_id);
    }

    uint32_t GLTexture::next_power_of_2(uint32_t n)
    {
        n--;
        n |= n >> 1u;
        n |= n >> 2u;
        n |= n >> 4u;
        n |= n >> 8u;
        n |= n >> 16u;
        n++;
        return n;
    }

    void GLTexture::create(unsigned int width, unsigned int height)
    {
        m_width = width = next_power_of_2(width);
        m_height = height = next_power_of_2(height);
        uint8_t * d = new uint8_t[width * height];
        memset(d, 0, width * height);
        bind();
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, d);
        delete[] d;
    }
}
