Mes recomanacions de cppcheck

This commit is contained in:
2024-10-13 19:26:27 +02:00
parent 46540ad7c3
commit babf02226c
22 changed files with 291 additions and 369 deletions

View File

@@ -29,210 +29,112 @@ double distanceSquared(int x1, int y1, int x2, int y2)
}
// Detector de colisiones entre dos circulos
bool checkCollision(Circle &a, Circle &b)
bool checkCollision(const Circle &a, const Circle &b)
{
// Calcula el radio total al cuadrado
int total_radius_squared = a.r + b.r;
total_radius_squared = total_radius_squared * total_radius_squared;
int total_radius_squared = (a.r + b.r) * (a.r + b.r);
// 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) < (total_radius_squared))
{
// Los circulos han colisionado
return true;
}
// En caso contrario
return false;
// Comprueba si la distancia entre los centros de los círculos es inferior a la suma de sus radios
return distanceSquared(a.x, a.y, b.x, b.y) < total_radius_squared;
}
// Detector de colisiones entre un circulo y un rectangulo
bool checkCollision(Circle &a, SDL_Rect &b)
bool checkCollision(const Circle &a, const SDL_Rect &b)
{
// Closest point on collision box
int cX, cY;
// Encuentra el punto más cercano en el rectángulo
int cX = std::clamp(a.x, b.x, b.x + b.w);
int cY = std::clamp(a.y, b.y, b.y + b.h);
// Find closest x offset
if (a.x < b.x)
{
cX = b.x;
}
else if (a.x > b.x + b.w)
{
cX = b.x + b.w;
}
else
{
cX = a.x;
}
// Find closest y offset
if (a.y < b.y)
{
cY = b.y;
}
else if (a.y > b.y + b.h)
{
cY = b.y + b.h;
}
else
{
cY = a.y;
}
// If the closest point is inside the Circle
if (distanceSquared(a.x, a.y, cX, cY) < a.r * a.r)
{
// This box and the Circle have collided
return true;
}
// If the shapes have not collided
return false;
// Si el punto más cercano está dentro del círculo
return distanceSquared(a.x, a.y, cX, cY) < a.r * a.r;
}
// 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;
const int topA = a.y;
const int bottomA = a.y + a.h;
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;
// Calcula las caras del rectangulo b
const int leftB = b.x;
const int rightB = b.x + b.w;
const int topB = b.y;
const int bottomB = b.y + b.h;
// Si cualquiera de las caras de a está fuera de b
if (bottomA <= topB)
{
return false;
}
if (topA >= bottomB)
{
return false;
}
if (rightA <= leftB)
{
return false;
}
if (leftA >= rightB)
{
return false;
}
// Si ninguna de las caras está fuera de b
return true;
}
// 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)
{
if (p.x < r.x || p.x > r.x + r.w)
return false;
}
// Comprueba si el punto está a la derecha del rectangulo
if (p.x > r.x + r.w)
{
if (p.y < r.y || p.y > r.y + r.h)
return false;
}
// Comprueba si el punto está por encima del rectangulo
if (p.y < r.y)
{
return false;
}
// Comprueba si el punto está por debajo del rectangulo
if (p.y > r.y + r.h)
{
return false;
}
// Si no está fuera, es que está dentro
return true;
}
// Convierte una cadena en un valor booleano
bool stringToBool(std::string str)
bool stringToBool(const std::string &str)
{
return str == "true" ? true : false;
return str == "true";
}
// Convierte un valor booleano en una cadena
std::string boolToString(bool value)
{
return value == true ? "true" : "false";
return value ? "true" : "false";
}
// Convierte un valor booleano en una cadena "on" o "off"
std::string boolToOnOff(bool value)
{
return value == true ? "on" : "off";
return value ? "on" : "off";
}
// Convierte una cadena a minusculas
std::string toLower(std::string str)
std::string toLower(const 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 result = str;
std::transform(result.begin(), result.end(), result.begin(),
[](unsigned char c)
{ return std::tolower(c); });
return result;
}
// Obtiene el fichero de sonido a partir de un nombre
JA_Sound_t *getSound(std::vector<SoundFile> sounds, std::string name)
JA_Sound_t *getSound(const std::vector<SoundFile> &sounds, const std::string &name)
{
for (auto s : sounds)
for (const auto &s : sounds)
{
if (s.name == name)
{
return s.file;
}
}
return nullptr;
}
// Obtiene el fichero de música a partir de un nombre
JA_Music_t *getMusic(std::vector<MusicFile> music, std::string name)
JA_Music_t *getMusic(const std::vector<MusicFile> &music, const std::string &name)
{
for (auto m : music)
for (const auto &m : music)
{
if (m.name == name)
{
return m.file;
}
}
return nullptr;
}
// Ordena las entradas de la tabla de records
HiScoreEntry sortHiScoreTable(HiScoreEntry entry1, HiScoreEntry entry2)
HiScoreEntry sortHiScoreTable(const HiScoreEntry &entry1, const HiScoreEntry &entry2)
{
if (entry1.score > entry2.score)
{
return entry1;
}
return entry2;
return (entry1.score > entry2.score) ? entry1 : entry2;
}
// Dibuja un circulo
@@ -275,41 +177,31 @@ void DrawCircle(SDL_Renderer *renderer, int32_t centerX, int32_t centerY, int32_
}
// Aclara el color
Color lightenColor(Color color, int amount)
Color lightenColor(const Color &color, int amount)
{
Color newColor;
newColor.r = std::min(255, (int)color.r + amount);
newColor.g = std::min(255, (int)color.g + amount);
newColor.b = std::min(255, (int)color.b + amount);
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;
}
// Oscurece el color
Color DarkenColor(Color color, int amount)
Color DarkenColor(const Color &color, int amount)
{
Color newColor;
newColor.r = std::max(0, (int)color.r - amount);
newColor.g = std::max(0, (int)color.g - amount);
newColor.b = std::max(0, (int)color.b - amount);
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;
}
// Quita los espacioes en un string
std::string trim(const std::string &str)
{
auto start = str.begin();
while (start != str.end() && std::isspace(*start))
{
start++;
}
auto end = str.end();
do
{
end--;
} while (std::distance(start, end) > 0 && std::isspace(*end));
return std::string(start, end + 1);
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