style: aplicar checks modernize-* (215 fixes)

Cambios aplicados:
- [[nodiscard]] añadido a funciones que retornan valores
- .starts_with() en lugar de .find() == 0
- Inicializadores designados {.x=0, .y=0}
- auto en castings obvios
- = default para constructores triviales
- Funciones deleted movidas a public
- std::numbers::pi_v<float> (C++20)

Checks excluidos:
- use-trailing-return-type: Estilo controversial
- avoid-c-arrays: Arrays C aceptables en ciertos contextos
This commit is contained in:
2025-12-18 20:16:46 +01:00
parent fdfb84170f
commit 7f6af6dd00
42 changed files with 178 additions and 178 deletions

View File

@@ -11,7 +11,7 @@
namespace Graphics {
Shape::Shape(const std::string& filepath)
: centre_({0.0F, 0.0F}),
: centre_({.x = 0.0F, .y = 0.0F}),
escala_defecte_(1.0F),
nom_("unnamed") {
carregar(filepath);
@@ -106,7 +106,7 @@ bool Shape::starts_with(const std::string& str,
if (str.length() < prefix.length()) {
return false;
}
return str.compare(0, prefix.length(), prefix) == 0;
return str.starts_with(prefix);
}
// Helper: extract value after ':'
@@ -128,7 +128,7 @@ void Shape::parse_center(const std::string& value) {
centre_.y = std::stof(trim(val.substr(comma + 1)));
} catch (...) {
std::cerr << "[Shape] Warning: centre invàlid, usant (0,0)" << std::endl;
centre_ = {0.0F, 0.0F};
centre_ = {.x = 0.0F, .y = 0.0F};
}
}
}

View File

@@ -36,16 +36,16 @@ class Shape {
bool parsejar_fitxer(const std::string& contingut);
// Getters
const std::vector<ShapePrimitive>& get_primitives() const {
[[nodiscard]] const std::vector<ShapePrimitive>& get_primitives() const {
return primitives_;
}
const Punt& get_centre() const { return centre_; }
float get_escala_defecte() const { return escala_defecte_; }
bool es_valida() const { return !primitives_.empty(); }
[[nodiscard]] const Punt& get_centre() const { return centre_; }
[[nodiscard]] float get_escala_defecte() const { return escala_defecte_; }
[[nodiscard]] bool es_valida() const { return !primitives_.empty(); }
// Info de depuració
std::string get_nom() const { return nom_; }
size_t get_num_primitives() const { return primitives_.size(); }
[[nodiscard]] std::string get_nom() const { return nom_; }
[[nodiscard]] size_t get_num_primitives() const { return primitives_.size(); }
private:
std::vector<ShapePrimitive> primitives_;
@@ -54,11 +54,11 @@ class Shape {
std::string nom_; // Nom de la forma (per depuració)
// Helpers privats per parsejar
std::string trim(const std::string& str) const;
bool starts_with(const std::string& str, const std::string& prefix) const;
std::string extract_value(const std::string& line) const;
[[nodiscard]] std::string trim(const std::string& str) const;
[[nodiscard]] bool starts_with(const std::string& str, const std::string& prefix) const;
[[nodiscard]] std::string extract_value(const std::string& line) const;
void parse_center(const std::string& value);
std::vector<Punt> parse_points(const std::string& str) const;
[[nodiscard]] std::vector<Punt> parse_points(const std::string& str) const;
};
} // namespace Graphics

View File

@@ -24,7 +24,7 @@ std::shared_ptr<Shape> ShapeLoader::load(const std::string& filename) {
// Normalize path: "ship.shp" → "shapes/ship.shp"
// "logo/letra_j.shp" → "shapes/logo/letra_j.shp"
std::string normalized = filename;
if (normalized.find("shapes/") != 0) {
if (!normalized.starts_with("shapes/")) {
// Doesn't start with "shapes/", so add it
normalized = "shapes/" + normalized;
}
@@ -75,7 +75,7 @@ std::string ShapeLoader::resolve_path(const std::string& filename) {
}
// Si ja conté el prefix base_path, usar-lo directament
if (filename.find(base_path_) == 0) {
if (filename.starts_with(base_path_)) {
return filename;
}

View File

@@ -57,13 +57,13 @@ class Starfield {
void inicialitzar_estrella(Estrella& estrella) const;
// Verificar si una estrella està fora de l'àrea
bool fora_area(const Estrella& estrella) const;
[[nodiscard]] bool fora_area(const Estrella& estrella) const;
// Calcular escala dinàmica segons distància del centre
float calcular_escala(const Estrella& estrella) const;
[[nodiscard]] float calcular_escala(const Estrella& estrella) const;
// Calcular brightness dinàmica segons distància del centre
float calcular_brightness(const Estrella& estrella) const;
[[nodiscard]] float calcular_brightness(const Estrella& estrella) const;
// Dades
std::vector<Estrella> estrelles_;

View File

@@ -201,7 +201,7 @@ void VectorText::render(const std::string& text, const Punt& posicio, float esca
// Iterar sobre cada byte del string (con detecció UTF-8)
for (size_t i = 0; i < text.length(); i++) {
unsigned char c = static_cast<unsigned char>(text[i]);
auto c = static_cast<unsigned char>(text[i]);
// Detectar copyright UTF-8 (0xC2 0xA9)
if (c == 0xC2 && i + 1 < text.length() &&
@@ -222,7 +222,7 @@ void VectorText::render(const std::string& text, const Punt& posicio, float esca
// Renderizar carácter
// Ajustar X e Y para que posicio represente esquina superior izquierda
// (render_shape espera el centro, así que sumamos la mitad de ancho y altura)
Punt char_pos = {current_x + (char_width_scaled / 2.0F), posicio.y + (char_height_scaled / 2.0F)};
Punt char_pos = {.x = current_x + (char_width_scaled / 2.0F), .y = posicio.y + (char_height_scaled / 2.0F)};
Rendering::render_shape(renderer_, it->second, char_pos, 0.0F, escala, true, 1.0F, brightness);
// Avanzar posición
@@ -244,8 +244,8 @@ void VectorText::render_centered(const std::string& text, const Punt& centre_pun
// Calcular posició de l'esquina superior esquerra
// restant la meitat de les dimensions del punt central
Punt posicio_esquerra = {
centre_punt.x - (text_width / 2.0F),
centre_punt.y - (text_height / 2.0F)};
.x = centre_punt.x - (text_width / 2.0F),
.y = centre_punt.y - (text_height / 2.0F)};
// Delegar al mètode render() existent
render(text, posicio_esquerra, escala, spacing, brightness);
@@ -262,7 +262,7 @@ float VectorText::get_text_width(const std::string& text, float escala, float sp
// Contar caracteres visuals (no bytes) - manejar UTF-8
size_t visual_chars = 0;
for (size_t i = 0; i < text.length(); i++) {
unsigned char c = static_cast<unsigned char>(text[i]);
auto c = static_cast<unsigned char>(text[i]);
// Detectar copyright UTF-8 (0xC2 0xA9) - igual que render()
if (c == 0xC2 && i + 1 < text.length() &&

View File

@@ -36,20 +36,20 @@ class VectorText {
void render_centered(const std::string& text, const Punt& centre_punt, float escala = 1.0F, float spacing = 2.0F, float brightness = 1.0F);
// Calcular ancho total de un string (útil para centrado)
float get_text_width(const std::string& text, float escala = 1.0F, float spacing = 2.0F) const;
[[nodiscard]] float get_text_width(const std::string& text, float escala = 1.0F, float spacing = 2.0F) const;
// Calcular altura del texto (útil para centrado vertical)
float get_text_height(float escala = 1.0F) const;
[[nodiscard]] float get_text_height(float escala = 1.0F) const;
// Verificar si un carácter está soportado
bool is_supported(char c) const;
[[nodiscard]] bool is_supported(char c) const;
private:
SDL_Renderer* renderer_;
std::unordered_map<char, std::shared_ptr<Shape>> chars_;
void load_charset();
std::string get_shape_filename(char c) const;
[[nodiscard]] std::string get_shape_filename(char c) const;
};
} // namespace Graphics