6d0df85e5e
Tanda grande de identifier-naming: 47 métodos privados pasan de snake_case (en su mayoría catalán/spanish) a camelBack inglés. Solo afecta a sus archivos hpp+cpp; ningún símbolo cruza fichero (los públicos quedan para una pasada manual con VS Code). Renames por clase: - ShapeLoader: resolve_path → resolvePath. - VectorText: load_charset → loadCharset, get_shape_filename → getShapeFilename. - Shape: starts_with → startsWith (cuidado de no tocar std::string::starts_with que también usaba), extract_value → extractValue, parse_center → parseCenter, parse_points → parsePoints. - Starfield: inicialitzar_estrella → initStar, fora_area → isOutsideArea, calcular_escala → computeScale, calcular_brightness → computeBrightness. - TitleScene: actualitzar_animacio_logo → updateLogoAnimation, inicialitzar_titol → initTitle. - LogoScene: inicialitzar_lletres → initLetters, actualitzar_explosions → updateExplosions, canviar_estat → changeState, totes_lletres_completes → allLettersComplete. - SpawnController: generar_spawn_events → generateSpawnEvents, seleccionar_tipus_aleatori → selectRandomType, spawn_enemic → spawnEnemy, aplicar_multiplicadors → applyMultipliers. - ShipAnimator: actualitzar_entering/floating/exiting → updateEntering/Floating/Exiting, configurar_nau_p1/p2 → configureShipP1/P2, calcular_posicio_fora_pantalla → computeOffscreenPosition. - GameScene: dibuixar_marges → drawMargins, dibuixar_marcador → drawScoreboard, disparar_bala → fireBullet, obtenir_punt_spawn → getSpawnPoint, unir_jugador → joinPlayer, dibuixar_continue → drawContinue, dibuixar_missatge_stage → drawStageMessage. - StageLoader: parse_metadata/stage/spawn_config/distribution/multipliers/ spawn_mode → parseMetadata/Stage/SpawnConfig/Distribution/Multipliers/ SpawnMode, validar_config → validateConfig. - StageManager: canviar_estat → changeState, processar_init_hud/level_start/playing/level_completed → processInitHud/LevelStart/Playing/LevelCompleted, carregar_stage → loadStage. Métodos públicos y funciones libres (cross-file) quedan a propósito sin tocar — los renombrará el usuario con la herramienta de rename de VS Code. 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
|
|
// © 2026 JailDesigner
|
|
|
|
#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}),
|
|
|
|
nom_("unnamed") {
|
|
load(filepath);
|
|
}
|
|
|
|
auto Shape::load(const std::string& filepath) -> bool {
|
|
// Llegir file
|
|
std::ifstream file(filepath);
|
|
if (!file.is_open()) {
|
|
std::cerr << "[Shape] Error: no es pot obrir " << filepath << '\n';
|
|
return false;
|
|
}
|
|
|
|
// Llegir todo el contingut
|
|
std::stringstream buffer;
|
|
buffer << file.rdbuf();
|
|
std::string contingut = buffer.str();
|
|
file.close();
|
|
|
|
// Parsejar
|
|
return parseFile(contingut);
|
|
}
|
|
|
|
auto Shape::parseFile(const std::string& contingut) -> bool {
|
|
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 (startsWith(line, "name:")) {
|
|
nom_ = trim(extractValue(line));
|
|
} else if (startsWith(line, "scale:")) {
|
|
try {
|
|
escala_defecte_ = std::stof(extractValue(line));
|
|
} catch (...) {
|
|
std::cerr << "[Shape] Warning: scale invàlida, usant 1.0" << '\n';
|
|
escala_defecte_ = 1.0F;
|
|
}
|
|
} else if (startsWith(line, "center:")) {
|
|
parseCenter(extractValue(line));
|
|
} else if (startsWith(line, "polyline:")) {
|
|
auto points = parsePoints(extractValue(line));
|
|
if (points.size() >= 2) {
|
|
primitives_.push_back({PrimitiveType::POLYLINE, points});
|
|
} else {
|
|
std::cerr << "[Shape] Warning: polyline con menys de 2 points ignorada"
|
|
<< '\n';
|
|
}
|
|
} else if (startsWith(line, "line:")) {
|
|
auto points = parsePoints(extractValue(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: sin primitiva carregada" << '\n';
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Helper: trim whitespace
|
|
auto Shape::trim(const std::string& str) const -> std::string {
|
|
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: startsWith
|
|
auto Shape::startsWith(const std::string& str,
|
|
const std::string& prefix) const -> bool {
|
|
if (str.length() < prefix.length()) {
|
|
return false;
|
|
}
|
|
return str.starts_with(prefix);
|
|
}
|
|
|
|
// Helper: extract value after ':'
|
|
auto Shape::extractValue(const std::string& line) const -> std::string {
|
|
size_t colon = line.find(':');
|
|
if (colon == std::string::npos) {
|
|
return "";
|
|
}
|
|
return line.substr(colon + 1);
|
|
}
|
|
|
|
// Helper: parse center "x, y"
|
|
void Shape::parseCenter(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: centro invàlid, usant (0,0)" << '\n';
|
|
center_ = {.x = 0.0F, .y = 0.0F};
|
|
}
|
|
}
|
|
}
|
|
|
|
// Helper: parse points "x1,y1 x2,y2 x3,y3"
|
|
auto Shape::parsePoints(const std::string& str) const -> std::vector<Vec2> {
|
|
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
|