refactor: introduir Config::EngineConfig com a struct POD a core/
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) <noreply@anthropic.com>
This commit is contained in:
@@ -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 <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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
|
||||||
+33
-64
@@ -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 <SDL3/SDL.h> // Para SDL_Scancode
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "core/config/engine_config.hpp"
|
||||||
|
|
||||||
namespace Options {
|
namespace Options {
|
||||||
|
|
||||||
// Estructures de configuración
|
// Ú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 Window {
|
// struct.
|
||||||
int width{1280};
|
inline Config::EngineConfig engine_config{
|
||||||
int height{720};
|
.player2 = {
|
||||||
bool fullscreen{false};
|
.keyboard = {
|
||||||
float zoom_factor{1.0F}; // Zoom level (0.5x to max_zoom)
|
.key_left = SDL_SCANCODE_A,
|
||||||
};
|
|
||||||
|
|
||||||
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,
|
|
||||||
.key_right = SDL_SCANCODE_D,
|
.key_right = SDL_SCANCODE_D,
|
||||||
.key_thrust = SDL_SCANCODE_W,
|
.key_thrust = SDL_SCANCODE_W,
|
||||||
.key_shoot = SDL_SCANCODE_LSHIFT,
|
.key_shoot = SDL_SCANCODE_LSHIFT,
|
||||||
.key_start = SDL_SCANCODE_2},
|
.key_start = SDL_SCANCODE_2,
|
||||||
.gamepad_name = "" // Segon gamepad disponible
|
},
|
||||||
|
.gamepad_name = "",
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Per compatibilitat con pollo (no utilitzat en orni, pero necessari per Input)
|
// Aliases (referències) per al codi existent.
|
||||||
inline KeyboardControls keyboard_controls{};
|
inline Config::WindowConfig& window = engine_config.window;
|
||||||
inline GamepadControls gamepad_controls{};
|
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()
|
inline std::string config_file_path{}; // Establert per setConfigFile()
|
||||||
|
|
||||||
// Funciones públiques
|
// Funciones públiques
|
||||||
|
|||||||
Reference in New Issue
Block a user