jugant amb clang-tidy
This commit is contained in:
272
source/utils.h
272
source/utils.h
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h> // Para Uint8, SDL_FRect, SDL_FPoint, SDL_Renderer
|
||||
#include <stdint.h> // Para int32_t
|
||||
|
||||
#include <algorithm> // Para max, min
|
||||
#include <array> // Para array
|
||||
#include <cctype> // Para isxdigit
|
||||
#include <cstdint> // Para int32_t
|
||||
#include <cstdlib> // Para abs, size_t
|
||||
#include <stdexcept> // Para invalid_argument
|
||||
#include <string> // Para string, basic_string, stoi
|
||||
@@ -18,105 +18,131 @@ constexpr size_t COLOR_CYCLE_SIZE = 6; // Mitad del ciclo espejado
|
||||
|
||||
// --- Estructuras y tipos ---
|
||||
struct Overrides {
|
||||
std::string param_file; // Fichero de parametros a utilizar
|
||||
bool clear_hi_score_table; // Reinicia la tabla de records
|
||||
std::string param_file; // Fichero de parametros a utilizar
|
||||
bool clear_hi_score_table{false}; // Reinicia la tabla de records
|
||||
|
||||
Overrides()
|
||||
: param_file(""), clear_hi_score_table(false) {}
|
||||
Overrides() = default;
|
||||
};
|
||||
extern Overrides overrides;
|
||||
|
||||
// Estructura para definir un circulo
|
||||
struct Circle {
|
||||
int x, y, r;
|
||||
Circle() : x(0), y(0), r(0) {}
|
||||
Circle(int xCoord, int yCoord, int radius)
|
||||
: x(xCoord), y(yCoord), r(radius) {}
|
||||
int x, y, r;
|
||||
Circle() : x(0), y(0), r(0) {}
|
||||
Circle(int x_coord, int y_coord, int radius)
|
||||
: x(x_coord), y(y_coord), r(radius) {}
|
||||
};
|
||||
|
||||
// Estructura para definir un color RGBA
|
||||
struct Color {
|
||||
Uint8 r, g, b, a;
|
||||
constexpr Color() : r(0), g(0), b(0), a(255) {}
|
||||
explicit constexpr Color(Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha = 255) : r(red), g(green), b(blue), a(alpha) {}
|
||||
private:
|
||||
static constexpr Uint8 MAX_COLOR_VALUE = 255;
|
||||
static constexpr Uint8 MIN_COLOR_VALUE = 0;
|
||||
static constexpr Uint8 DEFAULT_ALPHA = 255;
|
||||
static constexpr int DEFAULT_LIGHTEN_AMOUNT = 50;
|
||||
static constexpr int DEFAULT_DARKEN_AMOUNT = 50;
|
||||
static constexpr int DEFAULT_APPROACH_STEP = 1;
|
||||
static constexpr size_t HEX_RGB_LENGTH = 6;
|
||||
static constexpr size_t HEX_RGBA_LENGTH = 8;
|
||||
static constexpr int HEX_BASE = 16;
|
||||
static constexpr size_t HEX_COMPONENT_LENGTH = 2;
|
||||
|
||||
constexpr Color inverse() const { return Color(255 - r, 255 - g, 255 - b, a); }
|
||||
constexpr Color lighten(int amount = 50) const {
|
||||
return Color(
|
||||
std::min(255, r + amount),
|
||||
std::min(255, g + amount),
|
||||
std::min(255, b + amount),
|
||||
a);
|
||||
}
|
||||
constexpr Color darken(int amount = 50) const {
|
||||
return Color(
|
||||
std::max(0, r - amount),
|
||||
std::max(0, g - amount),
|
||||
std::max(0, b - amount),
|
||||
a);
|
||||
}
|
||||
public:
|
||||
Uint8 r, g, b, a;
|
||||
|
||||
// Método estático para crear Color desde string hexadecimal
|
||||
static Color fromHex(const std::string &hexStr) {
|
||||
std::string hex = hexStr;
|
||||
constexpr Color() : r(MIN_COLOR_VALUE), g(MIN_COLOR_VALUE), b(MIN_COLOR_VALUE), a(DEFAULT_ALPHA) {}
|
||||
|
||||
// Quitar '#' si existe
|
||||
if (!hex.empty() && hex[0] == '#') {
|
||||
hex = hex.substr(1);
|
||||
explicit constexpr Color(Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha = DEFAULT_ALPHA)
|
||||
: r(red), g(green), b(blue), a(alpha) {}
|
||||
|
||||
[[nodiscard]] constexpr auto INVERSE() const -> Color {
|
||||
return Color(MAX_COLOR_VALUE - r, MAX_COLOR_VALUE - g, MAX_COLOR_VALUE - b, a);
|
||||
}
|
||||
|
||||
// Verificar longitud válida (6 para RGB o 8 para RGBA)
|
||||
if (hex.length() != 6 && hex.length() != 8) {
|
||||
throw std::invalid_argument("String hexadecimal debe tener 6 o 8 caracteres");
|
||||
[[nodiscard]] constexpr auto LIGHTEN(int amount = DEFAULT_LIGHTEN_AMOUNT) const -> Color {
|
||||
return Color(
|
||||
std::min(static_cast<int>(MAX_COLOR_VALUE), r + amount),
|
||||
std::min(static_cast<int>(MAX_COLOR_VALUE), g + amount),
|
||||
std::min(static_cast<int>(MAX_COLOR_VALUE), b + amount),
|
||||
a);
|
||||
}
|
||||
|
||||
// Verificar que todos los caracteres sean hexadecimales válidos
|
||||
for (char c : hex) {
|
||||
if (!std::isxdigit(c)) {
|
||||
throw std::invalid_argument("String contiene caracteres no hexadecimales");
|
||||
[[nodiscard]] constexpr auto DARKEN(int amount = DEFAULT_DARKEN_AMOUNT) const -> Color {
|
||||
return Color(
|
||||
std::max(static_cast<int>(MIN_COLOR_VALUE), r - amount),
|
||||
std::max(static_cast<int>(MIN_COLOR_VALUE), g - amount),
|
||||
std::max(static_cast<int>(MIN_COLOR_VALUE), b - amount),
|
||||
a);
|
||||
}
|
||||
|
||||
// Método estático para crear Color desde string hexadecimal
|
||||
static auto fromHex(const std::string &hex_str) -> Color {
|
||||
std::string hex = hex_str;
|
||||
|
||||
// Quitar '#' si existe
|
||||
if (!hex.empty() && hex[0] == '#') {
|
||||
hex = hex.substr(1);
|
||||
}
|
||||
|
||||
// Verificar longitud válida (6 para RGB o 8 para RGBA)
|
||||
if (hex.length() != HEX_RGB_LENGTH && hex.length() != HEX_RGBA_LENGTH) {
|
||||
throw std::invalid_argument("String hexadecimal debe tener 6 o 8 caracteres");
|
||||
}
|
||||
|
||||
// Verificar que todos los caracteres sean hexadecimales válidos
|
||||
for (char c : hex) {
|
||||
if (std::isxdigit(c) == 0) {
|
||||
throw std::invalid_argument("String contiene caracteres no hexadecimales");
|
||||
}
|
||||
}
|
||||
|
||||
// Convertir cada par de caracteres a valores RGB(A)
|
||||
Uint8 r = static_cast<Uint8>(std::stoi(hex.substr(0, HEX_COMPONENT_LENGTH), nullptr, HEX_BASE));
|
||||
Uint8 g = static_cast<Uint8>(std::stoi(hex.substr(HEX_COMPONENT_LENGTH, HEX_COMPONENT_LENGTH), nullptr, HEX_BASE));
|
||||
Uint8 b = static_cast<Uint8>(std::stoi(hex.substr(HEX_COMPONENT_LENGTH * 2, HEX_COMPONENT_LENGTH), nullptr, HEX_BASE));
|
||||
Uint8 a = DEFAULT_ALPHA; // Alpha por defecto
|
||||
|
||||
// Si tiene 8 caracteres, extraer el alpha
|
||||
if (hex.length() == HEX_RGBA_LENGTH) {
|
||||
a = static_cast<Uint8>(std::stoi(hex.substr(HEX_COMPONENT_LENGTH * 3, HEX_COMPONENT_LENGTH), nullptr, HEX_BASE));
|
||||
}
|
||||
|
||||
return Color(r, g, b, a);
|
||||
}
|
||||
|
||||
// Convertir cada par de caracteres a valores RGB(A)
|
||||
Uint8 r = static_cast<Uint8>(std::stoi(hex.substr(0, 2), nullptr, 16));
|
||||
Uint8 g = static_cast<Uint8>(std::stoi(hex.substr(2, 2), nullptr, 16));
|
||||
Uint8 b = static_cast<Uint8>(std::stoi(hex.substr(4, 2), nullptr, 16));
|
||||
Uint8 a = 255; // Alpha por defecto
|
||||
|
||||
// Si tiene 8 caracteres, extraer el alpha
|
||||
if (hex.length() == 8) {
|
||||
a = static_cast<Uint8>(std::stoi(hex.substr(6, 2), nullptr, 16));
|
||||
[[nodiscard]] constexpr auto IS_EQUAL_TO(const Color &other) const -> bool {
|
||||
return r == other.r && g == other.g && b == other.b && a == other.a;
|
||||
}
|
||||
|
||||
return Color(r, g, b, a);
|
||||
}
|
||||
[[nodiscard]] constexpr auto APPROACH_TO(const Color &target, int step = DEFAULT_APPROACH_STEP) const -> Color {
|
||||
auto approach_component = [step](Uint8 current, Uint8 target_val) -> Uint8 {
|
||||
if (std::abs(current - target_val) <= step) {
|
||||
return target_val;
|
||||
}
|
||||
return (current < target_val) ? current + step : current - step;
|
||||
};
|
||||
|
||||
constexpr bool isEqualTo(const Color &other) const {
|
||||
return r == other.r && g == other.g && b == other.b && a == other.a;
|
||||
}
|
||||
Uint8 new_r = approach_component(r, target.r);
|
||||
Uint8 new_g = approach_component(g, target.g);
|
||||
Uint8 new_b = approach_component(b, target.b);
|
||||
Uint8 new_a = approach_component(a, target.a);
|
||||
|
||||
constexpr Color approachTo(const Color &target, int step = 1) const {
|
||||
Uint8 newR = (std::abs(r - target.r) <= step) ? target.r : (r < target.r ? r + step : r - step);
|
||||
Uint8 newG = (std::abs(g - target.g) <= step) ? target.g : (g < target.g ? g + step : g - step);
|
||||
Uint8 newB = (std::abs(b - target.b) <= step) ? target.b : (b < target.b ? b + step : b - step);
|
||||
Uint8 newA = (std::abs(a - target.a) <= step) ? target.a : (a < target.a ? a + step : a - step);
|
||||
|
||||
return Color(newR, newG, newB, newA);
|
||||
}
|
||||
return Color(new_r, new_g, new_b, new_a);
|
||||
}
|
||||
};
|
||||
|
||||
// Estructura para definir un color HSV
|
||||
struct HSV {
|
||||
float h, s, v;
|
||||
float h, s, v;
|
||||
};
|
||||
|
||||
// Estructura para definir el ciclo de color
|
||||
enum class ColorCycleStyle {
|
||||
SubtlePulse, // Variación leve en brillo (por defecto)
|
||||
HueWave, // Variación suave en tono (sin verde)
|
||||
Vibrant, // Cambios agresivos en tono y brillo
|
||||
DarkenGlow, // Oscurece hacia el centro y regresa
|
||||
LightFlash // Ilumina hacia el centro y regresa
|
||||
SUBTLE_PULSE, // Variación leve en brillo (por defecto)
|
||||
HUE_WAVE, // Variación suave en tono (sin verde)
|
||||
VIBRANT, // Cambios agresivos en tono y brillo
|
||||
DARKEN_GLOW, // Oscurece hacia el centro y regresa
|
||||
LIGHT_FLASH // Ilumina hacia el centro y regresa
|
||||
};
|
||||
|
||||
// Posiciones de las notificaciones
|
||||
@@ -130,40 +156,40 @@ enum class NotifyPosition {
|
||||
|
||||
// Estructura para datos de la demo
|
||||
struct DemoKeys {
|
||||
Uint8 left;
|
||||
Uint8 right;
|
||||
Uint8 no_input;
|
||||
Uint8 fire;
|
||||
Uint8 fire_left;
|
||||
Uint8 fire_right;
|
||||
Uint8 left;
|
||||
Uint8 right;
|
||||
Uint8 no_input;
|
||||
Uint8 fire;
|
||||
Uint8 fire_left;
|
||||
Uint8 fire_right;
|
||||
|
||||
explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0)
|
||||
: left(l), right(r), no_input(ni), fire(f), fire_left(fl), fire_right(fr) {}
|
||||
explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0)
|
||||
: left(l), right(r), no_input(ni), fire(f), fire_left(fl), fire_right(fr) {}
|
||||
};
|
||||
|
||||
using DemoData = std::vector<DemoKeys>;
|
||||
|
||||
struct Demo {
|
||||
bool enabled; // Indica si está activo el modo demo
|
||||
bool recording; // Indica si está activado el modo para grabar la demo
|
||||
int counter; // Contador para el modo demo
|
||||
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
|
||||
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
|
||||
bool enabled; // Indica si está activo el modo demo
|
||||
bool recording; // Indica si está activado el modo para grabar la demo
|
||||
int counter; // Contador para el modo demo
|
||||
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
|
||||
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
|
||||
|
||||
Demo() : enabled(false), recording(false), counter(0), keys(), data() {}
|
||||
Demo(bool e, bool r, int c, const DemoKeys &k, const std::vector<DemoData> &d)
|
||||
: enabled(e), recording(r), counter(c), keys(k), data(d) {}
|
||||
Demo() : enabled(false), recording(false), counter(0) {}
|
||||
Demo(bool e, bool r, int c, const DemoKeys &k, const std::vector<DemoData> &d)
|
||||
: enabled(e), recording(r), counter(c), keys(k), data(d) {}
|
||||
};
|
||||
|
||||
// Posiciones dentro de un rectangulo
|
||||
struct Zone {
|
||||
SDL_FRect rect; // Rectangulo que define la zona
|
||||
float center_x; // Anclaje al 50% del eje X
|
||||
float first_quarter_x; // Anclaje al 25% del eje X
|
||||
float third_quarter_x; // Anclaje al 75% del eje X
|
||||
float center_y; // Anclaje al 50% del eje Y
|
||||
float first_quarter_y; // Anclaje al 25% del eje Y
|
||||
float third_quarter_y; // Anclaje al 75% del eje Y
|
||||
SDL_FRect rect; // Rectangulo que define la zona
|
||||
float center_x; // Anclaje al 50% del eje X
|
||||
float first_quarter_x; // Anclaje al 25% del eje X
|
||||
float third_quarter_x; // Anclaje al 75% del eje X
|
||||
float center_y; // Anclaje al 50% del eje Y
|
||||
float first_quarter_y; // Anclaje al 25% del eje Y
|
||||
float third_quarter_y; // Anclaje al 75% del eje Y
|
||||
};
|
||||
|
||||
// --- Alias ---
|
||||
@@ -183,56 +209,56 @@ constexpr Color PINK_SKY_COLOR = Color(0XFF, 0X6B, 0X97);
|
||||
constexpr Color GREEN_SKY_COLOR = Color(0X00, 0X79, 0X6B);
|
||||
|
||||
// Colores y gráficos
|
||||
Color getColorLikeKnightRider(const std::vector<Color> &colors, int counter_);
|
||||
constexpr HSV rgbToHsv(Color color);
|
||||
constexpr Color hsvToRgb(HSV hsv);
|
||||
ColorCycle generateMirroredCycle(Color base, ColorCycleStyle style = ColorCycleStyle::SubtlePulse);
|
||||
auto getColorLikeKnightRider(const std::vector<Color> &colors, int counter) -> Color;
|
||||
constexpr auto rgbToHsv(Color color) -> HSV;
|
||||
constexpr auto hsvToRgb(HSV hsv) -> Color;
|
||||
auto generateMirroredCycle(Color base, ColorCycleStyle style = ColorCycleStyle::SUBTLE_PULSE) -> ColorCycle;
|
||||
|
||||
// Colisiones y geometría
|
||||
double distanceSquared(int x1, int y1, int x2, int y2);
|
||||
bool checkCollision(const Circle &a, const Circle &b);
|
||||
bool checkCollision(const Circle &a, const SDL_FRect &b);
|
||||
bool checkCollision(const SDL_FRect &a, const SDL_FRect &b);
|
||||
bool checkCollision(const SDL_FPoint &p, const SDL_FRect &r);
|
||||
auto distanceSquared(int x1, int y1, int x2, int y2) -> double;
|
||||
auto checkCollision(const Circle &a, const Circle &b) -> bool;
|
||||
auto checkCollision(const Circle &a, const SDL_FRect &b) -> bool;
|
||||
auto checkCollision(const SDL_FRect &a, const SDL_FRect &b) -> bool;
|
||||
auto checkCollision(const SDL_FPoint &p, const SDL_FRect &r) -> bool;
|
||||
|
||||
// Conversión y manipulación de cadenas
|
||||
bool stringToBool(const std::string &str);
|
||||
std::string boolToString(bool value);
|
||||
std::string boolToOnOff(bool value);
|
||||
std::string toLower(const std::string &str);
|
||||
std::string trim(const std::string &str);
|
||||
auto stringToBool(const std::string &str) -> bool;
|
||||
auto boolToString(bool value) -> std::string;
|
||||
auto boolToOnOff(bool value) -> std::string;
|
||||
auto toLower(const std::string &str) -> std::string;
|
||||
auto trim(const std::string &str) -> std::string;
|
||||
|
||||
// Dibujo
|
||||
void DrawCircle(SDL_Renderer *renderer, int32_t centerX, int32_t centerY, int32_t radius);
|
||||
void drawCircle(SDL_Renderer *renderer, int32_t center_x, int32_t center_y, int32_t radius);
|
||||
|
||||
// Manipulación de color
|
||||
Color lightenColor(const Color &color, int amount);
|
||||
Color DarkenColor(const Color &color, int amount);
|
||||
auto lightenColor(const Color &color, int amount) -> Color;
|
||||
auto darkenColor(const Color &color, int amount) -> Color;
|
||||
|
||||
// Funciones de suavizado (easing)
|
||||
double easeOutQuint(double t);
|
||||
double easeInQuint(double t);
|
||||
double easeInOutQuint(double t);
|
||||
double easeInQuad(double t);
|
||||
double easeOutQuad(double t);
|
||||
double easeInOutSine(double t);
|
||||
double easeInOut(double t);
|
||||
double easeInOutExpo(double t);
|
||||
double easeOutBounce(double t);
|
||||
double easeOutElastic(double t);
|
||||
double easeInElastic(double t);
|
||||
auto easeOutQuint(double time) -> double;
|
||||
auto easeInQuint(double time) -> double;
|
||||
auto easeInOutQuint(double time) -> double;
|
||||
auto easeInQuad(double time) -> double;
|
||||
auto easeOutQuad(double time) -> double;
|
||||
auto easeInOutSine(double time) -> double;
|
||||
auto easeInOut(double time) -> double;
|
||||
auto easeInOutExpo(double time) -> double;
|
||||
auto easeOutBounce(double time) -> double;
|
||||
auto easeOutElastic(double time) -> double;
|
||||
auto easeInElastic(double time) -> double;
|
||||
|
||||
// Utilidades varias
|
||||
bool stringInVector(const std::vector<std::string> &vec, const std::string &str); // Comprueba si un vector contiene una cadena
|
||||
auto stringInVector(const std::vector<std::string> &vec, const std::string &str) -> bool; // Comprueba si un vector contiene una cadena
|
||||
void printWithDots(const std::string &text1, const std::string &text2, const std::string &text3); // Imprime una línea con puntos
|
||||
|
||||
// Demo
|
||||
DemoData loadDemoDataFromFile(const std::string &file_path);
|
||||
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData;
|
||||
|
||||
#ifdef RECORDING
|
||||
bool saveDemoFile(const std::string &file_path, const DemoData &dd);
|
||||
#endif
|
||||
|
||||
// Ficheros y rutas
|
||||
std::string getFileName(const std::string &path); // Obtiene el nombre de un fichero a partir de una ruta
|
||||
std::string getPath(const std::string &full_path); // Obtiene la ruta eliminando el nombre del fichero
|
||||
auto getFileName(const std::string &path) -> std::string; // Obtiene el nombre de un fichero a partir de una ruta
|
||||
auto getPath(const std::string &full_path) -> std::string; // Obtiene la ruta eliminando el nombre del fichero
|
||||
Reference in New Issue
Block a user