working on game controller input
This commit is contained in:
@@ -11,8 +11,8 @@
|
|||||||
Director::Director(std::string path)
|
Director::Director(std::string path)
|
||||||
{
|
{
|
||||||
// Crea los objetos
|
// Crea los objetos
|
||||||
mInput1 = new Input();
|
mInput1 = new Input(USE_KEYBOARD);
|
||||||
mInput2 = new Input();
|
mInput2 = new Input(USE_GAMECONTROLLER);
|
||||||
mOptions = new options_t;
|
mOptions = new options_t;
|
||||||
|
|
||||||
// Inicializa variables
|
// Inicializa variables
|
||||||
@@ -99,23 +99,17 @@ void Director::init()
|
|||||||
mInput1->bindKey(INPUT_BUTTON_4, SDL_SCANCODE_ESCAPE); // PAUSE
|
mInput1->bindKey(INPUT_BUTTON_4, SDL_SCANCODE_ESCAPE); // PAUSE
|
||||||
mInput1->bindKey(INPUT_BUTTON_5, SDL_SCANCODE_ESCAPE); // ESCAPE
|
mInput1->bindKey(INPUT_BUTTON_5, SDL_SCANCODE_ESCAPE); // ESCAPE
|
||||||
|
|
||||||
mInput2->bindKey(INPUT_UP, SDL_SCANCODE_UP);
|
mInput2->bindGameController(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP);
|
||||||
mInput2->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
|
mInput2->bindGameController(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
|
||||||
mInput2->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
|
mInput2->bindGameController(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
|
||||||
mInput2->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT);
|
mInput2->bindGameController(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
|
||||||
mInput2->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN);
|
mInput2->bindGameController(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_A);
|
||||||
mInput2->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
|
mInput2->bindGameController(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_B);
|
||||||
#ifdef __MIPSEL__
|
mInput2->bindGameController(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_X);
|
||||||
mInput2->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_LSHIFT);
|
mInput2->bindGameController(INPUT_BUTTON_2, SDL_CONTROLLER_BUTTON_Y);
|
||||||
mInput2->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_SPACE);
|
mInput2->bindGameController(INPUT_BUTTON_3, SDL_CONTROLLER_BUTTON_B);
|
||||||
mInput2->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_LCTRL);
|
mInput2->bindGameController(INPUT_BUTTON_4, SDL_CONTROLLER_BUTTON_GUIDE); // PAUSE
|
||||||
#else
|
mInput2->bindGameController(INPUT_BUTTON_5, SDL_CONTROLLER_BUTTON_GUIDE); // ESCAPE
|
||||||
mInput2->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_Q);
|
|
||||||
mInput2->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_W);
|
|
||||||
mInput2->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_E);
|
|
||||||
#endif
|
|
||||||
mInput2->bindKey(INPUT_BUTTON_4, SDL_SCANCODE_ESCAPE); // PAUSE
|
|
||||||
mInput2->bindKey(INPUT_BUTTON_5, SDL_SCANCODE_ESCAPE); // ESCAPE
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inicializa JailAudio
|
// Inicializa JailAudio
|
||||||
|
|||||||
108
source/input.cpp
108
source/input.cpp
@@ -1,13 +1,28 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Input::Input()
|
Input::Input(int source)
|
||||||
{
|
{
|
||||||
|
// Inicializa las variables
|
||||||
for (int i = 0; i < 15; i++)
|
for (int i = 0; i < 15; i++)
|
||||||
{
|
{
|
||||||
mInput[i].scancode = 0;
|
mKeyBindings[i].scancode = 0;
|
||||||
mInput[i].active = false;
|
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
|
// Destructor
|
||||||
@@ -18,28 +33,36 @@ Input::~Input()
|
|||||||
// Asigna uno de los posibles inputs a una tecla del teclado
|
// Asigna uno de los posibles inputs a una tecla del teclado
|
||||||
void Input::bindKey(Uint8 input, SDL_Scancode code)
|
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
|
// Comprueba si un input esta activo
|
||||||
bool Input::checkInput(Uint8 input, bool repeat)
|
bool Input::checkInput(Uint8 input, bool repeat)
|
||||||
|
{
|
||||||
|
if (mSource == USE_KEYBOARD)
|
||||||
{
|
{
|
||||||
const Uint8 *mKeystates = SDL_GetKeyboardState(NULL);
|
const Uint8 *mKeystates = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
if (repeat)
|
if (repeat)
|
||||||
{
|
{
|
||||||
if (mKeystates[mInput[input].scancode] != 0)
|
if (mKeystates[mKeyBindings[input].scancode] != 0)
|
||||||
return true;
|
return true;
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!mInput[input].active)
|
if (!mKeyBindings[input].active)
|
||||||
{
|
{
|
||||||
if (mKeystates[mInput[input].scancode] != 0)
|
if (mKeystates[mKeyBindings[input].scancode] != 0)
|
||||||
{
|
{
|
||||||
mInput[input].active = true;
|
mKeyBindings[input].active = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -49,9 +72,9 @@ bool Input::checkInput(Uint8 input, bool repeat)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (mKeystates[mInput[input].scancode] == 0)
|
if (mKeystates[mKeyBindings[input].scancode] == 0)
|
||||||
{
|
{
|
||||||
mInput[input].active = false;
|
mKeyBindings[input].active = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -61,14 +84,52 @@ bool Input::checkInput(Uint8 input, bool repeat)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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
|
// Gestiona las entradas desde el mando de juego
|
||||||
bool Input::checkGameController(Uint8 state)
|
/*bool Input::checkGameController(Uint8 state)
|
||||||
{
|
{
|
||||||
bool success = false;
|
bool success = false;
|
||||||
|
|
||||||
// No hay mando. Siempre devuelve falso salvo NO_INPUT que siempre es cierto
|
// No hay mando. Siempre devuelve falso salvo NO_INPUT que siempre es cierto
|
||||||
/*if (!mGameControllerFound)
|
if (!mGameControllerFound)
|
||||||
{
|
{
|
||||||
if (state == NO_INPUT)
|
if (state == NO_INPUT)
|
||||||
return true;
|
return true;
|
||||||
@@ -117,12 +178,14 @@ bool Input::checkGameController(Uint8 state)
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}*/
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
return success;
|
||||||
|
}*/
|
||||||
|
|
||||||
// Comprueba si hay algun mando conectado
|
// Comprueba si hay un mando conectado
|
||||||
/*if (SDL_NumJoysticks() < 1)
|
bool Input::discoverGameController()
|
||||||
|
{
|
||||||
|
if (SDL_NumJoysticks() < 1)
|
||||||
{
|
{
|
||||||
printf("Warning: No joysticks connected!\n");
|
printf("Warning: No joysticks connected!\n");
|
||||||
mGameControllerFound = false;
|
mGameControllerFound = false;
|
||||||
@@ -130,7 +193,7 @@ bool Input::checkGameController(Uint8 state)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Carga el mando
|
// Carga el mando
|
||||||
mGameController = SDL_JoystickOpen(0);
|
mGameController = SDL_GameControllerOpen(0);
|
||||||
mGameControllerFound = true;
|
mGameControllerFound = true;
|
||||||
|
|
||||||
if (mGameController == NULL)
|
if (mGameController == NULL)
|
||||||
@@ -141,10 +204,10 @@ bool Input::checkGameController(Uint8 state)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("%i joysticks were found.\n", SDL_NumJoysticks());
|
printf("%i joysticks were found.\n", SDL_NumJoysticks());
|
||||||
std::cout << SDL_JoystickNumButtons(mGameController) << " buttons\n";
|
//printf("%i buttons\n", SDL_JoystickNumButtons(mGameController));
|
||||||
|
|
||||||
// Obtiene el dispositivo de control háptico
|
// Obtiene el dispositivo de control háptico
|
||||||
mControllerHaptic = SDL_HapticOpenFromJoystick(mGameController);
|
/*mControllerHaptic = SDL_HapticOpenFromJoystick(mGameController);
|
||||||
if (mControllerHaptic == NULL)
|
if (mControllerHaptic == NULL)
|
||||||
{
|
{
|
||||||
printf("Warning: Controller does not support haptics!\nSDL Error: %s\n", SDL_GetError());
|
printf("Warning: Controller does not support haptics!\nSDL Error: %s\n", SDL_GetError());
|
||||||
@@ -158,6 +221,9 @@ bool Input::checkGameController(Uint8 state)
|
|||||||
{
|
{
|
||||||
printf("Warning: Unable to initialize rumble!\nSDL Error: %s\n", SDL_GetError());
|
printf("Warning: Unable to initialize rumble!\nSDL Error: %s\n", SDL_GetError());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
}*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return mGameControllerFound;
|
||||||
|
}
|
||||||
@@ -23,24 +23,36 @@
|
|||||||
#define REPEAT_TRUE true
|
#define REPEAT_TRUE true
|
||||||
#define REPEAT_FALSE false
|
#define REPEAT_FALSE false
|
||||||
|
|
||||||
|
#define USE_KEYBOARD 0
|
||||||
|
#define USE_GAMECONTROLLER 1
|
||||||
|
|
||||||
// Clase Input
|
// Clase Input
|
||||||
class Input
|
class Input
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
struct input_t
|
struct keyBindings_t
|
||||||
{
|
{
|
||||||
Uint8 scancode; // Scancode asociado
|
Uint8 scancode; // Scancode asociado
|
||||||
bool active; // Indica si está activo
|
bool active; // Indica si está activo
|
||||||
};
|
};
|
||||||
input_t mInput[15]; // Vector con las teclas asociadas a los inputs predefinidos
|
keyBindings_t mKeyBindings[15]; // Vector con las teclas asociadas a los inputs predefinidos
|
||||||
|
|
||||||
//SDL_Joystick *mGameController; // Manejador para el mando 1
|
struct GameControllerBindings_t
|
||||||
//SDL_Haptic *mControllerHaptic; // Manejador para el mando con vibración
|
{
|
||||||
//bool mGameControllerFound; // Variable para saber si hay un mando conectado
|
SDL_GameControllerButton button; // GameControllerButton asociado
|
||||||
|
bool active; // Indica si está activo
|
||||||
|
};
|
||||||
|
GameControllerBindings_t mGameControllerBindings[15]; // Vector con las teclas asociadas a los inputs predefinidos
|
||||||
|
|
||||||
|
SDL_GameController *mGameController; // Manejador para el mando
|
||||||
|
SDL_Haptic *mControllerHaptic; // Manejador para el mando con vibración
|
||||||
|
bool mGameControllerFound; // Variable para saber si hay un mando conectado
|
||||||
|
|
||||||
|
int mSource; // Indica si el objeto usará un mando o un teclado
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Input();
|
Input(int source);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Input();
|
~Input();
|
||||||
@@ -48,11 +60,14 @@ public:
|
|||||||
// Asigna uno de los posibles inputs a una tecla del teclado
|
// Asigna uno de los posibles inputs a una tecla del teclado
|
||||||
void bindKey(Uint8 input, SDL_Scancode code);
|
void bindKey(Uint8 input, SDL_Scancode code);
|
||||||
|
|
||||||
|
// Asigna uno de los posibles inputs a un botón del mando
|
||||||
|
void bindGameController(Uint8 input, SDL_GameControllerButton button);
|
||||||
|
|
||||||
// Comprueba si un input esta activo
|
// Comprueba si un input esta activo
|
||||||
bool checkInput(Uint8 input, bool repeat);
|
bool checkInput(Uint8 input, bool repeat);
|
||||||
|
|
||||||
// Gestiona las entradas desde el mando de juego
|
// Comprueba si hay un mando conectado
|
||||||
bool checkGameController(Uint8 state);
|
bool discoverGameController();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user