Refactorización inicial: Crear estructura de clase Engine

PROGRESO INTERMEDIO - Estructura base de Engine implementada:

Nuevos archivos:
- engine.h: Declaración completa de clase Engine con encapsulación
- engine.cpp: Esqueleto de implementación con métodos stub
- main_new.cpp: Nuevo main simplificado (15 líneas vs 580)

Cambios en archivos existentes:
- defines.h: Añadir enum ColorTheme (centralizar definiciones)
- main.cpp: Eliminar enum ColorTheme duplicado

Arquitectura Engine:
- Encapsulación completa de variables globales (SDL, estado, timing, UI)
- Métodos organizados por responsabilidad (public/private)
- Eliminación de problemas de orden de declaración
- Base sólida para futuras extensiones

Estado: Compilación exitosa 
Pendiente: Migrar funcionalidad completa de métodos stub

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-18 17:09:42 +02:00
parent 78656cf17d
commit cada46f732
5 changed files with 253 additions and 12 deletions

137
source/engine.cpp Normal file
View File

@@ -0,0 +1,137 @@
#include "engine.h"
#include <SDL3/SDL_error.h> // for SDL_GetError
#include <SDL3/SDL_events.h> // for SDL_Event, SDL_PollEvent
#include <SDL3/SDL_init.h> // for SDL_Init, SDL_Quit, SDL_INIT_VIDEO
#include <SDL3/SDL_keycode.h> // for SDL_Keycode
#include <SDL3/SDL_render.h> // for SDL_SetRenderDrawColor, SDL_RenderPresent
#include <SDL3/SDL_timer.h> // for SDL_GetTicks
#include <SDL3/SDL_video.h> // for SDL_CreateWindow, SDL_DestroyWindow
#include <cstdlib> // for rand, srand
#include <ctime> // for time
#include <iostream> // for cout
#include <string> // for string
#include "ball.h" // for Ball
#include "external/dbgtxt.h" // for dbg_init, dbg_print
#include "external/texture.h" // for Texture
// Implementación de métodos públicos
bool Engine::initialize() {
bool success = true;
if (!SDL_Init(SDL_INIT_VIDEO)) {
std::cout << "¡SDL no se pudo inicializar! Error de SDL: " << SDL_GetError() << std::endl;
success = false;
} else {
// Crear ventana principal
window_ = SDL_CreateWindow(WINDOW_CAPTION, SCREEN_WIDTH * WINDOW_SIZE, SCREEN_HEIGHT * WINDOW_SIZE, SDL_WINDOW_OPENGL);
if (window_ == nullptr) {
std::cout << "¡No se pudo crear la ventana! Error de SDL: " << SDL_GetError() << std::endl;
success = false;
} else {
// Crear renderizador
renderer_ = SDL_CreateRenderer(window_, nullptr);
if (renderer_ == nullptr) {
std::cout << "¡No se pudo crear el renderizador! Error de SDL: " << SDL_GetError() << std::endl;
success = false;
} else {
// Establecer color inicial del renderizador
SDL_SetRenderDrawColor(renderer_, 0xFF, 0xFF, 0xFF, 0xFF);
// Establecer tamaño lógico para el renderizado
SDL_SetRenderLogicalPresentation(renderer_, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
// Configurar V-Sync inicial
SDL_SetRenderVSync(renderer_, vsync_enabled_ ? 1 : 0);
}
}
}
// Inicializar otros componentes si SDL se inicializó correctamente
if (success) {
texture_ = std::make_shared<Texture>(renderer_, "data/ball.png");
srand(static_cast<unsigned>(time(nullptr)));
dbg_init(renderer_);
initBalls(scenario_);
}
return success;
}
void Engine::run() {
while (!should_exit_) {
calculateDeltaTime();
update();
handleEvents();
render();
}
}
void Engine::shutdown() {
// Limpiar recursos SDL
if (renderer_) {
SDL_DestroyRenderer(renderer_);
renderer_ = nullptr;
}
if (window_) {
SDL_DestroyWindow(window_);
window_ = nullptr;
}
SDL_Quit();
}
// Métodos privados - esqueleto básico por ahora
void Engine::calculateDeltaTime() {
// TODO: Implementar cálculo de delta time
}
void Engine::update() {
// TODO: Implementar lógica de actualización
}
void Engine::handleEvents() {
// TODO: Implementar manejo de eventos
}
void Engine::render() {
// TODO: Implementar renderizado
}
void Engine::initBalls(int value) {
// TODO: Implementar inicialización de pelotas
}
void Engine::setText() {
// TODO: Implementar texto
}
void Engine::pushUpBalls() {
// TODO: Implementar impulso de pelotas
}
void Engine::switchBallsGravity() {
// TODO: Implementar cambio de gravedad
}
void Engine::changeGravityDirection(GravityDirection direction) {
// TODO: Implementar cambio de dirección
}
void Engine::toggleVSync() {
// TODO: Implementar toggle V-Sync
}
std::string Engine::gravityDirectionToString(GravityDirection direction) const {
// TODO: Implementar conversión a string
return "DOWN";
}
void Engine::renderGradientBackground() {
// TODO: Implementar fondo degradado
}
void Engine::addSpriteToBatch(float x, float y, float w, float h, int r, int g, int b) {
// TODO: Implementar batch rendering
}