reestructurat utils

This commit is contained in:
2025-11-12 14:02:17 +01:00
parent 9cf45062a3
commit 477ab34057
3 changed files with 64 additions and 111 deletions

View File

@@ -45,7 +45,7 @@ Title::Title()
initMarquee(); // Inicializa la marquesina
createCheevosTexture(); // Crea y rellena la textura para mostrar los logros
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde
playMusic("title.ogg"); // Inicia la musica
Audio::get()->playMusic("title.ogg"); // Inicia la musica
}
// Inicializa la marquesina

View File

@@ -282,6 +282,24 @@ void normalizeLine(LineDiagonal& l) {
}
}
// Convierte SDL_FRect a SDL_Rect
auto toSDLRect(const SDL_FRect& frect) -> SDL_Rect {
SDL_Rect rect = {
.x = static_cast<int>(frect.x),
.y = static_cast<int>(frect.y),
.w = static_cast<int>(frect.w),
.h = static_cast<int>(frect.h)};
return rect;
}
// Convierte SDL_FPoint a SDL_Point
auto toSDLPoint(const SDL_FPoint& fpoint) -> SDL_Point {
SDL_Point point = {
.x = static_cast<int>(fpoint.x),
.y = static_cast<int>(fpoint.y)};
return point;
}
// 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);
@@ -425,14 +443,6 @@ auto stringInVector(const std::vector<std::string>& vec, const std::string& str)
return std::ranges::find(vec, str) != vec.end();
}
// Hace sonar la música
void playMusic(const std::string& music_path) {
// Si la música no está sonando
if (JA_GetMusicState() == JA_MUSIC_INVALID || JA_GetMusicState() == JA_MUSIC_STOPPED) {
JA_PlayMusic(Resource::Cache::get()->getMusic(music_path));
}
}
// Rellena una textura de un color
void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
// Guardar el render target actual

View File

@@ -35,42 +35,36 @@ enum class PaletteColor : Uint8 {
// Estructura para definir un circulo
struct Circle {
int x;
int y;
int r;
int x{0};
int y{0};
int r{0};
};
// Estructura para definir una linea horizontal
struct LineHorizontal {
int x1, x2, y;
int x1{0}, x2{0}, y{0};
};
// Estructura para definir una linea vertical
struct LineVertical {
int x, y1, y2;
int x{0}, y1{0}, y2{0};
};
// Estructura para definir una linea diagonal
struct LineDiagonal {
int x1, y1, x2, y2;
int x1{0}, y1{0}, x2{0}, y2{0};
};
// Estructura para definir una linea
struct Line {
int x1, y1, x2, y2;
int x1{0}, y1{0}, x2{0}, y2{0};
};
// Estructura para definir un color
struct Color {
Uint8 r;
Uint8 g;
Uint8 b;
// Constructor por defecto
Color()
: r(0),
g(0),
b(0) {}
Uint8 r{0};
Uint8 g{0};
Uint8 b{0};
// Constructor
Color(Uint8 red, Uint8 green, Uint8 blue)
@@ -79,96 +73,45 @@ struct Color {
b(blue) {}
};
// Calcula el cuadrado de la distancia entre dos puntos
auto distanceSquared(int x1, int y1, int x2, int y2) -> double;
// 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
auto checkCollision(const LineHorizontal& l, const SDL_FRect& r) -> bool; // Colisión línea horizontal-rectángulo
auto checkCollision(const LineVertical& l, const SDL_FRect& r) -> bool; // Colisión línea vertical-rectángulo
auto checkCollision(const LineHorizontal& l, const SDL_FPoint& p) -> bool; // Colisión línea horizontal-punto
auto checkCollision(const Line& l1, const Line& l2) -> SDL_Point; // Colisión línea-línea (intersección)
auto checkCollision(const LineDiagonal& l1, const LineVertical& l2) -> SDL_Point; // Colisión diagonal-vertical
auto checkCollision(const SDL_FPoint& p, const LineDiagonal& l) -> bool; // Colisión punto-diagonal
void normalizeLine(LineDiagonal& l); // Normaliza línea diagonal (x1 < x2)
// Detector de colisiones entre dos circulos
auto checkCollision(const Circle& a, const Circle& b) -> bool;
// 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
// Detector de colisiones entre un circulo y un rectangulo
auto checkCollision(const Circle& a, const SDL_FRect& rect) -> bool;
// CONVERSIONES DE STRING
auto stringToColor(const std::string& str) -> Uint8; // String a índice de paleta
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
// Detector de colisiones entre un dos rectangulos
auto checkCollision(const SDL_FRect& a, const SDL_FRect& b) -> bool;
// 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
// Detector de colisiones entre un punto y un rectangulo
auto checkCollision(const SDL_FPoint& p, const SDL_FRect& r) -> bool;
// OPERACIONES CON COLORES
auto colorAreEqual(Color color1, Color color2) -> bool; // Compara dos colores RGB
// Detector de colisiones entre una linea horizontal y un rectangulo
auto checkCollision(const LineHorizontal& l, const SDL_FRect& r) -> bool;
// 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
// Detector de colisiones entre una linea vertical y un rectangulo
auto checkCollision(const LineVertical& l, const SDL_FRect& r) -> bool;
// RENDERIZADO
void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a); // Rellena textura
// Detector de colisiones entre una linea horizontal y un punto
auto checkCollision(const LineHorizontal& l, const SDL_FPoint& p) -> bool;
// Detector de colisiones entre dos lineas
auto checkCollision(const Line& l1, const Line& l2) -> SDL_Point;
// Detector de colisiones entre dos lineas
auto checkCollision(const LineDiagonal& l1, const LineVertical& l2) -> SDL_Point;
// Detector de colisiones entre un punto y una linea diagonal
auto checkCollision(const SDL_FPoint& p, const LineDiagonal& l) -> bool;
// Normaliza una linea diagonal
void normalizeLine(LineDiagonal& l);
// Devuelve un Color a partir de un string
auto stringToColor(const std::string& str) -> Uint8;
// Convierte una cadena a un entero de forma segura
auto safeStoi(const std::string& value, int default_value = 0) -> int;
// Convierte una cadena a un booleano
auto stringToBool(const std::string& str) -> bool;
// Convierte un booleano a una cadena
auto boolToString(bool value) -> std::string;
// Compara dos colores
auto colorAreEqual(Color color1, Color color2) -> bool;
// Convierte una cadena a minusculas
auto toLower(const std::string& str) -> std::string;
// Convierte una cadena a mayúsculas
auto toUpper(const std::string& str) -> std::string;
// Obtiene el nombre de un fichero a partir de una ruta
auto getFileName(const std::string& path) -> std::string;
// Obtiene la ruta eliminando el nombre del fichero
auto getPath(const std::string& full_path) -> std::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);
// Comprueba si una vector contiene una cadena
auto stringInVector(const std::vector<std::string>& vec, const std::string& str) -> bool;
// Hace sonar la música
void playMusic(const std::string& music_path);
// Rellena una textura de un color
void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
inline auto toSDLRect(const SDL_FRect& frect) -> SDL_Rect {
SDL_Rect rect = {
.x = static_cast<int>(frect.x),
.y = static_cast<int>(frect.y),
.w = static_cast<int>(frect.w),
.h = static_cast<int>(frect.h)};
return rect;
}
inline auto toSDLPoint(const SDL_FPoint& fpoint) -> SDL_Point {
SDL_Point point = {
.x = static_cast<int>(fpoint.x),
.y = static_cast<int>(fpoint.y)};
return point;
}
// Añade espacios entre las letras de un string
auto spaceBetweenLetters(const std::string& input) -> std::string;
// OUTPUT Y UTILIDADES DE CONSOLA
void printWithDots(const std::string& text1, const std::string& text2, const std::string& text3); // Imprime línea con puntos