393 lines
11 KiB
C++
393 lines
11 KiB
C++
#define _USE_MATH_DEFINES
|
|
#include "utils.h"
|
|
#include <SDL3/SDL.h> // Para SDL_IOFromFile, SDL_CloseIO, SDL_RIO
|
|
#include <stddef.h> // Para size_t
|
|
#include <algorithm> // Para min, clamp, find_if_not, find, transform
|
|
#include <cctype> // Para tolower, isspace
|
|
#include <cmath> // Para pow, sin, M_PI, cos
|
|
#include <compare> // Para operator<
|
|
#include <filesystem> // Para path
|
|
#include <iostream> // Para basic_ostream, cout, basic_ios, endl, ios
|
|
#include <stdexcept> // Para runtime_error
|
|
#include <string> // Para basic_string, string, char_traits, opera...
|
|
#include "lang.h"
|
|
|
|
// Variables
|
|
Overrides overrides = Overrides();
|
|
|
|
// Colores
|
|
const Color bg_color = Color(0X27, 0X27, 0X36);
|
|
const Color no_color = Color(0XFF, 0XFF, 0XFF);
|
|
const Color shdw_txt_color = Color(0X43, 0X43, 0X4F);
|
|
const Color separator_color = Color(0X0D, 0X1A, 0X2B);
|
|
const Color scoreboard_easy_color = Color(0X4B, 0X69, 0X2F);
|
|
const Color scoreboard_normal_color = Color(0X2E, 0X3F, 0X47);
|
|
const Color scoreboard_hard_color = Color(0X76, 0X42, 0X8A);
|
|
const Color flash_color = Color(0XFF, 0XFF, 0XFF);
|
|
const Color fade_color = Color(0X27, 0X27, 0X36);
|
|
const Color orange_color = Color(0XFF, 0X7A, 0X00);
|
|
const Color orange_soft_color = Color(0XFF, 0XA0, 0X33);
|
|
const Color green_color = Color(0X5B, 0XEC, 0X95);
|
|
const Color blue_sky_color = Color(0X02, 0X88, 0XD1);
|
|
const Color pink_sky_color = Color(0XFF, 0X6B, 0X97);
|
|
const Color green_sky_color = Color(0X00, 0X79, 0X6B);
|
|
|
|
// Obtiene un color del vector de colores imitando al Coche Fantástico
|
|
Color getColorLikeKnightRider(const std::vector<Color> &colors, int counter_)
|
|
{
|
|
int cycle_length = colors.size() * 2 - 2;
|
|
size_t n = counter_ % cycle_length;
|
|
|
|
size_t index;
|
|
if (n < colors.size())
|
|
{
|
|
index = n; // Avanza: 0,1,2,3
|
|
}
|
|
else
|
|
{
|
|
index = 2 * (colors.size() - 1) - n; // Retrocede: 2,1
|
|
}
|
|
|
|
return colors[index];
|
|
}
|
|
|
|
// Calcula el cuadrado de la distancia entre dos puntos
|
|
double distanceSquared(int x1, int y1, int x2, int y2)
|
|
{
|
|
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
|
|
bool checkCollision(const Circle &a, const Circle &b)
|
|
{
|
|
// 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
|
|
bool checkCollision(const Circle &a, const SDL_FRect &b)
|
|
{
|
|
// Encuentra el punto más cercano en el rectángulo
|
|
float cX = std::clamp(static_cast<float>(a.x), b.x, b.x + b.w);
|
|
float cY = 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), cX, cY) < static_cast<float>(a.r) * a.r;
|
|
}
|
|
|
|
// Detector de colisiones entre dos rectangulos
|
|
bool checkCollision(const SDL_FRect &a, const SDL_FRect &b)
|
|
{
|
|
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;
|
|
|
|
if (bottomA <= topB)
|
|
return false;
|
|
if (topA >= bottomB)
|
|
return false;
|
|
if (rightA <= leftB)
|
|
return false;
|
|
if (leftA >= rightB)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
// Detector de colisiones entre un punto y un rectangulo
|
|
bool checkCollision(const SDL_FPoint &p, const SDL_FRect &r)
|
|
{
|
|
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
|
|
bool stringToBool(const std::string &str)
|
|
{
|
|
return str == "true";
|
|
}
|
|
|
|
// Convierte un valor booleano en una cadena
|
|
std::string boolToString(bool value)
|
|
{
|
|
return value ? "true" : "false";
|
|
}
|
|
|
|
// Convierte un valor booleano en una cadena "on" o "off"
|
|
std::string boolToOnOff(bool value)
|
|
{
|
|
return value ? lang::getText(128) : lang::getText(129);
|
|
}
|
|
|
|
// Convierte una cadena a minusculas
|
|
std::string toLower(const std::string &str)
|
|
{
|
|
std::string result = str;
|
|
std::transform(result.begin(), result.end(), result.begin(),
|
|
[](unsigned char c)
|
|
{ return std::tolower(c); });
|
|
return result;
|
|
}
|
|
|
|
// Dibuja un circulo
|
|
void DrawCircle(SDL_Renderer *renderer, int32_t centerX, int32_t centerY, 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, centerX + x, centerY - y);
|
|
SDL_RenderPoint(renderer, centerX + x, centerY + y);
|
|
SDL_RenderPoint(renderer, centerX - x, centerY - y);
|
|
SDL_RenderPoint(renderer, centerX - x, centerY + y);
|
|
SDL_RenderPoint(renderer, centerX + y, centerY - x);
|
|
SDL_RenderPoint(renderer, centerX + y, centerY + x);
|
|
SDL_RenderPoint(renderer, centerX - y, centerY - x);
|
|
SDL_RenderPoint(renderer, centerX - y, centerY + x);
|
|
|
|
if (error <= 0)
|
|
{
|
|
++y;
|
|
error += ty;
|
|
ty += 2;
|
|
}
|
|
|
|
if (error > 0)
|
|
{
|
|
--x;
|
|
tx += 2;
|
|
error += (tx - diameter);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Aclara el color
|
|
Color lightenColor(const Color &color, int amount)
|
|
{
|
|
Color newColor;
|
|
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(const Color &color, int amount)
|
|
{
|
|
Color newColor;
|
|
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 = 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
|
|
double easeOutQuint(double t)
|
|
{
|
|
return 1 - std::pow(1 - t, 5);
|
|
}
|
|
|
|
// Función de suavizado
|
|
double easeInQuint(double t)
|
|
{
|
|
return pow(t, 5);
|
|
}
|
|
|
|
// Función de suavizado
|
|
double easeInOutQuint(double t)
|
|
{
|
|
return t < 0.5 ? 16 * pow(t, 5) : 1 - pow(-2 * t + 2, 5) / 2;
|
|
}
|
|
|
|
// Función de suavizado
|
|
double easeInQuad(double t)
|
|
{
|
|
return t * t;
|
|
}
|
|
|
|
// Función de suavizado
|
|
double easeOutQuad(double t)
|
|
{
|
|
return 1 - (1 - t) * (1 - t);
|
|
}
|
|
|
|
// Función de suavizado
|
|
double easeInOutSine(double t)
|
|
{
|
|
return -0.5 * (std::cos(M_PI * t) - 1);
|
|
}
|
|
|
|
// Función de suavizado
|
|
double easeInOut(double t)
|
|
{
|
|
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
|
|
}
|
|
|
|
// Función de suavizado
|
|
double easeInOutExpo(double t)
|
|
{
|
|
return t == 0 ? 0 : (t == 1 ? 1 : (t < 0.5 ? pow(2, 20 * t - 10) / 2 : (2 - pow(2, -20 * t + 10)) / 2));
|
|
}
|
|
|
|
// Función de suavizado (easeInElastic)
|
|
double easeInElastic(double t)
|
|
{
|
|
return t == 0 ? 0 : (t == 1 ? 1 : -pow(2, 10 * t - 10) * sin((t * 10 - 10.75) * (2 * M_PI) / 3));
|
|
}
|
|
|
|
// Función de suavizado
|
|
double easeOutBounce(double t)
|
|
{
|
|
if (t < 1 / 2.75)
|
|
{
|
|
return 7.5625 * t * t;
|
|
}
|
|
else if (t < 2 / 2.75)
|
|
{
|
|
t -= 1.5 / 2.75;
|
|
return 7.5625 * t * t + 0.75;
|
|
}
|
|
else if (t < 2.5 / 2.75)
|
|
{
|
|
t -= 2.25 / 2.75;
|
|
return 7.5625 * t * t + 0.9375;
|
|
}
|
|
else
|
|
{
|
|
t -= 2.625 / 2.75;
|
|
return 7.5625 * t * t + 0.984375;
|
|
}
|
|
}
|
|
|
|
// Función de suavizado
|
|
double easeOutElastic(double t)
|
|
{
|
|
const double c4 = (2 * M_PI) / 3; // Constante para controlar la elasticidad
|
|
|
|
return t == 0
|
|
? 0
|
|
: (t == 1
|
|
? 1
|
|
: pow(2, -10 * t) * sin((t * 10 - 0.75) * c4) + 1);
|
|
}
|
|
|
|
// Comprueba si una vector contiene una cadena
|
|
bool stringInVector(const std::vector<std::string> &vec, const std::string &str)
|
|
{
|
|
return std::find(vec.begin(), vec.end(), str) != vec.end();
|
|
}
|
|
|
|
// 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 << std::endl;
|
|
}
|
|
|
|
// Carga el fichero de datos para la demo
|
|
DemoData loadDemoDataFromFile(const std::string &file_path)
|
|
{
|
|
DemoData dd;
|
|
|
|
// Indicador de éxito en la carga
|
|
auto file = SDL_IOFromFile(file_path.c_str(), "r+b");
|
|
if (!file)
|
|
{
|
|
std::cerr << "Error: Fichero no encontrado " << file_path << std::endl;
|
|
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
|
}
|
|
else
|
|
{
|
|
printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]");
|
|
|
|
// Lee todos los datos del fichero y los deja en el destino
|
|
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
|
|
{
|
|
DemoKeys dk = DemoKeys();
|
|
SDL_ReadIO(file, &dk, sizeof(DemoKeys));
|
|
dd.push_back(dk);
|
|
}
|
|
|
|
// Cierra el fichero
|
|
SDL_CloseIO(file);
|
|
}
|
|
|
|
return dd;
|
|
}
|
|
|
|
#ifdef RECORDING
|
|
// Guarda el fichero de datos para la demo
|
|
bool saveDemoFile(const std::string &file_path, const DemoData &dd)
|
|
{
|
|
auto success = true;
|
|
auto file = SDL_IOFromFile(file_path.c_str(), "w+b");
|
|
|
|
if (file)
|
|
{
|
|
// Guarda los datos
|
|
for (const auto &data : dd)
|
|
{
|
|
if (SDL_RWwrite(file, &data, sizeof(DemoKeys), 1) != 1)
|
|
{
|
|
std::cerr << "Error al escribir el fichero " << getFileName(file_path) << std::endl;
|
|
success = false;
|
|
break;
|
|
}
|
|
}
|
|
if (success)
|
|
{
|
|
std::cout << "Writing file " << getFileName(file_path).c_str() << std::endl;
|
|
}
|
|
// Cierra el fichero
|
|
SDL_CloseIO(file);
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Error: Unable to save " << getFileName(file_path).c_str() << " file! " << SDL_GetError() << std::endl;
|
|
success = false;
|
|
}
|
|
|
|
return success;
|
|
}
|
|
#endif // RECORDING
|
|
|
|
// Obtiene el nombre de un fichero a partir de una ruta completa
|
|
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::filesystem::path path(full_path);
|
|
return path.parent_path().string();
|
|
} |