#include "rendering/FileTexture.h"
#include "images/PNGReader.h"
#include "memory/StackAllocator.h"

FileTexture::FileTexture(const char* path, int maxMipMaps) : texture(maxMipMaps) {
    PNGReader png(path);
    if(png.hasError()) {
        return;
    }
    StackAllocator::Array<ColorChannel> buffer(png.getBufferSize());
    if(buffer.getLength() == png.getBufferSize() && !png.readData(buffer)) {
        texture.setFormat(TextureFormat::color8(png.getChannels()));
        texture.setData(png.getWidth(), png.getHeight(), buffer);
    }
}

void FileTexture::bindTo(int index) const {
    texture.bindTo(index);
}