#include "utils.h" #include // for free, malloc #include // for max, min #include // for isspace #include // for distance #include struct JA_Music_t; // lines 3-3 struct JA_Sound_t; // lines 4-4 // Colores const Color bg_color = {0x27, 0x27, 0x36}; const Color no_color = {0xFF, 0xFF, 0xFF}; const Color shdw_txt_color = {0x43, 0x43, 0x4F}; const Color separator_color = {0x0D, 0x1A, 0x2B}; const Color scoreboard_color = {0x2E, 0x3F, 0x47}; const Color difficulty_easy_color = {0x4B, 0x69, 0x2F}; const Color difficulty_normal_color = {0xFF, 0x7A, 0x00}; const Color difficulty_hard_color = {0x76, 0x42, 0x8A}; const Color flash_color = {0xFF, 0xFF, 0xFF}; const Color fade_color = {0x27, 0x27, 0x36}; const Color orange_color = {0xFF, 0x7A, 0x00}; // 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(Circle &a, 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; // 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; } // Detector de colisiones entre un circulo y un rectangulo bool checkCollision(Circle &a, SDL_Rect &b) { // Closest point on collision box int cX, cY; // 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; } // Detector de colisiones entre dos rectangulos bool checkCollision(SDL_Rect &a, 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; // 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) { // Comprueba si el punto está a la izquierda del rectangulo if (p.x < r.x) { return false; } // Comprueba si el punto está a la derecha del rectangulo if (p.x > r.x + r.w) { 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) { return str == "true" ? true : false; } // Convierte un valor booleano en una cadena std::string boolToString(bool value) { return value == true ? "true" : "false"; } // Convierte un valor booleano en una cadena "on" o "off" std::string boolToOnOff(bool value) { return value == true ? "on" : "off"; } // Convierte una cadena a minusculas std::string toLower(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; } // Obtiene el fichero de sonido a partir de un nombre JA_Sound_t *getSound(std::vector sounds, std::string name) { for (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 music, std::string name) { for (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) { if (entry1.score > entry2.score) { return entry1; } return entry2; } // 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_RenderDrawPoint(renderer, centerX + x, centerY - y); SDL_RenderDrawPoint(renderer, centerX + x, centerY + y); SDL_RenderDrawPoint(renderer, centerX - x, centerY - y); SDL_RenderDrawPoint(renderer, centerX - x, centerY + y); SDL_RenderDrawPoint(renderer, centerX + y, centerY - x); SDL_RenderDrawPoint(renderer, centerX + y, centerY + x); SDL_RenderDrawPoint(renderer, centerX - y, centerY - x); SDL_RenderDrawPoint(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(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); return newColor; } // Oscurece el color Color DarkenColor(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); 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); } // Función de suavizado double easeOutQuint(double t) { return 1 - std::pow(1 - t, 5); } // Función de suavizado double easeInOutSine(double t) { return -0.5 * (std::cos(M_PI * t) - 1); }