This commit is contained in:
2026-04-17 22:20:37 +02:00
parent 513eacf356
commit 20b9a95619
38 changed files with 310 additions and 622 deletions

View File

@@ -1,8 +1,9 @@
#include "utils/utils.h"
#include <stdlib.h> // for abs, free, malloc
#include <cmath> // for round, abs
#include <algorithm> // for transform
#include <cctype> // for tolower
#include <cmath> // for round, abs
#include <cstdlib> // for abs
// Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2) {
@@ -12,7 +13,7 @@ double distanceSquared(int x1, int y1, int x2, int y2) {
}
// Detector de colisiones entre dos circulos
bool checkCollision(circle_t &a, circle_t &b) {
bool checkCollision(const circle_t &a, const circle_t &b) {
// Calcula el radio total al cuadrado
int totalRadiusSquared = a.r + b.r;
totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;
@@ -28,7 +29,7 @@ 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(const circle_t &a, const SDL_Rect &b) {
// Closest point on collision box
int cX, cY;
@@ -61,7 +62,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(const SDL_Rect &a, const SDL_Rect &b) {
// Calcula las caras del rectangulo a
const int leftA = a.x;
const int rightA = a.x + a.w;
@@ -96,7 +97,7 @@ 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(const SDL_Point &p, const SDL_Rect &r) {
// Comprueba si el punto está a la izquierda del rectangulo
if (p.x < r.x) {
return false;
@@ -122,7 +123,7 @@ 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(const h_line_t &l, const SDL_Rect &r) {
// Comprueba si la linea esta por encima del rectangulo
if (l.y < r.y) {
return false;
@@ -148,7 +149,7 @@ 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(const v_line_t &l, const SDL_Rect &r) {
// Comprueba si la linea esta por la izquierda del rectangulo
if (l.x < r.x) {
return false;
@@ -174,7 +175,7 @@ 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(const h_line_t &l, const SDL_Point &p) {
// Comprueba si el punto esta sobre la linea
if (p.y > l.y) {
return false;
@@ -200,7 +201,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(const line_t &l1, const line_t &l2) {
const float x1 = l1.x1;
const float y1 = l1.y1;
const float x2 = l1.x2;
@@ -227,7 +228,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(const d_line_t &l1, const v_line_t &l2) {
const float x1 = l1.x1;
const float y1 = l1.y1;
const float x2 = l1.x2;
@@ -290,7 +291,7 @@ 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(const SDL_Point &p, const 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)) {
return false;
@@ -330,7 +331,7 @@ 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) {
color_t stringToColor(palette_e pal, const std::string &str) {
if (pal == p_zxspectrum) {
if (str == "black") {
return {0x00, 0x00, 0x00};
@@ -467,7 +468,7 @@ color_t stringToColor(palette_e pal, std::string str) {
}
// Convierte una cadena en un valor booleano
bool stringToBool(std::string str) {
bool stringToBool(const std::string &str) {
if (str == "true") {
return true;
} else {
@@ -485,15 +486,9 @@ std::string boolToString(bool value) {
}
// Convierte una cadena a minusculas
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) {
char c = original[i];
lower[i] = (c >= 65 && c <= 90) ? c + 32 : c;
}
lower[str.size()] = 0;
std::string nova(lower);
free(lower);
return nova;
std::string toLower(const std::string &str) {
std::string result;
result.reserve(str.size());
std::transform(str.begin(), str.end(), std::back_inserter(result), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return result;
}

View File

@@ -86,46 +86,46 @@ struct input_t {
double distanceSquared(int x1, int y1, int x2, int y2);
// Detector de colisiones entre dos circulos
bool checkCollision(circle_t &a, circle_t &b);
bool checkCollision(const circle_t &a, const circle_t &b);
// Detector de colisiones entre un circulo y un rectangulo
bool checkCollision(circle_t &a, SDL_Rect &b);
bool checkCollision(const circle_t &a, const SDL_Rect &b);
// Detector de colisiones entre un dos rectangulos
bool checkCollision(SDL_Rect &a, SDL_Rect &b);
bool checkCollision(const SDL_Rect &a, const SDL_Rect &b);
// Detector de colisiones entre un punto y un rectangulo
bool checkCollision(SDL_Point &p, SDL_Rect &r);
bool checkCollision(const SDL_Point &p, const SDL_Rect &r);
// Detector de colisiones entre una linea horizontal y un rectangulo
bool checkCollision(h_line_t &l, SDL_Rect &r);
bool checkCollision(const h_line_t &l, const SDL_Rect &r);
// Detector de colisiones entre una linea vertical y un rectangulo
bool checkCollision(v_line_t &l, SDL_Rect &r);
bool checkCollision(const v_line_t &l, const SDL_Rect &r);
// Detector de colisiones entre una linea horizontal y un punto
bool checkCollision(h_line_t &l, SDL_Point &p);
bool checkCollision(const h_line_t &l, const SDL_Point &p);
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(line_t &l1, line_t &l2);
SDL_Point checkCollision(const line_t &l1, const line_t &l2);
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(d_line_t &l1, v_line_t &l2);
SDL_Point checkCollision(const d_line_t &l1, const v_line_t &l2);
// Detector de colisiones entre un punto y una linea diagonal
bool checkCollision(SDL_Point &p, d_line_t &l);
bool checkCollision(const SDL_Point &p, const d_line_t &l);
// Normaliza una linea diagonal
void normalizeLine(d_line_t &l);
// Devuelve un color_t a partir de un string
color_t stringToColor(palette_e pal, std::string str);
color_t stringToColor(palette_e pal, const std::string &str);
// Convierte una cadena en un valor booleano
bool stringToBool(std::string str);
bool stringToBool(const std::string &str);
// Convierte un valor booleano en una cadena
std::string boolToString(bool value);
// Convierte una cadena a minusculas
std::string toLower(std::string str);
std::string toLower(const std::string &str);