Fase 1a: Punt -> Vec2 amb operadors moderns

Primera sub-fase del naming sweep. Punt era un struct sense
operacions, conservat per compatibilitat amb el Pascal original.
Substituit per Vec2, un aggregate amb operadors aritmetics, dot,
length, normalized i length_squared (camelBack: lengthSquared)
seguint les regles del .clang-tidy del projecte.

Canvis:
- core/types.hpp reescrit: nou struct Vec2 amb +=,-=,*=,/=,
  unary -, ==, dot, length, lengthSquared, normalized
- Operadors fora de la classe: +, -, *, / (amb float per ambdues
  bandes), - unari, ==
- Vec2 segueix sent aggregate (sense constructors definits):
  els 'designated initializers' del codi existent funcionen igual:
  Vec2{.x = ..., .y = ...}
- Sed global sobre 35 fitxers: tots els 'Punt' -> 'Vec2'

Net: 35 fitxers tocats, +180 / -114. Compila i enllaça.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-19 11:33:27 +02:00
parent 6cf990bc1d
commit cd38101f99
35 changed files with 180 additions and 114 deletions
+1 -1
View File
@@ -464,7 +464,7 @@ constexpr float FLOATING_SCALE = 1.0F * SHIP_BASE_SCALE; // Flotant: escala
// Fórmula: (radi màxim de la nau * escala d'entrada) + marge
constexpr float ENTRY_OFFSET = (SHIP_MAX_RADIUS * ENTRY_SCALE_START) + ENTRY_OFFSET_MARGIN;
// Punt de fuga (centre per a l'animació de sortida)
// Vec2 de fuga (centre per a l'animació de sortida)
constexpr float VANISHING_POINT_X = CENTER_X; // 320.0f
constexpr float VANISHING_POINT_Y = CENTER_Y; // 240.0f
+2 -2
View File
@@ -27,7 +27,7 @@ class Entitat {
[[nodiscard]] virtual bool es_collidable() const { return false; }
// Getters comuns (inline, sense overhead)
[[nodiscard]] const Punt& get_centre() const { return centre_; }
[[nodiscard]] const Vec2& get_centre() const { return centre_; }
[[nodiscard]] float get_angle() const { return angle_; }
[[nodiscard]] float get_brightness() const { return brightness_; }
[[nodiscard]] const std::shared_ptr<Graphics::Shape>& get_forma() const { return forma_; }
@@ -36,7 +36,7 @@ class Entitat {
// Estat comú (accés directe, sense overhead)
SDL_Renderer* renderer_;
std::shared_ptr<Graphics::Shape> forma_;
Punt centre_;
Vec2 centre_;
float angle_{0.0F};
float brightness_{1.0F};
+2 -2
View File
@@ -134,8 +134,8 @@ void Shape::parse_center(const std::string& value) {
}
// Helper: parse points "x1,y1 x2,y2 x3,y3"
std::vector<Punt> Shape::parse_points(const std::string& str) const {
std::vector<Punt> points;
std::vector<Vec2> Shape::parse_points(const std::string& str) const {
std::vector<Vec2> points;
std::istringstream iss(trim(str));
std::string pair;
+4 -4
View File
@@ -19,7 +19,7 @@ enum class PrimitiveType {
// Primitiva individual (polyline o line)
struct ShapePrimitive {
PrimitiveType type;
std::vector<Punt> points; // 2+ punts per polyline, exactament 2 per line
std::vector<Vec2> points; // 2+ punts per polyline, exactament 2 per line
};
// Classe Shape - representa una forma vectorial carregada des de .shp
@@ -39,7 +39,7 @@ class Shape {
[[nodiscard]] const std::vector<ShapePrimitive>& get_primitives() const {
return primitives_;
}
[[nodiscard]] const Punt& get_centre() const { return centre_; }
[[nodiscard]] const Vec2& get_centre() const { return centre_; }
[[nodiscard]] float get_escala_defecte() const { return escala_defecte_; }
[[nodiscard]] bool es_valida() const { return !primitives_.empty(); }
@@ -49,7 +49,7 @@ class Shape {
private:
std::vector<ShapePrimitive> primitives_;
Punt centre_; // Centre/origen de la forma
Vec2 centre_; // Centre/origen de la forma
float escala_defecte_; // Escala per defecte (normalment 1.0)
std::string nom_; // Nom de la forma (per depuració)
@@ -58,7 +58,7 @@ class Shape {
[[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);
[[nodiscard]] std::vector<Punt> parse_points(const std::string& str) const;
[[nodiscard]] std::vector<Vec2> parse_points(const std::string& str) const;
};
} // namespace Graphics
+1 -1
View File
@@ -15,7 +15,7 @@ namespace Graphics {
// Constructor
Starfield::Starfield(SDL_Renderer* renderer,
const Punt& punt_fuga,
const Vec2& punt_fuga,
const SDL_FRect& area,
int densitat)
: renderer_(renderer),
+4 -4
View File
@@ -30,7 +30,7 @@ class Starfield {
// - area: rectangle on actuen les estrelles (SDL_FRect)
// - densitat: nombre total d'estrelles (es divideix entre capes)
Starfield(SDL_Renderer* renderer,
const Punt& punt_fuga,
const Vec2& punt_fuga,
const SDL_FRect& area,
int densitat = 150);
@@ -41,13 +41,13 @@ class Starfield {
void dibuixar();
// Setters per ajustar paràmetres en temps real
void set_punt_fuga(const Punt& punt) { punt_fuga_ = punt; }
void set_punt_fuga(const Vec2& punt) { punt_fuga_ = punt; }
void set_brightness(float multiplier);
private:
// Estructura interna per cada estrella
struct Estrella {
Punt posicio; // Posició actual
Vec2 posicio; // Posició actual
float angle; // Angle de moviment (radians)
float distancia_centre; // Distància normalitzada del centre (0.0-1.0)
int capa; // Índex de capa (0=lluny, 1=mitjà, 2=prop)
@@ -72,7 +72,7 @@ class Starfield {
SDL_Renderer* renderer_;
// Configuració
Punt punt_fuga_; // Punt d'origen de les estrelles
Vec2 punt_fuga_; // Vec2 d'origen de les estrelles
SDL_FRect area_; // Àrea activa
float radi_max_; // Distància màxima del centre al límit de pantalla
int densitat_; // Nombre total d'estrelles
+4 -4
View File
@@ -182,7 +182,7 @@ bool VectorText::is_supported(char c) const {
return chars_.contains(c);
}
void VectorText::render(const std::string& text, const Punt& posicio, float escala, float spacing, float brightness) const {
void VectorText::render(const std::string& text, const Vec2& posicio, float escala, float spacing, float brightness) const {
if (renderer_ == nullptr) {
return;
}
@@ -223,7 +223,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 = {.x = current_x + (char_width_scaled / 2.0F), .y = posicio.y + (char_height_scaled / 2.0F)};
Vec2 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, 1.0F, brightness);
// Avanzar posición
@@ -237,14 +237,14 @@ void VectorText::render(const std::string& text, const Punt& posicio, float esca
}
}
void VectorText::render_centered(const std::string& text, const Punt& centre_punt, float escala, float spacing, float brightness) const {
void VectorText::render_centered(const std::string& text, const Vec2& centre_punt, float escala, float spacing, float brightness) const {
// Calcular dimensions del text
float text_width = get_text_width(text, escala, spacing);
float text_height = get_text_height(escala);
// Calcular posició de l'esquina superior esquerra
// restant la meitat de les dimensions del punt central
Punt posicio_esquerra = {
Vec2 posicio_esquerra = {
.x = centre_punt.x - (text_width / 2.0F),
.y = centre_punt.y - (text_height / 2.0F)};
+2 -2
View File
@@ -25,7 +25,7 @@ class VectorText {
// - escala: factor de escala (1.0 = 20×40 px por carácter)
// - spacing: espacio entre caracteres en píxeles (a escala 1.0)
// - brightness: factor de brillantor (0.0-1.0, default 1.0 = màxima brillantor)
void render(const std::string& text, const Punt& posicio, float escala = 1.0F, float spacing = 2.0F, float brightness = 1.0F) const;
void render(const std::string& text, const Vec2& posicio, float escala = 1.0F, float spacing = 2.0F, float brightness = 1.0F) const;
// Renderizar string centrado en un punto
// - text: cadena a renderizar
@@ -33,7 +33,7 @@ class VectorText {
// - escala: factor de escala (1.0 = 20×40 px por carácter)
// - spacing: espacio entre caracteres en píxeles (a escala 1.0)
// - brightness: factor de brillantor (0.0-1.0, default 1.0 = màxima brillantor)
void render_centered(const std::string& text, const Punt& centre_punt, float escala = 1.0F, float spacing = 2.0F, float brightness = 1.0F) const;
void render_centered(const std::string& text, const Vec2& centre_punt, float escala = 1.0F, float spacing = 2.0F, float brightness = 1.0F) const;
// Calcular ancho total de un string (útil para centrado)
[[nodiscard]] float get_text_width(const std::string& text, float escala = 1.0F, float spacing = 2.0F) const;
+2 -2
View File
@@ -20,8 +20,8 @@ inline bool check_collision(const Entities::Entitat& a, const Entities::Entitat&
float suma_radis_sq = suma_radis * suma_radis;
// Comprovació distància al quadrat (sense sqrt)
const Punt& pos_a = a.get_centre();
const Punt& pos_b = b.get_centre();
const Vec2& pos_a = a.get_centre();
const Vec2& pos_b = b.get_centre();
float dx = pos_a.x - pos_b.x;
float dy = pos_a.y - pos_b.y;
float dist_sq = (dx * dx) + (dy * dy);
+9 -9
View File
@@ -11,7 +11,7 @@
namespace Rendering {
// Helper: aplicar rotació 3D a un punt 2D (assumeix Z=0)
static Punt apply_3d_rotation(float x, float y, const Rotation3D& rot) {
static Vec2 apply_3d_rotation(float x, float y, const Rotation3D& rot) {
float z = 0.0F; // Tots els punts 2D comencen a Z=0
// Pitch (rotació eix X): cabeceo arriba/baix
@@ -42,14 +42,14 @@ static Punt apply_3d_rotation(float x, float y, const Rotation3D& rot) {
}
// Helper: transformar un punt amb rotació, escala i trasllació
static Punt transform_point(const Punt& point, const Punt& shape_centre, const Punt& posicio, float angle, float escala, const Rotation3D* rotation_3d) {
static Vec2 transform_point(const Vec2& point, const Vec2& shape_centre, const Vec2& posicio, float angle, float escala, const Rotation3D* rotation_3d) {
// 1. Centrar el punt respecte al centre de la forma
float centered_x = point.x - shape_centre.x;
float centered_y = point.y - shape_centre.y;
// 2. Aplicar rotació 3D (si es proporciona)
if ((rotation_3d != nullptr) && rotation_3d->has_rotation()) {
Punt rotated_3d = apply_3d_rotation(centered_x, centered_y, *rotation_3d);
Vec2 rotated_3d = apply_3d_rotation(centered_x, centered_y, *rotation_3d);
centered_x = rotated_3d.x;
centered_y = rotated_3d.y;
}
@@ -74,7 +74,7 @@ static Punt transform_point(const Punt& point, const Punt& shape_centre, const P
void render_shape(SDL_Renderer* renderer,
const std::shared_ptr<Graphics::Shape>& shape,
const Punt& posicio,
const Vec2& posicio,
float angle,
float escala,
float progress,
@@ -91,15 +91,15 @@ void render_shape(SDL_Renderer* renderer,
}
// Obtenir el centre de la forma per a transformacions
const Punt& shape_centre = shape->get_centre();
const Vec2& shape_centre = shape->get_centre();
// Iterar sobre totes les primitives
for (const auto& primitive : shape->get_primitives()) {
if (primitive.type == Graphics::PrimitiveType::POLYLINE) {
// POLYLINE: connectar punts consecutius
for (size_t i = 0; i < primitive.points.size() - 1; i++) {
Punt p1 = transform_point(primitive.points[i], shape_centre, posicio, angle, escala, rotation_3d);
Punt p2 = transform_point(primitive.points[i + 1], shape_centre, posicio, angle, escala, rotation_3d);
Vec2 p1 = transform_point(primitive.points[i], shape_centre, posicio, angle, escala, rotation_3d);
Vec2 p2 = transform_point(primitive.points[i + 1], shape_centre, posicio, angle, escala, rotation_3d);
linea(renderer, static_cast<int>(p1.x), static_cast<int>(p1.y),
static_cast<int>(p2.x), static_cast<int>(p2.y), brightness);
@@ -107,8 +107,8 @@ void render_shape(SDL_Renderer* renderer,
} else { // PrimitiveType::LINE
// LINE: exactament 2 punts
if (primitive.points.size() >= 2) {
Punt p1 = transform_point(primitive.points[0], shape_centre, posicio, angle, escala, rotation_3d);
Punt p2 = transform_point(primitive.points[1], shape_centre, posicio, angle, escala, rotation_3d);
Vec2 p1 = transform_point(primitive.points[0], shape_centre, posicio, angle, escala, rotation_3d);
Vec2 p2 = transform_point(primitive.points[1], shape_centre, posicio, angle, escala, rotation_3d);
linea(renderer, static_cast<int>(p1.x), static_cast<int>(p1.y),
static_cast<int>(p2.x), static_cast<int>(p2.y), brightness);
+1 -1
View File
@@ -42,7 +42,7 @@ struct Rotation3D {
// - brightness: factor de brillantor (0.0-1.0, default 1.0 = màxima brillantor)
void render_shape(SDL_Renderer* renderer,
const std::shared_ptr<Graphics::Shape>& shape,
const Punt& posicio,
const Vec2& posicio,
float angle,
float escala = 1.0F,
float progress = 1.0F,
+69 -3
View File
@@ -1,6 +1,72 @@
// types.hpp - Tipos básicos compartidos
// © 2025 Orni Attack
#pragma once
// Punt cartesià - ÚNICA estructura de coordenades del joc
struct Punt {
float x, y;
#include <cmath>
// Vector 2D cartesiano - única estructura de coordenadas del juego.
// Aggregate (sin constructores definidos) para soportar designated initializers:
// Vec2{.x = 1.0F, .y = 2.0F}
// y aggregate initialization clásica:
// Vec2{1.0F, 2.0F}
struct Vec2 {
float x{0.0F};
float y{0.0F};
constexpr auto operator+=(const Vec2& o) -> Vec2& {
x += o.x;
y += o.y;
return *this;
}
constexpr auto operator-=(const Vec2& o) -> Vec2& {
x -= o.x;
y -= o.y;
return *this;
}
constexpr auto operator*=(float s) -> Vec2& {
x *= s;
y *= s;
return *this;
}
constexpr auto operator/=(float s) -> Vec2& {
x /= s;
y /= s;
return *this;
}
[[nodiscard]] auto lengthSquared() const -> float { return (x * x) + (y * y); }
[[nodiscard]] auto length() const -> float { return std::sqrt(lengthSquared()); }
[[nodiscard]] auto dot(const Vec2& o) const -> float { return (x * o.x) + (y * o.y); }
// Devuelve el vector normalizado; si la magnitud es 0 devuelve {0,0}.
[[nodiscard]] auto normalized() const -> Vec2 {
const float L = length();
return L > 0.0F ? Vec2{.x = x / L, .y = y / L} : Vec2{};
}
};
constexpr auto operator+(Vec2 a, const Vec2& b) -> Vec2 {
a += b;
return a;
}
constexpr auto operator-(Vec2 a, const Vec2& b) -> Vec2 {
a -= b;
return a;
}
constexpr auto operator*(Vec2 v, float s) -> Vec2 {
v *= s;
return v;
}
constexpr auto operator*(float s, Vec2 v) -> Vec2 {
v *= s;
return v;
}
constexpr auto operator/(Vec2 v, float s) -> Vec2 {
v /= s;
return v;
}
constexpr auto operator-(const Vec2& v) -> Vec2 { return {.x = -v.x, .y = -v.y}; }
constexpr auto operator==(const Vec2& a, const Vec2& b) -> bool {
return a.x == b.x && a.y == b.y;
}