Eliminat checkModInput
This commit is contained in:
@@ -41,11 +41,19 @@
|
||||
#include "text.h" // Para Text
|
||||
#include "title.h" // Para Title
|
||||
#include "utils.h" // Para Overrides, overrides
|
||||
#include <chrono>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <pwd.h> // para getpwuid, passwd
|
||||
#endif
|
||||
|
||||
// Inicia la semilla aleatoria
|
||||
void initRand()
|
||||
{
|
||||
unsigned int seed = static_cast<unsigned int>(std::chrono::system_clock::now().time_since_epoch().count());
|
||||
std::srand(seed);
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Director::Director(int argc, const char *argv[])
|
||||
{
|
||||
@@ -67,6 +75,8 @@ Director::Director(int argc, const char *argv[])
|
||||
|
||||
std::cout << "Game start" << std::endl;
|
||||
|
||||
initRand();
|
||||
|
||||
// Comprueba los parametros del programa
|
||||
checkProgramArguments(argc, argv);
|
||||
|
||||
@@ -89,7 +99,7 @@ Director::Director(int argc, const char *argv[])
|
||||
#else
|
||||
const std::string paramFilePath = overrides.param_file == "--320x240" ? Asset::get()->get("param_320x240.txt") : Asset::get()->get("param_320x256.txt");
|
||||
#endif
|
||||
loadParams(paramFilePath);
|
||||
loadParamsFromFile(paramFilePath);
|
||||
|
||||
// Carga el fichero de puntuaciones
|
||||
{
|
||||
@@ -191,14 +201,6 @@ void Director::bindInputs()
|
||||
|
||||
// Mando - Control del programa
|
||||
Input::get()->bindGameControllerButton(i, InputType::SERVICE, SDL_CONTROLLER_BUTTON_BACK);
|
||||
Input::get()->bindGameControllerButton(i, InputType::EXIT, InputType::START);
|
||||
Input::get()->bindGameControllerButton(i, InputType::PAUSE, InputType::FIRE_RIGHT);
|
||||
Input::get()->bindGameControllerButton(i, InputType::VIDEO_SHADERS, InputType::FIRE_LEFT);
|
||||
Input::get()->bindGameControllerButton(i, InputType::MUTE, InputType::LEFT);
|
||||
Input::get()->bindGameControllerButton(i, InputType::SHOWINFO, InputType::RIGHT);
|
||||
Input::get()->bindGameControllerButton(i, InputType::RESET, InputType::FIRE_CENTER);
|
||||
Input::get()->bindGameControllerButton(i, InputType::CONFIG, InputType::DOWN);
|
||||
Input::get()->bindGameControllerButton(i, InputType::SWAP_CONTROLLERS, InputType::UP);
|
||||
}
|
||||
|
||||
// Mapea las asignaciones a los botones desde el archivo de configuración, si se da el caso
|
||||
@@ -229,10 +231,10 @@ void Director::bindInputs()
|
||||
// Guarda las asignaciones de botones en las opciones
|
||||
for (int i = 0; i < num_gamepads; ++i)
|
||||
{
|
||||
options.controller[i].name = Input::get()->getControllerName(i);
|
||||
for (int j = 0; j < (int)options.controller[i].inputs.size(); ++j)
|
||||
options.controller.at(i).name = Input::get()->getControllerName(i);
|
||||
for (size_t j = 0; j < options.controller.at(i).inputs.size(); ++j)
|
||||
{
|
||||
options.controller[i].buttons[j] = Input::get()->getControllerBinding(i, options.controller[i].inputs[j]);
|
||||
options.controller.at(i).buttons.at(j) = Input::get()->getControllerBinding(i, options.controller.at(i).inputs.at(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -267,34 +269,35 @@ bool Director::initSDL()
|
||||
}
|
||||
else
|
||||
{
|
||||
// Inicia el generador de numeros aleatorios
|
||||
std::srand(static_cast<unsigned int>(SDL_GetTicks()));
|
||||
|
||||
/*
|
||||
// Muestra información de la pantalla
|
||||
/*std::cout << "\nDisplay modes list:" << std::endl;
|
||||
std::cout << "\nDisplay modes list:" << std::endl;
|
||||
for (int i = 0; i < SDL_GetNumDisplayModes(0); ++i)
|
||||
{
|
||||
SDL_DisplayMode DM;
|
||||
SDL_GetDisplayMode(0,i,&DM);
|
||||
std::cout << " - " + std::to_string(DM.w) + "x" + std::to_string(DM.h) + " @ " + std::to_string(DM.refresh_rate) + "Hz" << std::endl;
|
||||
}*/
|
||||
std::cout << " - " << DM.w << "x" << DM.h << " @ " << DM.refresh_rate << "Hz" << std::endl;
|
||||
}
|
||||
*/
|
||||
|
||||
SDL_DisplayMode DM;
|
||||
SDL_GetCurrentDisplayMode(0, &DM);
|
||||
std::cout << "\nCurrent display mode: " + std::to_string(DM.w) + "x" + std::to_string(DM.h) + " @ " + std::to_string(DM.refresh_rate) + "Hz" << std::endl;
|
||||
std::cout << "Window resolution : " + std::to_string(param.game.width) + "x" + std::to_string(param.game.height) + " x" + std::to_string(options.video.window.size) << std::endl;
|
||||
std::cout << "\nCurrent display mode: " << DM.w << "x" << DM.h << " @ " << DM.refresh_rate << "Hz" << std::endl;
|
||||
std::cout << "Window resolution : " << param.game.width << "x" << param.game.height << " x" << options.video.window.size << std::endl;
|
||||
|
||||
// Establece el filtro de la textura
|
||||
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(static_cast<int>(options.video.filter)).c_str()))
|
||||
{
|
||||
std::cout << "Warning: texture filtering not enabled!\n";
|
||||
}
|
||||
|
||||
#ifndef NO_SHADERS
|
||||
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"))
|
||||
{
|
||||
std::cout << "Warning: opengl not enabled!\n";
|
||||
}
|
||||
#endif // NO_SHADERS
|
||||
#endif
|
||||
|
||||
// Crea la ventana
|
||||
window_ = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, param.game.width * options.video.window.size, param.game.height * options.video.window.size, SDL_WINDOW_HIDDEN);
|
||||
if (!window_)
|
||||
@@ -310,10 +313,12 @@ bool Director::initSDL()
|
||||
{
|
||||
flags = SDL_RENDERER_PRESENTVSYNC;
|
||||
}
|
||||
|
||||
#ifndef NO_SHADERS
|
||||
// La aceleración se activa según el define
|
||||
flags = flags | SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
|
||||
#endif
|
||||
|
||||
renderer_ = SDL_CreateRenderer(window_, -1, flags);
|
||||
|
||||
if (!renderer_)
|
||||
@@ -323,14 +328,9 @@ bool Director::initSDL()
|
||||
}
|
||||
else
|
||||
{
|
||||
// Inicializa el color de renderizado
|
||||
SDL_SetRenderDrawColor(renderer_, 0x00, 0x00, 0x00, 0xFF);
|
||||
|
||||
// Establece el tamaño del buffer de renderizado
|
||||
SDL_RenderSetLogicalSize(renderer_, param.game.width, param.game.height);
|
||||
SDL_RenderSetIntegerScale(renderer_, static_cast<SDL_bool>(options.video.integer_scale));
|
||||
|
||||
// Establece el modo de mezcla
|
||||
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
|
||||
}
|
||||
}
|
||||
@@ -506,16 +506,10 @@ void Director::setFileList()
|
||||
throw std::runtime_error("Falta algun fichero");
|
||||
}
|
||||
|
||||
// Carga los parametros para configurar el juego
|
||||
void Director::loadParams(const std::string &file_path)
|
||||
{
|
||||
loadParamsFromFile(file_path);
|
||||
}
|
||||
|
||||
// Comprueba los parametros del programa
|
||||
void Director::checkProgramArguments(int argc, const char *argv[])
|
||||
{
|
||||
const std::vector<std::string> argument_list = {"--h", "--320x240", "--clear_score"};
|
||||
const std::vector<std::string> argument_list = {"--320x240", "--clear_score"};
|
||||
|
||||
// Establece la ruta del programa
|
||||
executable_path_ = argv[0];
|
||||
@@ -523,23 +517,13 @@ void Director::checkProgramArguments(int argc, const char *argv[])
|
||||
// Comprueba el resto de parámetros
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
if (strcmp(argv[i], "--h") == 0)
|
||||
{
|
||||
for (const auto &argument : argument_list)
|
||||
{
|
||||
std::cout << argument << std::endl;
|
||||
}
|
||||
// std::exit(EXIT_SUCCESS);
|
||||
section::name = section::Name::QUIT;
|
||||
section::options = section::Options::QUIT_FROM_EVENT;
|
||||
}
|
||||
std::string arg = argv[i];
|
||||
|
||||
if (strcmp(argv[i], "--320x240") == 0)
|
||||
if (arg == "--320x240")
|
||||
{
|
||||
overrides.param_file = argv[i];
|
||||
overrides.param_file = arg;
|
||||
}
|
||||
|
||||
if (strcmp(argv[i], "--clear_score") == 0)
|
||||
else if (arg == "--clear_score")
|
||||
{
|
||||
overrides.clear_hi_score_table = true;
|
||||
}
|
||||
@@ -673,54 +657,57 @@ int Director::run()
|
||||
case section::Name::INIT:
|
||||
section::name = section::Name::LOGO;
|
||||
break;
|
||||
|
||||
case section::Name::LOGO:
|
||||
runLogo();
|
||||
break;
|
||||
|
||||
case section::Name::INTRO:
|
||||
runIntro();
|
||||
break;
|
||||
|
||||
case section::Name::TITLE:
|
||||
runTitle();
|
||||
break;
|
||||
|
||||
case section::Name::GAME:
|
||||
runGame();
|
||||
break;
|
||||
|
||||
case section::Name::HI_SCORE_TABLE:
|
||||
runHiScoreTable();
|
||||
break;
|
||||
|
||||
case section::Name::GAME_DEMO:
|
||||
runDemoGame();
|
||||
break;
|
||||
|
||||
case section::Name::INSTRUCTIONS:
|
||||
runInstructions();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ARCADE
|
||||
// Comprueba si ha de apagar el sistema
|
||||
if (section::options == section::Options::QUIT_WITH_CONTROLLER)
|
||||
shutdownSystem();
|
||||
#endif
|
||||
std::string return_code;
|
||||
switch (section::options)
|
||||
{
|
||||
case section::Options::QUIT_WITH_KEYBOARD:
|
||||
return_code = "with keyboard";
|
||||
break;
|
||||
case section::Options::QUIT_WITH_CONTROLLER:
|
||||
return_code = "with controller";
|
||||
break;
|
||||
default:
|
||||
return_code = "from event";
|
||||
break;
|
||||
}
|
||||
|
||||
const auto return_code = (section::options == section::Options::QUIT_WITH_KEYBOARD) ? "with keyboard" : (section::options == section::Options::QUIT_WITH_CONTROLLER) ? "with controller"
|
||||
: "from event";
|
||||
std::cout << "\nGame end " << return_code << std::endl;
|
||||
|
||||
#ifndef VERBOSE
|
||||
// Habilita de nuevo los std::cout
|
||||
std::cout.rdbuf(orig_buf);
|
||||
#endif
|
||||
#ifdef ARCADE
|
||||
// Comprueba si ha de apagar el sistema
|
||||
if (section::options == section::Options::QUIT_WITH_CONTROLLER)
|
||||
shutdownSystem();
|
||||
#endif
|
||||
|
||||
return (section::options == section::Options::QUIT_WITH_CONTROLLER) ? 1 : 0;
|
||||
}
|
||||
@@ -733,15 +720,12 @@ std::string Director::getLangFile(lang::Code code)
|
||||
case lang::Code::ba_BA:
|
||||
return Asset::get()->get("ba_BA.txt");
|
||||
break;
|
||||
|
||||
case lang::Code::es_ES:
|
||||
return Asset::get()->get("es_ES.txt");
|
||||
break;
|
||||
|
||||
case lang::Code::en_UK:
|
||||
return Asset::get()->get("en_UK.txt");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -34,9 +34,6 @@ private:
|
||||
// Asigna los botones y teclas al objeto Input
|
||||
void bindInputs();
|
||||
|
||||
// Carga los parametros para configurar el juego
|
||||
void loadParams(const std::string &file_path);
|
||||
|
||||
// Crea el indice de ficheros
|
||||
void setFileList();
|
||||
|
||||
|
||||
@@ -1625,7 +1625,8 @@ void Game::checkPauseInput()
|
||||
{
|
||||
// Comprueba los mandos
|
||||
for (int i = 0; i < input_->getNumControllers(); ++i)
|
||||
if (input_->checkModInput(InputType::SERVICE, InputType::PAUSE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
if (input_->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
|
||||
input_->checkInput(InputType::PAUSE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
pause(!paused_); // Alterna entre pausado y no pausado.
|
||||
return;
|
||||
|
||||
@@ -81,19 +81,19 @@ namespace globalInputs
|
||||
}
|
||||
|
||||
// Comprueba si se va a resetear el juego
|
||||
else if (Input::get()->checkInput(InputType::RESET, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
|
||||
if (Input::get()->checkInput(InputType::RESET, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
|
||||
{
|
||||
reset();
|
||||
return;
|
||||
}
|
||||
|
||||
else if (Input::get()->checkInput(InputType::MUTE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
|
||||
if (Input::get()->checkInput(InputType::MUTE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
|
||||
{
|
||||
switchAudio();
|
||||
return;
|
||||
}
|
||||
|
||||
else if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
|
||||
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
|
||||
{
|
||||
service_pressed_counter[0]++;
|
||||
|
||||
@@ -104,29 +104,30 @@ namespace globalInputs
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
service_pressed_counter[0] = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
|
||||
{
|
||||
// Comprueba si se sale con el mando
|
||||
if (Input::get()->checkModInput(InputType::SERVICE, InputType::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
|
||||
Input::get()->checkInput(InputType::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
quit(section::Options::QUIT_WITH_CONTROLLER);
|
||||
return;
|
||||
}
|
||||
|
||||
// Comprueba si se va a resetear el juego
|
||||
else if (Input::get()->checkModInput(InputType::SERVICE, InputType::RESET, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
|
||||
Input::get()->checkInput(InputType::RESET, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
reset();
|
||||
return;
|
||||
}
|
||||
|
||||
// Comprueba si se va a activar o desactivar el audio
|
||||
else if (Input::get()->checkModInput(InputType::SERVICE, InputType::MUTE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
|
||||
Input::get()->checkInput(InputType::MUTE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
switchAudio();
|
||||
return;
|
||||
@@ -143,10 +144,8 @@ namespace globalInputs
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
service_pressed_counter[i + 1] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
294
source/input.cpp
294
source/input.cpp
@@ -4,6 +4,8 @@
|
||||
#include <SDL2/SDL_events.h> // para SDL_ENABLE
|
||||
#include <SDL2/SDL_keyboard.h> // para SDL_GetKeyboardState
|
||||
#include <iostream> // para basic_ostream, operator<<, cout, basi...
|
||||
#include <unordered_map>
|
||||
#include <algorithm>
|
||||
|
||||
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
|
||||
Input *Input::input_ = nullptr;
|
||||
@@ -28,49 +30,23 @@ Input *Input::get()
|
||||
|
||||
// Constructor
|
||||
Input::Input(const std::string &game_controller_db_path)
|
||||
: game_controller_db_path_(game_controller_db_path),
|
||||
enabled_(true)
|
||||
: game_controller_db_path_(game_controller_db_path)
|
||||
{
|
||||
// Busca si hay mandos conectados
|
||||
discoverGameControllers();
|
||||
|
||||
// Inicializa las vectores
|
||||
KeyBindings kb;
|
||||
kb.scancode = 0;
|
||||
kb.active = false;
|
||||
key_bindings_.resize(static_cast<int>(InputType::NUMBER_OF_INPUTS), kb);
|
||||
|
||||
ControllerBindings gcb;
|
||||
gcb.button = SDL_CONTROLLER_BUTTON_INVALID;
|
||||
gcb.active = false;
|
||||
controller_bindings_.resize(num_gamepads_);
|
||||
for (int i = 0; i < num_gamepads_; ++i)
|
||||
{
|
||||
controller_bindings_[i].resize(static_cast<int>(InputType::NUMBER_OF_INPUTS), gcb);
|
||||
}
|
||||
|
||||
// Listado de los inputs usados para jugar, excluyendo botones para la interfaz
|
||||
game_inputs_.clear();
|
||||
game_inputs_.push_back(InputType::FIRE_LEFT);
|
||||
game_inputs_.push_back(InputType::FIRE_CENTER);
|
||||
game_inputs_.push_back(InputType::FIRE_RIGHT);
|
||||
game_inputs_.push_back(InputType::UP);
|
||||
game_inputs_.push_back(InputType::DOWN);
|
||||
game_inputs_.push_back(InputType::LEFT);
|
||||
game_inputs_.push_back(InputType::RIGHT);
|
||||
// Inicializa los vectores
|
||||
key_bindings_.resize(static_cast<int>(InputType::NUMBER_OF_INPUTS), KeyBindings());
|
||||
controller_bindings_.resize(num_gamepads_, std::vector<ControllerBindings>(static_cast<int>(InputType::NUMBER_OF_INPUTS), ControllerBindings()));
|
||||
|
||||
// Listado de los inputs para jugar que utilizan botones, ni palancas ni crucetas
|
||||
button_inputs_.clear();
|
||||
button_inputs_.push_back(InputType::FIRE_LEFT);
|
||||
button_inputs_.push_back(InputType::FIRE_CENTER);
|
||||
button_inputs_.push_back(InputType::FIRE_RIGHT);
|
||||
button_inputs_.push_back(InputType::START);
|
||||
button_inputs_ = {InputType::FIRE_LEFT, InputType::FIRE_CENTER, InputType::FIRE_RIGHT, InputType::START};
|
||||
}
|
||||
|
||||
// Asigna inputs a teclas
|
||||
void Input::bindKey(InputType input, SDL_Scancode code)
|
||||
{
|
||||
key_bindings_[static_cast<int>(input)].scancode = code;
|
||||
key_bindings_.at(static_cast<int>(input)).scancode = code;
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
@@ -78,7 +54,7 @@ void Input::bindGameControllerButton(int controller_index, InputType input, SDL_
|
||||
{
|
||||
if (controller_index < num_gamepads_)
|
||||
{
|
||||
controller_bindings_[controller_index][static_cast<int>(input)].button = button;
|
||||
controller_bindings_.at(controller_index).at(static_cast<int>(input)).button = button;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,129 +63,13 @@ void Input::bindGameControllerButton(int controller_index, InputType input_targe
|
||||
{
|
||||
if (controller_index < num_gamepads_)
|
||||
{
|
||||
controller_bindings_[controller_index][static_cast<int>(input_target)].button = controller_bindings_[controller_index][static_cast<int>(input_source)].button;
|
||||
controller_bindings_.at(controller_index).at(static_cast<int>(input_target)).button = controller_bindings_.at(controller_index).at(static_cast<int>(input_source)).button;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool Input::checkInput(InputType input, bool repeat, int device, int controller_index)
|
||||
{
|
||||
if (!enabled_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool success_keyboard = false;
|
||||
bool success_controller = false;
|
||||
const int input_index = static_cast<int>(input);
|
||||
|
||||
if (device == INPUT_USE_ANY)
|
||||
{
|
||||
controller_index = 0;
|
||||
}
|
||||
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
|
||||
{
|
||||
const Uint8 *keyStates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
if (repeat)
|
||||
{
|
||||
if (keyStates[key_bindings_[input_index].scancode] != 0)
|
||||
{
|
||||
success_keyboard = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!key_bindings_[input_index].active)
|
||||
{
|
||||
if (keyStates[key_bindings_[input_index].scancode] != 0)
|
||||
{
|
||||
key_bindings_[input_index].active = true;
|
||||
success_keyboard = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (keyStates[key_bindings_[input_index].scancode] == 0)
|
||||
{
|
||||
key_bindings_[input_index].active = false;
|
||||
success_keyboard = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound() && controller_index < num_gamepads_)
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
|
||||
{
|
||||
success_controller = checkAxisInput(input, controller_index);
|
||||
if (!success_controller)
|
||||
{
|
||||
if (repeat)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) != 0)
|
||||
{
|
||||
success_controller = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!controller_bindings_[controller_index][input_index].active)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) != 0)
|
||||
{
|
||||
controller_bindings_[controller_index][input_index].active = true;
|
||||
success_controller = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) == 0)
|
||||
{
|
||||
controller_bindings_[controller_index][input_index].active = false;
|
||||
success_controller = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (success_keyboard || success_controller);
|
||||
}
|
||||
|
||||
// Comprueba si un input con modificador esta activo
|
||||
bool Input::checkModInput(InputType input_mod, InputType input, bool repeat, int device, int controller_index)
|
||||
{
|
||||
if (!enabled_ || controller_index >= num_gamepads_ || !checkInput(input_mod, INPUT_ALLOW_REPEAT, device, controller_index))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool success_keyboard = false;
|
||||
bool success_controller = false;
|
||||
const int input_index = static_cast<int>(input);
|
||||
@@ -395,11 +255,9 @@ bool Input::discoverGameControllers()
|
||||
}
|
||||
|
||||
if (SDL_GameControllerAddMappingsFromFile(game_controller_db_path_.c_str()) < 0)
|
||||
{
|
||||
{
|
||||
std::cout << "Error, could not load " << game_controller_db_path_.c_str() << " file: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
num_joysticks_ = SDL_NumJoysticks();
|
||||
num_gamepads_ = 0;
|
||||
@@ -408,7 +266,7 @@ bool Input::discoverGameControllers()
|
||||
joysticks_.clear();
|
||||
for (int i = 0; i < num_joysticks_; ++i)
|
||||
{
|
||||
SDL_Joystick *joy = SDL_JoystickOpen(i);
|
||||
auto joy = SDL_JoystickOpen(i);
|
||||
joysticks_.push_back(joy);
|
||||
if (SDL_IsGameController(i))
|
||||
{
|
||||
@@ -417,7 +275,6 @@ bool Input::discoverGameControllers()
|
||||
}
|
||||
|
||||
std::cout << "\n** LOOKING FOR GAME CONTROLLERS" << std::endl;
|
||||
// std::cout << " " << num_joysticks_ << " joysticks found" << std::endl;
|
||||
std::cout << "Gamepads found: " << num_gamepads_ << std::endl;
|
||||
|
||||
if (num_gamepads_ > 0)
|
||||
@@ -432,44 +289,30 @@ bool Input::discoverGameControllers()
|
||||
{
|
||||
connected_controllers_.push_back(pad);
|
||||
const std::string name = SDL_GameControllerNameForIndex(i);
|
||||
{
|
||||
std::cout << "#" << i << ": " << name << std::endl;
|
||||
}
|
||||
controller_names_.push_back(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_GameControllerEventState(SDL_ENABLE);
|
||||
}
|
||||
|
||||
std::cout << "\n** FINISHED LOOKING FOR GAME CONTROLLERS" << std::endl;
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool Input::gameControllerFound()
|
||||
{
|
||||
return num_gamepads_ > 0 ? true : false;
|
||||
}
|
||||
bool Input::gameControllerFound() { return num_gamepads_ > 0 ? true : false; }
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
std::string Input::getControllerName(int controller_index) const
|
||||
{
|
||||
return num_gamepads_ > 0 ? controller_names_[controller_index] : "";
|
||||
}
|
||||
std::string Input::getControllerName(int controller_index) const { return num_gamepads_ > 0 ? controller_names_.at(controller_index) : std::string(); }
|
||||
|
||||
// Obten el número de mandos conectados
|
||||
int Input::getNumControllers() const
|
||||
{
|
||||
return num_gamepads_;
|
||||
}
|
||||
int Input::getNumControllers() const { return num_gamepads_; }
|
||||
|
||||
// Obtiene el indice del controlador a partir de un event.id
|
||||
int Input::getJoyIndex(int id) const
|
||||
@@ -500,13 +343,12 @@ void Input::printBindings(int device, int controller_index) const
|
||||
}
|
||||
|
||||
// Muestra el nombre del mando
|
||||
std::cout << "\n"
|
||||
<< controller_names_[controller_index] << std::endl;
|
||||
std::cout << "\n" + controller_names_.at(controller_index) << std::endl;
|
||||
|
||||
// Muestra los botones asignados
|
||||
for (auto bi : button_inputs_)
|
||||
{
|
||||
std::cout << to_string(bi) << " : " << controller_bindings_[controller_index][static_cast<int>(bi)].button << std::endl;
|
||||
std::cout << to_string(bi) << " : " << controller_bindings_.at(controller_index).at(static_cast<int>(bi)).button << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -520,116 +362,58 @@ SDL_GameControllerButton Input::getControllerBinding(int controller_index, Input
|
||||
// Obtiene el indice a partir del nombre del mando
|
||||
int Input::getIndexByName(const std::string &name) const
|
||||
{
|
||||
for (int i = 0; i < num_gamepads_; ++i)
|
||||
{
|
||||
if (controller_names_[i] == name)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
auto it = std::find(controller_names_.begin(), controller_names_.end(), name);
|
||||
return it != controller_names_.end() ? std::distance(controller_names_.begin(), it) : -1;
|
||||
}
|
||||
|
||||
// Convierte un InputType a std::string
|
||||
std::string Input::to_string(InputType input) const
|
||||
{
|
||||
if (input == InputType::FIRE_LEFT)
|
||||
switch (input)
|
||||
{
|
||||
case InputType::FIRE_LEFT:
|
||||
return "input_fire_left";
|
||||
}
|
||||
|
||||
if (input == InputType::FIRE_CENTER)
|
||||
{
|
||||
case InputType::FIRE_CENTER:
|
||||
return "input_fire_center";
|
||||
}
|
||||
|
||||
if (input == InputType::FIRE_RIGHT)
|
||||
{
|
||||
case InputType::FIRE_RIGHT:
|
||||
return "input_fire_right";
|
||||
}
|
||||
|
||||
if (input == InputType::START)
|
||||
{
|
||||
case InputType::START:
|
||||
return "input_start";
|
||||
}
|
||||
|
||||
if (input == InputType::SERVICE)
|
||||
{
|
||||
case InputType::SERVICE:
|
||||
return "input_service";
|
||||
}
|
||||
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// Convierte un std::string a InputType
|
||||
InputType Input::to_inputs_e(const std::string &name) const
|
||||
{
|
||||
if (name == "input_fire_left")
|
||||
{
|
||||
return InputType::FIRE_LEFT;
|
||||
}
|
||||
static const std::unordered_map<std::string, InputType> inputMap = {
|
||||
{"input_fire_left", InputType::FIRE_LEFT},
|
||||
{"input_fire_center", InputType::FIRE_CENTER},
|
||||
{"input_fire_right", InputType::FIRE_RIGHT},
|
||||
{"input_start", InputType::START},
|
||||
{"input_service", InputType::SERVICE}};
|
||||
|
||||
if (name == "input_fire_center")
|
||||
{
|
||||
return InputType::FIRE_CENTER;
|
||||
}
|
||||
|
||||
if (name == "input_fire_right")
|
||||
{
|
||||
return InputType::FIRE_RIGHT;
|
||||
}
|
||||
|
||||
if (name == "input_start")
|
||||
{
|
||||
return InputType::START;
|
||||
}
|
||||
|
||||
if (name == "input_service")
|
||||
{
|
||||
return InputType::SERVICE;
|
||||
}
|
||||
|
||||
return InputType::NONE;
|
||||
auto it = inputMap.find(name);
|
||||
return it != inputMap.end() ? it->second : InputType::NONE;
|
||||
}
|
||||
|
||||
// Comprueba el eje del mando
|
||||
bool Input::checkAxisInput(InputType input, int controller_index) const
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
switch (input)
|
||||
{
|
||||
case InputType::LEFT:
|
||||
if (SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) < -30000)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
break;
|
||||
|
||||
return SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) < -30000;
|
||||
case InputType::RIGHT:
|
||||
if (SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) > 30000)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
break;
|
||||
|
||||
return SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) > 30000;
|
||||
case InputType::UP:
|
||||
if (SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) < -30000)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
break;
|
||||
|
||||
return SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) < -30000;
|
||||
case InputType::DOWN:
|
||||
if (SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) > 30000)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
break;
|
||||
|
||||
return SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) > 30000;
|
||||
default:
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
@@ -66,12 +66,20 @@ private:
|
||||
{
|
||||
Uint8 scancode; // Scancode asociado
|
||||
bool active; // Indica si está activo
|
||||
|
||||
// Constructor
|
||||
explicit KeyBindings(Uint8 sc = 0, bool act = false)
|
||||
: scancode(sc), active(act) {}
|
||||
};
|
||||
|
||||
struct ControllerBindings
|
||||
{
|
||||
SDL_GameControllerButton button; // GameControllerButton asociado
|
||||
bool active; // Indica si está activo
|
||||
|
||||
// Constructor
|
||||
explicit ControllerBindings(SDL_GameControllerButton btn = SDL_CONTROLLER_BUTTON_INVALID, bool act = false)
|
||||
: button(btn), active(act) {}
|
||||
};
|
||||
|
||||
// Variables
|
||||
@@ -80,12 +88,10 @@ private:
|
||||
std::vector<KeyBindings> key_bindings_; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<std::vector<ControllerBindings>> controller_bindings_; // Vector con los botones asociadas a los inputs predefinidos para cada mando
|
||||
std::vector<std::string> controller_names_; // Vector con los nombres de los mandos
|
||||
std::vector<InputType> game_inputs_; // Inputs usados para jugar, normalmente direcciones y botones
|
||||
std::vector<InputType> button_inputs_; // Inputs asignados al jugador y a botones, excluyendo direcciones
|
||||
int num_joysticks_; // Número de joysticks conectados
|
||||
int num_gamepads_; // Número de mandos conectados
|
||||
int num_joysticks_ = 0; // Número de joysticks conectados
|
||||
int num_gamepads_ = 0; // Número de mandos conectados
|
||||
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt
|
||||
bool enabled_; // Indica si está habilitado
|
||||
|
||||
// Comprueba el eje del mando
|
||||
bool checkAxisInput(InputType input, int controller_index = 0) const;
|
||||
@@ -116,9 +122,6 @@ public:
|
||||
// Comprueba si un input esta activo
|
||||
bool checkInput(InputType input, bool repeat = true, int device = INPUT_USE_ANY, int controller_index = 0);
|
||||
|
||||
// Comprueba si un input con modificador esta activo
|
||||
bool checkModInput(InputType input_mod, InputType input, bool repeat = true, int device = INPUT_USE_ANY, int controller_index = 0);
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
bool checkAnyInput(int device = INPUT_USE_ANY, int controller_index = 0);
|
||||
|
||||
|
||||
@@ -311,14 +311,16 @@ void Screen::checkInput()
|
||||
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
|
||||
{
|
||||
// Comprueba los mandos para activar o desactivar los shaders
|
||||
if (Input::get()->checkModInput(InputType::SERVICE, InputType::VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
|
||||
Input::get()->checkInput(InputType::VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
switchShaders();
|
||||
return;
|
||||
}
|
||||
|
||||
// Comprueba los mandos para mostrar la información de debug
|
||||
if (Input::get()->checkModInput(InputType::SERVICE, InputType::SHOWINFO, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
|
||||
Input::get()->checkInput(InputType::SHOWINFO, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
show_info_ = !show_info_;
|
||||
return;
|
||||
|
||||
@@ -249,14 +249,16 @@ void Title::checkInput()
|
||||
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
|
||||
{
|
||||
// Comprueba si se va a intercambiar la asignación de mandos a jugadores
|
||||
if (Input::get()->checkModInput(InputType::SERVICE, InputType::SWAP_CONTROLLERS, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
|
||||
Input::get()->checkInput(InputType::SWAP_CONTROLLERS, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
swapControllers();
|
||||
return;
|
||||
}
|
||||
|
||||
// Comprueba si algun mando quiere ser configurado
|
||||
if (Input::get()->checkModInput(InputType::SERVICE, InputType::CONFIG, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
|
||||
Input::get()->checkInput(InputType::CONFIG, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
define_buttons_->enable(i);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user