llevat soport per a colors amb nom

This commit is contained in:
2026-04-06 09:14:36 +02:00
parent c4a26ffa0f
commit cdf0665458
121 changed files with 676 additions and 5754 deletions

View File

@@ -15,11 +15,17 @@ namespace Tile {
constexpr int HALF_SIZE = SIZE / 2;
} // namespace Tile
// Dimensiones del mapa en tiles
namespace Map {
constexpr int WIDTH = 32; // Ancho del mapa en tiles
constexpr int HEIGHT = 21; // Alto del mapa en tiles (pantalla menos 3 tiles de statusbar)
} // namespace Map
namespace PlayArea {
constexpr int TOP = (0 * Tile::SIZE);
constexpr int BOTTOM = (16 * Tile::SIZE);
constexpr int BOTTOM = (Map::HEIGHT * Tile::SIZE);
constexpr int LEFT = (0 * Tile::SIZE);
constexpr int RIGHT = (32 * Tile::SIZE);
constexpr int RIGHT = (Map::WIDTH * Tile::SIZE);
constexpr int WIDTH = RIGHT - LEFT;
constexpr int HEIGHT = BOTTOM - TOP;
constexpr int CENTER_X = LEFT + (WIDTH / 2);

View File

@@ -333,77 +333,6 @@ auto checkCollision(const SDL_FPoint& point, const LineDiagonal& l) -> bool {
return true;
}
// Convierte una cadena a un indice de la paleta
auto stringToColor(const std::string& str) -> Uint8 {
// Mapas de colores para cada paleta
static const std::unordered_map<std::string, Uint8> PALETTE_MAP = {
{"black", 0},
{"bright_black", 1},
{"blue", 2},
{"bright_blue", 3},
{"red", 4},
{"bright_red", 5},
{"magenta", 6},
{"bright_magenta", 7},
{"green", 8},
{"bright_green", 9},
{"cyan", 10},
{"bright_cyan", 11},
{"yellow", 12},
{"bright_yellow", 13},
{"white", 14},
{"bright_white", 15},
// Índices extendidos (paletas de 32 colores): nombres genéricos COLOR_N
{"color_16", 16}, {"color_17", 17}, {"color_18", 18}, {"color_19", 19},
{"color_20", 20}, {"color_21", 21}, {"color_22", 22}, {"color_23", 23},
{"color_24", 24}, {"color_25", 25}, {"color_26", 26}, {"color_27", 27},
{"color_28", 28}, {"color_29", 29}, {"color_30", 30}, {"color_31", 31},
{"transparent", 255}};
// Busca el color en el mapa
auto it = PALETTE_MAP.find(str);
if (it != PALETTE_MAP.end()) {
return it->second;
}
// Fallback: si el string es numérico (p.ej. "4"), lo tratamos como índice de paleta
if (!str.empty() && std::ranges::all_of(str, [](char c) { return std::isdigit(static_cast<unsigned char>(c)) != 0; })) {
const int IDX = safeStoi(str, 0);
if (IDX >= 0 && IDX <= 255) {
return static_cast<Uint8>(IDX);
}
}
// Si no se encuentra el color, devolvemos negro por defecto
return 0;
}
// Inverso de stringToColor: devuelve el nombre canónico para un índice de paleta (o el propio número si no hay)
auto colorToString(Uint8 index) -> std::string {
static const std::array<const char*, 16> NAMES = {
"black", "bright_black",
"blue", "bright_blue",
"red", "bright_red",
"magenta", "bright_magenta",
"green", "bright_green",
"cyan", "bright_cyan",
"yellow", "bright_yellow",
"white", "bright_white"};
if (index < NAMES.size()) { return NAMES[index]; }
if (index < 32) { return "color_" + std::to_string(index); }
if (index == 255) { return "transparent"; }
return std::to_string(index);
}
// Convierte una cadena a un entero de forma segura
auto safeStoi(const std::string& value, int default_value) -> int {
try {

View File

@@ -5,34 +5,6 @@
#include <string> // Para string
#include <vector> // Para vector
enum class PaletteColor : Uint8 {
BLACK = 0,
BRIGHT_BLACK = 1,
BLUE = 2,
BRIGHT_BLUE = 3,
RED = 4,
BRIGHT_RED = 5,
MAGENTA = 6,
BRIGHT_MAGENTA = 7,
GREEN = 8,
BRIGHT_GREEN = 9,
CYAN = 10,
BRIGHT_CYAN = 11,
YELLOW = 12,
BRIGHT_YELLOW = 13,
WHITE = 14,
BRIGHT_WHITE = 15,
TRANSPARENT = 255,
};
// Estructura para definir un circulo
struct Circle {
int x{0};
@@ -92,8 +64,6 @@ auto toSDLRect(const SDL_FRect& frect) -> SDL_Rect; // Convierte SDL_FRect
auto toSDLPoint(const SDL_FPoint& fpoint) -> SDL_Point; // Convierte SDL_FPoint a SDL_Point
// CONVERSIONES DE STRING
auto stringToColor(const std::string& str) -> Uint8; // String a índice de paleta (acepta nombres o números)
auto colorToString(Uint8 index) -> std::string; // Índice de paleta a nombre canónico
auto safeStoi(const std::string& value, int default_value = 0) -> int; // String a int seguro (sin excepciones)
auto stringToBool(const std::string& str) -> bool; // String a bool (true/1/yes/on)
auto boolToString(bool value) -> std::string; // Bool a string (1/0)