Files
projecte_2026/source/utils/utils.hpp
2026-04-07 12:06:41 +02:00

61 lines
2.7 KiB
C++

#pragma once
#include <SDL3/SDL.h>
#include <string> // Para string
#include <vector> // Para vector
// Estructura para definir un circulo
struct Circle {
int x{0};
int y{0};
int r{0};
};
// Estructura para definir un color RGB
struct Rgb {
Uint8 r{0};
Uint8 g{0};
Uint8 b{0};
// Constructor
Rgb(Uint8 red, Uint8 green, Uint8 blue)
: r(red),
g(green),
b(blue) {}
};
// COLISIONES Y GEOMETRÍA
auto distanceSquared(int x1, int y1, int x2, int y2) -> double; // Distancia² entre dos puntos
auto checkCollision(const Circle& a, const Circle& b) -> bool; // Colisión círculo-círculo
auto checkCollision(const Circle& a, const SDL_FRect& rect) -> bool; // Colisión círculo-rectángulo
auto checkCollision(const SDL_FRect& a, const SDL_FRect& b) -> bool; // Colisión rectángulo-rectángulo
auto checkCollision(const SDL_FPoint& p, const SDL_FRect& r) -> bool; // Colisión punto-rectángulo
// CONVERSIONES DE TIPOS SDL
auto toSDLRect(const SDL_FRect& frect) -> SDL_Rect; // Convierte SDL_FRect a SDL_Rect
auto toSDLPoint(const SDL_FPoint& fpoint) -> SDL_Point; // Convierte SDL_FPoint a SDL_Point
// CONVERSIONES DE STRING
auto safeStoi(const std::string& value, int default_value = 0) -> int; // String a int seguro (sin excepciones)
auto stringToBool(const std::string& str) -> bool; // String a bool (true/1/yes/on)
auto boolToString(bool value) -> std::string; // Bool a string (1/0)
auto toLower(const std::string& str) -> std::string; // String a minúsculas
auto toUpper(const std::string& str) -> std::string; // String a mayúsculas
auto prettyName(const std::string& str) -> std::string; // Guiones a espacios ("crt-live" → "crt live")
// OPERACIONES CON STRINGS
auto stringInVector(const std::vector<std::string>& vec, const std::string& str) -> bool; // Busca string en vector
auto spaceBetweenLetters(const std::string& input) -> std::string; // Añade espacios entre letras
// OPERACIONES CON COLORES
auto colorAreEqual(Rgb color1, Rgb color2) -> bool; // Compara dos colores RGB
// OPERACIONES CON FICHEROS
auto getFileName(const std::string& path) -> std::string; // Extrae nombre de fichero de ruta
auto getPath(const std::string& full_path) -> std::string; // Extrae directorio de ruta completa
// RENDERIZADO
void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a); // Rellena textura
// OUTPUT Y UTILIDADES DE CONSOLA
void printWithDots(const std::string& text1, const std::string& text2, const std::string& text3); // Imprime línea con puntos