Dual input for P1 done

This commit is contained in:
2021-08-29 22:03:48 +02:00
parent 02b528ea8c
commit 224b176108
9 changed files with 113 additions and 137 deletions

View File

@@ -3,7 +3,7 @@
#include <iostream>
// Constructor
Input::Input(int source)
Input::Input()
{
// Inicializa las variables
for (int i = 0; i < 17; i++)
@@ -15,21 +15,7 @@ Input::Input(int source)
mGameControllerBindings[i].active = false;
}
mSource = source;
if (mSource == INPUT_USE_KEYBOARD)
printf("\nInput requested KEYBOARD\n");
else
printf("\nInput requested GAMECONTROLLER");
if (mSource == INPUT_USE_GAMECONTROLLER)
if (!discoverGameController())
mSource = INPUT_USE_KEYBOARD;
if (mSource == INPUT_USE_KEYBOARD)
printf("Input asigned was KEYBOARD\n");
else
printf("Input asigned was GAMECONTROLLER (%s)\n", SDL_GameControllerNameForIndex(0));
discoverGameController();
}
// Destructor
@@ -50,18 +36,21 @@ void Input::bindGameController(Uint8 input, SDL_GameControllerButton button)
}
// Comprueba si un input esta activo
bool Input::checkInput(Uint8 input, bool repeat)
bool Input::checkInput(Uint8 input, bool repeat, int device)
{
if (mSource == INPUT_USE_KEYBOARD)
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)
return true;
successKeyboard = true;
else
return false;
successKeyboard = false;
}
else
{
@@ -70,11 +59,11 @@ bool Input::checkInput(Uint8 input, bool repeat)
if (mKeystates[mKeyBindings[input].scancode] != 0)
{
mKeyBindings[input].active = true;
return true;
successKeyboard = true;
}
else
{
return false;
successKeyboard = false;
}
}
else
@@ -82,23 +71,24 @@ bool Input::checkInput(Uint8 input, bool repeat)
if (mKeystates[mKeyBindings[input].scancode] == 0)
{
mKeyBindings[input].active = false;
return false;
successKeyboard = false;
}
else
{
return false;
successKeyboard = false;
}
}
}
}
else // Utiliza mando
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
{
if (repeat)
{
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) != 0)
return true;
successGameController = true;
else
return false;
successGameController = false;
}
else
{
@@ -107,11 +97,11 @@ bool Input::checkInput(Uint8 input, bool repeat)
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) != 0)
{
mGameControllerBindings[input].active = true;
return true;
successGameController = true;
}
else
{
return false;
successGameController = false;
}
}
else
@@ -119,15 +109,17 @@ bool Input::checkInput(Uint8 input, bool repeat)
if (SDL_GameControllerGetButton(mGameController, mGameControllerBindings[input].button) == 0)
{
mGameControllerBindings[input].active = false;
return false;
successGameController = false;
}
else
{
return false;
successGameController = false;
}
}
}
}
return (successKeyboard || successGameController);
}
// Comprueba si hay un mando conectado
@@ -167,10 +159,4 @@ bool Input::discoverGameController()
}
return found;
}
// Establece el método de entrada
void Input::setSource(Uint8 value)
{
mSource = value;
}