Experimento parcial de migración a C++20 modules

- Creados módulos core, themes, physics, rendering, input
- Integrados core y themes exitosamente en main.cpp
- Physics, rendering, input comentados por conflictos SDL
- Aplicación funcional con módulos parciales
- Experimento archivado para futuras referencias

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-17 21:55:54 +02:00
parent ea6bb25d5d
commit d2648d2328
5 changed files with 582 additions and 182 deletions

124
source/modules/input.cppm Normal file
View File

@@ -0,0 +1,124 @@
export module vibe2.input;
import vibe2.core;
import vibe2.external.sdl_wrapper;
export namespace vibe2::input {
// Acciones del juego
enum class GameAction {
EXIT,
PUSH_BALLS,
TOGGLE_GRAVITY,
TOGGLE_VSYNC,
TOGGLE_DEBUG,
CYCLE_THEME,
SET_THEME_SUNSET,
SET_THEME_OCEAN,
SET_THEME_NEON,
SET_THEME_FOREST,
SET_SCENARIO_1,
SET_SCENARIO_2,
SET_SCENARIO_3,
SET_SCENARIO_4,
SET_SCENARIO_5,
SET_SCENARIO_6,
SET_SCENARIO_7,
SET_SCENARIO_8,
NONE
};
// Mapeador básico de teclas
class KeyMapper {
public:
static GameAction getActionForKey(int key) {
switch (key) {
case vibe2::sdl::KEY_ESCAPE: return GameAction::EXIT;
case vibe2::sdl::KEY_SPACE: return GameAction::PUSH_BALLS;
case vibe2::sdl::KEY_G: return GameAction::TOGGLE_GRAVITY;
case vibe2::sdl::KEY_V: return GameAction::TOGGLE_VSYNC;
case vibe2::sdl::KEY_H: return GameAction::TOGGLE_DEBUG;
case vibe2::sdl::KEY_T: return GameAction::CYCLE_THEME;
case vibe2::sdl::KEY_F1: return GameAction::SET_THEME_SUNSET;
case vibe2::sdl::KEY_F2: return GameAction::SET_THEME_OCEAN;
case vibe2::sdl::KEY_F3: return GameAction::SET_THEME_NEON;
case vibe2::sdl::KEY_F4: return GameAction::SET_THEME_FOREST;
case vibe2::sdl::KEY_1: return GameAction::SET_SCENARIO_1;
case vibe2::sdl::KEY_2: return GameAction::SET_SCENARIO_2;
case vibe2::sdl::KEY_3: return GameAction::SET_SCENARIO_3;
case vibe2::sdl::KEY_4: return GameAction::SET_SCENARIO_4;
case vibe2::sdl::KEY_5: return GameAction::SET_SCENARIO_5;
case vibe2::sdl::KEY_6: return GameAction::SET_SCENARIO_6;
case vibe2::sdl::KEY_7: return GameAction::SET_SCENARIO_7;
case vibe2::sdl::KEY_8: return GameAction::SET_SCENARIO_8;
default: return GameAction::NONE;
}
}
static const char* getActionDescription(GameAction action) {
switch (action) {
case GameAction::EXIT: return "Salir";
case GameAction::PUSH_BALLS: return "Impulsar pelotas";
case GameAction::TOGGLE_GRAVITY: return "Alternar gravedad";
case GameAction::TOGGLE_VSYNC: return "Alternar V-Sync";
case GameAction::TOGGLE_DEBUG: return "Alternar debug";
case GameAction::CYCLE_THEME: return "Cambiar tema";
case GameAction::SET_THEME_SUNSET: return "Tema Atardecer";
case GameAction::SET_THEME_OCEAN: return "Tema Océano";
case GameAction::SET_THEME_NEON: return "Tema Neón";
case GameAction::SET_THEME_FOREST: return "Tema Bosque";
case GameAction::SET_SCENARIO_1: return "1 pelota";
case GameAction::SET_SCENARIO_2: return "10 pelotas";
case GameAction::SET_SCENARIO_3: return "100 pelotas";
case GameAction::SET_SCENARIO_4: return "500 pelotas";
case GameAction::SET_SCENARIO_5: return "1K pelotas";
case GameAction::SET_SCENARIO_6: return "10K pelotas";
case GameAction::SET_SCENARIO_7: return "50K pelotas";
case GameAction::SET_SCENARIO_8: return "100K pelotas";
default: return "Desconocido";
}
}
};
// Utilidades para conversión de escenarios
namespace utils {
constexpr int SCENARIO_COUNTS[8] = {1, 10, 100, 500, 1000, 10000, 50000, 100000};
inline int getScenarioIndex(GameAction action) {
switch (action) {
case GameAction::SET_SCENARIO_1: return 0;
case GameAction::SET_SCENARIO_2: return 1;
case GameAction::SET_SCENARIO_3: return 2;
case GameAction::SET_SCENARIO_4: return 3;
case GameAction::SET_SCENARIO_5: return 4;
case GameAction::SET_SCENARIO_6: return 5;
case GameAction::SET_SCENARIO_7: return 6;
case GameAction::SET_SCENARIO_8: return 7;
default: return -1;
}
}
inline int getScenarioCount(GameAction action) {
int index = getScenarioIndex(action);
return (index >= 0) ? SCENARIO_COUNTS[index] : 0;
}
inline ColorTheme getThemeFromAction(GameAction action) {
switch (action) {
case GameAction::SET_THEME_SUNSET: return ColorTheme::SUNSET;
case GameAction::SET_THEME_OCEAN: return ColorTheme::OCEAN;
case GameAction::SET_THEME_NEON: return ColorTheme::NEON;
case GameAction::SET_THEME_FOREST: return ColorTheme::FOREST;
default: return ColorTheme::SUNSET;
}
}
inline bool isScenarioAction(GameAction action) {
return action >= GameAction::SET_SCENARIO_1 && action <= GameAction::SET_SCENARIO_8;
}
inline bool isThemeAction(GameAction action) {
return action >= GameAction::SET_THEME_SUNSET && action <= GameAction::SET_THEME_FOREST;
}
}
}

122
source/modules/physics.cppm Normal file
View File

@@ -0,0 +1,122 @@
export module vibe2.physics;
import vibe2.core;
// Declaraciones forward en lugar de includes pesados
struct SDL_FRect;
export namespace vibe2::physics {
// Entidad física básica
class PhysicsEntity {
private:
float x_, y_, w_, h_; // Posición y tamaño
float vx_, vy_; // Velocidad
float gravity_force_; // Gravedad
bool on_floor_; // En el suelo
bool stopped_; // Detenido
float bounce_loss_; // Pérdida de rebote
public:
PhysicsEntity(float x, float y, float w, float h, float vx = 0.0f, float vy = 0.0f)
: x_(x), y_(y), w_(w), h_(h),
vx_(vx * vibe2::physics::CONVERSION_FACTOR),
vy_(vy * vibe2::physics::CONVERSION_FACTOR),
gravity_force_(vibe2::GRAVITY_FORCE * vibe2::physics::CONVERSION_FACTOR * vibe2::physics::CONVERSION_FACTOR),
on_floor_(false), stopped_(false), bounce_loss_(0.7f) {}
void update(float deltaTime) {
if (stopped_) return;
// Aplicar gravedad
if (!on_floor_) {
vy_ += gravity_force_ * deltaTime;
}
// Actualizar posición
x_ += vx_ * deltaTime;
if (!on_floor_) {
y_ += vy_ * deltaTime;
} else {
y_ = vibe2::SCREEN_HEIGHT - h_;
}
handleCollisions();
}
void handleCollisions() {
// Colisiones laterales
if (x_ < 0) {
x_ = 0;
vx_ = -vx_;
}
if (x_ + w_ > vibe2::SCREEN_WIDTH) {
x_ = vibe2::SCREEN_WIDTH - w_;
vx_ = -vx_;
}
// Colisión superior
if (y_ < 0) {
y_ = 0;
vy_ = -vy_;
}
// Colisión inferior (suelo)
if (y_ + h_ > vibe2::SCREEN_HEIGHT) {
y_ = vibe2::SCREEN_HEIGHT - h_;
vy_ = -vy_ * bounce_loss_;
if (vy_ > -vibe2::physics::VELOCITY_THRESHOLD && vy_ < vibe2::physics::VELOCITY_THRESHOLD) {
vy_ = 0.0f;
on_floor_ = true;
}
}
// Fricción en el suelo
if (on_floor_) {
float friction = 1.0f - (1.0f - vibe2::physics::FRICTION_FACTOR) * deltaTime * 60.0f;
vx_ *= friction;
if (vx_ > -vibe2::physics::VELOCITY_THRESHOLD && vx_ < vibe2::physics::VELOCITY_THRESHOLD) {
vx_ = 0.0f;
stopped_ = true;
}
}
}
// Modificar velocidad
void addVelocity(float vx, float vy) {
if (stopped_) {
vx_ += vx * vibe2::physics::CONVERSION_FACTOR;
}
vy_ += vy * vibe2::physics::CONVERSION_FACTOR;
on_floor_ = false;
stopped_ = false;
}
// Cambiar gravedad
void toggleGravity() {
gravity_force_ = (gravity_force_ == 0.0f) ?
(vibe2::GRAVITY_FORCE * vibe2::physics::CONVERSION_FACTOR * vibe2::physics::CONVERSION_FACTOR) : 0.0f;
}
// Getters
float getX() const { return x_; }
float getY() const { return y_; }
float getW() const { return w_; }
float getH() const { return h_; }
float getVX() const { return vx_; }
float getVY() const { return vy_; }
float getGravityForce() const { return gravity_force_; }
bool isOnFloor() const { return on_floor_; }
bool isStopped() const { return stopped_; }
void setPosition(float x, float y) {
x_ = x;
y_ = y;
}
private:
float deltaTime = 0.016f; // Para cálculos internos
};
}

View File

@@ -0,0 +1,134 @@
export module vibe2.rendering;
import vibe2.core;
import vibe2.themes;
import vibe2.external.sdl_wrapper;
export namespace vibe2::rendering {
// Utilidades básicas de renderizado
namespace utils {
// Calcular posición centrada para texto
inline int getCenteredTextX(int text_length, int char_width = debug::DEFAULT_TEXT_SIZE) {
const int text_pixel_width = text_length * char_width;
return (SCREEN_WIDTH - text_pixel_width) / 2;
}
// Calcular posición alineada a la derecha
inline int getRightAlignedX(int text_length, int char_width = debug::DEFAULT_TEXT_SIZE, int margin = debug::MARGIN) {
const int text_pixel_width = text_length * char_width;
return SCREEN_WIDTH - text_pixel_width - margin;
}
// Convertir color RGB a valores normalizados
inline void rgbToFloat(int r, int g, int b, float& rf, float& gf, float& bf) {
rf = r / 255.0f;
gf = g / 255.0f;
bf = b / 255.0f;
}
}
// Renderizador de fondos degradados
class GradientRenderer {
private:
vibe2::sdl::Renderer* renderer_;
public:
GradientRenderer(vibe2::sdl::Renderer* renderer) : renderer_(renderer) {}
void renderBackground(const themes::ThemeData& theme_data) {
// Crear quad de pantalla completa con degradado
vibe2::sdl::Vertex bg_vertices[4];
// Vértice superior izquierdo
bg_vertices[0].position = {0, 0};
bg_vertices[0].tex_coord = {0.0f, 0.0f};
bg_vertices[0].color = {theme_data.bg_top_r, theme_data.bg_top_g, theme_data.bg_top_b, 1.0f};
// Vértice superior derecho
bg_vertices[1].position = {SCREEN_WIDTH, 0};
bg_vertices[1].tex_coord = {1.0f, 0.0f};
bg_vertices[1].color = {theme_data.bg_top_r, theme_data.bg_top_g, theme_data.bg_top_b, 1.0f};
// Vértice inferior derecho
bg_vertices[2].position = {SCREEN_WIDTH, SCREEN_HEIGHT};
bg_vertices[2].tex_coord = {1.0f, 1.0f};
bg_vertices[2].color = {theme_data.bg_bottom_r, theme_data.bg_bottom_g, theme_data.bg_bottom_b, 1.0f};
// Vértice inferior izquierdo
bg_vertices[3].position = {0, SCREEN_HEIGHT};
bg_vertices[3].tex_coord = {0.0f, 1.0f};
bg_vertices[3].color = {theme_data.bg_bottom_r, theme_data.bg_bottom_g, theme_data.bg_bottom_b, 1.0f};
// Índices para 2 triángulos
int bg_indices[6] = {0, 1, 2, 2, 3, 0};
// Renderizar sin textura
vibe2::sdl::renderGeometry(renderer_, nullptr, bg_vertices, 4, bg_indices, 6);
}
};
// Sistema de batch rendering básico
class BatchRenderer {
private:
static constexpr int MAX_SPRITES = 10000;
vibe2::sdl::Vertex vertices_[MAX_SPRITES * 4];
int indices_[MAX_SPRITES * 6];
int sprite_count_;
vibe2::sdl::Renderer* renderer_;
public:
BatchRenderer(vibe2::sdl::Renderer* renderer) : sprite_count_(0), renderer_(renderer) {}
void clear() {
sprite_count_ = 0;
}
void addSprite(float x, float y, float w, float h, const Color& color) {
if (sprite_count_ >= MAX_SPRITES) return;
int vertex_index = sprite_count_ * 4;
int index_start = sprite_count_ * 6;
// Convertir colores de Uint8 (0-255) a float (0.0-1.0)
float rf = color.r / 255.0f;
float gf = color.g / 255.0f;
float bf = color.b / 255.0f;
// Crear 4 vértices para el quad
vertices_[vertex_index + 0].position = {x, y};
vertices_[vertex_index + 0].tex_coord = {0.0f, 0.0f};
vertices_[vertex_index + 0].color = {rf, gf, bf, 1.0f};
vertices_[vertex_index + 1].position = {x + w, y};
vertices_[vertex_index + 1].tex_coord = {1.0f, 0.0f};
vertices_[vertex_index + 1].color = {rf, gf, bf, 1.0f};
vertices_[vertex_index + 2].position = {x + w, y + h};
vertices_[vertex_index + 2].tex_coord = {1.0f, 1.0f};
vertices_[vertex_index + 2].color = {rf, gf, bf, 1.0f};
vertices_[vertex_index + 3].position = {x, y + h};
vertices_[vertex_index + 3].tex_coord = {0.0f, 1.0f};
vertices_[vertex_index + 3].color = {rf, gf, bf, 1.0f};
// Índices para 2 triángulos
indices_[index_start + 0] = vertex_index + 0;
indices_[index_start + 1] = vertex_index + 1;
indices_[index_start + 2] = vertex_index + 2;
indices_[index_start + 3] = vertex_index + 2;
indices_[index_start + 4] = vertex_index + 3;
indices_[index_start + 5] = vertex_index + 0;
sprite_count_++;
}
void renderBatch(vibe2::sdl::Texture* texture = nullptr) {
if (sprite_count_ > 0) {
vibe2::sdl::renderGeometry(renderer_, texture, vertices_, sprite_count_ * 4, indices_, sprite_count_ * 6);
}
}
int getSpriteCount() const { return sprite_count_; }
};
}

150
source/modules/themes.cppm Normal file
View File

@@ -0,0 +1,150 @@
export module vibe2.themes;
import vibe2.core;
export namespace vibe2::themes {
// Estructura para datos de un tema
struct ThemeData {
// Colores de fondo (superior -> inferior) en formato float [0-1]
float bg_top_r, bg_top_g, bg_top_b;
float bg_bottom_r, bg_bottom_g, bg_bottom_b;
// Paletas de colores para elementos (RGB 0-255)
int element_colors[8][3]; // 8 colores por tema
// Nombres del tema
const char* name_en;
const char* name_es;
};
// Definición de todos los temas disponibles
constexpr ThemeData THEME_DATA[4] = {
// SUNSET: Naranjas, rojos, amarillos, rosas
{
180.0f / 255.0f, 140.0f / 255.0f, 100.0f / 255.0f, // Fondo superior
40.0f / 255.0f, 20.0f / 255.0f, 60.0f / 255.0f, // Fondo inferior
{{255, 140, 0}, {255, 69, 0}, {255, 215, 0}, {255, 20, 147},
{255, 99, 71}, {255, 165, 0}, {255, 192, 203}, {220, 20, 60}},
"SUNSET", "ATARDECER"
},
// OCEAN: Azules, cianes, verdes agua, blancos
{
100.0f / 255.0f, 150.0f / 255.0f, 200.0f / 255.0f, // Fondo superior
20.0f / 255.0f, 40.0f / 255.0f, 80.0f / 255.0f, // Fondo inferior
{{0, 191, 255}, {0, 255, 255}, {32, 178, 170}, {176, 224, 230},
{70, 130, 180}, {0, 206, 209}, {240, 248, 255}, {64, 224, 208}},
"OCEAN", "OCEANO"
},
// NEON: Cian, magenta, verde lima, amarillo vibrante
{
20.0f / 255.0f, 20.0f / 255.0f, 40.0f / 255.0f, // Fondo superior
0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, // Fondo inferior
{{0, 255, 255}, {255, 0, 255}, {50, 205, 50}, {255, 255, 0},
{255, 20, 147}, {0, 255, 127}, {138, 43, 226}, {255, 69, 0}},
"NEON", "NEON"
},
// FOREST: Verdes, marrones, amarillos otoño
{
144.0f / 255.0f, 238.0f / 255.0f, 144.0f / 255.0f, // Fondo superior
101.0f / 255.0f, 67.0f / 255.0f, 33.0f / 255.0f, // Fondo inferior
{{34, 139, 34}, {107, 142, 35}, {154, 205, 50}, {255, 215, 0},
{210, 180, 140}, {160, 82, 45}, {218, 165, 32}, {50, 205, 50}},
"FOREST", "BOSQUE"
}
};
// Colores específicos para debug UI por tema
constexpr int DEBUG_COLORS[4][3] = {
{255, 140, 60}, // ATARDECER: Naranja cálido
{80, 200, 255}, // OCEANO: Azul océano
{255, 60, 255}, // NEON: Magenta brillante
{100, 255, 100} // BOSQUE: Verde natural
};
// Gestor de temas
class ThemeManager {
private:
ColorTheme current_theme_;
public:
ThemeManager(ColorTheme initial_theme = ColorTheme::SUNSET)
: current_theme_(initial_theme) {}
// Cambiar tema
void setTheme(ColorTheme theme) {
current_theme_ = theme;
}
// Ciclar al siguiente tema
void cycleTheme() {
int next = (static_cast<int>(current_theme_) + 1) % 4;
current_theme_ = static_cast<ColorTheme>(next);
}
// Obtener tema actual
ColorTheme getCurrentTheme() const {
return current_theme_;
}
// Obtener datos del tema actual
const ThemeData& getCurrentThemeData() const {
return THEME_DATA[static_cast<int>(current_theme_)];
}
// Obtener color aleatorio de la paleta actual
Color getRandomColor() const {
const auto& theme_data = getCurrentThemeData();
// Usar random simple para evitar dependencias
static int seed = 1;
seed = seed * 1103515245 + 12345;
int color_index = (seed / 65536) % 8;
return Color(
theme_data.element_colors[color_index][0],
theme_data.element_colors[color_index][1],
theme_data.element_colors[color_index][2]
);
}
// Obtener color específico de la paleta actual
Color getColor(int index) const {
const auto& theme_data = getCurrentThemeData();
index = index % 8; // Asegurar que esté en rango
return Color(
theme_data.element_colors[index][0],
theme_data.element_colors[index][1],
theme_data.element_colors[index][2]
);
}
// Obtener nombre del tema en inglés
const char* getThemeName() const {
return getCurrentThemeData().name_en;
}
// Obtener nombre del tema en español
const char* getThemeNameES() const {
return getCurrentThemeData().name_es;
}
// Obtener color de debug para el tema actual
Color getDebugColor() const {
int theme_idx = static_cast<int>(current_theme_);
return Color(
DEBUG_COLORS[theme_idx][0],
DEBUG_COLORS[theme_idx][1],
DEBUG_COLORS[theme_idx][2]
);
}
};
// Funciones de utilidad
inline const char* getThemeNameEN(ColorTheme theme) {
return THEME_DATA[static_cast<int>(theme)].name_en;
}
inline const char* getThemeNameES(ColorTheme theme) {
return THEME_DATA[static_cast<int>(theme)].name_es;
}
}