LOGO explota

This commit is contained in:
2025-12-02 08:50:38 +01:00
parent 73f222fcb7
commit 20538af4c6
22 changed files with 1754 additions and 1598 deletions

21
.clang-format Normal file
View File

@@ -0,0 +1,21 @@
BasedOnStyle: Google
IndentWidth: 4
IndentAccessModifiers: true
ColumnLimit: 0 # Sin límite de longitud de línea
BreakBeforeBraces: Attach # Llaves en la misma línea
AllowShortIfStatementsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AlignOperands: DontAlign
AlignAfterOpenBracket: DontAlign
BinPackArguments: false
BinPackParameters: false
ContinuationIndentWidth: 4
ConstructorInitializerIndentWidth: 4
IndentWrappedFunctionNames: false
Cpp11BracedListStyle: true
BreakConstructorInitializers: BeforeColon
AllowAllConstructorInitializersOnNextLine: false
PackConstructorInitializers: Never
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false

View File

@@ -2,19 +2,24 @@
// © 2025 Port a C++20 amb SDL3
#include "sdl_manager.hpp"
#include "core/defaults.hpp"
#include "core/rendering/line_renderer.hpp"
#include "game/options.hpp"
#include "project.h"
#include <algorithm>
#include <format>
#include <iostream>
#include "core/defaults.hpp"
#include "core/rendering/line_renderer.hpp"
#include "game/options.hpp"
#include "project.h"
SDLManager::SDLManager()
: finestra_(nullptr), renderer_(nullptr),
: finestra_(nullptr),
renderer_(nullptr),
current_width_(Defaults::Window::WIDTH),
current_height_(Defaults::Window::HEIGHT), is_fullscreen_(false),
max_width_(1920), max_height_(1080) {
current_height_(Defaults::Window::HEIGHT),
is_fullscreen_(false),
max_width_(1920),
max_height_(1080) {
// Inicialitzar SDL3
if (!SDL_Init(SDL_INIT_VIDEO)) {
std::cerr << "Error inicialitzant SDL3: " << SDL_GetError() << std::endl;
@@ -25,8 +30,7 @@ SDLManager::SDLManager()
calculateMaxWindowSize();
// Construir títol dinàmic igual que en pollo
std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME,
Project::VERSION, Project::COPYRIGHT);
std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME, Project::VERSION, Project::COPYRIGHT);
// Crear finestra CENTRADA (SDL ho fa automàticament amb CENTERED)
finestra_ =
@@ -41,8 +45,7 @@ SDLManager::SDLManager()
}
// IMPORTANT: Centrar explícitament la finestra
SDL_SetWindowPosition(finestra_, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED);
SDL_SetWindowPosition(finestra_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
// Crear renderer amb acceleració
renderer_ = SDL_CreateRenderer(finestra_, nullptr);
@@ -64,8 +67,12 @@ SDLManager::SDLManager()
// Constructor amb configuració
SDLManager::SDLManager(int width, int height, bool fullscreen)
: finestra_(nullptr), renderer_(nullptr), current_width_(width),
current_height_(height), is_fullscreen_(fullscreen), max_width_(1920),
: finestra_(nullptr),
renderer_(nullptr),
current_width_(width),
current_height_(height),
is_fullscreen_(fullscreen),
max_width_(1920),
max_height_(1080) {
// Inicialitzar SDL3
if (!SDL_Init(SDL_INIT_VIDEO)) {
@@ -77,8 +84,7 @@ SDLManager::SDLManager(int width, int height, bool fullscreen)
calculateMaxWindowSize();
// Construir títol dinàmic
std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME,
Project::VERSION, Project::COPYRIGHT);
std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME, Project::VERSION, Project::COPYRIGHT);
// Configurar flags de la finestra
SDL_WindowFlags flags = SDL_WINDOW_RESIZABLE;
@@ -87,8 +93,7 @@ SDLManager::SDLManager(int width, int height, bool fullscreen)
}
// Crear finestra
finestra_ = SDL_CreateWindow(window_title.c_str(), current_width_,
current_height_, flags);
finestra_ = SDL_CreateWindow(window_title.c_str(), current_width_, current_height_, flags);
if (!finestra_) {
std::cerr << "Error creant finestra: " << SDL_GetError() << std::endl;
@@ -98,8 +103,7 @@ SDLManager::SDLManager(int width, int height, bool fullscreen)
// Centrar explícitament la finestra (si no és fullscreen)
if (!is_fullscreen_) {
SDL_SetWindowPosition(finestra_, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED);
SDL_SetWindowPosition(finestra_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
}
// Crear renderer amb acceleració
@@ -141,7 +145,7 @@ SDLManager::~SDLManager() {
void SDLManager::calculateMaxWindowSize() {
SDL_DisplayID display = SDL_GetPrimaryDisplay();
const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(display);
const SDL_DisplayMode* mode = SDL_GetCurrentDisplayMode(display);
if (mode) {
// Deixar marge de 100px per a decoracions de l'OS
@@ -262,7 +266,7 @@ void SDLManager::toggleFullscreen() {
// En sortir, restaura la mida anterior
}
bool SDLManager::handleWindowEvent(const SDL_Event &event) {
bool SDLManager::handleWindowEvent(const SDL_Event& event) {
if (event.type == SDL_EVENT_WINDOW_RESIZED) {
// Usuari ha redimensionat manualment (arrossegar vora)
// Actualitzar el nostre tracking

View File

@@ -4,27 +4,29 @@
#ifndef SDL_MANAGER_HPP
#define SDL_MANAGER_HPP
#include "core/rendering/color_oscillator.hpp"
#include <SDL3/SDL.h>
#include <cstdint>
#include "core/rendering/color_oscillator.hpp"
class SDLManager {
public:
public:
SDLManager(); // Constructor per defecte (usa Defaults::)
SDLManager(int width, int height,
bool fullscreen); // Constructor amb configuració
~SDLManager();
// No permetre còpia ni assignació
SDLManager(const SDLManager &) = delete;
SDLManager &operator=(const SDLManager &) = delete;
SDLManager(const SDLManager&) = delete;
SDLManager& operator=(const SDLManager&) = delete;
// [NUEVO] Gestió de finestra dinàmica
void increaseWindowSize(); // F2: +100px
void decreaseWindowSize(); // F1: -100px
void toggleFullscreen(); // F3
bool
handleWindowEvent(const SDL_Event &event); // Per a SDL_EVENT_WINDOW_RESIZED
handleWindowEvent(const SDL_Event& event); // Per a SDL_EVENT_WINDOW_RESIZED
// Funcions principals (renderitzat)
void neteja(uint8_t r = 0, uint8_t g = 0, uint8_t b = 0);
@@ -34,11 +36,11 @@ public:
void updateColors(float delta_time);
// Getters
SDL_Renderer *obte_renderer() { return renderer_; }
SDL_Renderer* obte_renderer() { return renderer_; }
private:
SDL_Window *finestra_;
SDL_Renderer *renderer_;
private:
SDL_Window* finestra_;
SDL_Renderer* renderer_;
// [NUEVO] Estat de la finestra
int current_width_; // Mida física actual

2
source/external/.clang-format vendored Normal file
View File

@@ -0,0 +1,2 @@
DisableFormat: true
SortIncludes: Never

View File

@@ -32,9 +32,8 @@ inline bool dins_zona_joc(float x, float y) {
return SDL_PointInRectFloat(&punt, &Defaults::Zones::PLAYAREA);
}
inline void obtenir_limits_zona(float &min_x, float &max_x, float &min_y,
float &max_y) {
const auto &zona = Defaults::Zones::PLAYAREA;
inline void obtenir_limits_zona(float& min_x, float& max_x, float& min_y, float& max_y) {
const auto& zona = Defaults::Zones::PLAYAREA;
min_x = zona.x;
max_x = zona.x + zona.w;
min_y = zona.y;

View File

@@ -2,18 +2,19 @@
// © 2025 Port a C++20 amb SDL3
#include "debris_manager.hpp"
#include "core/defaults.hpp"
#include "core/rendering/line_renderer.hpp"
#include <cmath>
#include <cstdlib>
#include <iostream>
#include "core/defaults.hpp"
#include "core/rendering/line_renderer.hpp"
namespace Effects {
// Helper: transformar punt amb rotació, escala i trasllació
// (Copiat de shape_renderer.cpp:12-34)
static Punt transform_point(const Punt &point, const Punt &shape_centre,
const Punt &posicio, float angle, float escala) {
static Punt transform_point(const Punt& point, const Punt& shape_centre, const Punt& posicio, float angle, float escala) {
// 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;
@@ -33,25 +34,28 @@ static Punt transform_point(const Punt &point, const Punt &shape_centre,
return {rotated_x + posicio.x, rotated_y + posicio.y};
}
DebrisManager::DebrisManager(SDL_Renderer *renderer) : renderer_(renderer) {
DebrisManager::DebrisManager(SDL_Renderer* renderer)
: renderer_(renderer) {
// Inicialitzar tots els debris com inactius
for (auto &debris : debris_pool_) {
for (auto& debris : debris_pool_) {
debris.actiu = false;
}
}
void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape> &shape,
const Punt &centre, float angle, float escala,
void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape>& shape,
const Punt& centre,
float angle,
float escala,
float velocitat_base) {
if (!shape || !shape->es_valida()) {
return;
}
// Obtenir centre de la forma per a transformacions
const Punt &shape_centre = shape->get_centre();
const Punt& shape_centre = shape->get_centre();
// Iterar sobre totes les primitives de la forma
for (const auto &primitive : shape->get_primitives()) {
for (const auto& primitive : shape->get_primitives()) {
// Processar cada segment de línia
std::vector<std::pair<Punt, Punt>> segments;
@@ -68,7 +72,7 @@ void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape> &shape,
}
// Crear debris per a cada segment
for (const auto &[local_p1, local_p2] : segments) {
for (const auto& [local_p1, local_p2] : segments) {
// 1. Transformar punts locals → coordenades mundials
Punt world_p1 =
transform_point(local_p1, shape_centre, centre, angle, escala);
@@ -76,7 +80,7 @@ void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape> &shape,
transform_point(local_p2, shape_centre, centre, angle, escala);
// 2. Trobar slot lliure
Debris *debris = trobar_slot_lliure();
Debris* debris = trobar_slot_lliure();
if (!debris) {
std::cerr << "[DebrisManager] Warning: no debris slots disponibles\n";
return; // Pool ple
@@ -125,7 +129,7 @@ void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape> &shape,
}
void DebrisManager::actualitzar(float delta_time) {
for (auto &debris : debris_pool_) {
for (auto& debris : debris_pool_) {
if (!debris.actiu)
continue;
@@ -194,20 +198,17 @@ void DebrisManager::actualitzar(float delta_time) {
}
void DebrisManager::dibuixar() const {
for (const auto &debris : debris_pool_) {
for (const auto& debris : debris_pool_) {
if (!debris.actiu)
continue;
// Dibuixar segment de línia
Rendering::linea(renderer_, static_cast<int>(debris.p1.x),
static_cast<int>(debris.p1.y),
static_cast<int>(debris.p2.x),
static_cast<int>(debris.p2.y), true);
Rendering::linea(renderer_, static_cast<int>(debris.p1.x), static_cast<int>(debris.p1.y), static_cast<int>(debris.p2.x), static_cast<int>(debris.p2.y), true);
}
}
Debris *DebrisManager::trobar_slot_lliure() {
for (auto &debris : debris_pool_) {
Debris* DebrisManager::trobar_slot_lliure() {
for (auto& debris : debris_pool_) {
if (!debris.actiu) {
return &debris;
}
@@ -215,8 +216,8 @@ Debris *DebrisManager::trobar_slot_lliure() {
return nullptr; // Pool ple
}
Punt DebrisManager::calcular_direccio_perpendicular(const Punt &p1,
const Punt &p2) const {
Punt DebrisManager::calcular_direccio_perpendicular(const Punt& p1,
const Punt& p2) const {
// 1. Calcular vector de la línia (p1 → p2)
float dx = p2.x - p1.x;
float dy = p2.y - p1.y;
@@ -258,14 +259,14 @@ Punt DebrisManager::calcular_direccio_perpendicular(const Punt &p1,
}
void DebrisManager::reiniciar() {
for (auto &debris : debris_pool_) {
for (auto& debris : debris_pool_) {
debris.actiu = false;
}
}
int DebrisManager::get_num_actius() const {
int count = 0;
for (const auto &debris : debris_pool_) {
for (const auto& debris : debris_pool_) {
if (debris.actiu)
count++;
}

View File

@@ -2,20 +2,22 @@
// © 2025 Port a C++20 amb SDL3
#pragma once
#include "debris.hpp"
#include "core/graphics/shape.hpp"
#include "core/types.hpp"
#include <SDL3/SDL.h>
#include <array>
#include <memory>
#include "core/graphics/shape.hpp"
#include "core/types.hpp"
#include "debris.hpp"
namespace Effects {
// Gestor de fragments d'explosions
// Manté un pool d'objectes Debris i gestiona el seu cicle de vida
class DebrisManager {
public:
explicit DebrisManager(SDL_Renderer *renderer);
public:
explicit DebrisManager(SDL_Renderer* renderer);
// Crear explosió a partir d'una forma
// - shape: forma vectorial a explotar
@@ -23,8 +25,10 @@ public:
// - angle: orientació de l'objecte (radians)
// - escala: escala de l'objecte (1.0 = normal)
// - velocitat_base: velocitat inicial dels fragments (px/s)
void explotar(const std::shared_ptr<Graphics::Shape> &shape,
const Punt &centre, float angle, float escala,
void explotar(const std::shared_ptr<Graphics::Shape>& shape,
const Punt& centre,
float angle,
float escala,
float velocitat_base);
// Actualitzar tots els fragments actius
@@ -39,8 +43,8 @@ public:
// Obtenir número de fragments actius
int get_num_actius() const;
private:
SDL_Renderer *renderer_;
private:
SDL_Renderer* renderer_;
// Pool de fragments (màxim concurrent)
// Un pentàgon té 5 línies, 15 enemics = 75 línies
@@ -50,10 +54,10 @@ private:
std::array<Debris, MAX_DEBRIS> debris_pool_;
// Trobar primer slot inactiu
Debris *trobar_slot_lliure();
Debris* trobar_slot_lliure();
// Calcular direcció perpendicular a un segment
Punt calcular_direccio_perpendicular(const Punt &p1, const Punt &p2) const;
Punt calcular_direccio_perpendicular(const Punt& p1, const Punt& p2) const;
};
} // namespace Effects

View File

@@ -3,16 +3,20 @@
// © 2025 Port a C++20 amb SDL3
#include "game/entities/bala.hpp"
#include "core/graphics/shape_loader.hpp"
#include "core/rendering/shape_renderer.hpp"
#include "game/constants.hpp"
#include <cmath>
#include <iostream>
Bala::Bala(SDL_Renderer *renderer)
: renderer_(renderer), centre_({0.0f, 0.0f}), angle_(0.0f),
velocitat_(0.0f), esta_(false) {
#include "core/graphics/shape_loader.hpp"
#include "core/rendering/shape_renderer.hpp"
#include "game/constants.hpp"
Bala::Bala(SDL_Renderer* renderer)
: renderer_(renderer),
centre_({0.0f, 0.0f}),
angle_(0.0f),
velocitat_(0.0f),
esta_(false) {
// [NUEVO] Carregar forma compartida des de fitxer
forma_ = Graphics::ShapeLoader::load("bullet.shp");
@@ -29,7 +33,7 @@ void Bala::inicialitzar() {
velocitat_ = 0.0f;
}
void Bala::disparar(const Punt &posicio, float angle) {
void Bala::disparar(const Punt& posicio, float angle) {
// Activar bala i posicionar-la a la nau
// Basat en joc_asteroides.cpp línies 188-200

View File

@@ -3,28 +3,31 @@
// © 2025 Port a C++20 amb SDL3
#pragma once
#include "core/graphics/shape.hpp"
#include "core/types.hpp"
#include <SDL3/SDL.h>
#include <memory>
#include "core/graphics/shape.hpp"
#include "core/types.hpp"
class Bala {
public:
Bala() : renderer_(nullptr) {}
Bala(SDL_Renderer *renderer);
public:
Bala()
: renderer_(nullptr) {}
Bala(SDL_Renderer* renderer);
void inicialitzar();
void disparar(const Punt &posicio, float angle);
void disparar(const Punt& posicio, float angle);
void actualitzar(float delta_time);
void dibuixar() const;
// Getters (API pública sense canvis)
bool esta_activa() const { return esta_; }
const Punt &get_centre() const { return centre_; }
const Punt& get_centre() const { return centre_; }
void desactivar() { esta_ = false; }
private:
SDL_Renderer *renderer_;
private:
SDL_Renderer* renderer_;
// [NUEVO] Forma vectorial (compartida entre totes les bales)
std::shared_ptr<Graphics::Shape> forma_;

View File

@@ -3,17 +3,23 @@
// © 2025 Port a C++20 amb SDL3
#include "game/entities/enemic.hpp"
#include "core/graphics/shape_loader.hpp"
#include "core/rendering/shape_renderer.hpp"
#include "game/constants.hpp"
#include <cmath>
#include <cstdlib>
#include <iostream>
Enemic::Enemic(SDL_Renderer *renderer)
: renderer_(renderer), centre_({0.0f, 0.0f}), angle_(0.0f),
velocitat_(0.0f), drotacio_(0.0f), rotacio_(0.0f), esta_(false) {
#include "core/graphics/shape_loader.hpp"
#include "core/rendering/shape_renderer.hpp"
#include "game/constants.hpp"
Enemic::Enemic(SDL_Renderer* renderer)
: renderer_(renderer),
centre_({0.0f, 0.0f}),
angle_(0.0f),
velocitat_(0.0f),
drotacio_(0.0f),
rotacio_(0.0f),
esta_(false) {
// [NUEVO] Carregar forma compartida des de fitxer
forma_ = Graphics::ShapeLoader::load("enemy_pentagon.shp");

View File

@@ -3,15 +3,18 @@
// © 2025 Port a C++20 amb SDL3
#pragma once
#include "core/graphics/shape.hpp"
#include "core/types.hpp"
#include <SDL3/SDL.h>
#include <memory>
#include "core/graphics/shape.hpp"
#include "core/types.hpp"
class Enemic {
public:
Enemic() : renderer_(nullptr) {}
Enemic(SDL_Renderer *renderer);
public:
Enemic()
: renderer_(nullptr) {}
Enemic(SDL_Renderer* renderer);
void inicialitzar();
void actualitzar(float delta_time);
@@ -19,12 +22,12 @@ public:
// Getters (API pública sense canvis)
bool esta_actiu() const { return esta_; }
const Punt &get_centre() const { return centre_; }
const std::shared_ptr<Graphics::Shape> &get_forma() const { return forma_; }
const Punt& get_centre() const { return centre_; }
const std::shared_ptr<Graphics::Shape>& get_forma() const { return forma_; }
void destruir() { esta_ = false; }
private:
SDL_Renderer *renderer_;
private:
SDL_Renderer* renderer_;
// [NUEVO] Forma vectorial (compartida entre tots els enemics)
std::shared_ptr<Graphics::Shape> forma_;

View File

@@ -3,18 +3,23 @@
// © 2025 Port a C++20 amb SDL3
#include "game/entities/nau.hpp"
#include <SDL3/SDL.h>
#include <cmath>
#include <iostream>
#include "core/defaults.hpp"
#include "core/graphics/shape_loader.hpp"
#include "core/rendering/shape_renderer.hpp"
#include "game/constants.hpp"
#include <SDL3/SDL.h>
#include <cmath>
#include <iostream>
Nau::Nau(SDL_Renderer *renderer)
: renderer_(renderer), centre_({0.0f, 0.0f}), angle_(0.0f),
velocitat_(0.0f), esta_tocada_(false) {
Nau::Nau(SDL_Renderer* renderer)
: renderer_(renderer),
centre_({0.0f, 0.0f}),
angle_(0.0f),
velocitat_(0.0f),
esta_tocada_(false) {
// [NUEVO] Carregar forma compartida des de fitxer
forma_ = Graphics::ShapeLoader::load("ship.shp");
@@ -49,7 +54,7 @@ void Nau::processar_input(float delta_time) {
return;
// Obtenir estat actual del teclat (no events, sinó estat continu)
const bool *keyboard_state = SDL_GetKeyboardState(nullptr);
const bool* keyboard_state = SDL_GetKeyboardState(nullptr);
// Rotació
if (keyboard_state[SDL_SCANCODE_RIGHT]) {

View File

@@ -3,15 +3,18 @@
// © 2025 Port a C++20 amb SDL3
#pragma once
#include "core/graphics/shape.hpp"
#include "core/types.hpp"
#include <SDL3/SDL.h>
#include <memory>
#include "core/graphics/shape.hpp"
#include "core/types.hpp"
class Nau {
public:
Nau() : renderer_(nullptr) {}
Nau(SDL_Renderer *renderer);
public:
Nau()
: renderer_(nullptr) {}
Nau(SDL_Renderer* renderer);
void inicialitzar();
void processar_input(float delta_time);
@@ -19,15 +22,15 @@ public:
void dibuixar() const;
// Getters (API pública sense canvis)
const Punt &get_centre() const { return centre_; }
const Punt& get_centre() const { return centre_; }
float get_angle() const { return angle_; }
bool esta_viva() const { return !esta_tocada_; }
// Col·lisions (Fase 10)
void marcar_tocada() { esta_tocada_ = true; }
private:
SDL_Renderer *renderer_;
private:
SDL_Renderer* renderer_;
// [NUEVO] Forma vectorial (compartida, només 1 instància de Nau però preparat
// per reutilització)

View File

@@ -3,25 +3,30 @@
// © 2025 Port a C++20 amb SDL3
#include "escena_joc.hpp"
#include "../../core/rendering/line_renderer.hpp"
#include "../../core/system/gestor_escenes.hpp"
#include "../../core/system/global_events.hpp"
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
EscenaJoc::EscenaJoc(SDLManager &sdl)
: sdl_(sdl), debris_manager_(sdl.obte_renderer()),
nau_(sdl.obte_renderer()), itocado_(0), text_(sdl.obte_renderer()) {
#include "../../core/rendering/line_renderer.hpp"
#include "../../core/system/gestor_escenes.hpp"
#include "../../core/system/global_events.hpp"
EscenaJoc::EscenaJoc(SDLManager& sdl)
: sdl_(sdl),
debris_manager_(sdl.obte_renderer()),
nau_(sdl.obte_renderer()),
itocado_(0),
text_(sdl.obte_renderer()) {
// Inicialitzar bales amb renderer
for (auto &bala : bales_) {
for (auto& bala : bales_) {
bala = Bala(sdl.obte_renderer());
}
// Inicialitzar enemics amb renderer
for (auto &enemy : orni_) {
for (auto& enemy : orni_) {
enemy = Enemic(sdl.obte_renderer());
}
}
@@ -93,12 +98,12 @@ void EscenaJoc::inicialitzar() {
nau_.inicialitzar();
// Inicialitzar enemics (ORNIs)
for (auto &enemy : orni_) {
for (auto& enemy : orni_) {
enemy.inicialitzar();
}
// Inicialitzar bales
for (auto &bala : bales_) {
for (auto& bala : bales_) {
bala.inicialitzar();
}
}
@@ -109,12 +114,12 @@ void EscenaJoc::actualitzar(float delta_time) {
nau_.actualitzar(delta_time);
// Actualitzar moviment i rotació dels enemics (ORNIs)
for (auto &enemy : orni_) {
for (auto& enemy : orni_) {
enemy.actualitzar(delta_time);
}
// Actualitzar moviment de bales (Fase 9)
for (auto &bala : bales_) {
for (auto& bala : bales_) {
bala.actualitzar(delta_time);
}
@@ -130,12 +135,12 @@ void EscenaJoc::dibuixar() {
nau_.dibuixar();
// Dibuixar ORNIs (enemics)
for (const auto &enemy : orni_) {
for (const auto& enemy : orni_) {
enemy.dibuixar();
}
// Dibuixar bales (Fase 9)
for (const auto &bala : bales_) {
for (const auto& bala : bales_) {
bala.dibuixar();
}
@@ -146,7 +151,7 @@ void EscenaJoc::dibuixar() {
dibuixar_marcador();
}
void EscenaJoc::processar_input(const SDL_Event &event) {
void EscenaJoc::processar_input(const SDL_Event& event) {
// Processament d'input per events puntuals (no continus)
// L'input continu (fletxes) es processa en actualitzar() amb
// SDL_GetKeyboardState()
@@ -196,7 +201,7 @@ void EscenaJoc::tocado() {
void EscenaJoc::dibuixar_marges() const {
// Dibuixar rectangle de la zona de joc
const SDL_FRect &zona = Defaults::Zones::PLAYAREA;
const SDL_FRect& zona = Defaults::Zones::PLAYAREA;
// Coordenades dels cantons
int x1 = static_cast<int>(zona.x);

View File

@@ -5,32 +5,34 @@
#ifndef ESCENA_JOC_HPP
#define ESCENA_JOC_HPP
#include "../../core/graphics/vector_text.hpp"
#include "../../core/rendering/sdl_manager.hpp"
#include "../effects/debris_manager.hpp"
#include "../../core/types.hpp"
#include "../constants.hpp"
#include "../entities/bala.hpp"
#include "../entities/enemic.hpp"
#include "../entities/nau.hpp"
#include <SDL3/SDL.h>
#include <array>
#include <cstdint>
#include "../../core/graphics/vector_text.hpp"
#include "../../core/rendering/sdl_manager.hpp"
#include "../../core/types.hpp"
#include "../constants.hpp"
#include "../effects/debris_manager.hpp"
#include "../entities/bala.hpp"
#include "../entities/enemic.hpp"
#include "../entities/nau.hpp"
// Classe principal del joc (escena)
class EscenaJoc {
public:
explicit EscenaJoc(SDLManager &sdl);
public:
explicit EscenaJoc(SDLManager& sdl);
~EscenaJoc() = default;
void executar(); // Bucle principal de l'escena
void inicialitzar();
void actualitzar(float delta_time);
void dibuixar();
void processar_input(const SDL_Event &event);
void processar_input(const SDL_Event& event);
private:
SDLManager &sdl_;
private:
SDLManager& sdl_;
// Efectes visuals
Effects::DebrisManager debris_manager_;

View File

@@ -2,18 +2,19 @@
// © 2025 Port a C++20
#include "escena_logo.hpp"
#include "../../core/graphics/shape_loader.hpp"
#include "../../core/rendering/shape_renderer.hpp"
#include "../../core/system/gestor_escenes.hpp"
#include "../../core/system/global_events.hpp"
#include <algorithm>
#include <cfloat>
#include <iostream>
#include "../../core/graphics/shape_loader.hpp"
#include "../../core/rendering/shape_renderer.hpp"
#include "../../core/system/gestor_escenes.hpp"
#include "../../core/system/global_events.hpp"
// Helper: calcular el progrés individual d'una lletra
// en funció del progrés global (efecte seqüencial)
static float calcular_progress_letra(size_t letra_index, size_t num_letras,
float global_progress, float threshold) {
static float calcular_progress_letra(size_t letra_index, size_t num_letras, float global_progress, float threshold) {
if (num_letras == 0)
return 1.0f;
@@ -33,9 +34,13 @@ static float calcular_progress_letra(size_t letra_index, size_t num_letras,
}
}
EscenaLogo::EscenaLogo(SDLManager &sdl)
: sdl_(sdl), estat_actual_(EstatAnimacio::PRE_ANIMATION),
temps_estat_actual_(0.0f) {
EscenaLogo::EscenaLogo(SDLManager& sdl)
: sdl_(sdl),
estat_actual_(EstatAnimacio::PRE_ANIMATION),
temps_estat_actual_(0.0f),
debris_manager_(std::make_unique<Effects::DebrisManager>(sdl.obte_renderer())),
lletra_explosio_index_(0),
temps_des_ultima_explosio_(0.0f) {
std::cout << "Escena Logo: Inicialitzant...\n";
inicialitzar_lletres();
}
@@ -89,14 +94,20 @@ void EscenaLogo::inicialitzar_lletres() {
// Llista de fitxers .shp (A repetida per a les dues A's)
std::vector<std::string> fitxers = {
"logo/letra_j.shp", "logo/letra_a.shp", "logo/letra_i.shp",
"logo/letra_l.shp", "logo/letra_g.shp", "logo/letra_a.shp",
"logo/letra_m.shp", "logo/letra_e.shp", "logo/letra_s.shp"};
"logo/letra_j.shp",
"logo/letra_a.shp",
"logo/letra_i.shp",
"logo/letra_l.shp",
"logo/letra_g.shp",
"logo/letra_a.shp",
"logo/letra_m.shp",
"logo/letra_e.shp",
"logo/letra_s.shp"};
// Pas 1: Carregar totes les formes i calcular amplades
float ancho_total = 0.0f;
for (const auto &fitxer : fitxers) {
for (const auto& fitxer : fitxers) {
auto forma = ShapeLoader::load(fitxer);
if (!forma || !forma->es_valida()) {
std::cerr << "[EscenaLogo] Error carregant " << fitxer << std::endl;
@@ -107,8 +118,8 @@ void EscenaLogo::inicialitzar_lletres() {
float min_x = FLT_MAX;
float max_x = -FLT_MAX;
for (const auto &prim : forma->get_primitives()) {
for (const auto &punt : prim.points) {
for (const auto& prim : forma->get_primitives()) {
for (const auto& punt : prim.points) {
min_x = std::min(min_x, punt.x);
max_x = std::max(max_x, punt.x);
}
@@ -142,7 +153,7 @@ void EscenaLogo::inicialitzar_lletres() {
// Pas 4: Assignar posicions a cada lletra
float x_actual = x_inicial;
for (auto &lletra : lletres_) {
for (auto& lletra : lletres_) {
// Posicionar el centre de la forma (shape_centre) en pantalla
// Usar offset_centre en lloc de ancho/2 perquè shape_centre
// pot no estar exactament al mig del bounding box
@@ -160,6 +171,13 @@ void EscenaLogo::inicialitzar_lletres() {
void EscenaLogo::canviar_estat(EstatAnimacio nou_estat) {
estat_actual_ = nou_estat;
temps_estat_actual_ = 0.0f; // Reset temps
// Inicialitzar estat d'explosió
if (nou_estat == EstatAnimacio::EXPLOSION) {
lletra_explosio_index_ = 0;
temps_des_ultima_explosio_ = 0.0f;
}
std::cout << "[EscenaLogo] Canvi a estat: " << static_cast<int>(nou_estat)
<< "\n";
}
@@ -169,6 +187,35 @@ bool EscenaLogo::totes_lletres_completes() const {
return temps_estat_actual_ >= DURACIO_ZOOM;
}
void EscenaLogo::actualitzar_explosions(float delta_time) {
temps_des_ultima_explosio_ += delta_time;
// Comprovar si és el moment d'explotar la següent lletra
if (temps_des_ultima_explosio_ >= DELAY_ENTRE_EXPLOSIONS) {
if (lletra_explosio_index_ < lletres_.size()) {
// Explotar lletra actual
const auto& lletra = lletres_[lletra_explosio_index_];
debris_manager_->explotar(
lletra.forma, // Forma a explotar
lletra.posicio, // Posició
0.0f, // Angle (sense rotació)
ESCALA_FINAL, // Escala (lletres a escala final)
VELOCITAT_EXPLOSIO // Velocitat base
);
std::cout << "[EscenaLogo] Explota lletra " << lletra_explosio_index_ << "\n";
// Passar a la següent lletra
lletra_explosio_index_++;
temps_des_ultima_explosio_ = 0.0f;
} else {
// Totes les lletres han explotat, transició a POST_EXPLOSION
canviar_estat(EstatAnimacio::POST_EXPLOSION);
}
}
}
void EscenaLogo::actualitzar(float delta_time) {
temps_estat_actual_ += delta_time;
@@ -186,11 +233,24 @@ void EscenaLogo::actualitzar(float delta_time) {
break;
case EstatAnimacio::POST_ANIMATION:
if (temps_estat_actual_ >= DURACIO_POST) {
if (temps_estat_actual_ >= DURACIO_POST_ANIMATION) {
canviar_estat(EstatAnimacio::EXPLOSION);
}
break;
case EstatAnimacio::EXPLOSION:
actualitzar_explosions(delta_time);
break;
case EstatAnimacio::POST_EXPLOSION:
if (temps_estat_actual_ >= DURACIO_POST_EXPLOSION) {
GestorEscenes::actual = GestorEscenes::Escena::JOC;
}
break;
}
// Actualitzar animacions de debris
debris_manager_->actualitzar(delta_time);
}
void EscenaLogo::dibuixar() {
@@ -203,58 +263,76 @@ void EscenaLogo::dibuixar() {
return; // No renderitzar lletres
}
// ANIMATION o POST_ANIMATION: Calcular progrés
// ANIMATION o POST_ANIMATION: Dibuixar lletres amb animació
if (estat_actual_ == EstatAnimacio::ANIMATION ||
estat_actual_ == EstatAnimacio::POST_ANIMATION) {
float global_progress =
(estat_actual_ == EstatAnimacio::ANIMATION)
? std::min(temps_estat_actual_ / DURACIO_ZOOM, 1.0f)
: 1.0f; // POST: mantenir al 100%
// Punt inicial del zoom (configurable amb ORIGEN_ZOOM_X/Y)
const Punt ORIGEN_ZOOM = {ORIGEN_ZOOM_X, ORIGEN_ZOOM_Y};
// Dibuixar cada lletra amb animació seqüencial
for (size_t i = 0; i < lletres_.size(); i++) {
const auto &lletra = lletres_[i];
const auto& lletra = lletres_[i];
// Calcular progrés individual d'aquesta lletra (0.0 → 1.0)
float letra_progress = calcular_progress_letra(
i, lletres_.size(), global_progress, THRESHOLD_LETRA);
i,
lletres_.size(),
global_progress,
THRESHOLD_LETRA);
// Si la lletra encara no ha començat, saltar-la
if (letra_progress <= 0.0f) {
continue;
}
// Interpolar posició: des del origen zoom cap a posició final
Punt pos_actual;
pos_actual.x =
ORIGEN_ZOOM.x + (lletra.posicio.x - ORIGEN_ZOOM.x) * letra_progress;
pos_actual.y =
ORIGEN_ZOOM.y + (lletra.posicio.y - ORIGEN_ZOOM.y) * letra_progress;
// Aplicar ease-out quadràtic per suavitat
float t = letra_progress;
float ease_factor = 1.0f - (1.0f - t) * (1.0f - t);
// Interpolar escala amb ease-out: des de ESCALA_INICIAL cap a ESCALA_FINAL
float escala_actual =
ESCALA_INICIAL + (ESCALA_FINAL - ESCALA_INICIAL) * ease_factor;
// Renderitzar la lletra
Rendering::render_shape(
sdl_.obte_renderer(), lletra.forma,
pos_actual, // Posició interpolada
0.0f, // Sense rotació
escala_actual, // Escala interpolada amb ease-out
true, // Dibuixar
1.0f // Progress = 1.0 (lletra completa, sense animació de primitives)
);
sdl_.obte_renderer(),
lletra.forma,
pos_actual,
0.0f,
escala_actual,
true,
1.0f);
}
}
// EXPLOSION: Dibuixar només lletres que encara no han explotat
if (estat_actual_ == EstatAnimacio::EXPLOSION) {
for (size_t i = lletra_explosio_index_; i < lletres_.size(); i++) {
const auto& lletra = lletres_[i];
Rendering::render_shape(
sdl_.obte_renderer(),
lletra.forma,
lletra.posicio,
0.0f,
ESCALA_FINAL,
true,
1.0f);
}
}
// POST_EXPLOSION: No dibuixar lletres, només debris (a baix)
// Sempre dibuixar debris (si n'hi ha d'actius)
debris_manager_->dibuixar();
sdl_.presenta();
}
void EscenaLogo::processar_events(const SDL_Event &event) {
void EscenaLogo::processar_events(const SDL_Event& event) {
// Qualsevol tecla o clic de ratolí salta al joc
if (event.type == SDL_EVENT_KEY_DOWN ||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {

View File

@@ -4,31 +4,43 @@
#pragma once
#include "../../core/graphics/shape.hpp"
#include "../../core/rendering/sdl_manager.hpp"
#include "../../core/types.hpp"
#include <SDL3/SDL.h>
#include <memory>
#include <vector>
#include "../../core/graphics/shape.hpp"
#include "../../core/rendering/sdl_manager.hpp"
#include "../../core/types.hpp"
#include "../effects/debris_manager.hpp"
class EscenaLogo {
public:
explicit EscenaLogo(SDLManager &sdl);
public:
explicit EscenaLogo(SDLManager& sdl);
void executar(); // Bucle principal de l'escena
private:
private:
// Màquina d'estats per l'animació
enum class EstatAnimacio {
PRE_ANIMATION, // Pantalla negra inicial
ANIMATION, // Animació de zoom de lletres
POST_ANIMATION // Logo complet visible
POST_ANIMATION, // Logo complet visible
EXPLOSION, // Explosió seqüencial de lletres
POST_EXPLOSION // Espera després de l'última explosió
};
SDLManager &sdl_;
SDLManager& sdl_;
EstatAnimacio estat_actual_; // Estat actual de la màquina
float
temps_estat_actual_; // Temps en l'estat actual (reset en cada transició)
// Gestor de fragments d'explosions
std::unique_ptr<Effects::DebrisManager> debris_manager_;
// Seguiment d'explosions seqüencials
size_t lletra_explosio_index_; // Índex de la següent lletra a explotar
float temps_des_ultima_explosio_; // Temps des de l'última explosió
// Estructura per a cada lletra del logo
struct LetraLogo {
std::shared_ptr<Graphics::Shape> forma;
@@ -40,28 +52,27 @@ private:
std::vector<LetraLogo> lletres_; // 9 lletres: J-A-I-L-G-A-M-E-S
// Constants d'animació
static constexpr float DURACIO_PRE =
1.5f; // Duració PRE_ANIMATION (pantalla negra)
static constexpr float DURACIO_PRE = 1.5f; // Duració PRE_ANIMATION (pantalla negra)
static constexpr float DURACIO_ZOOM = 4.0f; // Duració del zoom (segons)
static constexpr float DURACIO_POST =
4.0f; // Duració POST_ANIMATION (logo complet)
static constexpr float DURACIO_POST_ANIMATION = 3.0f; // Duració POST_ANIMATION (logo complet)
static constexpr float DURACIO_POST_EXPLOSION = 3.0f; // Duració POST_EXPLOSION (espera final)
static constexpr float DELAY_ENTRE_EXPLOSIONS = 1.0f; // Temps entre explosions de lletres
static constexpr float VELOCITAT_EXPLOSIO = 80.0f; // Velocitat base fragments (px/s)
static constexpr float ESCALA_INICIAL = 0.1f; // Escala inicial (10%)
static constexpr float ESCALA_FINAL = 0.8f; // Escala final (80%)
static constexpr float ESPAI_ENTRE_LLETRES = 10.0f; // Espaiat entre lletres
// Constants d'animació seqüencial
static constexpr float THRESHOLD_LETRA =
0.6f; // Umbral per activar següent lletra (0.0-1.0)
static constexpr float ORIGEN_ZOOM_X =
640.0f / 2.0f; // Punt inicial X del zoom (320)
static constexpr float ORIGEN_ZOOM_Y =
480.0f * 0.4f; // Punt inicial Y del zoom (240)
static constexpr float THRESHOLD_LETRA = 0.6f; // Umbral per activar següent lletra (0.0-1.0)
static constexpr float ORIGEN_ZOOM_X = 640.0f / 2.0f; // Punt inicial X del zoom (320)
static constexpr float ORIGEN_ZOOM_Y = 480.0f * 0.4f; // Punt inicial Y del zoom (240)
// Mètodes privats
void inicialitzar_lletres();
void actualitzar(float delta_time);
void actualitzar_explosions(float delta_time);
void dibuixar();
void processar_events(const SDL_Event &event);
void processar_events(const SDL_Event& event);
// Mètodes de gestió d'estats
void canviar_estat(EstatAnimacio nou_estat);

View File

@@ -1,12 +1,13 @@
#include "options.hpp"
#include "../core/defaults.hpp"
#include "../external/fkyaml_node.hpp"
#include "project.h"
#include <fstream>
#include <iostream>
#include <string>
#include "../core/defaults.hpp"
#include "../external/fkyaml_node.hpp"
#include "project.h"
namespace Options {
// Inicialitzar opcions amb valors per defecte de Defaults::
@@ -40,13 +41,13 @@ void init() {
}
// Establir la ruta del fitxer de configuració
void setConfigFile(const std::string &path) { config_file_path = path; }
void setConfigFile(const std::string& path) { config_file_path = path; }
// Funcions auxiliars per carregar seccions del YAML
static void loadWindowConfigFromYaml(const fkyaml::node &yaml) {
static void loadWindowConfigFromYaml(const fkyaml::node& yaml) {
if (yaml.contains("window")) {
const auto &win = yaml["window"];
const auto& win = yaml["window"];
if (win.contains("width")) {
try {
@@ -90,9 +91,9 @@ static void loadWindowConfigFromYaml(const fkyaml::node &yaml) {
}
}
static void loadPhysicsConfigFromYaml(const fkyaml::node &yaml) {
static void loadPhysicsConfigFromYaml(const fkyaml::node& yaml) {
if (yaml.contains("physics")) {
const auto &phys = yaml["physics"];
const auto& phys = yaml["physics"];
if (phys.contains("rotation_speed")) {
try {
@@ -154,9 +155,9 @@ static void loadPhysicsConfigFromYaml(const fkyaml::node &yaml) {
}
}
static void loadGameplayConfigFromYaml(const fkyaml::node &yaml) {
static void loadGameplayConfigFromYaml(const fkyaml::node& yaml) {
if (yaml.contains("gameplay")) {
const auto &game = yaml["gameplay"];
const auto& game = yaml["gameplay"];
if (game.contains("max_enemies")) {
try {
@@ -233,7 +234,7 @@ auto loadFromFile() -> bool {
return true;
} catch (const fkyaml::exception &e) {
} catch (const fkyaml::exception& e) {
// Error de parsejat YAML → regenerar config
if (console) {
std::cerr << "Error parsejant YAML: " << e.what() << '\n';

View File

@@ -41,7 +41,7 @@ inline std::string config_file_path{}; // Establert per setConfigFile()
void init(); // Inicialitzar amb valors per defecte
void setConfigFile(
const std::string &path); // Establir ruta del fitxer de config
const std::string& path); // Establir ruta del fitxer de config
auto loadFromFile() -> bool; // Carregar config YAML
auto saveToFile() -> bool; // Guardar config YAML

View File

@@ -1,4 +1,5 @@
#include <SDL2/SDL.h>
#include <string>
#include <vector>
@@ -32,7 +33,7 @@ struct triangle {
typedef std::vector<ipunt> ivector();
struct poligon {
ivector *ipunts;
ivector* ipunts;
ivector ipuntx;
punt centre;
float angl;

View File

@@ -2,11 +2,12 @@
// © 1999 Visente i Sergi (versió Pascal)
// © 2025 Port a C++20 amb SDL3
#include "core/system/director.hpp"
#include <string>
#include <vector>
int main(int argc, char *argv[]) {
#include "core/system/director.hpp"
int main(int argc, char* argv[]) {
// Convertir arguments a std::vector<std::string>
std::vector<std::string> args(argv, argv + argc);