This commit is contained in:
2025-10-27 18:35:53 +01:00
parent b1dca32a5b
commit 3179a08dac
63 changed files with 686 additions and 693 deletions

View File

@@ -6,7 +6,7 @@ DeltaTimer::DeltaTimer() noexcept
time_scale_(1.0F) {
}
float DeltaTimer::tick() noexcept {
auto DeltaTimer::tick() noexcept -> float {
const Uint64 NOW = SDL_GetPerformanceCounter();
const Uint64 DIFF = (NOW > last_counter_) ? (NOW - last_counter_) : 0;
last_counter_ = NOW;
@@ -14,7 +14,7 @@ float DeltaTimer::tick() noexcept {
return static_cast<float>(SECONDS * static_cast<double>(time_scale_));
}
float DeltaTimer::peek() const noexcept {
auto DeltaTimer::peek() const noexcept -> float {
const Uint64 NOW = SDL_GetPerformanceCounter();
const Uint64 DIFF = (NOW > last_counter_) ? (NOW - last_counter_) : 0;
const double SECONDS = static_cast<double>(DIFF) / perf_freq_;
@@ -33,7 +33,6 @@ void DeltaTimer::setTimeScale(float scale) noexcept {
time_scale_ = std::max(scale, 0.0F);
}
float DeltaTimer::getTimeScale() const noexcept {
auto DeltaTimer::getTimeScale() const noexcept -> float {
return time_scale_;
}

View File

@@ -8,17 +8,17 @@ public:
DeltaTimer() noexcept;
// Calcula delta en segundos y actualiza el contador interno
float tick() noexcept;
auto tick() noexcept -> float;
// Devuelve el delta estimado desde el último tick sin actualizar el contador
float peek() const noexcept;
[[nodiscard]] auto peek() const noexcept -> float;
// Reinicia el contador al valor actual o al valor pasado (en performance counter ticks)
void reset(Uint64 counter = 0) noexcept;
// Escala el tiempo retornado por tick/peek, por defecto 1.0f
void setTimeScale(float scale) noexcept;
float getTimeScale() const noexcept;
[[nodiscard]] auto getTimeScale() const noexcept -> float;
private:
Uint64 last_counter_;

View File

@@ -1,10 +1,9 @@
#include "utils/utils.hpp"
#include <stdlib.h> // Para abs
#include <algorithm> // Para find, transform
#include <cctype> // Para tolower
#include <cmath> // Para round, abs
#include <cstdlib> // Para abs
#include <exception> // Para exception
#include <filesystem> // Para path
#include <iostream> // Para basic_ostream, cout, basic_ios, ios, endl
@@ -16,14 +15,14 @@
#include "external/jail_audio.h" // Para JA_GetMusicState, JA_Music_state, JA_PlayMusic
// Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2) {
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);
}
// Detector de colisiones entre dos circulos
bool checkCollision(const Circle& a, const Circle& b) {
auto checkCollision(const Circle& a, const Circle& b) -> bool {
// Calcula el radio total al cuadrado
int total_radius_squared = a.r + b.r;
total_radius_squared = total_radius_squared * total_radius_squared;
@@ -33,7 +32,7 @@ bool checkCollision(const Circle& a, const Circle& b) {
}
// Detector de colisiones entre un circulo y un rectangulo
bool checkCollision(const Circle& a, const SDL_FRect& rect) {
auto checkCollision(const Circle& a, const SDL_FRect& rect) -> bool {
SDL_Rect b = toSDLRect(rect);
// Closest point on collision box
int c_x;
@@ -68,7 +67,7 @@ bool checkCollision(const Circle& a, const SDL_FRect& rect) {
}
// Detector de colisiones entre dos rectangulos
bool checkCollision(const SDL_FRect& rect_a, const SDL_FRect& rect_b) {
auto checkCollision(const SDL_FRect& rect_a, const SDL_FRect& rect_b) -> bool {
SDL_Rect a = toSDLRect(rect_a);
SDL_Rect b = toSDLRect(rect_b);
// Calcula las caras del rectangulo a
@@ -105,7 +104,7 @@ bool checkCollision(const SDL_FRect& rect_a, const SDL_FRect& rect_b) {
}
// Detector de colisiones entre un punto y un rectangulo
bool checkCollision(const SDL_FPoint& point, const SDL_FRect& rect) {
auto checkCollision(const SDL_FPoint& point, const SDL_FRect& rect) -> bool {
SDL_Rect r = toSDLRect(rect);
SDL_Point p = toSDLPoint(point);
@@ -134,7 +133,7 @@ bool checkCollision(const SDL_FPoint& point, const SDL_FRect& rect) {
}
// Detector de colisiones entre una linea horizontal y un rectangulo
bool checkCollision(const LineHorizontal& l, const SDL_FRect& rect) {
auto checkCollision(const LineHorizontal& l, const SDL_FRect& rect) -> bool {
SDL_Rect r = toSDLRect(rect);
// Comprueba si la linea esta por encima del rectangulo
if (l.y < r.y) {
@@ -161,7 +160,7 @@ bool checkCollision(const LineHorizontal& l, const SDL_FRect& rect) {
}
// Detector de colisiones entre una linea vertical y un rectangulo
bool checkCollision(const LineVertical& l, const SDL_FRect& rect) {
auto checkCollision(const LineVertical& l, const SDL_FRect& rect) -> bool {
SDL_Rect r = toSDLRect(rect);
// Comprueba si la linea esta por la izquierda del rectangulo
if (l.x < r.x) {
@@ -188,7 +187,7 @@ bool checkCollision(const LineVertical& l, const SDL_FRect& rect) {
}
// Detector de colisiones entre una linea horizontal y un punto
bool checkCollision(const LineHorizontal& l, const SDL_FPoint& point) {
auto checkCollision(const LineHorizontal& l, const SDL_FPoint& point) -> bool {
SDL_Point p = toSDLPoint(point);
// Comprueba si el punto esta sobre la linea
@@ -216,7 +215,7 @@ bool checkCollision(const LineHorizontal& l, const SDL_FPoint& point) {
}
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(const Line& l1, const Line& l2) {
auto checkCollision(const Line& l1, const Line& l2) -> SDL_Point {
const float X1 = l1.x1;
const float Y1 = l1.y1;
const float X2 = l1.x2;
@@ -237,13 +236,13 @@ SDL_Point checkCollision(const Line& l1, const Line& l2) {
const float X = X1 + (u_a * (X2 - X1));
const float Y = Y1 + (u_a * (Y2 - Y1));
return {static_cast<int>(round(X)), static_cast<int>(round(Y))};
return {static_cast<int>(std::round(X)), static_cast<int>(std::round(Y))};
}
return {-1, -1};
}
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(const LineDiagonal& l1, const LineVertical& l2) {
auto checkCollision(const LineDiagonal& l1, const LineVertical& l2) -> SDL_Point {
const float X1 = l1.x1;
const float Y1 = l1.y1;
const float X2 = l1.x2;
@@ -284,7 +283,7 @@ void normalizeLine(LineDiagonal& l) {
}
// Detector de colisiones entre un punto y una linea diagonal
bool checkCollision(const SDL_FPoint& point, const LineDiagonal& l) {
auto checkCollision(const SDL_FPoint& point, const LineDiagonal& l) -> bool {
SDL_Point p = toSDLPoint(point);
// Comprueba si el punto está en alineado con la linea
@@ -317,7 +316,7 @@ bool checkCollision(const SDL_FPoint& point, const LineDiagonal& l) {
}
// Convierte una cadena a un indice de la paleta
Uint8 stringToColor(const std::string& str) {
auto stringToColor(const std::string& str) -> Uint8 {
// Mapas de colores para cada paleta
static const std::unordered_map<std::string, Uint8> PALETTE_MAP = {
{"black", 0},
@@ -355,7 +354,7 @@ Uint8 stringToColor(const std::string& str) {
}
// Convierte una cadena a un entero de forma segura
int safeStoi(const std::string& value, int default_value) {
auto safeStoi(const std::string& value, int default_value) -> int {
try {
return std::stoi(value);
} catch (const std::exception&) {
@@ -364,19 +363,19 @@ int safeStoi(const std::string& value, int default_value) {
}
// Convierte una cadena a un booleano
bool stringToBool(const std::string& str) {
auto stringToBool(const std::string& str) -> bool {
std::string lower_str = str;
std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(), ::tolower);
std::ranges::transform(lower_str, lower_str.begin(), ::tolower);
return (lower_str == "true" || lower_str == "1" || lower_str == "yes" || lower_str == "on");
}
// Convierte un booleano a una cadena
std::string boolToString(bool value) {
auto boolToString(bool value) -> std::string {
return value ? "1" : "0";
}
// Compara dos colores
bool colorAreEqual(Color color1, Color color2) {
auto colorAreEqual(Color color1, Color color2) -> bool {
const bool R = color1.r == color2.r;
const bool G = color1.g == color2.g;
const bool B = color1.b == color2.b;
@@ -385,26 +384,26 @@ bool colorAreEqual(Color color1, Color color2) {
}
// Función para convertir un string a minúsculas
std::string toLower(const std::string& str) {
auto toLower(const std::string& str) -> std::string {
std::string lower_str = str;
std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(), ::tolower);
std::ranges::transform(lower_str, lower_str.begin(), ::tolower);
return lower_str;
}
// Función para convertir un string a mayúsculas
std::string toUpper(const std::string& str) {
auto toUpper(const std::string& str) -> std::string {
std::string upper_str = str;
std::transform(upper_str.begin(), upper_str.end(), upper_str.begin(), ::toupper);
std::ranges::transform(upper_str, upper_str.begin(), ::toupper);
return upper_str;
}
// Obtiene el nombre de un fichero a partir de una ruta completa
std::string getFileName(const std::string& path) {
auto getFileName(const std::string& path) -> std::string {
return std::filesystem::path(path).filename().string();
}
// Obtiene la ruta eliminando el nombre del fichero
std::string getPath(const std::string& full_path) {
auto getPath(const std::string& full_path) -> std::string {
std::filesystem::path path(full_path);
return path.parent_path().string();
}
@@ -418,12 +417,12 @@ void printWithDots(const std::string& text1, const std::string& text2, const std
std::cout.fill('.');
std::cout << text2;
std::cout << text3 << std::endl;
std::cout << text3 << '\n';
}
// 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();
auto stringInVector(const std::vector<std::string>& vec, const std::string& str) -> bool {
return std::ranges::find(vec, str) != vec.end();
}
// Hace sonar la música

View File

@@ -80,73 +80,73 @@ struct Color {
};
// Calcula el cuadrado de la distancia entre dos puntos
double distanceSquared(int x1, int y1, int x2, int y2);
auto distanceSquared(int x1, int y1, int x2, int y2) -> double;
// Detector de colisiones entre dos circulos
bool checkCollision(const Circle& a, const Circle& b);
auto checkCollision(const Circle& a, const Circle& b) -> bool;
// Detector de colisiones entre un circulo y un rectangulo
bool checkCollision(const Circle& a, const SDL_FRect& rect);
auto checkCollision(const Circle& a, const SDL_FRect& rect) -> bool;
// Detector de colisiones entre un dos rectangulos
bool checkCollision(const SDL_FRect& a, const SDL_FRect& b);
auto checkCollision(const SDL_FRect& a, const SDL_FRect& b) -> bool;
// Detector de colisiones entre un punto y un rectangulo
bool checkCollision(const SDL_FPoint& p, const SDL_FRect& r);
auto checkCollision(const SDL_FPoint& p, const SDL_FRect& r) -> bool;
// Detector de colisiones entre una linea horizontal y un rectangulo
bool checkCollision(const LineHorizontal& l, const SDL_FRect& r);
auto checkCollision(const LineHorizontal& l, const SDL_FRect& r) -> bool;
// Detector de colisiones entre una linea vertical y un rectangulo
bool checkCollision(const LineVertical& l, const SDL_FRect& r);
auto checkCollision(const LineVertical& l, const SDL_FRect& r) -> bool;
// Detector de colisiones entre una linea horizontal y un punto
bool checkCollision(const LineHorizontal& l, const SDL_FPoint& p);
auto checkCollision(const LineHorizontal& l, const SDL_FPoint& p) -> bool;
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(const Line& l1, const Line& l2);
auto checkCollision(const Line& l1, const Line& l2) -> SDL_Point;
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(const LineDiagonal& l1, const LineVertical& l2);
auto checkCollision(const LineDiagonal& l1, const LineVertical& l2) -> SDL_Point;
// Detector de colisiones entre un punto y una linea diagonal
bool checkCollision(const SDL_FPoint& p, const LineDiagonal& l);
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
Uint8 stringToColor(const std::string& str);
auto stringToColor(const std::string& str) -> Uint8;
// Convierte una cadena a un entero de forma segura
int safeStoi(const std::string& value, int default_value = 0);
auto safeStoi(const std::string& value, int default_value = 0) -> int;
// Convierte una cadena a un booleano
bool stringToBool(const std::string& str);
auto stringToBool(const std::string& str) -> bool;
// Convierte un booleano a una cadena
std::string boolToString(bool value);
auto boolToString(bool value) -> std::string;
// Compara dos colores
bool colorAreEqual(Color color1, Color color2);
auto colorAreEqual(Color color1, Color color2) -> bool;
// Convierte una cadena a minusculas
std::string toLower(const std::string& str);
auto toLower(const std::string& str) -> std::string;
// Convierte una cadena a mayúsculas
std::string toUpper(const std::string& str);
auto toUpper(const std::string& str) -> std::string;
// Obtiene el nombre de un fichero a partir de una ruta
std::string getFileName(const std::string& path);
auto getFileName(const std::string& path) -> std::string;
// Obtiene la ruta eliminando el nombre del fichero
std::string getPath(const std::string& full_path);
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
bool stringInVector(const std::vector<std::string>& vec, const std::string& str);
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);
@@ -154,7 +154,7 @@ 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 SDL_Rect toSDLRect(const SDL_FRect& frect) {
inline auto toSDLRect(const SDL_FRect& frect) -> SDL_Rect {
SDL_Rect rect;
rect.x = static_cast<int>(frect.x);
rect.y = static_cast<int>(frect.y);
@@ -163,7 +163,7 @@ inline SDL_Rect toSDLRect(const SDL_FRect& frect) {
return rect;
}
inline SDL_Point toSDLPoint(const SDL_FPoint& fpoint) {
inline auto toSDLPoint(const SDL_FPoint& fpoint) -> SDL_Point {
SDL_Point point;
point.x = static_cast<int>(fpoint.x);
point.y = static_cast<int>(fpoint.y);