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 "";
|
||||
}
|
||||
@@ -45,8 +45,8 @@ enum inputs_e
|
||||
input_number_of_inputs
|
||||
};
|
||||
|
||||
#define REPEAT_TRUE true
|
||||
#define REPEAT_FALSE false
|
||||
#define ALLOW_REPEAT true
|
||||
#define DO_NOT_ALLOW_REPEAT false
|
||||
|
||||
#define INPUT_USE_KEYBOARD 0
|
||||
#define INPUT_USE_GAMECONTROLLER 1
|
||||
@@ -75,19 +75,22 @@ private:
|
||||
};
|
||||
|
||||
// Variables
|
||||
std::vector<SDL_GameController *> connectedControllers; // Vector con todos los mandos conectados
|
||||
std::vector<SDL_Joystick *> joysticks; // Vector con todos los joysticks conectados
|
||||
std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<GameControllerBindings_t> gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<std::string> controllerNames; // Vector con los nombres de los mandos
|
||||
std::vector<inputs_e> gameInputs; // Inputs usados para jugar, normalmente direcciones y botones
|
||||
std::vector<inputs_e> buttonInputs; // Inputs asignados al jugador y a botones, excluyendo direcciones
|
||||
int numJoysticks; // Número de joysticks conectados
|
||||
int numGamepads; // Número de mandos conectados
|
||||
std::string dbPath; // Ruta al archivo gamecontrollerdb.txt
|
||||
bool verbose; // Indica si ha de mostrar mensajes
|
||||
i_disable_e disabledUntil; // Tiempo que esta deshabilitado
|
||||
bool enabled; // Indica si está habilitado
|
||||
std::vector<SDL_GameController *> connectedControllers; // Vector con todos los mandos conectados
|
||||
std::vector<SDL_Joystick *> joysticks; // Vector con todos los joysticks conectados
|
||||
std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<std::vector<GameControllerBindings_t>> gameControllerBindings; // Vector con los botones asociadas a los inputs predefinidos para cada mando
|
||||
std::vector<std::string> controllerNames; // Vector con los nombres de los mandos
|
||||
std::vector<inputs_e> gameInputs; // Inputs usados para jugar, normalmente direcciones y botones
|
||||
std::vector<inputs_e> buttonInputs; // Inputs asignados al jugador y a botones, excluyendo direcciones
|
||||
int numJoysticks; // Número de joysticks conectados
|
||||
int numGamepads; // Número de mandos conectados
|
||||
std::string dbPath; // Ruta al archivo gamecontrollerdb.txt
|
||||
bool verbose; // Indica si ha de mostrar mensajes
|
||||
i_disable_e disabledUntil; // Tiempo que esta deshabilitado
|
||||
bool enabled; // Indica si está habilitado
|
||||
|
||||
// Convierte un inputs_e a std::string
|
||||
std::string to_String(inputs_e input);
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
@@ -100,7 +103,7 @@ public:
|
||||
void bindKey(inputs_e input, SDL_Scancode code);
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void bindGameControllerButton(inputs_e input, SDL_GameControllerButton button);
|
||||
void bindGameControllerButton(int index, inputs_e input, SDL_GameControllerButton button);
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool checkInput(inputs_e input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0);
|
||||
@@ -109,10 +112,10 @@ public:
|
||||
bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0);
|
||||
|
||||
// Comprueba si hay algún botón pulsado
|
||||
int checkAnyButtonPressed();
|
||||
int checkAnyButtonPressed(bool repeat = DO_NOT_ALLOW_REPEAT);
|
||||
|
||||
// Busca si hay un mando conectado
|
||||
bool discoverGameController();
|
||||
// Busca si hay mandos conectados
|
||||
bool discoverGameControllers();
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool gameControllerFound();
|
||||
@@ -134,6 +137,9 @@ public:
|
||||
|
||||
// Obtiene el indice del controlador a partir de un event.id
|
||||
int getJoyIndex(int id);
|
||||
|
||||
// Muestra por consola los controles asignados
|
||||
void printBindings(int device = INPUT_USE_KEYBOARD, int index = 0);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -901,7 +901,7 @@ void Menu::setDefaultActionWhenCancel(int item)
|
||||
// Gestiona la entrada de teclado y mando durante el menu
|
||||
void Menu::checkInput()
|
||||
{
|
||||
if (input->checkInput(input_up, REPEAT_FALSE))
|
||||
if (input->checkInput(input_up, DO_NOT_ALLOW_REPEAT))
|
||||
{
|
||||
if (decreaseSelectorIndex())
|
||||
{
|
||||
@@ -912,7 +912,7 @@ void Menu::checkInput()
|
||||
}
|
||||
}
|
||||
|
||||
if (input->checkInput(input_down, REPEAT_FALSE))
|
||||
if (input->checkInput(input_down, DO_NOT_ALLOW_REPEAT))
|
||||
{
|
||||
if (increaseSelectorIndex())
|
||||
{
|
||||
@@ -923,7 +923,7 @@ void Menu::checkInput()
|
||||
}
|
||||
}
|
||||
|
||||
if (input->checkInput(input_accept, REPEAT_FALSE))
|
||||
if (input->checkInput(input_accept, DO_NOT_ALLOW_REPEAT))
|
||||
{
|
||||
itemSelected = selector.index;
|
||||
if (soundAccept)
|
||||
@@ -932,7 +932,7 @@ void Menu::checkInput()
|
||||
}
|
||||
}
|
||||
|
||||
if (input->checkInput(input_cancel, REPEAT_FALSE))
|
||||
if (input->checkInput(input_cancel, DO_NOT_ALLOW_REPEAT))
|
||||
{
|
||||
itemSelected = defaultActionWhenCancel;
|
||||
if (soundCancel)
|
||||
|
||||
@@ -373,22 +373,22 @@ void Screen::update()
|
||||
// Comprueba las entradas
|
||||
void Screen::checkInput()
|
||||
{
|
||||
if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
|
||||
if (input->checkInput(input_window_fullscreen, DO_NOT_ALLOW_REPEAT))
|
||||
{
|
||||
switchVideoMode();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_window_dec_size, DO_NOT_ALLOW_REPEAT))
|
||||
{
|
||||
decWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_window_inc_size, DO_NOT_ALLOW_REPEAT))
|
||||
{
|
||||
incWindowSize();
|
||||
}
|
||||
|
||||
else if (input->checkInput(input_video_shaders, REPEAT_FALSE))
|
||||
else if (input->checkInput(input_video_shaders, DO_NOT_ALLOW_REPEAT))
|
||||
{
|
||||
switchShaders();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user