Files

436 lines
14 KiB
C++

#define STB_IMAGE_IMPLEMENTATION
#include "texture.h"
#include <SDL3/SDL.h> // Para SDL_LogError, SDL_LogCategory, Uint8, SDL_...
#include <cstdint> // Para uint32_t
#include <cstring> // Para memcpy
#include <fstream> // Para basic_ifstream, basic_istream, basic_ios
#include <sstream> // Para basic_istringstream
#include <stdexcept> // Para runtime_error
#include <string> // Para basic_string, char_traits, operator+, string
#include <utility>
#include <vector> // Para vector
#include "color.h" // Para getFileName, Color, printWithDots
#include "external/gif.h" // Para Gif
#include "resource_helper.h" // Para ResourceHelper
#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_alpha
#include "utils.h"
// Constructor
Texture::Texture(SDL_Renderer *renderer, std::string path)
: renderer_(renderer),
path_(std::move(path)) {
// Carga el fichero en la textura
if (!path_.empty()) {
// Obtiene la extensión
const std::string EXTENSION = path_.substr(path_.find_last_of('.') + 1);
// .png
if (EXTENSION == "png") {
loadFromFile(path_);
}
// .gif
else if (EXTENSION == "gif") {
// Crea la surface desde un fichero
surface_ = loadSurface(path_);
// Añade la propia paleta del fichero a la lista
addPaletteFromGifFile(path_, true); // Usar modo silencioso
// Crea la textura, establece el BlendMode y copia la surface a la textura
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND);
flipSurface();
}
}
}
// Destructor
Texture::~Texture() {
unloadTexture();
unloadSurface();
palettes_.clear();
}
// Carga una imagen desde un fichero
auto Texture::loadFromFile(const std::string &file_path) -> bool {
if (file_path.empty()) {
return false;
}
int req_format = STBI_rgb_alpha;
int width;
int height;
int orig_format;
unsigned char *data = nullptr;
// Intentar cargar desde ResourceHelper primero
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
data = stbi_load_from_memory(resource_data.data(), resource_data.size(), &width, &height, &orig_format, req_format);
}
// Fallback a filesystem directo
if (data == nullptr) {
data = stbi_load(file_path.c_str(), &width, &height, &orig_format, req_format);
}
if (data == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", getFileName(file_path).c_str());
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
}
printWithDots("Texture : ", getFileName(file_path), "[ LOADED ]");
int pitch;
SDL_PixelFormat pixel_format;
pitch = 4 * width;
pixel_format = SDL_PIXELFORMAT_RGBA32;
// Limpia
unloadTexture();
// La textura final
SDL_Texture *new_texture = nullptr;
// Carga la imagen desde una ruta específica
auto *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, static_cast<void *>(data), pitch);
if (loaded_surface == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to load image %s", file_path.c_str());
} else {
// Crea la textura desde los pixels de la surface
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
if (new_texture == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create texture from %s! SDL Error: %s", file_path.c_str(), SDL_GetError());
} else {
// Obtiene las dimensiones de la imagen
width_ = loaded_surface->w;
height_ = loaded_surface->h;
}
// Elimina la textura cargada
SDL_DestroySurface(loaded_surface);
}
// Return success
stbi_image_free(data);
texture_ = new_texture;
return texture_ != nullptr;
}
// Crea una textura en blanco
auto Texture::createBlank(int width, int height, SDL_PixelFormat format, SDL_TextureAccess access) -> bool {
// Crea una textura sin inicializar
texture_ = SDL_CreateTexture(renderer_, format, access, width, height);
if (texture_ == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create blank texture! SDL Error: %s", SDL_GetError());
} else {
width_ = width;
height_ = height;
}
return texture_ != nullptr;
}
// Libera la memoria de la textura
void Texture::unloadTexture() {
// Libera la textura
if (texture_ != nullptr) {
SDL_DestroyTexture(texture_);
texture_ = nullptr;
width_ = 0;
height_ = 0;
}
}
// Establece el color para la modulacion
void Texture::setColor(Uint8 red, Uint8 green, Uint8 blue) {
SDL_SetTextureColorMod(texture_, red, green, blue);
}
void Texture::setColor(Color color) {
SDL_SetTextureColorMod(texture_, color.r, color.g, color.b);
}
// Establece el blending
void Texture::setBlendMode(SDL_BlendMode blending) {
SDL_SetTextureBlendMode(texture_, blending);
}
// Establece el alpha para la modulación
void Texture::setAlpha(Uint8 alpha) {
SDL_SetTextureAlphaMod(texture_, alpha);
}
// Renderiza la textura en un punto específico
void Texture::render(int x, int y, SDL_FRect *clip, float horizontal_zoom, float vertical_zoom, double angle, SDL_FPoint *center, SDL_FlipMode flip) {
// Establece el destino de renderizado en la pantalla
SDL_FRect render_quad = {static_cast<float>(x), static_cast<float>(y), static_cast<float>(width_), static_cast<float>(height_)};
// Obtiene las dimesiones del clip de renderizado
if (clip != nullptr) {
render_quad.w = clip->w;
render_quad.h = clip->h;
}
// Calcula el zoom y las coordenadas
if (vertical_zoom != 1.0F || horizontal_zoom != 1.0F) {
render_quad.x = render_quad.x + (render_quad.w / 2);
render_quad.y = render_quad.y + (render_quad.h / 2);
render_quad.w = render_quad.w * horizontal_zoom;
render_quad.h = render_quad.h * vertical_zoom;
render_quad.x = render_quad.x - (render_quad.w / 2);
render_quad.y = render_quad.y - (render_quad.h / 2);
}
// Renderiza a pantalla
SDL_RenderTextureRotated(renderer_, texture_, clip, &render_quad, angle, center, flip);
}
// Establece la textura como objetivo de renderizado
void Texture::setAsRenderTarget(SDL_Renderer *renderer) {
SDL_SetRenderTarget(renderer, texture_);
}
// Obtiene el ancho de la imagen
auto Texture::getWidth() const -> int {
return width_;
}
// Obtiene el alto de la imagen
auto Texture::getHeight() const -> int {
return height_;
}
// Recarga la textura
auto Texture::reLoad() -> bool {
return loadFromFile(path_);
}
// Obtiene la textura
auto Texture::getSDLTexture() -> SDL_Texture * {
return texture_;
}
// Desencadenar la superficie actual
void Texture::unloadSurface() {
surface_.reset(); // Resetea el shared_ptr
width_ = 0;
height_ = 0;
}
// Crea una surface desde un fichero .gif
auto Texture::loadSurface(const std::string &file_path) -> std::shared_ptr<Surface> {
// Libera la superficie actual
unloadSurface();
std::vector<Uint8> buffer;
// Intentar cargar desde ResourceHelper primero
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
buffer = resource_data;
} else {
// Fallback a filesystem directo
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
if (!file) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
throw std::runtime_error("Fichero no encontrado: " + file_path);
}
// Obtener el tamaño del archivo
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
// Leer el contenido del archivo en un buffer
buffer.resize(size);
if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al leer el fichero %s", file_path.c_str());
throw std::runtime_error("Error al leer el fichero: " + file_path);
}
}
// Crear un objeto Gif y llamar a la función loadGif
GIF::Gif gif;
Uint16 w = 0;
Uint16 h = 0;
std::vector<Uint8> raw_pixels = gif.loadGif(buffer.data(), w, h);
if (raw_pixels.empty()) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo cargar el GIF %s", file_path.c_str());
return nullptr;
}
// Si el constructor de Surface espera un std::shared_ptr<Uint8[]>:
size_t pixel_count = raw_pixels.size();
auto pixels = std::shared_ptr<Uint8[]>(new Uint8[pixel_count], std::default_delete<Uint8[]>()); // NOLINT(modernize-avoid-c-arrays)
std::memcpy(pixels.get(), raw_pixels.data(), pixel_count);
auto surface = std::make_shared<Surface>(w, h, pixels);
// Actualizar las dimensiones
width_ = w;
height_ = h;
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "GIF %s cargado correctamente.", file_path.c_str());
return surface;
}
// Vuelca la surface en la textura
void Texture::flipSurface() {
// Limpia la textura
auto *temp = SDL_GetRenderTarget(renderer_);
SDL_SetRenderTarget(renderer_, texture_);
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 0);
SDL_RenderClear(renderer_);
SDL_SetRenderTarget(renderer_, temp);
// Vuelca los datos
Uint32 *pixels;
int pitch;
SDL_LockTexture(texture_, nullptr, reinterpret_cast<void **>(&pixels), &pitch);
for (int i = 0; i < width_ * height_; ++i) {
pixels[i] = palettes_[current_palette_][surface_->data[i]];
}
SDL_UnlockTexture(texture_);
}
// Establece un color de la paleta
void Texture::setPaletteColor(int palette, int index, Uint32 color) {
palettes_.at(palette)[index] = color;
}
// Carga una paleta desde un fichero
auto Texture::loadPaletteFromFile(const std::string &file_path, bool quiet) -> Palette {
Palette palette;
std::vector<Uint8> buffer;
// Intentar cargar desde ResourceHelper primero
auto resource_data = ResourceHelper::loadFile(file_path);
if (!resource_data.empty()) {
buffer = resource_data;
} else {
// Fallback a filesystem directo
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
if (!file) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
throw std::runtime_error("Fichero no encontrado: " + file_path);
}
// Obtener el tamaño del archivo y leerlo en un buffer
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
buffer.resize(size);
if (!file.read(reinterpret_cast<char *>(buffer.data()), size)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo leer completamente el fichero %s", file_path.c_str());
throw std::runtime_error("Error al leer el fichero: " + file_path);
}
}
if (!quiet) {
printWithDots("Palette : ", getFileName(file_path), "[ LOADED ]");
}
// Usar la nueva función loadPalette, que devuelve un vector<uint32_t>
GIF::Gif gif;
std::vector<uint32_t> pal = gif.loadPalette(buffer.data());
if (pal.empty()) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Advertencia: No se encontró paleta en el archivo %s", file_path.c_str());
return palette; // Devuelve un vector vacío si no hay paleta
}
// Modificar la conversión para obtener formato RGBA (0xRRGGBBAA)
for (size_t i = 0; i < pal.size() && i < palette.size(); ++i) {
palette[i] = (pal[i] << 8) | 0xFF; // Resultado: 0xRRGGBBAA
}
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "Paleta cargada correctamente desde %s", file_path.c_str());
return palette;
}
// Añade una paleta a la lista
void Texture::addPaletteFromGifFile(const std::string &path, bool quiet) {
palettes_.emplace_back(loadPaletteFromFile(path, quiet));
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
}
// Añade una paleta a la lista
void Texture::addPaletteFromPalFile(const std::string &path) {
palettes_.emplace_back(readPalFile(path, true)); // Usar modo silencioso
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
}
// Cambia la paleta de la textura
void Texture::setPalette(size_t palette) {
if (palette < palettes_.size()) {
current_palette_ = palette;
flipSurface();
}
}
// Obtiene el renderizador
auto Texture::getRenderer() -> SDL_Renderer * { return renderer_; }
// Carga una paleta desde un archivo .pal
auto Texture::readPalFile(const std::string &file_path, bool quiet) -> Palette {
Palette palette{};
palette.fill(0); // Inicializar todo con 0 (transparente por defecto)
// Intentar cargar desde ResourceHelper primero
auto resource_data = ResourceHelper::loadFile(file_path);
std::istringstream stream;
bool using_resource_data = false;
if (!resource_data.empty()) {
std::string content(resource_data.begin(), resource_data.end());
stream.str(content);
using_resource_data = true;
}
// Fallback a archivo directo
std::ifstream file;
if (!using_resource_data) {
file.open(file_path);
if (!file.is_open()) {
throw std::runtime_error("No se pudo abrir el archivo .pal");
}
}
std::istream &input_stream = using_resource_data ? stream : static_cast<std::istream &>(file);
std::string line;
int line_number = 0;
int color_index = 0;
while (std::getline(input_stream, line)) {
++line_number;
// Ignorar las tres primeras líneas del archivo
if (line_number <= 3) {
continue;
}
// Procesar las líneas restantes con valores RGB
std::istringstream ss(line);
int r;
int g;
int b;
if (ss >> r >> g >> b) {
// Construir el color RGBA (A = 255 por defecto)
Uint32 color = (r << 24) | (g << 16) | (b << 8) | 255;
palette[color_index++] = color;
// Limitar a un máximo de 256 colores (opcional)
if (color_index >= 256) {
break;
}
}
}
if (!using_resource_data && file.is_open()) {
file.close();
}
return palette;
}