//
// Created by sasha on 27.11.2025.
//

#include "image.h"

#include "engine/engine.h"
#include "engine/asset_cached/texture_cache.h"

Image::Image(IWidget *parent, float x, float y, float width, float height,
             const std::string &filename)  : IWidget(parent,x,y,width,height) {

    if (!filename.empty()) {
        if (!setTexture(filename)){
            SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "[Image::Image] Error load texture %s",filename.c_str());
        }
    }
}


Image::~Image() {

}

// Установить текстуру
bool Image::setTexture(const std::string &filename){
    if (filename.empty()) return true;

    texture = TextureCache::getTexture(filename);

    return true;
}

// рисование
void Image::draw() {
    if (texture) {
        SDL_FRect dest{getScaleFRect()};
        SDL_RenderTexture(Engine::getRenderer(), texture, NULL, &dest);
    }
}
