canvi de pc

This commit is contained in:
2025-02-21 19:45:29 +01:00
parent 5f68c6256f
commit 7a0bc5c9ae
18 changed files with 918 additions and 889 deletions

View File

@@ -6,31 +6,47 @@
#include <SDL2/SDL_keyboard.h> // Para SDL_GetKeyboardState
#include <iostream> // Para basic_ostream, operator<<, cout, basi...
// Constructor
Input::Input(std::string file)
{
// Fichero gamecontrollerdb.txt
dbPath = file;
// [SINGLETON]
Input *Input::input_ = nullptr;
// [SINGLETON] Crearemos el objeto con esta función estática
void Input::init(const std::string &game_controller_db_path)
{
Input::input_ = new Input(game_controller_db_path);
}
// [SINGLETON] Destruiremos el objeto con esta función estática
void Input::destroy()
{
delete Input::input_;
}
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
Input *Input::get()
{
return Input::input_;
}
// Constructor
Input::Input(const std::string &game_controller_db_path)
: db_path_(game_controller_db_path)
{
// Inicializa las variables
keyBindings_t kb;
kb.scancode = 0;
kb.active = false;
keyBindings.resize(input_number_of_inputs, kb);
key_bindings_.resize(input_number_of_inputs, kb);
GameControllerBindings_t gcb;
gcb.button = SDL_CONTROLLER_BUTTON_INVALID;
gcb.active = false;
gameControllerBindings.resize(input_number_of_inputs, gcb);
verbose = true;
enabled = true;
game_controller_bindings_.resize(input_number_of_inputs, gcb);
}
// Actualiza el estado del objeto
void Input::update()
{
if (disabledUntil == d_keyPressed && !checkAnyInput())
if (disabled_until_ == d_keyPressed && !checkAnyInput())
{
enable();
}
@@ -39,19 +55,19 @@ void Input::update()
// Asigna inputs a teclas
void Input::bindKey(Uint8 input, SDL_Scancode code)
{
keyBindings[input].scancode = code;
key_bindings_[input].scancode = code;
}
// Asigna inputs a botones del mando
void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button)
{
gameControllerBindings[input].button = button;
game_controller_bindings_[input].button = button;
}
// Comprueba si un input esta activo
bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
{
if (!enabled)
if (!enabled_)
{
return false;
}
@@ -70,7 +86,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
if (repeat)
{
if (keyStates[keyBindings[input].scancode] != 0)
if (keyStates[key_bindings_[input].scancode] != 0)
{
successKeyboard = true;
}
@@ -81,11 +97,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
}
else
{
if (!keyBindings[input].active)
if (!key_bindings_[input].active)
{
if (keyStates[keyBindings[input].scancode] != 0)
if (keyStates[key_bindings_[input].scancode] != 0)
{
keyBindings[input].active = true;
key_bindings_[input].active = true;
successKeyboard = true;
}
else
@@ -95,9 +111,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
}
else
{
if (keyStates[keyBindings[input].scancode] == 0)
if (keyStates[key_bindings_[input].scancode] == 0)
{
keyBindings[input].active = false;
key_bindings_[input].active = false;
successKeyboard = false;
}
else
@@ -113,7 +129,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
{
if (repeat)
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
if (SDL_GameControllerGetButton(connected_controllers_[index], game_controller_bindings_[input].button) != 0)
{
successGameController = true;
}
@@ -124,11 +140,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
}
else
{
if (!gameControllerBindings[input].active)
if (!game_controller_bindings_[input].active)
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
if (SDL_GameControllerGetButton(connected_controllers_[index], game_controller_bindings_[input].button) != 0)
{
gameControllerBindings[input].active = true;
game_controller_bindings_[input].active = true;
successGameController = true;
}
else
@@ -138,9 +154,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
}
else
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) == 0)
if (SDL_GameControllerGetButton(connected_controllers_[index], game_controller_bindings_[input].button) == 0)
{
gameControllerBindings[input].active = false;
game_controller_bindings_[input].active = false;
successGameController = false;
}
else
@@ -166,9 +182,9 @@ bool Input::checkAnyInput(int device, int index)
{
const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr);
for (int i = 0; i < (int)keyBindings.size(); ++i)
for (int i = 0; i < (int)key_bindings_.size(); ++i)
{
if (mKeystates[keyBindings[i].scancode] != 0)
if (mKeystates[key_bindings_[i].scancode] != 0)
{
return true;
}
@@ -179,9 +195,9 @@ bool Input::checkAnyInput(int device, int index)
{
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY)
{
for (int i = 0; i < (int)gameControllerBindings.size(); ++i)
for (int i = 0; i < (int)game_controller_bindings_.size(); ++i)
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0)
if (SDL_GameControllerGetButton(connected_controllers_[index], game_controller_bindings_[i].button) != 0)
{
return true;
}
@@ -202,56 +218,56 @@ bool Input::discoverGameController()
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
}
if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0)
if (SDL_GameControllerAddMappingsFromFile(db_path_.c_str()) < 0)
{
if (verbose)
if (verbose_)
{
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl;
std::cout << "Error, could not load " << db_path_.c_str() << " file: " << SDL_GetError() << std::endl;
}
}
const int nJoysticks = SDL_NumJoysticks();
numGamepads = 0;
num_gamepads_ = 0;
// Cuenta el numero de mandos
for (int i = 0; i < nJoysticks; ++i)
{
if (SDL_IsGameController(i))
{
numGamepads++;
num_gamepads_++;
}
}
if (verbose)
if (verbose_)
{
std::cout << "\nChecking for game controllers...\n";
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
std::cout << nJoysticks << " joysticks found, " << num_gamepads_ << " are gamepads\n";
}
if (numGamepads > 0)
if (num_gamepads_ > 0)
{
found = true;
for (int i = 0; i < numGamepads; i++)
for (int i = 0; i < num_gamepads_; i++)
{
// Abre el mando y lo añade a la lista
SDL_GameController *pad = SDL_GameControllerOpen(i);
if (SDL_GameControllerGetAttached(pad) == 1)
{
connectedControllers.push_back(pad);
connected_controllers_.push_back(pad);
const std::string separator(" #");
std::string name = SDL_GameControllerNameForIndex(i);
name.resize(25);
name = name + separator + std::to_string(i);
if (verbose)
if (verbose_)
{
std::cout << name << std::endl;
}
controllerNames.push_back(name);
controller_names_.push_back(name);
}
else
{
if (verbose)
if (verbose_)
{
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
}
@@ -267,7 +283,7 @@ bool Input::discoverGameController()
// Comprueba si hay algun mando conectado
bool Input::gameControllerFound()
{
if (numGamepads > 0)
if (num_gamepads_ > 0)
{
return true;
}
@@ -280,9 +296,9 @@ bool Input::gameControllerFound()
// Obten el nombre de un mando de juego
std::string Input::getControllerName(int index)
{
if (numGamepads > 0)
if (num_gamepads_ > 0)
{
return controllerNames[index];
return controller_names_[index];
}
else
{
@@ -293,25 +309,25 @@ std::string Input::getControllerName(int index)
// Obten el numero de mandos conectados
int Input::getNumControllers()
{
return numGamepads;
return num_gamepads_;
}
// Establece si ha de mostrar mensajes
void Input::setVerbose(bool value)
{
verbose = value;
verbose_ = value;
}
// Deshabilita las entradas durante un periodo de tiempo
void Input::disableUntil(i_disable_e value)
{
disabledUntil = value;
enabled = false;
disabled_until_ = value;
enabled_ = false;
}
// Hablita las entradas
void Input::enable()
{
enabled = true;
disabledUntil = d_notDisabled;
enabled_ = true;
disabled_until_ = d_notDisabled;
}