working on game controller input

This commit is contained in:
2021-08-24 17:57:21 +02:00
parent b61fd84e22
commit 7c0201f913
3 changed files with 156 additions and 81 deletions

View File

@@ -1,13 +1,28 @@
#include "input.h"
#include <stdio.h>
// Constructor
Input::Input()
Input::Input(int source)
{
// Inicializa las variables
for (int i = 0; i < 15; i++)
{
mInput[i].scancode = 0;
mInput[i].active = false;
mKeyBindings[i].scancode = 0;
mKeyBindings[i].active = false;
mGameControllerBindings[i].button = SDL_CONTROLLER_BUTTON_INVALID;
mGameControllerBindings[i].active = false;
}
// Comprueba si hay algún mando conectado
discoverGameController();
// En caso de haber un mando, el objeto se puede utilizar con entradas de mando
if (mGameControllerFound)
mSource = source;
// Si no hay un mando, el objeto se configura como teclado
else
mSource = USE_KEYBOARD;
}
// Destructor
@@ -18,57 +33,103 @@ Input::~Input()
// Asigna uno de los posibles inputs a una tecla del teclado
void Input::bindKey(Uint8 input, SDL_Scancode code)
{
mInput[input].scancode = code;
mKeyBindings[input].scancode = code;
}
// Asigna uno de los posibles inputs a un botón del mando
void Input::bindGameController(Uint8 input, SDL_GameControllerButton button)
{
mGameControllerBindings[input].button = button;
}
// Comprueba si un input esta activo
bool Input::checkInput(Uint8 input, bool repeat)
{
const Uint8 *mKeystates = SDL_GetKeyboardState(NULL);
if (mSource == USE_KEYBOARD)
{
const Uint8 *mKeystates = SDL_GetKeyboardState(NULL);
if (repeat)
{
if (mKeystates[mInput[input].scancode] != 0)
return true;
else
return false;
}
else
{
if (!mInput[input].active)
if (repeat)
{
if (mKeystates[mInput[input].scancode] != 0)
{
mInput[input].active = true;
if (mKeystates[mKeyBindings[input].scancode] != 0)
return true;
}
else
{
return false;
}
}
else
{
if (mKeystates[mInput[input].scancode] == 0)
if (!mKeyBindings[input].active)
{
mInput[input].active = false;
return false;
if (mKeystates[mKeyBindings[input].scancode] != 0)
{
mKeyBindings[input].active = true;
return true;
}
else
{
return false;
}
}
else
{
if (mKeystates[mKeyBindings[input].scancode] == 0)
{
mKeyBindings[input].active = false;
return false;
}
else
{
return false;
}
}
}
}
else // Utiliza mando
{
if (repeat)
{
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) != 0)
return true;
else
return false;
}
else
{
if (!mGameControllerBindings[input].active)
{
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) != 0)
{
mGameControllerBindings[input].active = true;
return true;
}
else
{
return false;
}
}
else
{
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) == 0)
{
mGameControllerBindings[input].active = false;
return false;
}
else
{
return false;
}
}
}
}
}
// Gestiona las entradas desde el mando de juego
bool Input::checkGameController(Uint8 state)
/*bool Input::checkGameController(Uint8 state)
{
bool success = false;
// No hay mando. Siempre devuelve falso salvo NO_INPUT que siempre es cierto
/*if (!mGameControllerFound)
if (!mGameControllerFound)
{
if (state == NO_INPUT)
return true;
@@ -117,47 +178,52 @@ bool Input::checkGameController(Uint8 state)
default:
break;
}*/
}
return success;
}
}*/
// Comprueba si hay algun mando conectado
/*if (SDL_NumJoysticks() < 1)
// Comprueba si hay un mando conectado
bool Input::discoverGameController()
{
if (SDL_NumJoysticks() < 1)
{
printf("Warning: No joysticks connected!\n");
mGameControllerFound = false;
}
else
{
// Carga el mando
mGameController = SDL_GameControllerOpen(0);
mGameControllerFound = true;
if (mGameController == NULL)
{
printf("Warning: No joysticks connected!\n");
printf("Warning: Unable to open game controller!\nSDL Error: %s\n", SDL_GetError());
mGameControllerFound = false;
}
else
{
// Carga el mando
mGameController = SDL_JoystickOpen(0);
mGameControllerFound = true;
printf("%i joysticks were found.\n", SDL_NumJoysticks());
//printf("%i buttons\n", SDL_JoystickNumButtons(mGameController));
if (mGameController == NULL)
// Obtiene el dispositivo de control háptico
/*mControllerHaptic = SDL_HapticOpenFromJoystick(mGameController);
if (mControllerHaptic == NULL)
{
printf("Warning: Unable to open game controller!\nSDL Error: %s\n", SDL_GetError());
mGameControllerFound = false;
printf("Warning: Controller does not support haptics!\nSDL Error: %s\n", SDL_GetError());
}
else
{
printf("%i joysticks were found.\n", SDL_NumJoysticks());
std::cout << SDL_JoystickNumButtons(mGameController) << " buttons\n";
printf("Haptics detected\n");
// Obtiene el dispositivo de control háptico
mControllerHaptic = SDL_HapticOpenFromJoystick(mGameController);
if (mControllerHaptic == NULL)
// Inicializa la vibración
if (SDL_HapticRumbleInit(mControllerHaptic) < 0)
{
printf("Warning: Controller does not support haptics!\nSDL Error: %s\n", SDL_GetError());
printf("Warning: Unable to initialize rumble!\nSDL Error: %s\n", SDL_GetError());
}
else
{
printf("Haptics detected\n");
}*/
}
}
// Inicializa la vibración
if (SDL_HapticRumbleInit(mControllerHaptic) < 0)
{
printf("Warning: Unable to initialize rumble!\nSDL Error: %s\n", SDL_GetError());
}
}
}
}*/
return mGameControllerFound;
}