From 5f6d51b6cb404f92542e3ec22cca5fefe2310568 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 20 May 2026 19:24:06 +0200 Subject: [PATCH] refactor: introduir Config::EngineConfig com a struct POD a core/ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pas 1/N del hallazgo #28. Sense canvi de comportament: Nou: source/core/config/engine_config.hpp - Defineix Config::EngineConfig (POD) amb les sub-structs WindowConfig, RenderingConfig, KeyboardBindings, GamepadBindings, PlayerBindings i el flag console. - Sense singletons ni virtuals; la inversió real es fa en commits posteriors injectant Config::EngineConfig& per constructor. Modificat: source/game/options.hpp - Elimina les struct definitions locals (Window, Rendering, ...). - Afegeix Options::engine_config (única font de veritat). - Conserva Options::window, Options::rendering, player1, player2, keyboard_controls, gamepad_controls i console com a referències inline a camps d'engine_config. Cost runtime zero, callsites existents no requereixen cap canvi. Hallazgo #28 de CODE_REVIEW.md. Co-Authored-By: Claude Opus 4.7 (1M context) --- source/core/config/engine_config.hpp | 61 +++++++++++++++++ source/game/options.hpp | 97 ++++++++++------------------ 2 files changed, 94 insertions(+), 64 deletions(-) create mode 100644 source/core/config/engine_config.hpp diff --git a/source/core/config/engine_config.hpp b/source/core/config/engine_config.hpp new file mode 100644 index 0000000..47fd099 --- /dev/null +++ b/source/core/config/engine_config.hpp @@ -0,0 +1,61 @@ +// engine_config.hpp - Configuració runtime del motor (window, render, input) +// © 2026 JailDesigner +// +// Struct POD que conté la configuració runtime que els sistemes de `core/` +// llegeixen i muten. La capa de persistència (YAML) viu a `game/options.cpp`, +// que omple aquesta struct a init() i loadFromFile(). +// +// Es passa per referència (mutable quan cal) al constructor dels sistemes +// que la necessiten, mantenint `core/` agnòstic a `game/`. + +#pragma once + +#include + +#include + +namespace Config { + + struct WindowConfig { + int width{1280}; + int height{720}; + bool fullscreen{false}; + float zoom_factor{1.0F}; // Zoom level (0.5x to max_zoom) + }; + + struct RenderingConfig { + int vsync{1}; // 0=disabled, 1=enabled + }; + + struct KeyboardBindings { + SDL_Scancode key_left{SDL_SCANCODE_LEFT}; + SDL_Scancode key_right{SDL_SCANCODE_RIGHT}; + SDL_Scancode key_thrust{SDL_SCANCODE_UP}; + SDL_Scancode key_shoot{SDL_SCANCODE_SPACE}; + SDL_Scancode key_start{SDL_SCANCODE_1}; + }; + + struct GamepadBindings { + int button_left{SDL_GAMEPAD_BUTTON_DPAD_LEFT}; + int button_right{SDL_GAMEPAD_BUTTON_DPAD_RIGHT}; + int button_thrust{SDL_GAMEPAD_BUTTON_WEST}; // X button + int button_shoot{SDL_GAMEPAD_BUTTON_SOUTH}; // A button + }; + + struct PlayerBindings { + KeyboardBindings keyboard{}; + GamepadBindings gamepad{}; + std::string gamepad_name; // Empty = auto-assign by index + }; + + struct EngineConfig { + WindowConfig window{}; + RenderingConfig rendering{}; + PlayerBindings player1{}; + PlayerBindings player2{}; + KeyboardBindings keyboard_controls{}; // Defaults globals per Input + GamepadBindings gamepad_controls{}; + bool console{false}; + }; + +} // namespace Config diff --git a/source/game/options.hpp b/source/game/options.hpp index e4b0f01..ef8b3dd 100644 --- a/source/game/options.hpp +++ b/source/game/options.hpp @@ -1,79 +1,48 @@ -#pragma once +// options.hpp - Persistència YAML i globals d'opcions (capa game) +// © 2026 JailDesigner +// +// La configuració runtime viu en una struct única (Config::EngineConfig, +// definida a `core/config/engine_config.hpp`). Aquest fitxer hi posa al +// damunt la capa de persistència YAML i les compatibilitats històriques +// (Options::window, Options::rendering, ...) com a referències inline a +// camps d'aquesta struct. Cost runtime zero, callsites existents no +// requereixen cap canvi. -#include // Para SDL_Scancode +#pragma once #include +#include "core/config/engine_config.hpp" + namespace Options { - // Estructures de configuración - - struct Window { - int width{1280}; - int height{720}; - bool fullscreen{false}; - float zoom_factor{1.0F}; // Zoom level (0.5x to max_zoom) - }; - - struct Rendering { - int vsync{1}; // 0=disabled, 1=enabled - }; - - // Controles de jugadors - - struct KeyboardControls { - SDL_Scancode key_left{SDL_SCANCODE_LEFT}; - SDL_Scancode key_right{SDL_SCANCODE_RIGHT}; - SDL_Scancode key_thrust{SDL_SCANCODE_UP}; - SDL_Scancode key_shoot{SDL_SCANCODE_SPACE}; - SDL_Scancode key_start{SDL_SCANCODE_1}; - }; - - struct GamepadControls { - int button_left{SDL_GAMEPAD_BUTTON_DPAD_LEFT}; - int button_right{SDL_GAMEPAD_BUTTON_DPAD_RIGHT}; - int button_thrust{SDL_GAMEPAD_BUTTON_WEST}; // X button - int button_shoot{SDL_GAMEPAD_BUTTON_SOUTH}; // A button - }; - - struct PlayerControls { - KeyboardControls keyboard{}; - GamepadControls gamepad{}; - std::string gamepad_name; // Buit = auto-assignar per índex - }; - - // Variables globals (inline per evitar ODR violations) - - inline std::string version{}; // Versión del config per validació - inline bool console{false}; // Eixida de debug - inline Window window{}; - inline Rendering rendering{}; - - // Controles per player - inline PlayerControls player1{ - .keyboard = - {.key_left = SDL_SCANCODE_LEFT, - .key_right = SDL_SCANCODE_RIGHT, - .key_thrust = SDL_SCANCODE_UP, - .key_shoot = SDL_SCANCODE_SPACE, - .key_start = SDL_SCANCODE_1}, - .gamepad_name = "" // Primer gamepad disponible - }; - - inline PlayerControls player2{ - .keyboard = - {.key_left = SDL_SCANCODE_A, + // Única font de veritat de la configuració runtime. Tota la resta de + // globals d'aquest namespace són referències inline a camps d'aquesta + // struct. + inline Config::EngineConfig engine_config{ + .player2 = { + .keyboard = { + .key_left = SDL_SCANCODE_A, .key_right = SDL_SCANCODE_D, .key_thrust = SDL_SCANCODE_W, .key_shoot = SDL_SCANCODE_LSHIFT, - .key_start = SDL_SCANCODE_2}, - .gamepad_name = "" // Segon gamepad disponible + .key_start = SDL_SCANCODE_2, + }, + .gamepad_name = "", + }, }; - // Per compatibilitat con pollo (no utilitzat en orni, pero necessari per Input) - inline KeyboardControls keyboard_controls{}; - inline GamepadControls gamepad_controls{}; + // Aliases (referències) per al codi existent. + inline Config::WindowConfig& window = engine_config.window; + inline Config::RenderingConfig& rendering = engine_config.rendering; + inline Config::PlayerBindings& player1 = engine_config.player1; + inline Config::PlayerBindings& player2 = engine_config.player2; + inline Config::KeyboardBindings& keyboard_controls = engine_config.keyboard_controls; + inline Config::GamepadBindings& gamepad_controls = engine_config.gamepad_controls; + inline bool& console = engine_config.console; + // Persistència YAML (es queda en game/, no en core/) + inline std::string version{}; // Versión del config per validació inline std::string config_file_path{}; // Establert per setConfigFile() // Funciones públiques