singletons

This commit is contained in:
2026-04-17 21:27:30 +02:00
parent 5889df2a47
commit 513eacf356
27 changed files with 536 additions and 505 deletions

View File

@@ -5,35 +5,35 @@
namespace GlobalInputs {
auto handle(Screen *screen, Input *input) -> bool {
if (screen == nullptr || input == nullptr) { return false; }
auto handle() -> bool {
if (Screen::get() == nullptr || Input::get() == nullptr) { return false; }
if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->toggleVideoMode();
if (Input::get()->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
Screen::get()->toggleVideoMode();
return true;
}
if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
screen->decWindowZoom();
if (Input::get()->checkInput(input_window_dec_size, REPEAT_FALSE)) {
Screen::get()->decWindowZoom();
return true;
}
if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
screen->incWindowZoom();
if (Input::get()->checkInput(input_window_inc_size, REPEAT_FALSE)) {
Screen::get()->incWindowZoom();
return true;
}
if (input->checkInput(input_prev_preset, REPEAT_FALSE)) {
screen->prevPreset();
if (Input::get()->checkInput(input_prev_preset, REPEAT_FALSE)) {
Screen::get()->prevPreset();
return true;
}
if (input->checkInput(input_next_preset, REPEAT_FALSE)) {
screen->nextPreset();
if (Input::get()->checkInput(input_next_preset, REPEAT_FALSE)) {
Screen::get()->nextPreset();
return true;
}
if (input->checkInput(input_toggle_shader, REPEAT_FALSE)) {
screen->toggleShaderEnabled();
if (Input::get()->checkInput(input_toggle_shader, REPEAT_FALSE)) {
Screen::get()->toggleShaderEnabled();
return true;
}
if (input->checkInput(input_toggle_shader_type, REPEAT_FALSE)) {
screen->toggleActiveShader();
if (Input::get()->checkInput(input_toggle_shader_type, REPEAT_FALSE)) {
Screen::get()->toggleActiveShader();
return true;
}
return false;

View File

@@ -1,13 +1,10 @@
#pragma once
class Input;
class Screen;
namespace GlobalInputs {
// Gestiona els atalls globals disponibles en qualsevol escena: zoom de
// finestra (F1/F2), fullscreen (F3), presets de shader (F8/F9), toggle
// shader (F10) i tipus de shader POSTFX↔CRTPI (F11). Retorna true si ha
// consumit alguna tecla (per si la capa cridant vol suprimir-la del
// processament específic de l'escena).
auto handle(Screen *screen, Input *input) -> bool;
auto handle() -> bool;
} // namespace GlobalInputs

View File

@@ -39,6 +39,23 @@ static void installWebStandardMapping(SDL_JoystickID jid) {
#endif
}
// Instancia única
Input *Input::instance = nullptr;
// Singleton API
void Input::init(const std::string &gameControllerDbPath) {
Input::instance = new Input(gameControllerDbPath);
}
void Input::destroy() {
delete Input::instance;
Input::instance = nullptr;
}
auto Input::get() -> Input * {
return Input::instance;
}
// Constructor
Input::Input(std::string file) {
// Fichero gamecontrollerdb.txt

View File

@@ -79,10 +79,18 @@ class Input {
// Construye el nombre visible de un mando (name truncado + sufijo #N)
std::string buildControllerName(SDL_Gamepad *pad, int padIndex);
public:
// Constructor
// Constructor privado (usar Input::init)
Input(std::string file);
// Instancia única
static Input *instance;
public:
// Singleton API
static void init(const std::string &gameControllerDbPath); // Crea la instancia
static void destroy(); // Libera la instancia
static auto get() -> Input *; // Obtiene el puntero a la instancia
// Destructor
~Input();