Texture :: struct {
    path : string;
    name : string;
    index : u32;

    // NOTE: (vfs) stb_image wants s32 values for the width and height.
    width      : s32;
    height     : s32;
    channels   : s32;

    srgb        : bool;
    uploaded    : bool;

    id              : GLuint;
    internal_format : GLint;
    format          : GLenum;
}

make_texture :: (path: string, srgb := true) -> *Texture {
    texture := New(Texture);

    init_texture(texture, path, srgb);

    return texture;
}

init_texture :: (texture: *Texture, path: string, srgb := true, bilinear := true) {
    fullpath, basename, extension, basename_with_extension := path_decomp(path);

    file := read_entire_file(path);

    width : s32;
    height : s32;
    channels : s32;
    data := stbi_load_from_memory(file.data, xx file.count, *width, *height, *channels, 4);
    channels = 4;

    if !data {
        log("[Error] Could not load the texture %, probably invalid file name.", basename);

        return;
    }

    texture.path = path;
    texture.name = basename;
    texture.internal_format = cast(GLint) ifx channels == 3 then GL_SRGB8 else GL_SRGB8_ALPHA8;
    texture.format = cast(GLenum) ifx channels == 3 then GL_RGB else GL_RGBA;

    glGenTextures(1, *texture.id);
    glBindTexture(GL_TEXTURE_2D, texture.id);
    glTexImage2D(GL_TEXTURE_2D, 0, texture.internal_format, xx width, xx height, 0, texture.format, GL_UNSIGNED_BYTE, data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

    if bilinear {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    } else {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    }

    border_color := float.[0.0, 0.0, 0.0, 0.0];
    glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color.data);

    texture.width = width;
    texture.height = height;
    texture.channels = channels;
    texture.srgb = srgb;
}
