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:
@@ -611,7 +611,7 @@ void Player::setPlayingState(PlayerState state)
|
|||||||
player_sprite_->setAccelY(0.2f);
|
player_sprite_->setAccelY(0.2f);
|
||||||
player_sprite_->setVelY(-4.0f);
|
player_sprite_->setVelY(-4.0f);
|
||||||
player_sprite_->setVelX(0.0f);
|
player_sprite_->setVelX(0.0f);
|
||||||
player_sprite_->setCurrentAnimation("dying");
|
player_sprite_->setCurrentAnimation("rolling");
|
||||||
player_sprite_->setAnimationSpeed(5);
|
player_sprite_->setAnimationSpeed(5);
|
||||||
setScoreboardMode(ScoreboardMode::GAME_OVER);
|
setScoreboardMode(ScoreboardMode::GAME_OVER);
|
||||||
playSound("voice_aw_aw_aw.wav");
|
playSound("voice_aw_aw_aw.wav");
|
||||||
|
|||||||
@@ -279,14 +279,14 @@ void Resource::addPalettes()
|
|||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> PALETTES");
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> PALETTES");
|
||||||
|
|
||||||
// Paletas para el jugador 1
|
// Paletas para el jugador 1
|
||||||
getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_coffee1.pal"));
|
getTexture("player1.gif")->addPaletteFromPalFile(Asset::get()->get("player1_coffee1.pal"));
|
||||||
getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_coffee2.pal"));
|
getTexture("player1.gif")->addPaletteFromPalFile(Asset::get()->get("player1_coffee2.pal"));
|
||||||
getTexture("player1.gif")->addPaletteFromFile(Asset::get()->get("player1_invencible.pal"));
|
getTexture("player1.gif")->addPaletteFromPalFile(Asset::get()->get("player1_invencible.pal"));
|
||||||
|
|
||||||
// Paletas para el jugador 2
|
// Paletas para el jugador 2
|
||||||
getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_coffee1.pal"));
|
getTexture("player2.gif")->addPaletteFromPalFile(Asset::get()->get("player2_coffee1.pal"));
|
||||||
getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_coffee2.pal"));
|
getTexture("player2.gif")->addPaletteFromPalFile(Asset::get()->get("player2_coffee2.pal"));
|
||||||
getTexture("player2.gif")->addPaletteFromFile(Asset::get()->get("player2_invencible.pal"));
|
getTexture("player2.gif")->addPaletteFromPalFile(Asset::get()->get("player2_invencible.pal"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Crea texturas a partir de textos para mostrar puntuaciones y mensajes
|
// Crea texturas a partir de textos para mostrar puntuaciones y mensajes
|
||||||
|
|||||||
@@ -8,8 +8,11 @@
|
|||||||
#include <fstream> // Para basic_ifstream, basic_ios, ios, operator|
|
#include <fstream> // Para basic_ifstream, basic_ios, ios, operator|
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
#include <string> // Para operator+, char_traits, string, operat...
|
#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 <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 "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a...
|
||||||
#include "utils.h" // Para getFileName, Color, printWithDots
|
#include "utils.h" // Para getFileName, Color, printWithDots
|
||||||
|
|
||||||
@@ -37,7 +40,7 @@ Texture::Texture(SDL_Renderer *renderer, const std::string &path)
|
|||||||
surface_ = loadSurface(path_);
|
surface_ = loadSurface(path_);
|
||||||
|
|
||||||
// Añade la propia paleta del fichero a la lista
|
// 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
|
// Crea la textura, establece el BlendMode y copia la surface a la textura
|
||||||
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
|
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
|
// 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
|
// Abrir el archivo GIF
|
||||||
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
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)
|
// 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());
|
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
|
// 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));
|
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
|
// 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;
|
current_palette_ = palette;
|
||||||
flipSurface();
|
flipSurface();
|
||||||
@@ -380,4 +390,51 @@ void Texture::setPalette(int palette)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Obtiene el renderizador
|
// 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;
|
||||||
|
}
|
||||||
@@ -12,6 +12,9 @@
|
|||||||
|
|
||||||
struct Color;
|
struct Color;
|
||||||
|
|
||||||
|
// Alias
|
||||||
|
using Palette = std::array<Uint32, 256>;
|
||||||
|
|
||||||
// Definición de Surface para imágenes con paleta
|
// Definición de Surface para imágenes con paleta
|
||||||
struct Surface
|
struct Surface
|
||||||
{
|
{
|
||||||
@@ -47,9 +50,10 @@ public:
|
|||||||
void setAlpha(Uint8 alpha); // Establece el alpha para la modulación
|
void setAlpha(Uint8 alpha); // Establece el alpha para la modulación
|
||||||
|
|
||||||
// --- Paletas ---
|
// --- 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 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 ---
|
// --- Getters ---
|
||||||
int getWidth(); // Obtiene el ancho de la imagen
|
int getWidth(); // Obtiene el ancho de la imagen
|
||||||
@@ -64,16 +68,17 @@ private:
|
|||||||
std::shared_ptr<Surface> surface_ = nullptr; // Surface para usar imágenes en formato gif con paleta
|
std::shared_ptr<Surface> surface_ = nullptr; // Surface para usar imágenes en formato gif con paleta
|
||||||
|
|
||||||
// --- Variables ---
|
// --- Variables ---
|
||||||
std::string path_; // Ruta de la imagen de la textura
|
std::string path_; // Ruta de la imagen de la textura
|
||||||
int width_ = 0; // Ancho de la imagen
|
int width_ = 0; // Ancho de la imagen
|
||||||
int height_ = 0; // Alto de la imagen
|
int height_ = 0; // Alto de la imagen
|
||||||
std::vector<std::vector<Uint32>> palettes_; // Vector con las diferentes paletas
|
std::vector<Palette> palettes_; // Vector con las diferentes paletas
|
||||||
int current_palette_ = 0; // Índice de la paleta en uso
|
int current_palette_ = 0; // Índice de la paleta en uso
|
||||||
|
|
||||||
// --- Métodos internos ---
|
// --- Métodos internos ---
|
||||||
std::shared_ptr<Surface> loadSurface(const std::string &file_name); // Crea una surface desde un fichero .gif
|
std::shared_ptr<Surface> loadSurface(const std::string &file_name); // Crea una surface desde un fichero .gif
|
||||||
void flipSurface(); // Vuelca la surface en la textura
|
void flipSurface(); // Vuelca la surface en la textura
|
||||||
std::vector<Uint32> loadPaletteFromFile(const std::string &file_name); // Carga una paleta desde un fichero
|
Palette loadPaletteFromFile(const std::string &file_name); // Carga una paleta desde un fichero
|
||||||
void unloadTexture(); // Libera la memoria de la textura
|
void unloadTexture(); // Libera la memoria de la textura
|
||||||
void unloadSurface(); // Libera la surface actual
|
void unloadSurface(); // Libera la surface actual
|
||||||
|
Palette readPalFile(const std::string &file_path); // Carga una paleta desde un archivo .pal
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user