#include "utils/utils.hpp" #include // Para find, transform #include // Para tolower #include // Para round, abs #include // Para abs #include // Para exception #include // Para path #include // Para ifstream #include // Para basic_ostream, cout, basic_ios, ios, endl #include // Para basic_string, string, char_traits, allocator #include // Para unordered_map // Calcula el cuadrado de la distancia entre dos puntos 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 auto checkCollision(const Circle& a, const Circle& b) -> bool { int total_radius_squared = a.r + b.r; total_radius_squared = total_radius_squared * total_radius_squared; return distanceSquared(a.x, a.y, b.x, b.y) < total_radius_squared; } // Detector de colisiones entre un circulo y un rectangulo auto checkCollision(const Circle& a, const SDL_FRect& rect) -> bool { SDL_Rect b = toSDLRect(rect); int c_x; int c_y; if (a.x < b.x) { c_x = b.x; } else if (a.x > b.x + b.w) { c_x = b.x + b.w; } else { c_x = a.x; } if (a.y < b.y) { c_y = b.y; } else if (a.y > b.y + b.h) { c_y = b.y + b.h; } else { c_y = a.y; } return distanceSquared(a.x, a.y, c_x, c_y) < a.r * a.r; } // Detector de colisiones entre dos rectangulos 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); const int LEFT_A = a.x; const int RIGHT_A = a.x + a.w; const int TOP_A = a.y; const int BOTTOM_A = a.y + a.h; const int LEFT_B = b.x; const int RIGHT_B = b.x + b.w; const int TOP_B = b.y; const int BOTTOM_B = b.y + b.h; if (BOTTOM_A <= TOP_B) return false; if (TOP_A >= BOTTOM_B) return false; if (RIGHT_A <= LEFT_B) return false; if (LEFT_A >= RIGHT_B) return false; return true; } // Detector de colisiones entre un punto y un rectangulo auto checkCollision(const SDL_FPoint& point, const SDL_FRect& rect) -> bool { SDL_Rect r = toSDLRect(rect); SDL_Point p = toSDLPoint(point); if (p.x < r.x) return false; if (p.x > r.x + r.w) return false; if (p.y < r.y) return false; if (p.y > r.y + r.h) return false; return true; } // Detector de colisiones entre una linea horizontal y un rectangulo auto checkCollision(const LineHorizontal& l, const SDL_FRect& rect) -> bool { SDL_Rect r = toSDLRect(rect); if (l.y < r.y) return false; if (l.y >= r.y + r.h) return false; if (l.x1 >= r.x + r.w) return false; if (l.x2 < r.x) return false; return true; } // Detector de colisiones entre una linea vertical y un rectangulo auto checkCollision(const LineVertical& l, const SDL_FRect& rect) -> bool { SDL_Rect r = toSDLRect(rect); if (l.x < r.x) return false; if (l.x >= r.x + r.w) return false; if (l.y1 >= r.y + r.h) return false; if (l.y2 < r.y) return false; return true; } // Detector de colisiones entre una linea horizontal y un punto auto checkCollision(const LineHorizontal& l, const SDL_FPoint& point) -> bool { SDL_Point p = toSDLPoint(point); if (p.y > l.y) return false; if (p.y < l.y) return false; if (p.x < l.x1) return false; if (p.x > l.x2) return false; return true; } // Detector de colisiones entre dos lineas 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; const float Y2 = l1.y2; const float X3 = l2.x1; const float Y3 = l2.y1; const float X4 = l2.x2; const float Y4 = l2.y2; float u_a = (((X4 - X3) * (Y1 - Y3)) - ((Y4 - Y3) * (X1 - X3))) / (((Y4 - Y3) * (X2 - X1)) - ((X4 - X3) * (Y2 - Y1))); float u_b = (((X2 - X1) * (Y1 - Y3)) - ((Y2 - Y1) * (X1 - X3))) / (((Y4 - Y3) * (X2 - X1)) - ((X4 - X3) * (Y2 - Y1))); if (u_a >= 0 && u_a <= 1 && u_b >= 0 && u_b <= 1) { const float X = X1 + (u_a * (X2 - X1)); const float Y = Y1 + (u_a * (Y2 - Y1)); return {.x = static_cast(std::round(X)), .y = static_cast(std::round(Y))}; } return {.x = -1, .y = -1}; } // Detector de colisiones entre dos lineas (diagonal-vertical) 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; const float Y2 = l1.y2; const float X3 = l2.x; const float Y3 = l2.y1; const float X4 = l2.x; const float Y4 = l2.y2; float u_a = (((X4 - X3) * (Y1 - Y3)) - ((Y4 - Y3) * (X1 - X3))) / (((Y4 - Y3) * (X2 - X1)) - ((X4 - X3) * (Y2 - Y1))); float u_b = (((X2 - X1) * (Y1 - Y3)) - ((Y2 - Y1) * (X1 - X3))) / (((Y4 - Y3) * (X2 - X1)) - ((X4 - X3) * (Y2 - Y1))); if (u_a >= 0 && u_a <= 1 && u_b >= 0 && u_b <= 1) { const float X = X1 + (u_a * (X2 - X1)); const float Y = Y1 + (u_a * (Y2 - Y1)); return {.x = static_cast(X), .y = static_cast(Y)}; } return {.x = -1, .y = -1}; } // Normaliza una linea diagonal void normalizeLine(LineDiagonal& l) { if (l.x2 < l.x1) { const int X = l.x1; const int Y = l.y1; l.x1 = l.x2; l.y1 = l.y2; l.x2 = X; l.y2 = Y; } } // Convierte SDL_FRect a SDL_Rect auto toSDLRect(const SDL_FRect& frect) -> SDL_Rect { return SDL_Rect{ .x = static_cast(frect.x), .y = static_cast(frect.y), .w = static_cast(frect.w), .h = static_cast(frect.h)}; } // Convierte SDL_FPoint a SDL_Point auto toSDLPoint(const SDL_FPoint& fpoint) -> SDL_Point { return SDL_Point{ .x = static_cast(fpoint.x), .y = static_cast(fpoint.y)}; } // Detector de colisiones entre un punto y una linea diagonal auto checkCollision(const SDL_FPoint& point, const LineDiagonal& l) -> bool { SDL_Point p = toSDLPoint(point); if (abs(p.x - l.x1) != abs(p.y - l.y1)) return false; if (p.x > l.x1 && p.x > l.x2) return false; if (p.x < l.x1 && p.x < l.x2) return false; if (p.y > l.y1 && p.y > l.y2) return false; if (p.y < l.y1 && p.y < l.y2) return false; return true; } // Convierte una cadena a un indice de la paleta auto stringToColor(const std::string& str) -> Uint8 { static const std::unordered_map PALETTE_MAP = { {"black", 0}, {"bright_black", 1}, {"blue", 2}, {"bright_blue", 3}, {"red", 4}, {"bright_red", 5}, {"magenta", 6}, {"bright_magenta", 7}, {"green", 8}, {"bright_green", 9}, {"cyan", 10}, {"bright_cyan", 11}, {"yellow", 12}, {"bright_yellow", 13}, {"white", 14}, {"bright_white", 15}, {"transparent", 255}}; auto it = PALETTE_MAP.find(str); return (it != PALETTE_MAP.end()) ? it->second : 0; } // Convierte una cadena a un entero de forma segura auto safeStoi(const std::string& value, int default_value) -> int { try { return std::stoi(value); } catch (const std::exception&) { return default_value; } } // Convierte una cadena a un booleano auto stringToBool(const std::string& str) -> bool { std::string lower_str = str; 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 auto boolToString(bool value) -> std::string { return value ? "1" : "0"; } // Compara dos colores auto colorAreEqual(Color color1, Color color2) -> bool { return color1.r == color2.r && color1.g == color2.g && color1.b == color2.b; } // Función para convertir un string a minúsculas auto toLower(const std::string& str) -> std::string { std::string lower_str = str; std::ranges::transform(lower_str, lower_str.begin(), ::tolower); return lower_str; } // Función para convertir un string a mayúsculas auto toUpper(const std::string& str) -> std::string { std::string upper_str = str; std::ranges::transform(upper_str, upper_str.begin(), ::toupper); return upper_str; } // Carga un fichero como vector de bytes auto loadFileBytes(const std::string& path) -> std::vector { std::ifstream file(path, std::ios::binary); if (!file) return {}; return {std::istreambuf_iterator(file), std::istreambuf_iterator()}; } // Obtiene el nombre de un fichero a partir de una ruta completa auto getFileName(const std::string& path) -> std::string { return std::filesystem::path(path).filename().string(); } // Obtiene la ruta eliminando el nombre del fichero auto getPath(const std::string& full_path) -> std::string { return std::filesystem::path(full_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) { std::cout.setf(std::ios::left, std::ios::adjustfield); std::cout << text1; std::cout.width(50 - text1.length() - text3.length()); std::cout.fill('.'); std::cout << text2; std::cout << text3 << '\n'; } // Comprueba si una vector contiene una cadena auto stringInVector(const std::vector& vec, const std::string& str) -> bool { return std::ranges::find(vec, str) != vec.end(); } // Rellena una textura de un color void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { SDL_Texture* previous_target = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer, texture); SDL_SetRenderDrawColor(renderer, r, g, b, a); SDL_RenderClear(renderer); SDL_SetRenderTarget(renderer, previous_target); } // Añade espacios entre las letras de un string auto spaceBetweenLetters(const std::string& input) -> std::string { std::string result; for (size_t i = 0; i < input.size(); ++i) { result += input[i]; if (i != input.size() - 1) result += ' '; } return result; }