162 lines
3.4 KiB
C++
162 lines
3.4 KiB
C++
#include "input.h"
|
|
#include <stdio.h>
|
|
#include <iostream>
|
|
|
|
// Constructor
|
|
Input::Input()
|
|
{
|
|
// 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;
|
|
}
|
|
|
|
discoverGameController();
|
|
}
|
|
|
|
// 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, int device)
|
|
{
|
|
bool successKeyboard = false;
|
|
bool successGameController = false;
|
|
|
|
if ((device == INPUT_USE_KEYBOARD) || (device == INPUT_USE_ANY))
|
|
{
|
|
const Uint8 *mKeystates = SDL_GetKeyboardState(NULL);
|
|
|
|
if (repeat)
|
|
{
|
|
if (mKeystates[mKeyBindings[input].scancode] != 0)
|
|
successKeyboard = true;
|
|
else
|
|
successKeyboard = false;
|
|
}
|
|
else
|
|
{
|
|
if (!mKeyBindings[input].active)
|
|
{
|
|
if (mKeystates[mKeyBindings[input].scancode] != 0)
|
|
{
|
|
mKeyBindings[input].active = true;
|
|
successKeyboard = true;
|
|
}
|
|
else
|
|
{
|
|
successKeyboard = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (mKeystates[mKeyBindings[input].scancode] == 0)
|
|
{
|
|
mKeyBindings[input].active = false;
|
|
successKeyboard = false;
|
|
}
|
|
else
|
|
{
|
|
successKeyboard = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
|
|
{
|
|
if (repeat)
|
|
{
|
|
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) != 0)
|
|
successGameController = true;
|
|
else
|
|
successGameController = false;
|
|
}
|
|
else
|
|
{
|
|
if (!mGameControllerBindings[input].active)
|
|
{
|
|
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) != 0)
|
|
{
|
|
mGameControllerBindings[input].active = true;
|
|
successGameController = true;
|
|
}
|
|
else
|
|
{
|
|
successGameController = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) == 0)
|
|
{
|
|
mGameControllerBindings[input].active = false;
|
|
successGameController = false;
|
|
}
|
|
else
|
|
{
|
|
successGameController = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return (successKeyboard || successGameController);
|
|
}
|
|
|
|
// Comprueba si hay un mando conectado
|
|
bool Input::discoverGameController()
|
|
{
|
|
bool found = false;
|
|
|
|
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
|
|
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
|
|
|
int nJoysticks = SDL_NumJoysticks();
|
|
mNumGamepads = 0;
|
|
|
|
// Cuenta el numero de mandos
|
|
for (int i = 0; i < nJoysticks; i++)
|
|
if (SDL_IsGameController(i))
|
|
mNumGamepads++;
|
|
|
|
printf(" (%i joysticks found, %i are gamepads)\n", nJoysticks, mNumGamepads);
|
|
|
|
if (mNumGamepads > 0)
|
|
{
|
|
found = true;
|
|
|
|
for (int i = 0; i < mNumGamepads; i++)
|
|
{
|
|
// Abre el mando y lo añade a la lista
|
|
SDL_GameController *pad = SDL_GameControllerOpen(i);
|
|
if (SDL_GameControllerGetAttached(pad) == 1)
|
|
mConnectedControllers.push_back(pad);
|
|
else
|
|
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
|
}
|
|
|
|
mGameController = mConnectedControllers[0];
|
|
SDL_GameControllerEventState(SDL_ENABLE);
|
|
}
|
|
|
|
return found;
|
|
} |