316 lines
9.0 KiB
C++
316 lines
9.0 KiB
C++
#define _USE_MATH_DEFINES
|
|
#include "utils.hpp"
|
|
|
|
#include <SDL3/SDL.h> // Para SDL_RenderPoint, SDL_FRect, SDL_FPoint, SDL_Renderer
|
|
|
|
#include <algorithm> // Para clamp, __transform_fn, transform
|
|
#include <cctype> // Para tolower, isspace
|
|
#include <cmath> // Para pow, sin, M_PI, cos, sqrt
|
|
#include <compare> // Para operator<
|
|
#include <filesystem> // Para path
|
|
#include <ranges> // Para __find_if_not_fn, find_if_not, reverse_view, __find_fn, find, ref_view
|
|
#include <string> // Para basic_string, string, allocator, char_traits, operator==, operator+
|
|
|
|
#include "lang.hpp" // Para getText
|
|
|
|
// Variables
|
|
Overrides overrides = Overrides();
|
|
|
|
// 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);
|
|
}
|
|
|
|
// Obtiene el punto de colisión entre dos circulos
|
|
auto getCollisionPoint(const Circle& a, const Circle& b) -> SDL_FPoint {
|
|
float dx = b.x - a.x;
|
|
float dy = b.y - a.y;
|
|
float dist = std::sqrt((dx * dx) + (dy * dy));
|
|
|
|
// Normaliza el vector
|
|
float nx = dx / dist;
|
|
float ny = dy / dist;
|
|
|
|
// Punto en el borde del círculo A hacia B
|
|
SDL_FPoint contact;
|
|
contact.x = a.x + nx * a.r;
|
|
contact.y = a.y + ny * a.r;
|
|
|
|
return contact;
|
|
}
|
|
|
|
// Detector de colisiones entre dos circulos
|
|
auto checkCollision(const Circle& a, const Circle& b) -> bool {
|
|
// Calcula el radio total al cuadrado
|
|
int total_radius_squared = (a.r + b.r) * (a.r + b.r);
|
|
|
|
// 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
|
|
auto checkCollision(const Circle& a, const SDL_FRect& b) -> bool {
|
|
// Encuentra el punto más cercano en el rectángulo
|
|
float c_x = std::clamp(static_cast<float>(a.x), b.x, b.x + b.w);
|
|
float c_y = std::clamp(static_cast<float>(a.y), b.y, b.y + b.h);
|
|
|
|
// Si el punto más cercano está dentro del círculo
|
|
return distanceSquared(static_cast<float>(a.x), static_cast<float>(a.y), c_x, c_y) < static_cast<float>(a.r) * a.r;
|
|
}
|
|
|
|
// Detector de colisiones entre dos rectangulos
|
|
auto checkCollision(const SDL_FRect& a, const SDL_FRect& b) -> bool {
|
|
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& p, const SDL_FRect& r) -> bool {
|
|
if (p.x < r.x || p.x > r.x + r.w) {
|
|
return false;
|
|
}
|
|
if (p.y < r.y || p.y > r.y + r.h) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Convierte una cadena en un valor booleano
|
|
auto stringToBool(const std::string& str) -> bool {
|
|
std::string s = trim(toLower(str));
|
|
return (s == "true" || s == "1" || s == "yes" || s == "on");
|
|
}
|
|
|
|
// Convierte un valor booleano en una cadena
|
|
auto boolToString(bool value) -> std::string {
|
|
return value ? "true" : "false";
|
|
}
|
|
|
|
// Convierte un valor booleano en una cadena "on" o "off"
|
|
auto boolToOnOff(bool value) -> std::string {
|
|
return value ? Lang::getText("[NOTIFICATIONS] 06") : Lang::getText("[NOTIFICATIONS] 07");
|
|
}
|
|
|
|
// Convierte una cadena a minusculas
|
|
auto toLower(const std::string& str) -> std::string {
|
|
std::string result = str;
|
|
std::ranges::transform(result, result.begin(), [](unsigned char c) { return std::tolower(c); });
|
|
return result;
|
|
}
|
|
|
|
// Dibuja un circulo
|
|
void drawCircle(SDL_Renderer* renderer, int32_t center_x, int32_t center_y, int32_t radius) {
|
|
const int32_t DIAMETER = (radius * 2);
|
|
|
|
int32_t x = (radius - 1);
|
|
int32_t y = 0;
|
|
int32_t tx = 1;
|
|
int32_t ty = 1;
|
|
int32_t error = (tx - DIAMETER);
|
|
|
|
while (x >= y) {
|
|
// Each of the following renders an octant of the circle
|
|
SDL_RenderPoint(renderer, center_x + x, center_y - y);
|
|
SDL_RenderPoint(renderer, center_x + x, center_y + y);
|
|
SDL_RenderPoint(renderer, center_x - x, center_y - y);
|
|
SDL_RenderPoint(renderer, center_x - x, center_y + y);
|
|
SDL_RenderPoint(renderer, center_x + y, center_y - x);
|
|
SDL_RenderPoint(renderer, center_x + y, center_y + x);
|
|
SDL_RenderPoint(renderer, center_x - y, center_y - x);
|
|
SDL_RenderPoint(renderer, center_x - y, center_y + x);
|
|
|
|
if (error <= 0) {
|
|
++y;
|
|
error += ty;
|
|
ty += 2;
|
|
}
|
|
|
|
if (error > 0) {
|
|
--x;
|
|
tx += 2;
|
|
error += (tx - DIAMETER);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Quita los espacioes en un string
|
|
auto trim(const std::string& str) -> std::string {
|
|
auto start = std::ranges::find_if_not(str, ::isspace);
|
|
auto end = std::ranges::find_if_not(std::ranges::reverse_view(str), ::isspace).base();
|
|
return (start < end ? std::string(start, end) : std::string());
|
|
}
|
|
|
|
// Función de suavizado
|
|
auto easeOutQuint(double time) -> double {
|
|
return 1 - std::pow(1 - time, 5);
|
|
}
|
|
|
|
// Función de suavizado
|
|
auto easeInQuint(double time) -> double {
|
|
return pow(time, 5);
|
|
}
|
|
|
|
// Función de suavizado
|
|
auto easeInOutQuint(double time) -> double {
|
|
return time < 0.5 ? 16 * pow(time, 5) : 1 - (pow((-2 * time) + 2, 5) / 2);
|
|
}
|
|
|
|
// Función de suavizado
|
|
auto easeInQuad(double time) -> double {
|
|
return time * time;
|
|
}
|
|
|
|
// Función de suavizado
|
|
auto easeOutQuad(double time) -> double {
|
|
return 1 - ((1 - time) * (1 - time));
|
|
}
|
|
|
|
// Función de suavizado
|
|
auto easeInOutSine(double time) -> double {
|
|
return -0.5 * (std::cos(M_PI * time) - 1);
|
|
}
|
|
|
|
// Función de suavizado
|
|
auto easeInOut(double time) -> double {
|
|
return time < 0.5 ? 2 * time * time : -1 + ((4 - 2 * time) * time);
|
|
}
|
|
|
|
// Función de suavizado (easeInOutExpo)
|
|
auto easeInOutExpo(double time) -> double {
|
|
if (time == 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (time == 1) {
|
|
return 1;
|
|
}
|
|
|
|
if (time < 0.5) {
|
|
return pow(2, (20 * time) - 10) / 2;
|
|
}
|
|
|
|
return (2 - pow(2, (-20 * time) + 10)) / 2;
|
|
}
|
|
|
|
// Función de suavizado (easeInElastic)
|
|
auto easeInElastic(double time) -> double {
|
|
if (time == 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (time == 1) {
|
|
return 1;
|
|
}
|
|
|
|
const double C4 = (2 * M_PI) / 3;
|
|
return -pow(2, (10 * time) - 10) * sin((time * 10 - 10.75) * C4);
|
|
}
|
|
|
|
// Función de suavizado
|
|
auto easeOutBounce(double time) -> double {
|
|
if (time < 1 / 2.75) {
|
|
return 7.5625 * time * time;
|
|
}
|
|
if (time < 2 / 2.75) {
|
|
time -= 1.5 / 2.75;
|
|
return (7.5625 * time * time) + 0.75;
|
|
}
|
|
if (time < 2.5 / 2.75) {
|
|
time -= 2.25 / 2.75;
|
|
return (7.5625 * time * time) + 0.9375;
|
|
}
|
|
time -= 2.625 / 2.75;
|
|
return (7.5625 * time * time) + 0.984375;
|
|
}
|
|
|
|
// Función de suavizado (easeOutElastic)
|
|
auto easeOutElastic(double time) -> double {
|
|
if (time == 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (time == 1) {
|
|
return 1;
|
|
}
|
|
|
|
const double C4 = (2 * M_PI) / 3; // Constante para controlar la elasticidad
|
|
return (pow(2, -10 * time) * sin((time * 10 - 0.75) * C4)) + 1;
|
|
}
|
|
|
|
// Ease Out Expo - Muy suave al final (más dramático)
|
|
auto easeOutExpo(double time) -> double {
|
|
return time == 1.0F ? 1.0F : 1.0F - pow(2.0F, -10.0F * time);
|
|
}
|
|
|
|
// Ease In Expo - Arranque muy gradual
|
|
auto easeInExpo(double time) -> double {
|
|
return time == 0.0F ? 0.0F : pow(2.0F, 10.0F * (time - 1.0F));
|
|
}
|
|
|
|
// Ease Out Back - Con un pequeño "rebote"
|
|
auto easeOutBack(double time) -> double {
|
|
const double C1 = 1.70158F;
|
|
const double C3 = C1 + 1.0F;
|
|
return 1.0F + (C3 * pow(time - 1.0F, 3.0F)) + (C1 * pow(time - 1.0F, 2.0F));
|
|
}
|
|
|
|
// Ease Out Cubic - Desaceleración suave al final
|
|
auto easeOutCubic(double time) -> double {
|
|
return 1.0F - pow(1.0F - time, 3.0F);
|
|
}
|
|
|
|
// Ease In Cubic - Aceleración gradual
|
|
auto easeInCubic(double time) -> double {
|
|
return time * time * time;
|
|
}
|
|
|
|
// Comprueba si una vector contiene una cadena
|
|
auto stringInVector(const std::vector<std::string>& vec, const std::string& str) -> bool {
|
|
return std::ranges::find(vec, str) != vec.end();
|
|
}
|
|
|
|
// 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 {
|
|
std::filesystem::path path(full_path);
|
|
return path.parent_path().string();
|
|
}
|
|
|
|
// Trunca un string y le añade puntos suspensivos
|
|
auto truncateWithEllipsis(const std::string& input, size_t length) -> std::string {
|
|
if (input.size() <= length) {
|
|
return input;
|
|
}
|
|
if (length <= 3) {
|
|
std::string result(length, '.');
|
|
return result;
|
|
}
|
|
return input.substr(0, length) + "...";
|
|
} |