clang-format

This commit is contained in:
2026-04-03 10:58:04 +02:00
parent 550a005ca6
commit c25d4dc7e5
50 changed files with 9735 additions and 11503 deletions

View File

@@ -1,25 +1,24 @@
#include "utils.h"
#include <stdlib.h> // for abs, free, malloc
#include <cmath> // for round, abs
#include <cmath> // for round, abs
// Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2)
{
double distanceSquared(int x1, int y1, int x2, int y2) {
const int deltaX = x2 - x1;
const int deltaY = y2 - y1;
return deltaX * deltaX + deltaY * deltaY;
}
// Detector de colisiones entre dos circulos
bool checkCollision(circle_t &a, circle_t &b)
{
bool checkCollision(circle_t &a, circle_t &b) {
// Calcula el radio total al cuadrado
int totalRadiusSquared = a.r + b.r;
totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;
// Si la distancia entre el centro de los circulos es inferior a la suma de sus radios
if (distanceSquared(a.x, a.y, b.x, b.y) < (totalRadiusSquared))
{
if (distanceSquared(a.x, a.y, b.x, b.y) < (totalRadiusSquared)) {
// Los circulos han colisionado
return true;
}
@@ -29,42 +28,30 @@ bool checkCollision(circle_t &a, circle_t &b)
}
// Detector de colisiones entre un circulo y un rectangulo
bool checkCollision(circle_t &a, SDL_Rect &b)
{
bool checkCollision(circle_t &a, SDL_Rect &b) {
// Closest point on collision box
int cX, cY;
// Find closest x offset
if (a.x < b.x)
{
if (a.x < b.x) {
cX = b.x;
}
else if (a.x > b.x + b.w)
{
} else if (a.x > b.x + b.w) {
cX = b.x + b.w;
}
else
{
} else {
cX = a.x;
}
// Find closest y offset
if (a.y < b.y)
{
if (a.y < b.y) {
cY = b.y;
}
else if (a.y > b.y + b.h)
{
} else if (a.y > b.y + b.h) {
cY = b.y + b.h;
}
else
{
} else {
cY = a.y;
}
// If the closest point is inside the circle_t
if (distanceSquared(a.x, a.y, cX, cY) < a.r * a.r)
{
if (distanceSquared(a.x, a.y, cX, cY) < a.r * a.r) {
// This box and the circle_t have collided
return true;
}
@@ -74,8 +61,7 @@ bool checkCollision(circle_t &a, SDL_Rect &b)
}
// Detector de colisiones entre dos rectangulos
bool checkCollision(SDL_Rect &a, SDL_Rect &b)
{
bool checkCollision(SDL_Rect &a, SDL_Rect &b) {
// Calcula las caras del rectangulo a
const int leftA = a.x;
const int rightA = a.x + a.w;
@@ -89,23 +75,19 @@ bool checkCollision(SDL_Rect &a, SDL_Rect &b)
const int bottomB = b.y + b.h;
// Si cualquiera de las caras de a está fuera de b
if (bottomA <= topB)
{
if (bottomA <= topB) {
return false;
}
if (topA >= bottomB)
{
if (topA >= bottomB) {
return false;
}
if (rightA <= leftB)
{
if (rightA <= leftB) {
return false;
}
if (leftA >= rightB)
{
if (leftA >= rightB) {
return false;
}
@@ -114,29 +96,24 @@ bool checkCollision(SDL_Rect &a, SDL_Rect &b)
}
// Detector de colisiones entre un punto y un rectangulo
bool checkCollision(SDL_Point &p, SDL_Rect &r)
{
bool checkCollision(SDL_Point &p, SDL_Rect &r) {
// Comprueba si el punto está a la izquierda del rectangulo
if (p.x < r.x)
{
if (p.x < r.x) {
return false;
}
// Comprueba si el punto está a la derecha del rectangulo
if (p.x > r.x + r.w)
{
if (p.x > r.x + r.w) {
return false;
}
// Comprueba si el punto está por encima del rectangulo
if (p.y < r.y)
{
if (p.y < r.y) {
return false;
}
// Comprueba si el punto está por debajo del rectangulo
if (p.y > r.y + r.h)
{
if (p.y > r.y + r.h) {
return false;
}
@@ -145,29 +122,24 @@ bool checkCollision(SDL_Point &p, SDL_Rect &r)
}
// Detector de colisiones entre una linea horizontal y un rectangulo
bool checkCollision(h_line_t &l, SDL_Rect &r)
{
bool checkCollision(h_line_t &l, SDL_Rect &r) {
// Comprueba si la linea esta por encima del rectangulo
if (l.y < r.y)
{
if (l.y < r.y) {
return false;
}
// Comprueba si la linea esta por debajo del rectangulo
if (l.y >= r.y + r.h)
{
if (l.y >= r.y + r.h) {
return false;
}
// Comprueba si el inicio de la linea esta a la derecha del rectangulo
if (l.x1 >= r.x + r.w)
{
if (l.x1 >= r.x + r.w) {
return false;
}
// Comprueba si el final de la linea esta a la izquierda del rectangulo
if (l.x2 < r.x)
{
if (l.x2 < r.x) {
return false;
}
@@ -176,29 +148,24 @@ bool checkCollision(h_line_t &l, SDL_Rect &r)
}
// Detector de colisiones entre una linea vertical y un rectangulo
bool checkCollision(v_line_t &l, SDL_Rect &r)
{
bool checkCollision(v_line_t &l, SDL_Rect &r) {
// Comprueba si la linea esta por la izquierda del rectangulo
if (l.x < r.x)
{
if (l.x < r.x) {
return false;
}
// Comprueba si la linea esta por la derecha del rectangulo
if (l.x >= r.x + r.w)
{
if (l.x >= r.x + r.w) {
return false;
}
// Comprueba si el inicio de la linea esta debajo del rectangulo
if (l.y1 >= r.y + r.h)
{
if (l.y1 >= r.y + r.h) {
return false;
}
// Comprueba si el final de la linea esta encima del rectangulo
if (l.y2 < r.y)
{
if (l.y2 < r.y) {
return false;
}
@@ -207,29 +174,24 @@ bool checkCollision(v_line_t &l, SDL_Rect &r)
}
// Detector de colisiones entre una linea horizontal y un punto
bool checkCollision(h_line_t &l, SDL_Point &p)
{
bool checkCollision(h_line_t &l, SDL_Point &p) {
// Comprueba si el punto esta sobre la linea
if (p.y > l.y)
{
if (p.y > l.y) {
return false;
}
// Comprueba si el punto esta bajo la linea
if (p.y < l.y)
{
if (p.y < l.y) {
return false;
}
// Comprueba si el punto esta a la izquierda de la linea
if (p.x < l.x1)
{
if (p.x < l.x1) {
return false;
}
// Comprueba si el punto esta a la derecha de la linea
if (p.x > l.x2)
{
if (p.x > l.x2) {
return false;
}
@@ -238,8 +200,7 @@ bool checkCollision(h_line_t &l, SDL_Point &p)
}
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(line_t &l1, line_t &l2)
{
SDL_Point checkCollision(line_t &l1, line_t &l2) {
const float x1 = l1.x1;
const float y1 = l1.y1;
const float x2 = l1.x2;
@@ -255,8 +216,7 @@ SDL_Point checkCollision(line_t &l1, line_t &l2)
float uB = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
// if uA and uB are between 0-1, lines are colliding
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1)
{
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
// Calcula la intersección
const float x = x1 + (uA * (x2 - x1));
const float y = y1 + (uA * (y2 - y1));
@@ -267,8 +227,7 @@ SDL_Point checkCollision(line_t &l1, line_t &l2)
}
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(d_line_t &l1, v_line_t &l2)
{
SDL_Point checkCollision(d_line_t &l1, v_line_t &l2) {
const float x1 = l1.x1;
const float y1 = l1.y1;
const float x2 = l1.x2;
@@ -284,8 +243,7 @@ SDL_Point checkCollision(d_line_t &l1, v_line_t &l2)
float uB = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
// if uA and uB are between 0-1, lines are colliding
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1)
{
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
// Calcula la intersección
const float x = x1 + (uA * (x2 - x1));
const float y = y1 + (uA * (y2 - y1));
@@ -318,12 +276,10 @@ SDL_Point checkCollision(d_line_t &l1, v_line_t &l2)
}*/
// Normaliza una linea diagonal
void normalizeLine(d_line_t &l)
{
void normalizeLine(d_line_t &l) {
// Las lineas diagonales van de izquierda a derecha
// x2 mayor que x1
if (l.x2 < l.x1)
{
if (l.x2 < l.x1) {
const int x = l.x1;
const int y = l.y1;
l.x1 = l.x2;
@@ -334,35 +290,29 @@ void normalizeLine(d_line_t &l)
}
// Detector de colisiones entre un punto y una linea diagonal
bool checkCollision(SDL_Point &p, d_line_t &l)
{
bool checkCollision(SDL_Point &p, d_line_t &l) {
// Comprueba si el punto está en alineado con la linea
if (abs(p.x - l.x1) != abs(p.y - l.y1))
{
if (abs(p.x - l.x1) != abs(p.y - l.y1)) {
return false;
}
// Comprueba si está a la derecha de la linea
if (p.x > l.x1 && p.x > l.x2)
{
if (p.x > l.x1 && p.x > l.x2) {
return false;
}
// Comprueba si está a la izquierda de la linea
if (p.x < l.x1 && p.x < l.x2)
{
if (p.x < l.x1 && p.x < l.x2) {
return false;
}
// Comprueba si está por encima de la linea
if (p.y > l.y1 && p.y > l.y2)
{
if (p.y > l.y1 && p.y > l.y2) {
return false;
}
// Comprueba si está por debajo de la linea
if (p.y < l.y1 && p.y < l.y2)
{
if (p.y < l.y1 && p.y < l.y2) {
return false;
}
@@ -380,170 +330,135 @@ bool checkCollision(SDL_Point &p, d_line_t &l)
}
// Devuelve un color_t a partir de un string
color_t stringToColor(palette_e pal, std::string str)
{
if (pal == p_zxspectrum)
{
if (str == "black")
{
color_t stringToColor(palette_e pal, std::string str) {
if (pal == p_zxspectrum) {
if (str == "black") {
return {0x00, 0x00, 0x00};
}
else if (str == "bright_black")
{
else if (str == "bright_black") {
return {0x00, 0x00, 0x00};
}
else if (str == "blue")
{
else if (str == "blue") {
return {0x00, 0x00, 0xd8};
}
else if (str == "bright_blue")
{
else if (str == "bright_blue") {
return {0x00, 0x00, 0xFF};
}
else if (str == "red")
{
else if (str == "red") {
return {0xd8, 0x00, 0x00};
}
else if (str == "bright_red")
{
else if (str == "bright_red") {
return {0xFF, 0x00, 0x00};
}
else if (str == "magenta")
{
else if (str == "magenta") {
return {0xd8, 0x00, 0xd8};
}
else if (str == "bright_magenta")
{
else if (str == "bright_magenta") {
return {0xFF, 0x00, 0xFF};
}
else if (str == "green")
{
else if (str == "green") {
return {0x00, 0xd8, 0x00};
}
else if (str == "bright_green")
{
else if (str == "bright_green") {
return {0x00, 0xFF, 0x00};
}
else if (str == "cyan")
{
else if (str == "cyan") {
return {0x00, 0xd8, 0xd8};
}
else if (str == "bright_cyan")
{
else if (str == "bright_cyan") {
return {0x00, 0xFF, 0xFF};
}
else if (str == "yellow")
{
else if (str == "yellow") {
return {0xd8, 0xd8, 0x00};
}
else if (str == "bright_yellow")
{
else if (str == "bright_yellow") {
return {0xFF, 0xFF, 0x00};
}
else if (str == "white")
{
else if (str == "white") {
return {0xd8, 0xd8, 0xd8};
}
else if (str == "bright_white")
{
else if (str == "bright_white") {
return {0xFF, 0xFF, 0xFF};
}
}
else if (pal == p_zxarne)
{ // zxarne
if (str == "black")
{
else if (pal == p_zxarne) { // zxarne
if (str == "black") {
return {0x00, 0x00, 0x00};
}
else if (str == "bright_black")
{
else if (str == "bright_black") {
return {0x3C, 0x35, 0x1F};
}
else if (str == "blue")
{
else if (str == "blue") {
return {0x31, 0x33, 0x90};
}
else if (str == "bright_blue")
{
else if (str == "bright_blue") {
return {0x15, 0x59, 0xDB};
}
else if (str == "red")
{
else if (str == "red") {
return {0xA7, 0x32, 0x11};
}
else if (str == "bright_red")
{
else if (str == "bright_red") {
return {0xD8, 0x55, 0x25};
}
else if (str == "magenta")
{
else if (str == "magenta") {
return {0xA1, 0x55, 0x89};
}
else if (str == "bright_magenta")
{
else if (str == "bright_magenta") {
return {0xCD, 0x7A, 0x50};
}
else if (str == "green")
{
else if (str == "green") {
return {0x62, 0x9A, 0x31};
}
else if (str == "bright_green")
{
else if (str == "bright_green") {
return {0x9C, 0xD3, 0x3C};
}
else if (str == "cyan")
{
else if (str == "cyan") {
return {0x28, 0xA4, 0xCB};
}
else if (str == "bright_cyan")
{
else if (str == "bright_cyan") {
return {0x65, 0xDC, 0xD6};
}
else if (str == "yellow")
{
else if (str == "yellow") {
return {0xE8, 0xBC, 0x50};
}
else if (str == "bright_yellow")
{
else if (str == "bright_yellow") {
return {0xF1, 0xE7, 0x82};
}
else if (str == "white")
{
else if (str == "white") {
return {0xBF, 0xBF, 0xBD};
}
else if (str == "bright_white")
{
else if (str == "bright_white") {
return {0xF2, 0xF1, 0xED};
}
}
@@ -552,38 +467,28 @@ color_t stringToColor(palette_e pal, std::string str)
}
// Convierte una cadena en un valor booleano
bool stringToBool(std::string str)
{
if (str == "true")
{
bool stringToBool(std::string str) {
if (str == "true") {
return true;
}
else
{
} else {
return false;
}
}
// Convierte un valor booleano en una cadena
std::string boolToString(bool value)
{
if (value)
{
std::string boolToString(bool value) {
if (value) {
return "true";
}
else
{
} else {
return "false";
}
}
// Convierte una cadena a minusculas
std::string toLower(std::string str)
{
std::string toLower(std::string str) {
const char *original = str.c_str();
char *lower = (char *)malloc(str.size() + 1);
for (int i = 0; i < (int)str.size(); ++i)
{
for (int i = 0; i < (int)str.size(); ++i) {
char c = original[i];
lower[i] = (c >= 65 && c <= 90) ? c + 32 : c;
}