//
// Created by Vicente Ferrari Smith on 14.02.26.
//

#include "texture.h"
#include <stb_image.h>
#include "renderer.h"

TextureManager::TextureManager() {

}

Texture TextureManager::load(const std::string& path, Renderer &renderer) {
    // Dedup: Don't load the same file twice!
    // if (path_to_id.contains(path)) return path_to_id[path];

    int w, h, ch;
    unsigned char* data = stbi_load(path.c_str(), &w, &h, &ch, STBI_rgb_alpha);

    // Tell the renderer to make the GPU version
    Texture res;
    res.width = w;
    res.height = h;
    res.channels = STBI_rgb_alpha;
    res.srgb = true;
    renderer.upload_texture(w, h, data, &res.image, &res.allocation, &res.view, &res.descriptor_index);

    stbi_image_free(data);

    res.id = path;
    res.path = path;
    res.uploaded = true;

    textures[path] = res;
    // path_to_id[path] = id;

    return res; // This is the textureID for your sprites
}
