Texture: afegit suport per a fitxers .pal
fix: quedava un player_sprite_->setCurrentAnimation("dying") per canviar a player_sprite_->setCurrentAnimation("rolling")
This commit is contained in:
@@ -8,8 +8,11 @@
|
||||
#include <fstream> // Para basic_ifstream, basic_ios, ios, operator|
|
||||
#include <stdexcept> // Para runtime_error
|
||||
#include <string> // Para operator+, char_traits, string, operat...
|
||||
#include <fstream> // Para basic_ifstream, basic_ostream, basic_ist...
|
||||
#include <iostream> // Para cerr
|
||||
#include <sstream> // Para basic_istringstream
|
||||
#include <vector> // 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<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
|
||||
Palette Texture::loadPaletteFromFile(const std::string &file_path)
|
||||
{
|
||||
std::vector<Uint32> palette;
|
||||
Palette palette;
|
||||
|
||||
// Abrir el archivo GIF
|
||||
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
||||
@@ -353,9 +356,9 @@ std::vector<Uint32> 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<Uint32> 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_; }
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user