//
// Created by sasha on 03.12.2025.
//

#include "texture_cache.h"

#include <format>
#include <map>

#include <SDL3_image/SDL_image.h>

#include "engine/engine.h"
#include "engine/global_params.h"

std::map<std::string,SDL_Texture*> listCacheTextures;

SDL_Texture *TextureCache::getTexture(const std::string &filename) {
    if (filename.empty()) return nullptr;

    SDL_Texture *texture;

    std::string asset_path = GlobalParams::assetPath();

    // Если в кэше еще нет этого шрифта, создаем
    if (!listCacheTextures.contains(filename)) {
        SDL_Surface *surface = IMG_Load((asset_path+filename).c_str());
        if (!surface) {
            SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "[TextureCache::getTexture] error IMG_Load %s",(asset_path+filename).c_str());
            return nullptr;
        }

        texture = SDL_CreateTextureFromSurface(Engine::getRenderer(), surface);
        SDL_DestroySurface(surface);

        listCacheTextures[filename] = texture;
    }

    return listCacheTextures[filename];
}
