guardar partida pq ja estic fent canvis a cegues a vore si trac açò
This commit is contained in:
@@ -4,10 +4,17 @@
|
||||
// Constructor
|
||||
Input::Input(std::string file)
|
||||
{
|
||||
// Inicializa variables
|
||||
verbose = false;
|
||||
enabled = true;
|
||||
|
||||
// Fichero gamecontrollerdb.txt
|
||||
dbPath = file;
|
||||
|
||||
// Inicializa las variables
|
||||
// Busca si hay mandos conectados
|
||||
discoverGameControllers();
|
||||
|
||||
// Inicializa las vectores
|
||||
keyBindings_t kb;
|
||||
kb.scancode = 0;
|
||||
kb.active = false;
|
||||
@@ -16,10 +23,11 @@ Input::Input(std::string file)
|
||||
GameControllerBindings_t gcb;
|
||||
gcb.button = SDL_CONTROLLER_BUTTON_INVALID;
|
||||
gcb.active = false;
|
||||
gameControllerBindings.resize(input_number_of_inputs, gcb);
|
||||
|
||||
verbose = true;
|
||||
enabled = true;
|
||||
gameControllerBindings.resize(numGamepads);
|
||||
for (int i = 0; i < numGamepads; ++i)
|
||||
{
|
||||
gameControllerBindings[i].resize(input_number_of_inputs, gcb);
|
||||
}
|
||||
|
||||
// Listado de los inputs usados para jugar, excluyendo botones para la interfaz
|
||||
gameInputs.clear();
|
||||
@@ -55,9 +63,13 @@ void Input::bindKey(inputs_e input, SDL_Scancode code)
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(inputs_e input, SDL_GameControllerButton button)
|
||||
void Input::bindGameControllerButton(int index, inputs_e input, SDL_GameControllerButton button)
|
||||
{
|
||||
gameControllerBindings[input].button = button;
|
||||
if (index >= (int)connectedControllers.size())
|
||||
{
|
||||
return;
|
||||
}
|
||||
gameControllerBindings[index][input].button = button;
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
@@ -125,7 +137,7 @@ bool Input::checkInput(inputs_e input, bool repeat, int device, int index)
|
||||
{
|
||||
if (repeat)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
|
||||
{
|
||||
successGameController = true;
|
||||
}
|
||||
@@ -136,11 +148,11 @@ bool Input::checkInput(inputs_e input, bool repeat, int device, int index)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!gameControllerBindings[input].active)
|
||||
if (!gameControllerBindings[index][input].active)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
|
||||
{
|
||||
gameControllerBindings[input].active = true;
|
||||
gameControllerBindings[index][input].active = true;
|
||||
successGameController = true;
|
||||
}
|
||||
else
|
||||
@@ -150,9 +162,9 @@ bool Input::checkInput(inputs_e input, bool repeat, int device, int index)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) == 0)
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) == 0)
|
||||
{
|
||||
gameControllerBindings[input].active = false;
|
||||
gameControllerBindings[index][input].active = false;
|
||||
successGameController = false;
|
||||
}
|
||||
else
|
||||
@@ -180,8 +192,9 @@ bool Input::checkAnyInput(int device, int index)
|
||||
|
||||
for (int i = 0; i < (int)keyBindings.size(); ++i)
|
||||
{
|
||||
if (mKeystates[keyBindings[i].scancode] != 0)
|
||||
if (mKeystates[keyBindings[i].scancode] != 0 && !keyBindings[i].active)
|
||||
{
|
||||
keyBindings[i].active = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -193,8 +206,9 @@ bool Input::checkAnyInput(int device, int index)
|
||||
{
|
||||
for (int i = 0; i < (int)gameControllerBindings.size(); ++i)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][i].button) != 0 && !gameControllerBindings[index][i].active)
|
||||
{
|
||||
gameControllerBindings[index][i].active = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -204,21 +218,22 @@ bool Input::checkAnyInput(int device, int index)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Comprueba si hay algún botón pulsado. Devuelve 0 en caso de no encontrar nada o el indice del dispositivo + 1.
|
||||
int Input::checkAnyButtonPressed()
|
||||
// Comprueba si hay algún botón pulsado. Devuelve 0 en caso de no encontrar nada o el indice del dispositivo + 1. Se hace así para poder gastar el valor devuelto como un valor "booleano"
|
||||
int Input::checkAnyButtonPressed(bool repeat)
|
||||
{
|
||||
const Uint8 *keyStates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
// Solo comprueba los botones
|
||||
for (auto bi : buttonInputs)
|
||||
{
|
||||
if (keyStates[keyBindings[bi].scancode] != 0)
|
||||
// Comprueba el teclado
|
||||
if (checkInput(bi, repeat, INPUT_USE_KEYBOARD))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Comprueba los mandos
|
||||
for (int i = 0; i < numGamepads; ++i)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[i], gameControllerBindings[bi].button) != 0)
|
||||
if (checkInput(bi, repeat, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
return i + 1;
|
||||
}
|
||||
@@ -228,8 +243,8 @@ int Input::checkAnyButtonPressed()
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Busca si hay un mando conectado
|
||||
bool Input::discoverGameController()
|
||||
// Busca si hay mandos conectados
|
||||
bool Input::discoverGameControllers()
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
@@ -281,7 +296,7 @@ bool Input::discoverGameController()
|
||||
const std::string separator(" #");
|
||||
std::string name = SDL_GameControllerNameForIndex(i);
|
||||
// name.resize(25);
|
||||
name = name + separator + std::to_string(i);
|
||||
// name = name + separator + std::to_string(i);
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << name << std::endl;
|
||||
@@ -359,7 +374,68 @@ void Input::enable()
|
||||
int Input::getJoyIndex(int id)
|
||||
{
|
||||
for (int i = 0; i < numJoysticks; ++i)
|
||||
{
|
||||
if (SDL_JoystickInstanceID(joysticks[i]) == id)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Muestra por consola los controles asignados
|
||||
void Input::printBindings(int device, int index)
|
||||
{
|
||||
if (device == INPUT_USE_ANY)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (device == INPUT_USE_KEYBOARD)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (device == INPUT_USE_GAMECONTROLLER)
|
||||
{
|
||||
if (index >= numGamepads)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Muestra el nombre del mando
|
||||
std::cout << "\n" << controllerNames[index] << std::endl;
|
||||
|
||||
// Muestra los botones asignados
|
||||
for (auto bi : buttonInputs)
|
||||
{
|
||||
std::cout << to_String(bi) << " : " << gameControllerBindings[index][bi].button << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convierte un inputs_e a std::string
|
||||
std::string Input::to_String(inputs_e input)
|
||||
{
|
||||
if (input == input_fire_left)
|
||||
{
|
||||
return "input_fire_left";
|
||||
}
|
||||
|
||||
if (input == input_fire_center)
|
||||
{
|
||||
return "input_fire_center";
|
||||
}
|
||||
|
||||
if (input == input_fire_right)
|
||||
{
|
||||
return "input_fire_right";
|
||||
}
|
||||
|
||||
if (input == input_start)
|
||||
{
|
||||
return "input_start";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
Reference in New Issue
Block a user