clang-format

This commit is contained in:
2026-04-03 10:58:04 +02:00
parent 550a005ca6
commit c25d4dc7e5
50 changed files with 9735 additions and 11503 deletions

View File

@@ -1,324 +1,250 @@
#include "input.h"
#include <SDL3/SDL.h>
#include <iostream> // for basic_ostream, operator<<, cout, basi...
#include <iostream> // for basic_ostream, operator<<, cout, basi...
// Constructor
Input::Input(std::string file)
{
// Fichero gamecontrollerdb.txt
dbPath = file;
Input::Input(std::string file) {
// Fichero gamecontrollerdb.txt
dbPath = file;
// Inicializa las variables
keyBindings_t kb;
kb.scancode = 0;
kb.active = false;
keyBindings.resize(input_number_of_inputs, kb);
// Inicializa las variables
keyBindings_t kb;
kb.scancode = 0;
kb.active = false;
keyBindings.resize(input_number_of_inputs, kb);
GameControllerBindings_t gcb;
gcb.button = SDL_GAMEPAD_BUTTON_INVALID;
gcb.active = false;
gameControllerBindings.resize(input_number_of_inputs, gcb);
GameControllerBindings_t gcb;
gcb.button = SDL_GAMEPAD_BUTTON_INVALID;
gcb.active = false;
gameControllerBindings.resize(input_number_of_inputs, gcb);
verbose = true;
enabled = true;
verbose = true;
enabled = true;
}
// Actualiza el estado del objeto
void Input::update()
{
if (disabledUntil == d_keyPressed && !checkAnyInput())
{
enable();
}
void Input::update() {
if (disabledUntil == d_keyPressed && !checkAnyInput()) {
enable();
}
}
// Asigna inputs a teclas
void Input::bindKey(Uint8 input, SDL_Scancode code)
{
keyBindings[input].scancode = code;
void Input::bindKey(Uint8 input, SDL_Scancode code) {
keyBindings[input].scancode = code;
}
// Asigna inputs a botones del mando
void Input::bindGameControllerButton(Uint8 input, SDL_GamepadButton button)
{
gameControllerBindings[input].button = button;
void Input::bindGameControllerButton(Uint8 input, SDL_GamepadButton button) {
gameControllerBindings[input].button = button;
}
// Comprueba si un input esta activo
bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
{
if (!enabled)
{
return false;
}
bool Input::checkInput(Uint8 input, bool repeat, int device, int index) {
if (!enabled) {
return false;
}
bool successKeyboard = false;
bool successGameController = false;
bool successKeyboard = false;
bool successGameController = false;
if (device == INPUT_USE_ANY)
{
index = 0;
}
if (device == INPUT_USE_ANY) {
index = 0;
}
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
{
const bool *keyStates = SDL_GetKeyboardState(nullptr);
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) {
const bool *keyStates = SDL_GetKeyboardState(nullptr);
if (repeat)
{
if (keyStates[keyBindings[input].scancode])
{
successKeyboard = true;
}
else
{
successKeyboard = false;
}
}
else
{
if (!keyBindings[input].active)
{
if (keyStates[keyBindings[input].scancode])
{
keyBindings[input].active = true;
successKeyboard = true;
}
else
{
successKeyboard = false;
}
}
else
{
if (!keyStates[keyBindings[input].scancode])
{
keyBindings[input].active = false;
successKeyboard = false;
}
else
{
successKeyboard = false;
}
}
}
}
if (repeat) {
if (keyStates[keyBindings[input].scancode]) {
successKeyboard = true;
} else {
successKeyboard = false;
}
} else {
if (!keyBindings[input].active) {
if (keyStates[keyBindings[input].scancode]) {
keyBindings[input].active = true;
successKeyboard = true;
} else {
successKeyboard = false;
}
} else {
if (!keyStates[keyBindings[input].scancode]) {
keyBindings[input].active = false;
successKeyboard = false;
} else {
successKeyboard = false;
}
}
}
}
if (gameControllerFound())
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
{
if (repeat)
{
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button))
{
successGameController = true;
}
else
{
successGameController = false;
}
}
else
{
if (!gameControllerBindings[input].active)
{
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button))
{
gameControllerBindings[input].active = true;
successGameController = true;
}
else
{
successGameController = false;
}
}
else
{
if (!SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button))
{
gameControllerBindings[input].active = false;
successGameController = false;
}
else
{
successGameController = false;
}
}
}
}
if (gameControllerFound())
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) {
if (repeat) {
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) {
successGameController = true;
} else {
successGameController = false;
}
} else {
if (!gameControllerBindings[input].active) {
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) {
gameControllerBindings[input].active = true;
successGameController = true;
} else {
successGameController = false;
}
} else {
if (!SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) {
gameControllerBindings[input].active = false;
successGameController = false;
} else {
successGameController = false;
}
}
}
}
return (successKeyboard || successGameController);
return (successKeyboard || successGameController);
}
// Comprueba si hay almenos un input activo
bool Input::checkAnyInput(int device, int index)
{
if (device == INPUT_USE_ANY)
{
index = 0;
}
bool Input::checkAnyInput(int device, int index) {
if (device == INPUT_USE_ANY) {
index = 0;
}
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
{
const bool *mKeystates = SDL_GetKeyboardState(nullptr);
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) {
const bool *mKeystates = SDL_GetKeyboardState(nullptr);
for (int i = 0; i < (int)keyBindings.size(); ++i)
{
if (mKeystates[keyBindings[i].scancode])
{
return true;
}
}
}
for (int i = 0; i < (int)keyBindings.size(); ++i) {
if (mKeystates[keyBindings[i].scancode]) {
return true;
}
}
}
if (gameControllerFound())
{
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY)
{
for (int i = 0; i < (int)gameControllerBindings.size(); ++i)
{
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[i].button))
{
return true;
}
}
}
}
if (gameControllerFound()) {
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) {
for (int i = 0; i < (int)gameControllerBindings.size(); ++i) {
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[i].button)) {
return true;
}
}
}
}
return false;
return false;
}
// Busca si hay un mando conectado
bool Input::discoverGameController()
{
bool found = false;
bool Input::discoverGameController() {
bool found = false;
if (SDL_WasInit(SDL_INIT_GAMEPAD) != SDL_INIT_GAMEPAD)
{
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
}
if (SDL_WasInit(SDL_INIT_GAMEPAD) != SDL_INIT_GAMEPAD) {
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
}
if (SDL_AddGamepadMappingsFromFile(dbPath.c_str()) < 0)
{
if (verbose)
{
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl;
}
}
if (SDL_AddGamepadMappingsFromFile(dbPath.c_str()) < 0) {
if (verbose) {
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl;
}
}
int nJoysticks = 0;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&nJoysticks);
numGamepads = 0;
int nJoysticks = 0;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&nJoysticks);
numGamepads = 0;
if (joysticks)
{
// Cuenta el numero de mandos
for (int i = 0; i < nJoysticks; ++i)
{
if (SDL_IsGamepad(joysticks[i]))
{
numGamepads++;
}
}
if (joysticks) {
// Cuenta el numero de mandos
for (int i = 0; i < nJoysticks; ++i) {
if (SDL_IsGamepad(joysticks[i])) {
numGamepads++;
}
}
if (verbose)
{
std::cout << "\nChecking for game controllers...\n";
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
}
if (verbose) {
std::cout << "\nChecking for game controllers...\n";
std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
}
if (numGamepads > 0)
{
found = true;
int padIndex = 0;
if (numGamepads > 0) {
found = true;
int padIndex = 0;
for (int i = 0; i < nJoysticks; i++)
{
if (!SDL_IsGamepad(joysticks[i])) continue;
for (int i = 0; i < nJoysticks; i++) {
if (!SDL_IsGamepad(joysticks[i])) continue;
// Abre el mando y lo añade a la lista
SDL_Gamepad *pad = SDL_OpenGamepad(joysticks[i]);
if (pad != nullptr)
{
connectedControllers.push_back(pad);
const std::string separator(" #");
const char *padName = SDL_GetGamepadName(pad);
std::string name = padName ? padName : "Unknown";
name.resize(25);
name = name + separator + std::to_string(padIndex);
if (verbose)
{
std::cout << name << std::endl;
}
controllerNames.push_back(name);
padIndex++;
}
else
{
if (verbose)
{
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
}
}
}
// Abre el mando y lo añade a la lista
SDL_Gamepad *pad = SDL_OpenGamepad(joysticks[i]);
if (pad != nullptr) {
connectedControllers.push_back(pad);
const std::string separator(" #");
const char *padName = SDL_GetGamepadName(pad);
std::string name = padName ? padName : "Unknown";
name.resize(25);
name = name + separator + std::to_string(padIndex);
if (verbose) {
std::cout << name << std::endl;
}
controllerNames.push_back(name);
padIndex++;
} else {
if (verbose) {
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
}
}
}
SDL_SetGamepadEventsEnabled(true);
}
SDL_SetGamepadEventsEnabled(true);
}
SDL_free(joysticks);
}
SDL_free(joysticks);
}
return found;
return found;
}
// Comprueba si hay algun mando conectado
bool Input::gameControllerFound()
{
if (numGamepads > 0)
{
return true;
}
else
{
return false;
}
bool Input::gameControllerFound() {
if (numGamepads > 0) {
return true;
} else {
return false;
}
}
// Obten el nombre de un mando de juego
std::string Input::getControllerName(int index)
{
if (numGamepads > 0)
{
return controllerNames[index];
}
else
{
return "";
}
std::string Input::getControllerName(int index) {
if (numGamepads > 0) {
return controllerNames[index];
} else {
return "";
}
}
// Obten el numero de mandos conectados
int Input::getNumControllers()
{
return numGamepads;
int Input::getNumControllers() {
return numGamepads;
}
// Establece si ha de mostrar mensajes
void Input::setVerbose(bool value)
{
verbose = value;
void Input::setVerbose(bool value) {
verbose = value;
}
// Deshabilita las entradas durante un periodo de tiempo
void Input::disableUntil(i_disable_e value)
{
disabledUntil = value;
enabled = false;
void Input::disableUntil(i_disable_e value) {
disabledUntil = value;
enabled = false;
}
// Hablita las entradas
void Input::enable()
{
enabled = true;
disabledUntil = d_notDisabled;
void Input::enable() {
enabled = true;
disabledUntil = d_notDisabled;
}