From 122d44a710b48365c2e6b14cd9007a4710ae4cae Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 12 Jul 2025 13:04:07 +0200 Subject: [PATCH] Texture: afegit suport per a fitxers .pal fix: quedava un player_sprite_->setCurrentAnimation("dying") per canviar a player_sprite_->setCurrentAnimation("rolling") --- source/player.cpp | 2 +- source/resource.cpp | 12 +++---- source/texture.cpp | 79 ++++++++++++++++++++++++++++++++++++++------- source/texture.h | 29 ++++++++++------- 4 files changed, 92 insertions(+), 30 deletions(-) diff --git a/source/player.cpp b/source/player.cpp index bf18567..de1e265 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -611,7 +611,7 @@ void Player::setPlayingState(PlayerState state) player_sprite_->setAccelY(0.2f); player_sprite_->setVelY(-4.0f); player_sprite_->setVelX(0.0f); - player_sprite_->setCurrentAnimation("dying"); + player_sprite_->setCurrentAnimation("rolling"); player_sprite_->setAnimationSpeed(5); setScoreboardMode(ScoreboardMode::GAME_OVER); playSound("voice_aw_aw_aw.wav"); diff --git a/source/resource.cpp b/source/resource.cpp index fd73677..b91920b 100644 --- a/source/resource.cpp +++ b/source/resource.cpp @@ -279,14 +279,14 @@ void Resource::addPalettes() SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> PALETTES"); // Paletas para el jugador 1 - getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_coffee1.pal")); - getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_coffee2.pal")); - getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_invencible.pal")); + getTexture("player1.gif")->addPaletteFromPalFile(Asset::get()->get("player1_coffee1.pal")); + getTexture("player1.gif")->addPaletteFromPalFile(Asset::get()->get("player1_coffee2.pal")); + getTexture("player1.gif")->addPaletteFromPalFile(Asset::get()->get("player1_invencible.pal")); // Paletas para el jugador 2 - getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_coffee1.pal")); - getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_coffee2.pal")); - getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_invencible.pal")); + getTexture("player2.gif")->addPaletteFromPalFile(Asset::get()->get("player2_coffee1.pal")); + getTexture("player2.gif")->addPaletteFromPalFile(Asset::get()->get("player2_coffee2.pal")); + getTexture("player2.gif")->addPaletteFromPalFile(Asset::get()->get("player2_invencible.pal")); } // Crea texturas a partir de textos para mostrar puntuaciones y mensajes diff --git a/source/texture.cpp b/source/texture.cpp index 566a283..83caafc 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -8,8 +8,11 @@ #include // Para basic_ifstream, basic_ios, ios, operator| #include // Para runtime_error #include // Para operator+, char_traits, string, operat... +#include // Para basic_ifstream, basic_ostream, basic_ist... +#include // Para cerr +#include // Para basic_istringstream #include // Para vector -#include "external/gif.h" // Para Gif +#include "external/gif.h" // Para Gif #include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a... #include "utils.h" // Para getFileName, Color, printWithDots @@ -37,7 +40,7 @@ Texture::Texture(SDL_Renderer *renderer, const std::string &path) surface_ = loadSurface(path_); // Añade la propia paleta del fichero a la lista - addPaletteFromFile(path_); + addPaletteFromGifFile(path_); // Crea la textura, establece el BlendMode y copia la surface a la textura createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING); @@ -316,9 +319,9 @@ void Texture::setPaletteColor(int palette, int index, Uint32 color) } // Carga una paleta desde un fichero -std::vector Texture::loadPaletteFromFile(const std::string &file_path) +Palette Texture::loadPaletteFromFile(const std::string &file_path) { - std::vector palette; + Palette palette; // Abrir el archivo GIF std::ifstream file(file_path, std::ios::binary | std::ios::ate); @@ -353,9 +356,9 @@ std::vector Texture::loadPaletteFromFile(const std::string &file_path) } // Modificar la conversión para obtener formato RGBA (0xRRGGBBAA) - for (const auto &color : pal) + for (size_t i = 0; i < pal.size() && i < palette.size(); ++i) { - palette.push_back((color << 8) | 0xFF); // Resultado: 0xRRGGBBAA + palette[i] = (pal[i] << 8) | 0xFF; // Resultado: 0xRRGGBBAA } SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "Paleta cargada correctamente desde %s", file_path.c_str()); @@ -363,16 +366,23 @@ std::vector Texture::loadPaletteFromFile(const std::string &file_path) } // Añade una paleta a la lista -void Texture::addPaletteFromFile(const std::string &path) +void Texture::addPaletteFromGifFile(const std::string &path) { palettes_.emplace_back(loadPaletteFromFile(path)); - setPaletteColor((int)palettes_.size() - 1, 0, 0x00000000); + 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)); + setPaletteColor(palettes_.size() - 1, 0, 0x00000000); } // Cambia la paleta de la textura -void Texture::setPalette(int palette) +void Texture::setPalette(size_t palette) { - if (palette < (int)palettes_.size()) + if (palette < palettes_.size()) { current_palette_ = palette; flipSurface(); @@ -380,4 +390,51 @@ void Texture::setPalette(int palette) } // Obtiene el renderizador -SDL_Renderer *Texture::getRenderer() { return renderer_; } \ No newline at end of file +SDL_Renderer *Texture::getRenderer() { return renderer_; } + +// Carga una paleta desde un archivo .pal +Palette Texture::readPalFile(const std::string &file_path) +{ + Palette palette{}; + palette.fill(0); // Inicializar todo con 0 (transparente por defecto) + + std::ifstream file(file_path); + if (!file.is_open()) + { + throw std::runtime_error("No se pudo abrir el archivo .pal"); + } + + std::string line; + int line_number = 0; + int color_index = 0; + + while (std::getline(file, 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, g, 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; + } + } + } + + file.close(); + return palette; +} \ No newline at end of file diff --git a/source/texture.h b/source/texture.h index 0c2395e..6a2cc4a 100644 --- a/source/texture.h +++ b/source/texture.h @@ -12,6 +12,9 @@ struct Color; +// Alias +using Palette = std::array; + // Definición de Surface para imágenes con paleta struct Surface { @@ -47,9 +50,10 @@ public: void setAlpha(Uint8 alpha); // Establece el alpha para la modulación // --- Paletas --- - void addPaletteFromFile(const std::string &path); // Añade una paleta a la lista + void addPaletteFromGifFile(const std::string &path); // Añade una paleta a la lista + void addPaletteFromPalFile(const std::string &path); // Añade una paleta a la lista void setPaletteColor(int palette, int index, Uint32 color); // Establece un color de la paleta - void setPalette(int palette); // Cambia la paleta de la textura + void setPalette(size_t palette); // Cambia la paleta de la textura // --- Getters --- int getWidth(); // Obtiene el ancho de la imagen @@ -64,16 +68,17 @@ private: std::shared_ptr surface_ = nullptr; // Surface para usar imágenes en formato gif con paleta // --- Variables --- - std::string path_; // Ruta de la imagen de la textura - int width_ = 0; // Ancho de la imagen - int height_ = 0; // Alto de la imagen - std::vector> palettes_; // Vector con las diferentes paletas - int current_palette_ = 0; // Índice de la paleta en uso + std::string path_; // Ruta de la imagen de la textura + int width_ = 0; // Ancho de la imagen + int height_ = 0; // Alto de la imagen + std::vector palettes_; // Vector con las diferentes paletas + int current_palette_ = 0; // Índice de la paleta en uso // --- Métodos internos --- - std::shared_ptr loadSurface(const std::string &file_name); // Crea una surface desde un fichero .gif - void flipSurface(); // Vuelca la surface en la textura - std::vector loadPaletteFromFile(const std::string &file_name); // Carga una paleta desde un fichero - void unloadTexture(); // Libera la memoria de la textura - void unloadSurface(); // Libera la surface actual + std::shared_ptr loadSurface(const std::string &file_name); // Crea una surface desde un fichero .gif + void flipSurface(); // Vuelca la surface en la textura + Palette loadPaletteFromFile(const std::string &file_name); // Carga una paleta desde un fichero + void unloadTexture(); // Libera la memoria de la textura + void unloadSurface(); // Libera la surface actual + Palette readPalFile(const std::string &file_path); // Carga una paleta desde un archivo .pal }; \ No newline at end of file