229 lines
5.6 KiB
C++
229 lines
5.6 KiB
C++
#include "input.h"
|
|
#include <stdio.h>
|
|
|
|
// Constructor
|
|
Input::Input(int source)
|
|
{
|
|
// Inicializa las variables
|
|
for (int i = 0; i < 15; i++)
|
|
{
|
|
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
|
|
Input::~Input()
|
|
{
|
|
}
|
|
|
|
// Asigna uno de los posibles inputs a una tecla del teclado
|
|
void Input::bindKey(Uint8 input, SDL_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)
|
|
{
|
|
if (mSource == USE_KEYBOARD)
|
|
{
|
|
const Uint8 *mKeystates = SDL_GetKeyboardState(NULL);
|
|
|
|
if (repeat)
|
|
{
|
|
if (mKeystates[mKeyBindings[input].scancode] != 0)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
if (!mKeyBindings[input].active)
|
|
{
|
|
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 success = false;
|
|
|
|
// No hay mando. Siempre devuelve falso salvo NO_INPUT que siempre es cierto
|
|
if (!mGameControllerFound)
|
|
{
|
|
if (state == NO_INPUT)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
|
|
switch (state)
|
|
{
|
|
case INPUT_NULL:
|
|
success = !checkGameController(INPUT_UP) && !checkGameController(INPUT_DOWN) && !checkGameController(INPUT_LEFT) && !checkGameController(INPUT_RIGHT) &&
|
|
!checkGameController(INPUT_ACCEPT) && !checkGameController(INPUT_CANCEL) && !checkGameController(INPUT_PAUSE) &&
|
|
!checkGameController(INPUT_FIRE_UP) && !checkGameController(INPUT_FIRE_LEFT) && !checkGameController(INPUT_FIRE_RIGHT);
|
|
break;
|
|
case INPUT_UP:
|
|
success = (SDL_JoystickGetAxis(mGameController, 1) < -JOYSTICK_DEAD_ZONE) || (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_DPAD_UP));
|
|
break;
|
|
case INPUT_DOWN:
|
|
success = (SDL_JoystickGetAxis(mGameController, 1) > JOYSTICK_DEAD_ZONE) || (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_DPAD_DOWN));
|
|
break;
|
|
case INPUT_LEFT:
|
|
success = (SDL_JoystickGetAxis(mGameController, 0) < -JOYSTICK_DEAD_ZONE) || (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_DPAD_LEFT));
|
|
break;
|
|
case INPUT_RIGHT:
|
|
success = (SDL_JoystickGetAxis(mGameController, 0) > JOYSTICK_DEAD_ZONE) || (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
|
|
break;
|
|
case INPUT_ACCEPT:
|
|
success = (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_B));
|
|
break;
|
|
case INPUT_CANCEL:
|
|
success = (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_A));
|
|
break;
|
|
case INPUT_PAUSE:
|
|
success = (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_START));
|
|
break;
|
|
case INPUT_FIRE_UP:
|
|
success = (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_Y));
|
|
break;
|
|
case INPUT_FIRE_LEFT:
|
|
success = (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_X));
|
|
break;
|
|
case INPUT_FIRE_RIGHT:
|
|
success = (SDL_JoystickGetButton(mGameController, SDL_CONTROLLER_BUTTON_B));
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return success;
|
|
}*/
|
|
|
|
// 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: Unable to open game controller!\nSDL Error: %s\n", SDL_GetError());
|
|
mGameControllerFound = false;
|
|
}
|
|
else
|
|
{
|
|
printf("%i joysticks were found.\n", SDL_NumJoysticks());
|
|
//printf("%i buttons\n", SDL_JoystickNumButtons(mGameController));
|
|
|
|
// Obtiene el dispositivo de control háptico
|
|
/*mControllerHaptic = SDL_HapticOpenFromJoystick(mGameController);
|
|
if (mControllerHaptic == NULL)
|
|
{
|
|
printf("Warning: Controller does not support haptics!\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;
|
|
} |