This commit is contained in:
2025-10-27 18:35:53 +01:00
parent b1dca32a5b
commit 3179a08dac
63 changed files with 686 additions and 693 deletions

View File

@@ -1,10 +1,9 @@
#include "utils/utils.hpp"
#include <stdlib.h> // Para abs
#include <algorithm> // Para find, transform
#include <cctype> // Para tolower
#include <cmath> // Para round, abs
#include <cstdlib> // Para abs
#include <exception> // Para exception
#include <filesystem> // Para path
#include <iostream> // Para basic_ostream, cout, basic_ios, ios, endl
@@ -16,14 +15,14 @@
#include "external/jail_audio.h" // Para JA_GetMusicState, JA_Music_state, JA_PlayMusic
// Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2) {
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;
total_radius_squared = total_radius_squared * total_radius_squared;
@@ -33,7 +32,7 @@ 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& rect) {
auto checkCollision(const Circle& a, const SDL_FRect& rect) -> bool {
SDL_Rect b = toSDLRect(rect);
// Closest point on collision box
int c_x;
@@ -68,7 +67,7 @@ bool checkCollision(const Circle& a, const SDL_FRect& rect) {
}
// Detector de colisiones entre dos rectangulos
bool checkCollision(const SDL_FRect& rect_a, const SDL_FRect& rect_b) {
auto checkCollision(const SDL_FRect& rect_a, const SDL_FRect& rect_b) -> bool {
SDL_Rect a = toSDLRect(rect_a);
SDL_Rect b = toSDLRect(rect_b);
// Calcula las caras del rectangulo a
@@ -105,7 +104,7 @@ bool checkCollision(const SDL_FRect& rect_a, const SDL_FRect& rect_b) {
}
// Detector de colisiones entre un punto y un rectangulo
bool checkCollision(const SDL_FPoint& point, const SDL_FRect& rect) {
auto checkCollision(const SDL_FPoint& point, const SDL_FRect& rect) -> bool {
SDL_Rect r = toSDLRect(rect);
SDL_Point p = toSDLPoint(point);
@@ -134,7 +133,7 @@ bool checkCollision(const SDL_FPoint& point, const SDL_FRect& rect) {
}
// Detector de colisiones entre una linea horizontal y un rectangulo
bool checkCollision(const LineHorizontal& l, const SDL_FRect& rect) {
auto checkCollision(const LineHorizontal& l, const SDL_FRect& rect) -> bool {
SDL_Rect r = toSDLRect(rect);
// Comprueba si la linea esta por encima del rectangulo
if (l.y < r.y) {
@@ -161,7 +160,7 @@ bool checkCollision(const LineHorizontal& l, const SDL_FRect& rect) {
}
// Detector de colisiones entre una linea vertical y un rectangulo
bool checkCollision(const LineVertical& l, const SDL_FRect& rect) {
auto checkCollision(const LineVertical& l, const SDL_FRect& rect) -> bool {
SDL_Rect r = toSDLRect(rect);
// Comprueba si la linea esta por la izquierda del rectangulo
if (l.x < r.x) {
@@ -188,7 +187,7 @@ bool checkCollision(const LineVertical& l, const SDL_FRect& rect) {
}
// Detector de colisiones entre una linea horizontal y un punto
bool checkCollision(const LineHorizontal& l, const SDL_FPoint& point) {
auto checkCollision(const LineHorizontal& l, const SDL_FPoint& point) -> bool {
SDL_Point p = toSDLPoint(point);
// Comprueba si el punto esta sobre la linea
@@ -216,7 +215,7 @@ bool checkCollision(const LineHorizontal& l, const SDL_FPoint& point) {
}
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(const Line& l1, const Line& l2) {
auto checkCollision(const Line& l1, const Line& l2) -> SDL_Point {
const float X1 = l1.x1;
const float Y1 = l1.y1;
const float X2 = l1.x2;
@@ -237,13 +236,13 @@ SDL_Point checkCollision(const Line& l1, const Line& l2) {
const float X = X1 + (u_a * (X2 - X1));
const float Y = Y1 + (u_a * (Y2 - Y1));
return {static_cast<int>(round(X)), static_cast<int>(round(Y))};
return {static_cast<int>(std::round(X)), static_cast<int>(std::round(Y))};
}
return {-1, -1};
}
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(const LineDiagonal& l1, const LineVertical& l2) {
auto checkCollision(const LineDiagonal& l1, const LineVertical& l2) -> SDL_Point {
const float X1 = l1.x1;
const float Y1 = l1.y1;
const float X2 = l1.x2;
@@ -284,7 +283,7 @@ void normalizeLine(LineDiagonal& l) {
}
// Detector de colisiones entre un punto y una linea diagonal
bool checkCollision(const SDL_FPoint& point, const LineDiagonal& l) {
auto checkCollision(const SDL_FPoint& point, const LineDiagonal& l) -> bool {
SDL_Point p = toSDLPoint(point);
// Comprueba si el punto está en alineado con la linea
@@ -317,7 +316,7 @@ bool checkCollision(const SDL_FPoint& point, const LineDiagonal& l) {
}
// Convierte una cadena a un indice de la paleta
Uint8 stringToColor(const std::string& str) {
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},
@@ -355,7 +354,7 @@ Uint8 stringToColor(const std::string& str) {
}
// Convierte una cadena a un entero de forma segura
int safeStoi(const std::string& value, int default_value) {
auto safeStoi(const std::string& value, int default_value) -> int {
try {
return std::stoi(value);
} catch (const std::exception&) {
@@ -364,19 +363,19 @@ int safeStoi(const std::string& value, int default_value) {
}
// Convierte una cadena a un booleano
bool stringToBool(const std::string& str) {
auto stringToBool(const std::string& str) -> bool {
std::string lower_str = str;
std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(), ::tolower);
std::ranges::transform(lower_str, lower_str.begin(), ::tolower);
return (lower_str == "true" || lower_str == "1" || lower_str == "yes" || lower_str == "on");
}
// Convierte un booleano a una cadena
std::string boolToString(bool value) {
auto boolToString(bool value) -> std::string {
return value ? "1" : "0";
}
// Compara dos colores
bool colorAreEqual(Color color1, Color color2) {
auto colorAreEqual(Color color1, Color color2) -> bool {
const bool R = color1.r == color2.r;
const bool G = color1.g == color2.g;
const bool B = color1.b == color2.b;
@@ -385,26 +384,26 @@ bool colorAreEqual(Color color1, Color color2) {
}
// Función para convertir un string a minúsculas
std::string toLower(const std::string& str) {
auto toLower(const std::string& str) -> std::string {
std::string lower_str = str;
std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(), ::tolower);
std::ranges::transform(lower_str, lower_str.begin(), ::tolower);
return lower_str;
}
// Función para convertir un string a mayúsculas
std::string toUpper(const std::string& str) {
auto toUpper(const std::string& str) -> std::string {
std::string upper_str = str;
std::transform(upper_str.begin(), upper_str.end(), upper_str.begin(), ::toupper);
std::ranges::transform(upper_str, upper_str.begin(), ::toupper);
return upper_str;
}
// 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();
}
@@ -418,12 +417,12 @@ void printWithDots(const std::string& text1, const std::string& text2, const std
std::cout.fill('.');
std::cout << text2;
std::cout << text3 << std::endl;
std::cout << text3 << '\n';
}
// Comprueba si una vector contiene una cadena
bool stringInVector(const std::vector<std::string>& vec, const std::string& str) {
return std::find(vec.begin(), vec.end(), str) != vec.end();
auto stringInVector(const std::vector<std::string>& vec, const std::string& str) -> bool {
return std::ranges::find(vec, str) != vec.end();
}
// Hace sonar la música