jugant amb clang-tidy

This commit is contained in:
2025-07-19 22:25:46 +02:00
parent e06503a8fc
commit 1d3fd79a9e
30 changed files with 779 additions and 606 deletions

View File

@@ -17,9 +17,9 @@
Overrides overrides = Overrides();
// Obtiene un color del vector de colores imitando al Coche Fantástico
Color getColorLikeKnightRider(const std::vector<Color> &colors, int counter_) {
auto getColorLikeKnightRider(const std::vector<Color> &colors, int counter) -> Color {
int cycle_length = colors.size() * 2 - 2;
size_t n = counter_ % cycle_length;
size_t n = counter % cycle_length;
size_t index;
if (n < colors.size()) {
@@ -32,14 +32,14 @@ Color getColorLikeKnightRider(const std::vector<Color> &colors, int counter_) {
}
// Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2) {
const int delta_x = x2 - x1;
const int delta_y = y2 - y1;
return delta_x * delta_x + delta_y * delta_y;
auto distanceSquared(int x1, int y1, int x2, int y2) -> double {
const int DELTA_X = x2 - x1;
const int DELTA_Y = y2 - y1;
return DELTA_X * DELTA_X + DELTA_Y * DELTA_Y;
}
// Detector de colisiones entre dos circulos
bool checkCollision(const Circle &a, const Circle &b) {
auto checkCollision(const Circle &a, const Circle &b) -> bool {
// Calcula el radio total al cuadrado
int total_radius_squared = (a.r + b.r) * (a.r + b.r);
@@ -48,84 +48,96 @@ bool checkCollision(const Circle &a, const Circle &b) {
}
// Detector de colisiones entre un circulo y un rectangulo
bool checkCollision(const Circle &a, const SDL_FRect &b) {
auto checkCollision(const Circle &a, const SDL_FRect &b) -> bool {
// Encuentra el punto más cercano en el rectángulo
float cX = std::clamp(static_cast<float>(a.x), b.x, b.x + b.w);
float cY = std::clamp(static_cast<float>(a.y), b.y, b.y + b.h);
float c_x = std::clamp(static_cast<float>(a.x), b.x, b.x + b.w);
float c_y = std::clamp(static_cast<float>(a.y), b.y, b.y + b.h);
// Si el punto más cercano está dentro del círculo
return distanceSquared(static_cast<float>(a.x), static_cast<float>(a.y), cX, cY) < static_cast<float>(a.r) * a.r;
return distanceSquared(static_cast<float>(a.x), static_cast<float>(a.y), c_x, c_y) < static_cast<float>(a.r) * a.r;
}
// Detector de colisiones entre dos rectangulos
bool checkCollision(const SDL_FRect &a, const SDL_FRect &b) {
const int leftA = a.x, rightA = a.x + a.w, topA = a.y, bottomA = a.y + a.h;
const int leftB = b.x, rightB = b.x + b.w, topB = b.y, bottomB = b.y + b.h;
auto checkCollision(const SDL_FRect &a, const SDL_FRect &b) -> bool {
const int LEFT_A = a.x;
const int RIGHT_A = a.x + a.w;
const int TOP_A = a.y;
const int BOTTOM_A = a.y + a.h;
const int LEFT_B = b.x;
const int RIGHT_B = b.x + b.w;
const int TOP_B = b.y;
const int BOTTOM_B = b.y + b.h;
if (bottomA <= topB)
if (BOTTOM_A <= TOP_B) {
return false;
if (topA >= bottomB)
}
if (TOP_A >= BOTTOM_B) {
return false;
if (rightA <= leftB)
}
if (RIGHT_A <= LEFT_B) {
return false;
if (leftA >= rightB)
}
if (LEFT_A >= RIGHT_B) {
return false;
}
return true;
}
// Detector de colisiones entre un punto y un rectangulo
bool checkCollision(const SDL_FPoint &p, const SDL_FRect &r) {
if (p.x < r.x || p.x > r.x + r.w)
auto checkCollision(const SDL_FPoint &p, const SDL_FRect &r) -> bool {
if (p.x < r.x || p.x > r.x + r.w) {
return false;
if (p.y < r.y || p.y > r.y + r.h)
}
if (p.y < r.y || p.y > r.y + r.h) {
return false;
}
return true;
}
// Convierte una cadena en un valor booleano
bool stringToBool(const std::string &str) {
auto stringToBool(const std::string &str) -> bool {
std::string s = trim(toLower(str));
return (s == "true" || s == "1" || s == "yes" || s == "on");
}
// Convierte un valor booleano en una cadena
std::string boolToString(bool value) {
auto boolToString(bool value) -> std::string {
return value ? "true" : "false";
}
// Convierte un valor booleano en una cadena "on" o "off"
std::string boolToOnOff(bool value) {
auto boolToOnOff(bool value) -> std::string {
return value ? Lang::getText("[NOTIFICATIONS] 06") : Lang::getText("[NOTIFICATIONS] 07");
}
// Convierte una cadena a minusculas
std::string toLower(const std::string &str) {
auto toLower(const std::string &str) -> std::string {
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); });
return result;
}
// Dibuja un circulo
void DrawCircle(SDL_Renderer *renderer, int32_t centerX, int32_t centerY, int32_t radius) {
const int32_t diameter = (radius * 2);
void drawCircle(SDL_Renderer *renderer, int32_t center_x, int32_t center_y, int32_t radius) {
const int32_t DIAMETER = (radius * 2);
int32_t x = (radius - 1);
int32_t y = 0;
int32_t tx = 1;
int32_t ty = 1;
int32_t error = (tx - diameter);
int32_t error = (tx - DIAMETER);
while (x >= y) {
// Each of the following renders an octant of the circle
SDL_RenderPoint(renderer, centerX + x, centerY - y);
SDL_RenderPoint(renderer, centerX + x, centerY + y);
SDL_RenderPoint(renderer, centerX - x, centerY - y);
SDL_RenderPoint(renderer, centerX - x, centerY + y);
SDL_RenderPoint(renderer, centerX + y, centerY - x);
SDL_RenderPoint(renderer, centerX + y, centerY + x);
SDL_RenderPoint(renderer, centerX - y, centerY - x);
SDL_RenderPoint(renderer, centerX - y, centerY + x);
SDL_RenderPoint(renderer, center_x + x, center_y - y);
SDL_RenderPoint(renderer, center_x + x, center_y + y);
SDL_RenderPoint(renderer, center_x - x, center_y - y);
SDL_RenderPoint(renderer, center_x - x, center_y + y);
SDL_RenderPoint(renderer, center_x + y, center_y - x);
SDL_RenderPoint(renderer, center_x + y, center_y + x);
SDL_RenderPoint(renderer, center_x - y, center_y - x);
SDL_RenderPoint(renderer, center_x - y, center_y + x);
if (error <= 0) {
++y;
@@ -136,110 +148,135 @@ void DrawCircle(SDL_Renderer *renderer, int32_t centerX, int32_t centerY, int32_
if (error > 0) {
--x;
tx += 2;
error += (tx - diameter);
error += (tx - DIAMETER);
}
}
}
// Aclara el color
Color lightenColor(const Color &color, int amount) {
Color newColor;
newColor.r = std::min(255, color.r + amount);
newColor.g = std::min(255, color.g + amount);
newColor.b = std::min(255, color.b + amount);
return newColor;
auto lightenColor(const Color &color, int amount) -> Color {
Color new_color;
new_color.r = std::min(255, color.r + amount);
new_color.g = std::min(255, color.g + amount);
new_color.b = std::min(255, color.b + amount);
return new_color;
}
// Oscurece el color
Color DarkenColor(const Color &color, int amount) {
Color newColor;
newColor.r = std::min(255, color.r - +amount);
newColor.g = std::min(255, color.g - +amount);
newColor.b = std::min(255, color.b - +amount);
return newColor;
auto darkenColor(const Color &color, int amount) -> Color {
Color new_color;
new_color.r = std::min(255, color.r - +amount);
new_color.g = std::min(255, color.g - +amount);
new_color.b = std::min(255, color.b - +amount);
return new_color;
}
// Quita los espacioes en un string
std::string trim(const std::string &str) {
auto trim(const std::string &str) -> std::string {
auto start = std::find_if_not(str.begin(), str.end(), ::isspace);
auto end = std::find_if_not(str.rbegin(), str.rend(), ::isspace).base();
return (start < end ? std::string(start, end) : std::string());
}
// Función de suavizado
double easeOutQuint(double t) {
return 1 - std::pow(1 - t, 5);
auto easeOutQuint(double time) -> double {
return 1 - std::pow(1 - time, 5);
}
// Función de suavizado
double easeInQuint(double t) {
return pow(t, 5);
auto easeInQuint(double time) -> double {
return pow(time, 5);
}
// Función de suavizado
double easeInOutQuint(double t) {
return t < 0.5 ? 16 * pow(t, 5) : 1 - pow(-2 * t + 2, 5) / 2;
auto easeInOutQuint(double time) -> double {
return time < 0.5 ? 16 * pow(time, 5) : 1 - pow(-2 * time + 2, 5) / 2;
}
// Función de suavizado
double easeInQuad(double t) {
return t * t;
auto easeInQuad(double time) -> double {
return time * time;
}
// Función de suavizado
double easeOutQuad(double t) {
return 1 - (1 - t) * (1 - t);
auto easeOutQuad(double time) -> double {
return 1 - (1 - time) * (1 - time);
}
// Función de suavizado
double easeInOutSine(double t) {
return -0.5 * (std::cos(M_PI * t) - 1);
auto easeInOutSine(double time) -> double {
return -0.5 * (std::cos(M_PI * time) - 1);
}
// Función de suavizado
double easeInOut(double t) {
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
auto easeInOut(double time) -> double {
return time < 0.5 ? 2 * time * time : -1 + (4 - 2 * time) * time;
}
// Función de suavizado
double easeInOutExpo(double t) {
return t == 0 ? 0 : (t == 1 ? 1 : (t < 0.5 ? pow(2, 20 * t - 10) / 2 : (2 - pow(2, -20 * t + 10)) / 2));
// Función de suavizado (easeInOutExpo)
auto easeInOutExpo(double time) -> double {
if (time == 0) {
return 0;
}
if (time == 1) {
return 1;
}
if (time < 0.5) {
return pow(2, 20 * time - 10) / 2;
}
return (2 - pow(2, -20 * time + 10)) / 2;
}
// Función de suavizado (easeInElastic)
double easeInElastic(double t) {
return t == 0 ? 0 : (t == 1 ? 1 : -pow(2, 10 * t - 10) * sin((t * 10 - 10.75) * (2 * M_PI) / 3));
}
// Función de suavizado
double easeOutBounce(double t) {
if (t < 1 / 2.75) {
return 7.5625 * t * t;
} else if (t < 2 / 2.75) {
t -= 1.5 / 2.75;
return 7.5625 * t * t + 0.75;
} else if (t < 2.5 / 2.75) {
t -= 2.25 / 2.75;
return 7.5625 * t * t + 0.9375;
} else {
t -= 2.625 / 2.75;
return 7.5625 * t * t + 0.984375;
auto easeInElastic(double time) -> double {
if (time == 0) {
return 0;
}
if (time == 1) {
return 1;
}
const double C4 = (2 * M_PI) / 3;
return -pow(2, 10 * time - 10) * sin((time * 10 - 10.75) * C4);
}
// Función de suavizado
double easeOutElastic(double t) {
const double c4 = (2 * M_PI) / 3; // Constante para controlar la elasticidad
auto easeOutBounce(double time) -> double {
if (time < 1 / 2.75) {
return 7.5625 * time * time;
}
if (time < 2 / 2.75) {
time -= 1.5 / 2.75;
return 7.5625 * time * time + 0.75;
}
if (time < 2.5 / 2.75) {
time -= 2.25 / 2.75;
return 7.5625 * time * time + 0.9375;
}
time -= 2.625 / 2.75;
return 7.5625 * time * time + 0.984375;
}
return t == 0
? 0
: (t == 1
? 1
: pow(2, -10 * t) * sin((t * 10 - 0.75) * c4) + 1);
// Función de suavizado (easeOutElastic)
auto easeOutElastic(double time) -> double {
if (time == 0) {
return 0;
}
if (time == 1) {
return 1;
}
const double C4 = (2 * M_PI) / 3; // Constante para controlar la elasticidad
return pow(2, -10 * time) * sin((time * 10 - 0.75) * C4) + 1;
}
// Comprueba si una vector contiene una cadena
bool stringInVector(const std::vector<std::string> &vec, const std::string &str) {
auto stringInVector(const std::vector<std::string> &vec, const std::string &str) -> bool {
return std::find(vec.begin(), vec.end(), str) != vec.end();
}
@@ -270,27 +307,26 @@ void printWithDots(const std::string &text1, const std::string &text2, const std
}
// Carga el fichero de datos para la demo
DemoData loadDemoDataFromFile(const std::string &file_path) {
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData {
DemoData dd;
// Indicador de éxito en la carga
auto file = SDL_IOFromFile(file_path.c_str(), "r+b");
if (!file) {
auto *file = SDL_IOFromFile(file_path.c_str(), "r+b");
if (file == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
throw std::runtime_error("Fichero no encontrado: " + file_path);
} else {
printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]");
// Lee todos los datos del fichero y los deja en el destino
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
DemoKeys dk = DemoKeys();
SDL_ReadIO(file, &dk, sizeof(DemoKeys));
dd.push_back(dk);
}
// Cierra el fichero
SDL_CloseIO(file);
}
printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]");
// Lee todos los datos del fichero y los deja en el destino
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
DemoKeys dk = DemoKeys();
SDL_ReadIO(file, &dk, sizeof(DemoKeys));
dd.push_back(dk);
}
// Cierra el fichero
SDL_CloseIO(file);
return dd;
}
@@ -325,50 +361,54 @@ bool saveDemoFile(const std::string &file_path, const DemoData &dd) {
#endif // RECORDING
// Obtiene el nombre de un fichero a partir de una ruta completa
std::string getFileName(const std::string &path) {
auto getFileName(const std::string &path) -> std::string {
return std::filesystem::path(path).filename().string();
}
// Obtiene la ruta eliminando el nombre del fichero
std::string getPath(const std::string &full_path) {
auto getPath(const std::string &full_path) -> std::string {
std::filesystem::path path(full_path);
return path.parent_path().string();
}
constexpr HSV rgbToHsv(Color color) {
float r = color.r / 255.0f;
float g = color.g / 255.0f;
float b = color.b / 255.0f;
constexpr auto rgbToHsv(Color color) -> HSV {
float r = color.r / 255.0F;
float g = color.g / 255.0F;
float b = color.b / 255.0F;
float max = fmaxf(fmaxf(r, g), b);
float min = fminf(fminf(r, g), b);
float delta = max - min;
float h = 0.0f;
if (delta > 0.00001f) {
if (max == r)
h = fmodf((g - b) / delta, 6.0f);
else if (max == g)
h = ((b - r) / delta) + 2.0f;
else
h = ((r - g) / delta) + 4.0f;
h *= 60.0f;
if (h < 0.0f)
h += 360.0f;
float h = 0.0F;
if (delta > 0.00001F) {
if (max == r) {
h = fmodf((g - b) / delta, 6.0F);
} else if (max == g) {
h = ((b - r) / delta) + 2.0F;
} else {
h = ((r - g) / delta) + 4.0F;
}
h *= 60.0F;
if (h < 0.0F) {
h += 360.0F;
}
}
float s = (max <= 0.0f) ? 0.0f : delta / max;
float s = (max <= 0.0F) ? 0.0F : delta / max;
float v = max;
return {h, s, v};
}
constexpr Color hsvToRgb(HSV hsv) {
constexpr auto hsvToRgb(HSV hsv) -> Color {
float c = hsv.v * hsv.s;
float x = c * (1 - std::abs(std::fmod(hsv.h / 60.0f, 2) - 1));
float x = c * (1 - std::abs(std::fmod(hsv.h / 60.0F, 2) - 1));
float m = hsv.v - c;
float r = 0, g = 0, b = 0;
float r = 0;
float g = 0;
float b = 0;
if (hsv.h < 60) {
r = c;
@@ -402,50 +442,50 @@ constexpr Color hsvToRgb(HSV hsv) {
static_cast<uint8_t>(roundf((b + m) * 255)));
}
ColorCycle generateMirroredCycle(Color base, ColorCycleStyle style) {
auto generateMirroredCycle(Color base, ColorCycleStyle style) -> ColorCycle {
ColorCycle result{};
HSV baseHSV = rgbToHsv(base);
HSV base_hsv = rgbToHsv(base);
for (size_t i = 0; i < COLOR_CYCLE_SIZE; ++i) {
float t = static_cast<float>(i) / (COLOR_CYCLE_SIZE - 1); // 0 → 1
float hueShift = 0.0f;
float satShift = 0.0f;
float valShift = 0.0f;
float hue_shift = 0.0F;
float sat_shift = 0.0F;
float val_shift = 0.0F;
switch (style) {
case ColorCycleStyle::SubtlePulse:
case ColorCycleStyle::SUBTLE_PULSE:
// Solo brillo suave
valShift = 0.07f * sinf(t * M_PI);
val_shift = 0.07F * sinf(t * M_PI);
break;
case ColorCycleStyle::HueWave:
case ColorCycleStyle::HUE_WAVE:
// Oscilación leve de tono
hueShift = 15.0f * (t - 0.5f) * 2.0f;
valShift = 0.05f * sinf(t * M_PI);
hue_shift = 15.0F * (t - 0.5F) * 2.0F;
val_shift = 0.05F * sinf(t * M_PI);
break;
case ColorCycleStyle::Vibrant:
case ColorCycleStyle::VIBRANT:
// Cambios fuertes en tono y brillo
hueShift = 35.0f * sinf(t * M_PI);
valShift = 0.2f * sinf(t * M_PI);
satShift = -0.2f * sinf(t * M_PI);
hue_shift = 35.0F * sinf(t * M_PI);
val_shift = 0.2F * sinf(t * M_PI);
sat_shift = -0.2F * sinf(t * M_PI);
break;
case ColorCycleStyle::DarkenGlow:
case ColorCycleStyle::DARKEN_GLOW:
// Se oscurece al centro
valShift = -0.15f * sinf(t * M_PI);
val_shift = -0.15F * sinf(t * M_PI);
break;
case ColorCycleStyle::LightFlash:
case ColorCycleStyle::LIGHT_FLASH:
// Se ilumina al centro
valShift = 0.25f * sinf(t * M_PI);
val_shift = 0.25F * sinf(t * M_PI);
break;
}
HSV adjusted = {
fmodf(baseHSV.h + hueShift + 360.0f, 360.0f),
fminf(1.0f, fmaxf(0.0f, baseHSV.s + satShift)),
fminf(1.0f, fmaxf(0.0f, baseHSV.v + valShift))};
fmodf(base_hsv.h + hue_shift + 360.0F, 360.0F),
fminf(1.0F, fmaxf(0.0F, base_hsv.s + sat_shift)),
fminf(1.0F, fmaxf(0.0F, base_hsv.v + val_shift))};
Color c = hsvToRgb(adjusted);
result[i] = c;