bbbb8d47ae
Identifier-naming: rename de métodos públicos y cross-file al inglés
(camelBack), traducción de campos y locales en el proceso (TitleShip,
StageManager, SpawnController, ShipAnimator, helpers de PlayArea, etc.).
Refactor por cognitive-complexity (>25): GameScene::draw (59→3) con 9
helpers de estado, PhysicsWorld::resolveBodyCollisions (35→5) extrayendo
resolveBodyPair, Options::load{Window,Physics,Audio}ConfigFromYaml
(32/49/57→5/2/3) con templates readField, TitleScene::update (68→4) con
5 sub-pasos por estado + handleSkipInput/handleStartInput +
triggerExitForJoinedPlayers, DebrisManager::explode (39→3) con
extractSegments/spawnDebris/applyAngularVelocity/applyVisualRotation.
use-anyofallof: bucles → std::ranges::any_of/all_of en Input,
ShipAnimator y SpawnController.
readability-static-accessed-through-instance: Director::run y
VectorText::getTextWidth/Height invocados por clase.
readability-convert-member-functions-to-static: ResourcePack::decryptData.
unused-includes: eliminación de <utility>, <cstdint>, <vector>,
<iostream>, defaults.hpp y otros no usados directamente en headers y
unidades de traducción. Restablecido core/defaults.hpp en title_scene.cpp
(falsa "unused" del header).
Bug fix: eliminado isActive() duplicado en Bullet (redeclaración tras
rename de esta_activa→isActive que chocaba con el override de Entity).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
111 lines
4.4 KiB
C++
111 lines
4.4 KiB
C++
// shape_renderer.cpp - Implementació del renderizado de formes
|
|
// © 2026 JailDesigner
|
|
|
|
#include "core/rendering/shape_renderer.hpp"
|
|
|
|
#include <cmath>
|
|
|
|
#include "core/rendering/line_renderer.hpp"
|
|
|
|
namespace Rendering {
|
|
|
|
// Helper: aplicar rotación 3D a un point 2D (assumeix Z=0)
|
|
static auto apply3dRotation(float x, float y, const Rotation3D& rot) -> Vec2 {
|
|
float z = 0.0F; // Todos los points 2D comencen a Z=0
|
|
|
|
// Pitch (rotación eix X): cabeceo arriba/baix
|
|
float cos_pitch = std::cos(rot.pitch);
|
|
float sin_pitch = std::sin(rot.pitch);
|
|
float y1 = (y * cos_pitch) - (z * sin_pitch);
|
|
float z1 = (y * sin_pitch) + (z * cos_pitch);
|
|
|
|
// Yaw (rotación eix Y): guiñada izquierda/derecha
|
|
float cos_yaw = std::cos(rot.yaw);
|
|
float sin_yaw = std::sin(rot.yaw);
|
|
float x2 = (x * cos_yaw) + (z1 * sin_yaw);
|
|
float z2 = (-x * sin_yaw) + (z1 * cos_yaw);
|
|
|
|
// Roll (rotación eix Z): alabeo lateral
|
|
float cos_roll = std::cos(rot.roll);
|
|
float sin_roll = std::sin(rot.roll);
|
|
float x3 = (x2 * cos_roll) - (y1 * sin_roll);
|
|
float y3 = (x2 * sin_roll) + (y1 * cos_roll);
|
|
|
|
// Proyecció perspectiva (Z-divide simple)
|
|
// Naves quieren hacia el point de fuga (320, 240) a "infinit" (Z → +∞)
|
|
// Z més grande = més lluny = més pequeño a pantalla
|
|
constexpr float PERSPECTIVE_FACTOR = 500.0F;
|
|
float scale_factor = PERSPECTIVE_FACTOR / (PERSPECTIVE_FACTOR + z2);
|
|
|
|
return {.x = x3 * scale_factor, .y = y3 * scale_factor};
|
|
}
|
|
|
|
// Helper: transformar un point con rotación, scale i traslación
|
|
static auto transformPoint(const Vec2& point, const Vec2& shape_centre, const Vec2& position, float angle, float scale, const Rotation3D* rotation_3d) -> Vec2 {
|
|
// 1. Centrar el point respecte al centro de la shape
|
|
float centered_x = point.x - shape_centre.x;
|
|
float centered_y = point.y - shape_centre.y;
|
|
|
|
// 2. Aplicar rotación 3D (si es proporciona)
|
|
if ((rotation_3d != nullptr) && rotation_3d->hasRotation()) {
|
|
Vec2 rotated_3d = apply3dRotation(centered_x, centered_y, *rotation_3d);
|
|
centered_x = rotated_3d.x;
|
|
centered_y = rotated_3d.y;
|
|
}
|
|
|
|
// 3. Aplicar scale al point (después de rotación 3D)
|
|
float scaled_x = centered_x * scale;
|
|
float scaled_y = centered_y * scale;
|
|
|
|
// 4. Aplicar rotación 2D (Z-axis, tradicional)
|
|
// IMPORTANT: En el sistema original, angle=0 apunta AMUNT (no derecha)
|
|
// Per això usem (angle - PI/2) per compensar
|
|
// Pero aquí angle ya ve en el sistema correcte del juego
|
|
float cos_a = std::cos(angle);
|
|
float sin_a = std::sin(angle);
|
|
|
|
float rotated_x = (scaled_x * cos_a) - (scaled_y * sin_a);
|
|
float rotated_y = (scaled_x * sin_a) + (scaled_y * cos_a);
|
|
|
|
// 5. Aplicar traslación a posición mundial
|
|
return {.x = rotated_x + position.x, .y = rotated_y + position.y};
|
|
}
|
|
|
|
void renderShape(Rendering::Renderer* renderer,
|
|
const std::shared_ptr<Graphics::Shape>& shape,
|
|
const Vec2& position,
|
|
float angle,
|
|
float scale,
|
|
float progress,
|
|
float brightness,
|
|
const Rotation3D* rotation_3d,
|
|
SDL_Color color) {
|
|
if (!shape || !shape->isValid()) {
|
|
return;
|
|
}
|
|
if (progress < 1.0F) {
|
|
return;
|
|
}
|
|
|
|
const Vec2& shape_centre = shape->getCenter();
|
|
|
|
for (const auto& primitive : shape->getPrimitives()) {
|
|
if (primitive.type == Graphics::PrimitiveType::POLYLINE) {
|
|
// POLYLINE: conectar puntos consecutivos.
|
|
for (size_t i = 0; i < primitive.points.size() - 1; i++) {
|
|
const Vec2 P1 = transformPoint(primitive.points[i], shape_centre, position, angle, scale, rotation_3d);
|
|
const Vec2 P2 = transformPoint(primitive.points[i + 1], shape_centre, position, angle, scale, 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, 0.0F, color);
|
|
}
|
|
} else if (primitive.points.size() >= 2) { // LINE
|
|
const Vec2 P1 = transformPoint(primitive.points[0], shape_centre, position, angle, scale, rotation_3d);
|
|
const Vec2 P2 = transformPoint(primitive.points[1], shape_centre, position, angle, scale, 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, 0.0F, color);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Rendering
|