Redistribuits els .cpp en carpetes
Actualitzat cmake Modificats els include de SDL2 a SDL3
This commit is contained in:
229
source/utils.cpp
229
source/utils.cpp
@@ -1,35 +1,35 @@
|
||||
#include "utils.h"
|
||||
#include <stdlib.h> // Para abs
|
||||
#include <algorithm> // Para find, transform
|
||||
#include <cctype> // Para tolower
|
||||
#include <cmath> // Para round, abs
|
||||
#include <exception> // Para exception
|
||||
#include <filesystem> // Para path
|
||||
#include <iostream> // Para basic_ostream, cout, basic_ios, ios, endl
|
||||
#include <string> // Para basic_string, string, char_traits, allocator
|
||||
#include <unordered_map> // Para unordered_map, operator==, _Node_const_iter...
|
||||
#include <utility> // Para pair
|
||||
|
||||
#include <stdlib.h> // Para abs
|
||||
|
||||
#include <algorithm> // Para find, transform
|
||||
#include <cctype> // Para tolower
|
||||
#include <cmath> // Para round, abs
|
||||
#include <exception> // Para exception
|
||||
#include <filesystem> // Para path
|
||||
#include <iostream> // Para basic_ostream, cout, basic_ios, ios, endl
|
||||
#include <string> // Para basic_string, string, char_traits, allocator
|
||||
#include <unordered_map> // Para unordered_map, operator==, _Node_const_iter...
|
||||
#include <utility> // Para pair
|
||||
|
||||
#include "jail_audio.h" // Para JA_GetMusicState, JA_Music_state, JA_PlayMusic
|
||||
#include "resource.h" // Para Resource
|
||||
|
||||
// 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(const Circle &a, const Circle &b)
|
||||
{
|
||||
bool checkCollision(const Circle& a, const Circle& 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;
|
||||
}
|
||||
@@ -39,42 +39,30 @@ bool checkCollision(const Circle &a, const Circle &b)
|
||||
}
|
||||
|
||||
// Detector de colisiones entre un circulo y un rectangulo
|
||||
bool checkCollision(const Circle &a, const SDL_Rect &b)
|
||||
{
|
||||
bool checkCollision(const Circle& a, const 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;
|
||||
}
|
||||
@@ -84,8 +72,7 @@ bool checkCollision(const Circle &a, const SDL_Rect &b)
|
||||
}
|
||||
|
||||
// Detector de colisiones entre dos rectangulos
|
||||
bool checkCollision(const SDL_Rect &a, const 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;
|
||||
@@ -99,23 +86,19 @@ bool checkCollision(const SDL_Rect &a, const 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;
|
||||
}
|
||||
|
||||
@@ -124,29 +107,24 @@ bool checkCollision(const SDL_Rect &a, const SDL_Rect &b)
|
||||
}
|
||||
|
||||
// Detector de colisiones entre un punto y un rectangulo
|
||||
bool checkCollision(const SDL_Point &p, const 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) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -155,29 +133,24 @@ bool checkCollision(const SDL_Point &p, const SDL_Rect &r)
|
||||
}
|
||||
|
||||
// Detector de colisiones entre una linea horizontal y un rectangulo
|
||||
bool checkCollision(const LineHorizontal &l, const SDL_Rect &r)
|
||||
{
|
||||
bool checkCollision(const LineHorizontal& l, const 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;
|
||||
}
|
||||
|
||||
@@ -186,29 +159,24 @@ bool checkCollision(const LineHorizontal &l, const SDL_Rect &r)
|
||||
}
|
||||
|
||||
// Detector de colisiones entre una linea vertical y un rectangulo
|
||||
bool checkCollision(const LineVertical &l, const SDL_Rect &r)
|
||||
{
|
||||
bool checkCollision(const LineVertical& l, const 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;
|
||||
}
|
||||
|
||||
@@ -217,29 +185,24 @@ bool checkCollision(const LineVertical &l, const SDL_Rect &r)
|
||||
}
|
||||
|
||||
// Detector de colisiones entre una linea horizontal y un punto
|
||||
bool checkCollision(const LineHorizontal &l, const SDL_Point &p)
|
||||
{
|
||||
bool checkCollision(const LineHorizontal& l, const 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;
|
||||
}
|
||||
|
||||
@@ -248,8 +211,7 @@ bool checkCollision(const LineHorizontal &l, const SDL_Point &p)
|
||||
}
|
||||
|
||||
// Detector de colisiones entre dos lineas
|
||||
SDL_Point checkCollision(const Line &l1, const Line &l2)
|
||||
{
|
||||
SDL_Point checkCollision(const Line& l1, const Line& l2) {
|
||||
const float x1 = l1.x1;
|
||||
const float y1 = l1.y1;
|
||||
const float x2 = l1.x2;
|
||||
@@ -265,8 +227,7 @@ SDL_Point checkCollision(const Line &l1, const Line &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));
|
||||
@@ -277,8 +238,7 @@ SDL_Point checkCollision(const Line &l1, const Line &l2)
|
||||
}
|
||||
|
||||
// Detector de colisiones entre dos lineas
|
||||
SDL_Point checkCollision(const LineDiagonal &l1, const LineVertical &l2)
|
||||
{
|
||||
SDL_Point checkCollision(const LineDiagonal& l1, const LineVertical& l2) {
|
||||
const float x1 = l1.x1;
|
||||
const float y1 = l1.y1;
|
||||
const float x2 = l1.x2;
|
||||
@@ -294,8 +254,7 @@ SDL_Point checkCollision(const LineDiagonal &l1, const LineVertical &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));
|
||||
@@ -306,12 +265,10 @@ SDL_Point checkCollision(const LineDiagonal &l1, const LineVertical &l2)
|
||||
}
|
||||
|
||||
// Normaliza una linea diagonal
|
||||
void normalizeLine(LineDiagonal &l)
|
||||
{
|
||||
void normalizeLine(LineDiagonal& 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;
|
||||
@@ -322,35 +279,29 @@ void normalizeLine(LineDiagonal &l)
|
||||
}
|
||||
|
||||
// Detector de colisiones entre un punto y una linea diagonal
|
||||
bool checkCollision(const SDL_Point &p, const LineDiagonal &l)
|
||||
{
|
||||
bool checkCollision(const SDL_Point& p, const LineDiagonal& 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;
|
||||
}
|
||||
|
||||
@@ -359,8 +310,7 @@ bool checkCollision(const SDL_Point &p, const LineDiagonal &l)
|
||||
}
|
||||
|
||||
// Convierte una cadena a un indice de la paleta
|
||||
Uint8 stringToColor(const std::string &str)
|
||||
{
|
||||
Uint8 stringToColor(const std::string& str) {
|
||||
// Mapas de colores para cada paleta
|
||||
static const std::unordered_map<std::string, Uint8> paletteMap = {
|
||||
{"black", 0},
|
||||
@@ -391,47 +341,37 @@ Uint8 stringToColor(const std::string &str)
|
||||
|
||||
// Busca el color en el mapa
|
||||
auto it = paletteMap.find(str);
|
||||
if (it != paletteMap.end())
|
||||
{
|
||||
if (it != paletteMap.end()) {
|
||||
return it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Si no se encuentra el color, devolvemos negro por defecto
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Convierte una cadena a un entero de forma segura
|
||||
int safeStoi(const std::string &value, int defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
int safeStoi(const std::string& value, int defaultValue) {
|
||||
try {
|
||||
return std::stoi(value);
|
||||
}
|
||||
catch (const std::exception &)
|
||||
{
|
||||
} catch (const std::exception&) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Convierte una cadena a un booleano
|
||||
bool stringToBool(const std::string &str)
|
||||
{
|
||||
bool stringToBool(const std::string& str) {
|
||||
std::string lowerStr = str;
|
||||
std::transform(lowerStr.begin(), lowerStr.end(), lowerStr.begin(), ::tolower);
|
||||
return (lowerStr == "true" || lowerStr == "1" || lowerStr == "yes" || lowerStr == "on");
|
||||
}
|
||||
|
||||
// Convierte un booleano a una cadena
|
||||
std::string boolToString(bool value)
|
||||
{
|
||||
std::string boolToString(bool value) {
|
||||
return value ? "1" : "0";
|
||||
}
|
||||
|
||||
// Compara dos colores
|
||||
bool colorAreEqual(Color color1, Color color2)
|
||||
{
|
||||
bool colorAreEqual(Color color1, Color color2) {
|
||||
const bool r = color1.r == color2.r;
|
||||
const bool g = color1.g == color2.g;
|
||||
const bool b = color1.b == color2.b;
|
||||
@@ -440,37 +380,32 @@ bool colorAreEqual(Color color1, Color color2)
|
||||
}
|
||||
|
||||
// Función para convertir un string a minúsculas
|
||||
std::string toLower(const std::string &str)
|
||||
{
|
||||
std::string toLower(const std::string& str) {
|
||||
std::string lower_str = str;
|
||||
std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(), ::tolower);
|
||||
return lower_str;
|
||||
}
|
||||
|
||||
// Función para convertir un string a mayúsculas
|
||||
std::string toUpper(const std::string &str)
|
||||
{
|
||||
std::string toUpper(const std::string& str) {
|
||||
std::string upper_str = str;
|
||||
std::transform(upper_str.begin(), upper_str.end(), 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)
|
||||
{
|
||||
std::string getFileName(const std::string& path) {
|
||||
return std::filesystem::path(path).filename().string();
|
||||
}
|
||||
|
||||
// Obtiene la ruta eliminando el nombre del fichero
|
||||
std::string getPath(const std::string &full_path)
|
||||
{
|
||||
std::string getPath(const std::string& full_path) {
|
||||
std::filesystem::path path(full_path);
|
||||
return path.parent_path().string();
|
||||
}
|
||||
|
||||
// Imprime por pantalla una linea de texto de tamaño fijo rellena con puntos
|
||||
void printWithDots(const std::string &text1, const std::string &text2, const std::string &text3)
|
||||
{
|
||||
void printWithDots(const std::string& text1, const std::string& text2, const std::string& text3) {
|
||||
std::cout.setf(std::ios::left, std::ios::adjustfield);
|
||||
std::cout << text1;
|
||||
|
||||
@@ -482,26 +417,22 @@ void printWithDots(const std::string &text1, const std::string &text2, const std
|
||||
}
|
||||
|
||||
// Comprueba si una vector contiene una cadena
|
||||
bool stringInVector(const std::vector<std::string> &vec, const std::string &str)
|
||||
{
|
||||
bool stringInVector(const std::vector<std::string>& vec, const std::string& str) {
|
||||
return std::find(vec.begin(), vec.end(), str) != vec.end();
|
||||
}
|
||||
|
||||
// Hace sonar la música
|
||||
void playMusic(const std::string &music_path)
|
||||
{
|
||||
void playMusic(const std::string& music_path) {
|
||||
// Si la música no está sonando
|
||||
if (JA_GetMusicState() == JA_MUSIC_INVALID || JA_GetMusicState() == JA_MUSIC_STOPPED)
|
||||
{
|
||||
if (JA_GetMusicState() == JA_MUSIC_INVALID || JA_GetMusicState() == JA_MUSIC_STOPPED) {
|
||||
JA_PlayMusic(Resource::get()->getMusic(music_path));
|
||||
}
|
||||
}
|
||||
|
||||
// Rellena una textura de un color
|
||||
void fillTextureWithColor(SDL_Renderer *renderer, SDL_Texture *texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
|
||||
{
|
||||
void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
|
||||
// Guardar el render target actual
|
||||
SDL_Texture *previous_target = SDL_GetRenderTarget(renderer);
|
||||
SDL_Texture* previous_target = SDL_GetRenderTarget(renderer);
|
||||
|
||||
// Establecer la textura como el render target
|
||||
SDL_SetRenderTarget(renderer, texture);
|
||||
|
||||
Reference in New Issue
Block a user