7ee359b910
Sweep final del naming a CamelCase/camelBack/lower_case:
Fitxers renombrats:
- effects/gestor_puntuacio_flotant.{hpp,cpp} -> floating_score_manager.{hpp,cpp}
- effects/puntuacio_flotant.hpp -> floating_score.hpp
Tipus (CamelCase):
- GestorPuntuacioFlotant -> FloatingScoreManager
- PuntuacioFlotant -> FloatingScore
- ConfigStage -> StageConfig
- ConfigSistemaStages -> StageSystemConfig
- NauTitol -> TitleShip
- EstatNau -> ShipState
Metodes publics (camelBack):
- obte_renderer -> getRenderer
- get_num_actius -> getActiveCount
- calcular_direccio_explosio -> computeExplosionDirection
- trobar_slot_lliure -> findFreeSlot
- explotar -> explode
- reiniciar -> reset
- es_valida -> isValid
- parsejar_fitxer -> parseFile
- carregar -> load
- crear_explosio -> createExplosion
- registrar_puntuacio -> registerScore
- construir_marcador -> buildScoreboard
- render_centered -> renderCentered
Camps struct publics (snake_case):
- actiu/actius -> active
- rotacio -> rotation, rotacio_visual -> visual_rotation
- acceleracio -> acceleration
- velocitat -> velocity
- escala/escala_inicial/objectiu/actual -> scale/initial_scale/...
- posicio/posicio_inicial/objectiu/actual -> position/initial_position/...
- fase_oscilacio -> oscillation_phase
- temps_estat -> state_time
- jugador_id -> player_id
- estat -> state
- brillantor -> brightness
- tipus -> type
Camps privats (sufix _):
- naus_ -> ships_, orni_ -> enemies_, bales_ -> bullets_
- gestor_puntuacio_ -> floating_score_manager_
- punt_mort_ -> death_position_, punt_spawn_ -> spawn_position_
- itocado_per_jugador_ -> hit_timer_per_player_
- vides_per_jugador_ -> lives_per_player_
- puntuacio_per_jugador_ -> score_per_player_
- estat_game_over_ -> game_over_state_
- continues_usados_ -> continues_used_
Constants:
- MARGE_ESQ/DRET/DALT/BAIX -> MARGIN_LEFT/RIGHT/TOP/BOTTOM
Variables locals i parametres comuns (snake_case):
- nau -> ship, enemic -> enemy, bala -> bullet
- forma -> shape, punt(s) -> point(s)
- jugador -> player, partida -> match
- temps -> time, missatge -> message
Diff: 59 fitxers, +1000/-1000 (simetric). Compila i enllaça.
Pendents per a futures fases (no bloquejants):
- Comentaris de capçalera en catala -> castella
- Variables locals/parametres minoritaris en catala
- Include guards (queden alguns #ifndef en lloc de #pragma once)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
160 lines
4.6 KiB
C++
160 lines
4.6 KiB
C++
// shape.cpp - Implementació del sistema de formes vectorials
|
|
// © 2025 Port a C++20 amb SDL3
|
|
|
|
#include "core/graphics/shape.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
|
|
namespace Graphics {
|
|
|
|
Shape::Shape(const std::string& filepath)
|
|
: center_({.x = 0.0F, .y = 0.0F}),
|
|
escala_defecte_(1.0F),
|
|
nom_("unnamed") {
|
|
load(filepath);
|
|
}
|
|
|
|
bool Shape::load(const std::string& filepath) {
|
|
// Llegir fitxer
|
|
std::ifstream file(filepath);
|
|
if (!file.is_open()) {
|
|
std::cerr << "[Shape] Error: no es pot obrir " << filepath << '\n';
|
|
return false;
|
|
}
|
|
|
|
// Llegir tot el contingut
|
|
std::stringstream buffer;
|
|
buffer << file.rdbuf();
|
|
std::string contingut = buffer.str();
|
|
file.close();
|
|
|
|
// Parsejar
|
|
return parseFile(contingut);
|
|
}
|
|
|
|
bool Shape::parseFile(const std::string& contingut) {
|
|
std::istringstream iss(contingut);
|
|
std::string line;
|
|
|
|
while (std::getline(iss, line)) {
|
|
// Trim whitespace
|
|
line = trim(line);
|
|
|
|
// Skip comments and blanks
|
|
if (line.empty() || line[0] == '#') {
|
|
continue;
|
|
}
|
|
|
|
// Parse command
|
|
if (starts_with(line, "name:")) {
|
|
nom_ = trim(extract_value(line));
|
|
} else if (starts_with(line, "scale:")) {
|
|
try {
|
|
escala_defecte_ = std::stof(extract_value(line));
|
|
} catch (...) {
|
|
std::cerr << "[Shape] Warning: scale invàlida, usant 1.0" << '\n';
|
|
escala_defecte_ = 1.0F;
|
|
}
|
|
} else if (starts_with(line, "center:")) {
|
|
parse_center(extract_value(line));
|
|
} else if (starts_with(line, "polyline:")) {
|
|
auto points = parse_points(extract_value(line));
|
|
if (points.size() >= 2) {
|
|
primitives_.push_back({PrimitiveType::POLYLINE, points});
|
|
} else {
|
|
std::cerr << "[Shape] Warning: polyline amb menys de 2 points ignorada"
|
|
<< '\n';
|
|
}
|
|
} else if (starts_with(line, "line:")) {
|
|
auto points = parse_points(extract_value(line));
|
|
if (points.size() == 2) {
|
|
primitives_.push_back({PrimitiveType::LINE, points});
|
|
} else {
|
|
std::cerr << "[Shape] Warning: line ha de tenir exactament 2 points"
|
|
<< '\n';
|
|
}
|
|
}
|
|
// Comandes desconegudes ignorades silenciosament
|
|
}
|
|
|
|
if (primitives_.empty()) {
|
|
std::cerr << "[Shape] Error: cap primitiva carregada" << '\n';
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Helper: trim whitespace
|
|
std::string Shape::trim(const std::string& str) const {
|
|
const char* whitespace = " \t\n\r";
|
|
size_t start = str.find_first_not_of(whitespace);
|
|
if (start == std::string::npos) {
|
|
return "";
|
|
}
|
|
|
|
size_t end = str.find_last_not_of(whitespace);
|
|
return str.substr(start, end - start + 1);
|
|
}
|
|
|
|
// Helper: starts_with
|
|
bool Shape::starts_with(const std::string& str,
|
|
const std::string& prefix) const {
|
|
if (str.length() < prefix.length()) {
|
|
return false;
|
|
}
|
|
return str.starts_with(prefix);
|
|
}
|
|
|
|
// Helper: extract value after ':'
|
|
std::string Shape::extract_value(const std::string& line) const {
|
|
size_t colon = line.find(':');
|
|
if (colon == std::string::npos) {
|
|
return "";
|
|
}
|
|
return line.substr(colon + 1);
|
|
}
|
|
|
|
// Helper: parse center "x, y"
|
|
void Shape::parse_center(const std::string& value) {
|
|
std::string val = trim(value);
|
|
size_t comma = val.find(',');
|
|
if (comma != std::string::npos) {
|
|
try {
|
|
center_.x = std::stof(trim(val.substr(0, comma)));
|
|
center_.y = std::stof(trim(val.substr(comma + 1)));
|
|
} catch (...) {
|
|
std::cerr << "[Shape] Warning: centre invàlid, usant (0,0)" << '\n';
|
|
center_ = {.x = 0.0F, .y = 0.0F};
|
|
}
|
|
}
|
|
}
|
|
|
|
// Helper: parse points "x1,y1 x2,y2 x3,y3"
|
|
std::vector<Vec2> Shape::parse_points(const std::string& str) const {
|
|
std::vector<Vec2> points;
|
|
std::istringstream iss(trim(str));
|
|
std::string pair;
|
|
|
|
while (iss >> pair) { // Whitespace-separated
|
|
size_t comma = pair.find(',');
|
|
if (comma != std::string::npos) {
|
|
try {
|
|
float x = std::stof(pair.substr(0, comma));
|
|
float y = std::stof(pair.substr(comma + 1));
|
|
points.push_back({x, y});
|
|
} catch (...) {
|
|
std::cerr << "[Shape] Warning: point invàlid ignorat: " << pair
|
|
<< '\n';
|
|
}
|
|
}
|
|
}
|
|
|
|
return points;
|
|
}
|
|
|
|
} // namespace Graphics
|