#include "Renderer.h"

Renderer::Renderer(SDL_Renderer* r, AssetManager* a) : renderer(r), assets(a) {}

void Renderer::clear() {
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);
}

void Renderer::present() {
    SDL_RenderPresent(renderer);
}

void Renderer::drawTexture(SDL_Texture* tex, int x, int y, int w, int h) {
    if (!tex) return;
    SDL_Rect dst = {x, y, w, h};
    SDL_RenderCopy(renderer, tex, nullptr, &dst);
}

void Renderer::drawImage(const std::string& path, int x, int y, int w, int h) {
    drawTexture(assets->getTexture(path), x, y, w, h);
}

void Renderer::drawText(const std::string& text, int x, int y, SDL_Color color) {
    TTF_Font* font = assets->getFont();
    if (!font || text.empty()) return;
    SDL_Surface* surf = TTF_RenderUTF8_Blended(font, text.c_str(), color);
    if (!surf) return;
    SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf);
    if (tex) {
        SDL_Rect dst = {x, y, surf->w, surf->h};
        SDL_RenderCopy(renderer, tex, nullptr, &dst);
        SDL_DestroyTexture(tex);
    }
    SDL_FreeSurface(surf);
}

void Renderer::drawInt(int value, int x, int y, SDL_Color color) {
    drawText(std::to_string(value), x, y, color);
}
