324 lines
8.0 KiB
C++
324 lines
8.0 KiB
C++
#include "input.h"
|
|
#include <stdio.h>
|
|
#include <iostream>
|
|
|
|
// Constructor
|
|
Input::Input(int source)
|
|
{
|
|
// Inicializa las variables
|
|
for (int i = 0; i < 17; i++)
|
|
{
|
|
mKeyBindings[i].scancode = 0;
|
|
mKeyBindings[i].active = false;
|
|
|
|
mGameControllerBindings[i].button = SDL_CONTROLLER_BUTTON_INVALID;
|
|
mGameControllerBindings[i].active = false;
|
|
}
|
|
|
|
mSource = source;
|
|
|
|
if (mSource == USE_GAMECONTROLLER)
|
|
{
|
|
/*
|
|
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
|
|
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
|
|
|
int nJoysticks = SDL_NumJoysticks();
|
|
int numGamepads = 0;
|
|
|
|
// Count how many controllers there are
|
|
for (int i = 0; i < nJoysticks; i++)
|
|
if (SDL_IsGameController(i))
|
|
numGamepads++;
|
|
|
|
printf("%i joysticks found, %i are gamepads\n", nJoysticks, numGamepads);
|
|
//SDL_JoystickEventState(SDL_ENABLE);
|
|
*/
|
|
if (!discoverGameController())
|
|
mSource = USE_KEYBOARD;
|
|
}
|
|
|
|
// 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;
|
|
|
|
if (mSource == USE_KEYBOARD)
|
|
printf("Input using KEYBOARD\n");
|
|
else
|
|
printf("Input using GAMECONTROLLER\n");
|
|
}
|
|
|
|
// 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()
|
|
{
|
|
bool found = false;
|
|
/*
|
|
printf("%i joystics found\n", SDL_NumJoysticks());
|
|
for (int i = 0; i < SDL_NumJoysticks(); ++i)
|
|
{
|
|
if (SDL_IsGameController(i))
|
|
{
|
|
char *mapping;
|
|
printf("Index %i is a compatible controller, named %s\n", i, SDL_GameControllerNameForIndex(i));
|
|
mGameController = SDL_GameControllerOpen(i);
|
|
mapping = SDL_GameControllerMapping(mGameController);
|
|
printf("Controller %i is mapped as %s\n", i, mapping);
|
|
SDL_free(mapping);
|
|
found = true;
|
|
}
|
|
else
|
|
{
|
|
printf("Index %i is not a compatible controller.\n", i);
|
|
found = false;
|
|
}
|
|
}
|
|
*/
|
|
|
|
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
|
|
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
|
|
|
int nJoysticks = SDL_NumJoysticks();
|
|
int numGamepads = 0;
|
|
|
|
// Count how many controllers there are
|
|
for (int i = 0; i < nJoysticks; i++)
|
|
if (SDL_IsGameController(i))
|
|
numGamepads++;
|
|
|
|
printf("%i joysticks found, %i are gamepads\n", nJoysticks, numGamepads);
|
|
|
|
//if (numGamepads > 0)
|
|
// found = true;
|
|
//SDL_JoystickEventState(SDL_ENABLE);
|
|
|
|
if (numGamepads > 0)
|
|
{
|
|
for (int i = 0; i < numGamepads; i++)
|
|
{
|
|
// Open the controller and add it to our list
|
|
mGameController = SDL_GameControllerOpen(i);
|
|
if (SDL_GameControllerGetAttached(mGameController) == 1)
|
|
//connectedControllers.push_back(pad);
|
|
{
|
|
found = true;
|
|
printf("%s\n", SDL_GameControllerNameForIndex(i));
|
|
}
|
|
else
|
|
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
|
}
|
|
SDL_GameControllerEventState(SDL_ENABLE);
|
|
}
|
|
|
|
//SDL_GameControllerGetButton(mGameController, SDL_CONTROLLER_BUTTON_A);
|
|
//printf("hola");
|
|
return found;
|
|
}
|
|
/*if (SDL_NumJoysticks() < 1)
|
|
{
|
|
printf("Warning: No joysticks connected!\n");
|
|
mGameControllerFound = false;
|
|
std::cout << "Warning: No joysticks connected!\n";
|
|
}
|
|
else
|
|
{*/
|
|
// Carga el mando
|
|
|
|
/*mGameController = SDL_GameControllerOpen(0);
|
|
|
|
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());
|
|
std::cout << "joysticks were found!\n";
|
|
mGameControllerFound = true;
|
|
|
|
//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;
|
|
}*/ |