forked from jaildesigner-jailgames/jaildoctors_dilemma
Reubicados los cpp de librerias comunes
This commit is contained in:
243
source/utils/input.cpp
Normal file
243
source/utils/input.cpp
Normal file
@@ -0,0 +1,243 @@
|
||||
#include "input.h"
|
||||
#include <iostream>
|
||||
|
||||
// Constructor
|
||||
Input::Input(std::string file)
|
||||
{
|
||||
// Fichero gamecontrollerdb.txt
|
||||
dbPath = file;
|
||||
|
||||
// Inicializa las variables
|
||||
for (int i = 0; i < 17; ++i)
|
||||
{
|
||||
keyBindings[i].scancode = 0;
|
||||
keyBindings[i].active = false;
|
||||
|
||||
gameControllerBindings[i].button = SDL_CONTROLLER_BUTTON_INVALID;
|
||||
gameControllerBindings[i].active = false;
|
||||
}
|
||||
|
||||
discoverGameController();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Input::~Input()
|
||||
{
|
||||
for (int i = 0; i < numGamepads; ++i)
|
||||
connectedControllers[i] = nullptr;
|
||||
}
|
||||
|
||||
// Asigna uno de los posibles inputs a una tecla del teclado
|
||||
void Input::bindKey(Uint8 input, SDL_Scancode code)
|
||||
{
|
||||
keyBindings[input].scancode = code;
|
||||
}
|
||||
|
||||
// Asigna uno de los posibles inputs a un botón del mando
|
||||
void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button)
|
||||
{
|
||||
gameControllerBindings[input].button = button;
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool Input::checkInput(Uint8 input, bool repeat, int device, int index)
|
||||
{
|
||||
bool successKeyboard = false;
|
||||
bool successGameController = false;
|
||||
|
||||
if (device == INPUT_USE_ANY)
|
||||
index = 0;
|
||||
|
||||
if ((device == INPUT_USE_KEYBOARD) || (device == INPUT_USE_ANY))
|
||||
{
|
||||
const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
if (repeat)
|
||||
{
|
||||
if (mKeystates[keyBindings[input].scancode] != 0)
|
||||
successKeyboard = true;
|
||||
else
|
||||
successKeyboard = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!keyBindings[input].active)
|
||||
{
|
||||
if (mKeystates[keyBindings[input].scancode] != 0)
|
||||
{
|
||||
keyBindings[input].active = true;
|
||||
successKeyboard = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successKeyboard = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mKeystates[keyBindings[input].scancode] == 0)
|
||||
{
|
||||
keyBindings[input].active = false;
|
||||
successKeyboard = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
successKeyboard = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound())
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
|
||||
{
|
||||
if (repeat)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||
successGameController = true;
|
||||
else
|
||||
successGameController = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!gameControllerBindings[input].active)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||
{
|
||||
gameControllerBindings[input].active = true;
|
||||
successGameController = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) == 0)
|
||||
{
|
||||
gameControllerBindings[input].active = false;
|
||||
successGameController = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (successKeyboard || successGameController);
|
||||
}
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
bool Input::checkAnyInput(int device, int index)
|
||||
{
|
||||
bool successKeyboard = false;
|
||||
bool successGameController = false;
|
||||
|
||||
if (device == INPUT_USE_ANY)
|
||||
index = 0;
|
||||
|
||||
if ((device == INPUT_USE_KEYBOARD) || (device == INPUT_USE_ANY))
|
||||
{
|
||||
const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (int i = 0; i < 17; ++i)
|
||||
{
|
||||
if (mKeystates[keyBindings[i].scancode] != 0)
|
||||
{
|
||||
successKeyboard |= true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound())
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
|
||||
{
|
||||
for (int i = 0; i < 17; ++i)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0)
|
||||
{
|
||||
successGameController |= true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0)
|
||||
printf("Error, could not load %s file: %s\n", dbPath.c_str(), SDL_GetError());
|
||||
|
||||
int nJoysticks = SDL_NumJoysticks();
|
||||
numGamepads = 0;
|
||||
|
||||
// Cuenta el numero de mandos
|
||||
for (int i = 0; i < nJoysticks; ++i)
|
||||
if (SDL_IsGameController(i))
|
||||
numGamepads++;
|
||||
|
||||
printf("** Checking for game controllers ...\n");
|
||||
printf("%i joysticks found, %i are gamepads\n\n", nJoysticks, numGamepads);
|
||||
|
||||
if (numGamepads > 0)
|
||||
{
|
||||
found = true;
|
||||
|
||||
for (int i = 0; i < numGamepads; ++i)
|
||||
{
|
||||
// Abre el mando y lo añade a la lista
|
||||
SDL_GameController *pad = SDL_GameControllerOpen(i);
|
||||
if (SDL_GameControllerGetAttached(pad) == 1)
|
||||
{
|
||||
connectedControllers.push_back(pad);
|
||||
std::string separator(" #");
|
||||
std::string name = SDL_GameControllerNameForIndex(i);
|
||||
name.resize(25);
|
||||
name = name + separator + std::to_string(i);
|
||||
std::cout << name << std::endl;
|
||||
controllerNames.push_back(name);
|
||||
}
|
||||
else
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
// mGameController = connectedControllers[0];
|
||||
SDL_GameControllerEventState(SDL_ENABLE);
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool Input::gameControllerFound()
|
||||
{
|
||||
if (numGamepads > 0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
std::string Input::getControllerName(int index)
|
||||
{
|
||||
if (numGamepads > 0)
|
||||
return controllerNames[index];
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
// Obten el numero de mandos conectados
|
||||
int Input::getNumControllers()
|
||||
{
|
||||
return numGamepads;
|
||||
}
|
||||
Reference in New Issue
Block a user