diff --git a/source/common/input.cpp b/source/common/input.cpp index 6ecbf94..0e231a8 100644 --- a/source/common/input.cpp +++ b/source/common/input.cpp @@ -65,17 +65,135 @@ void Input::bindKey(inputs_e input, SDL_Scancode code) // Asigna inputs a botones del mando void Input::bindGameControllerButton(int index, inputs_e input, SDL_GameControllerButton button) { - if (index >= (int)connectedControllers.size()) + if (index < numGamepads) { - return; + gameControllerBindings[index][input].button = button; + } +} + +// Asigna inputs a botones del mando +void Input::bindGameControllerButton(int index, inputs_e inputTarget, inputs_e inputSource) +{ + if (index < numGamepads) + { + gameControllerBindings[index][inputTarget].button = gameControllerBindings[index][inputSource].button; } - gameControllerBindings[index][input].button = button; } // Comprueba si un input esta activo bool Input::checkInput(inputs_e input, bool repeat, int device, int index) { - if (!enabled) + if (!enabled || index >= numGamepads) + { + return false; + } + + bool successKeyboard = false; + bool successGameController = false; + + if (device == INPUT_USE_ANY) + { + index = 0; + } + + if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) + { + const Uint8 *keyStates = SDL_GetKeyboardState(nullptr); + + if (repeat) + { + if (keyStates[keyBindings[input].scancode] != 0) + { + successKeyboard = true; + } + else + { + successKeyboard = false; + } + } + else + { + if (!keyBindings[input].active) + { + if (keyStates[keyBindings[input].scancode] != 0) + { + keyBindings[input].active = true; + successKeyboard = true; + } + else + { + successKeyboard = false; + } + } + else + { + if (keyStates[keyBindings[input].scancode] == 0) + { + keyBindings[input].active = false; + successKeyboard = false; + } + else + { + successKeyboard = false; + } + } + } + } + + if (gameControllerFound()) + if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) + { + successGameController = checkAxisInput(input, index); + if (!successGameController) + { + if (repeat) + { + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0) + { + successGameController = true; + } + else + { + successGameController = false; + } + } + else + { + if (!gameControllerBindings[index][input].active) + { + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0) + { + gameControllerBindings[index][input].active = true; + successGameController = true; + } + else + { + successGameController = false; + } + } + else + { + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) == 0) + { + gameControllerBindings[index][input].active = false; + successGameController = false; + } + else + { + successGameController = false; + } + } + } + } + } + + return (successKeyboard || successGameController); +} + +// Comprueba si un input con modificador esta activo +bool Input::checkModInput(inputs_e inputMod, inputs_e input, bool repeat, int device, int index) +{ + if (!enabled || index >= numGamepads || !checkInput(inputMod, INPUT_ALLOW_REPEAT, device, index)) { return false; } @@ -331,27 +449,13 @@ bool Input::discoverGameControllers() // Comprueba si hay algun mando conectado bool Input::gameControllerFound() { - if (numGamepads > 0) - { - return true; - } - else - { - return false; - } + return numGamepads > 0 ? true : false; } // Obten el nombre de un mando de juego std::string Input::getControllerName(int index) { - if (numGamepads > 0) - { - return controllerNames[index]; - } - else - { - return ""; - } + return numGamepads > 0 ? controllerNames[index] : ""; } // Obten el número de mandos conectados @@ -396,12 +500,7 @@ int Input::getJoyIndex(int id) // Muestra por consola los controles asignados void Input::printBindings(int device, int index) { - if (device == INPUT_USE_ANY) - { - return; - } - - if (device == INPUT_USE_KEYBOARD) + if (device == INPUT_USE_ANY || device == INPUT_USE_KEYBOARD) { return; } @@ -467,6 +566,11 @@ std::string Input::to_string(inputs_e input) return "input_start"; } + if (input == input_service) + { + return "input_service"; + } + return ""; } @@ -493,6 +597,11 @@ inputs_e Input::to_inputs_e(std::string name) return input_start; } + if (name == "input_service") + { + return input_service; + } + return input_null; } @@ -509,7 +618,7 @@ void Input::allActive(int index) bool Input::checkAxisInput(inputs_e input, int index) { bool success = false; - + switch (input) { case input_left: diff --git a/source/common/input.h b/source/common/input.h index a9f32e4..2747edc 100644 --- a/source/common/input.h +++ b/source/common/input.h @@ -38,10 +38,12 @@ enum inputs_e input_reset, input_mute, input_showinfo, + input_config, + input_swap_controllers, // Input obligatorio input_null, - input_number_of_inputs + input_number_of_inputs, }; #define INPUT_ALLOW_REPEAT true @@ -103,10 +105,14 @@ public: // Asigna inputs a botones del mando void bindGameControllerButton(int index, inputs_e input, SDL_GameControllerButton button); + void bindGameControllerButton(int index, inputs_e inputTarget, inputs_e inputSource); // Comprueba si un input esta activo bool checkInput(inputs_e input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0); + // Comprueba si un input con modificador esta activo + bool checkModInput(inputs_e inputMod, inputs_e input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0); + // Comprueba si hay almenos un input activo bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0); diff --git a/source/common/screen.cpp b/source/common/screen.cpp index 5631b31..71a4e0b 100644 --- a/source/common/screen.cpp +++ b/source/common/screen.cpp @@ -7,7 +7,6 @@ #include "jshader.h" #endif #include "dbgtxt.h" -#include "../service.h" // Constructor Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input, options_t *options) @@ -320,47 +319,63 @@ void Screen::update() void Screen::checkInput() { #ifndef ARCADE - if (input->checkInput(input_window_fullscreen, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba el teclado para cambiar entre pantalla completa y ventana + if (input->checkInput(input_window_fullscreen, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { switchVideoMode(); const std::string mode = options->video.mode == SCREEN_VIDEO_MODE_WINDOW ? "Window" : "Fullscreen"; showNotification(mode + " mode"); + return; } - else if (input->checkInput(input_window_dec_size, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba el teclado para decrementar el tamaño de la ventana + if (input->checkInput(input_window_dec_size, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { decWindowSize(); const std::string size = std::to_string(options->video.window.size); showNotification("Window size x" + size); + return; } - else if (input->checkInput(input_window_inc_size, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba el teclado para incrementar el tamaño de la ventana + if (input->checkInput(input_window_inc_size, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { incWindowSize(); const std::string size = std::to_string(options->video.window.size); showNotification("Window size x" + size); + return; } #endif - if (input->checkInput(input_video_shaders, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba el teclado para activar o desactivar los shaders + if (input->checkInput(input_video_shaders, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { switchShaders(); + return; } - else if (input->checkInput(input_showinfo, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba el teclado para mostrar la información de debug + if (input->checkInput(input_showinfo, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { showInfo = !showInfo; + return; } - // Comprueba el botón de SERVICE - switch (checkServiceButton(input)) + for (int i = 0; i < input->getNumControllers(); ++i) { - case SERVICE_SHADERS: - switchShaders(); - break; + // Comprueba los mandos para activar o desactivar los shaders + if (input->checkModInput(input_service, input_video_shaders, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + switchShaders(); + return; + } - default: - break; + // Comprueba los mandos para mostrar la información de debug + if (input->checkModInput(input_service, input_showinfo, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + showInfo = !showInfo; + return; + } } } diff --git a/source/director.cpp b/source/director.cpp index e110472..f7a8d20 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -128,7 +128,7 @@ void Director::initInput() input->bindKey(input_start, SDL_SCANCODE_RETURN); - // Teclado - Otros + // Teclado - Control del programa input->bindKey(input_exit, SDL_SCANCODE_ESCAPE); input->bindKey(input_pause, SDL_SCANCODE_P); input->bindKey(input_window_dec_size, SDL_SCANCODE_F1); @@ -139,6 +139,7 @@ void Director::initInput() input->bindKey(input_showinfo, SDL_SCANCODE_F6); input->bindKey(input_reset, SDL_SCANCODE_F10); + // Asigna botones a inputs const int numGamePads = input->getNumControllers(); for (int i = 0; i < numGamePads; ++i) { @@ -154,8 +155,16 @@ void Director::initInput() input->bindGameControllerButton(i, input_start, SDL_CONTROLLER_BUTTON_START); - // Mando - Otros + // Mando - Control del programa input->bindGameControllerButton(i, input_service, SDL_CONTROLLER_BUTTON_BACK); + input->bindGameControllerButton(i, input_exit, input_start); + input->bindGameControllerButton(i, input_pause, input_fire_right); + input->bindGameControllerButton(i, input_video_shaders, input_fire_left); + input->bindGameControllerButton(i, input_mute, input_left); + input->bindGameControllerButton(i, input_showinfo, input_right); + input->bindGameControllerButton(i, input_reset, input_fire_center); + input->bindGameControllerButton(i, input_config, input_down); + input->bindGameControllerButton(i, input_swap_controllers, input_up); } // Mapea las asignaciones a los botones desde el archivo de configuración, si se da el caso @@ -167,13 +176,26 @@ void Director::initInput() input->bindGameControllerButton(i, options->controller[index].inputs[j], options->controller[index].buttons[j]); } - // Modifica las opciones para colocar los valores asignados - for (int index = 0; index < numGamePads; ++index) + // Asigna botones a inputs desde otros inputs + for (int i = 0; i < numGamePads; ++i) { - options->controller[index].name = input->getControllerName(index); - for (int j = 0; j < (int)options->controller[index].inputs.size(); ++j) + input->bindGameControllerButton(i, input_exit, input_start); + input->bindGameControllerButton(i, input_reset, input_fire_center); + input->bindGameControllerButton(i, input_pause, input_fire_right); + input->bindGameControllerButton(i, input_video_shaders, input_fire_left); + input->bindGameControllerButton(i, input_mute, input_left); + input->bindGameControllerButton(i, input_showinfo, input_right); + input->bindGameControllerButton(i, input_config, input_down); + input->bindGameControllerButton(i, input_swap_controllers, input_up); + } + + // Guarda las asignaciones de botones en las opciones + for (int i = 0; i < numGamePads; ++i) + { + options->controller[i].name = input->getControllerName(i); + for (int j = 0; j < (int)options->controller[i].inputs.size(); ++j) { - options->controller[index].buttons[j] = input->getControllerBinding(index, options->controller[index].inputs[j]); + options->controller[i].buttons[j] = input->getControllerBinding(i, options->controller[i].inputs[j]); } } } @@ -506,6 +528,7 @@ void Director::initOptions() c.deviceType = INPUT_USE_GAMECONTROLLER; c.name = "NO NAME"; + // Inputs que se guardan en las opciones y, por tanto, a disco c.inputs.clear(); c.inputs.push_back(input_fire_left); c.inputs.push_back(input_fire_center); @@ -513,6 +536,7 @@ void Director::initOptions() c.inputs.push_back(input_start); c.inputs.push_back(input_service); + // Botones asociados a los inputs anteriores c.buttons.clear(); c.buttons.push_back(SDL_CONTROLLER_BUTTON_X); c.buttons.push_back(SDL_CONTROLLER_BUTTON_Y); @@ -777,11 +801,11 @@ bool Director::saveConfigFile() const std::string joyIndex = std::to_string(index + 1); file << "controller" + joyIndex + ".name=" + options->controller[index].name + "\n"; file << "controller" + joyIndex + ".player=" + std::to_string(options->controller[index].playerId) + "\n"; - file << "controller" + joyIndex + ".inputs.fire_left=" + std::to_string((int)options->controller[index].buttons[0]) + "\n"; - file << "controller" + joyIndex + ".inputs.fire_center=" + std::to_string((int)options->controller[index].buttons[1]) + "\n"; - file << "controller" + joyIndex + ".inputs.fire_right=" + std::to_string((int)options->controller[index].buttons[2]) + "\n"; - file << "controller" + joyIndex + ".inputs.start=" + std::to_string((int)options->controller[index].buttons[3]) + "\n"; - file << "controller" + joyIndex + ".inputs.service=" + std::to_string((int)options->controller[index].buttons[4]) + "\n"; + file << "controller" + joyIndex + ".button.fire_left=" + std::to_string((int)options->controller[index].buttons[0]) + "\n"; + file << "controller" + joyIndex + ".button.fire_center=" + std::to_string((int)options->controller[index].buttons[1]) + "\n"; + file << "controller" + joyIndex + ".button.fire_right=" + std::to_string((int)options->controller[index].buttons[2]) + "\n"; + file << "controller" + joyIndex + ".button.start=" + std::to_string((int)options->controller[index].buttons[3]) + "\n"; + file << "controller" + joyIndex + ".button.service=" + std::to_string((int)options->controller[index].buttons[4]) + "\n"; if (index < numPlayers - 1) { @@ -1073,27 +1097,27 @@ bool Director::setOptions(options_t *options, std::string var, std::string value options->controller[0].playerId = std::max(1, std::min(2, std::stoi(value))); } - else if (var == "controller1.inputs.fire_left") + else if (var == "controller1.button.fire_left") { options->controller[0].buttons[0] = (SDL_GameControllerButton)std::stoi(value); } - else if (var == "controller1.inputs.fire_center") + else if (var == "controller1.button.fire_center") { options->controller[0].buttons[1] = (SDL_GameControllerButton)std::stoi(value); } - else if (var == "controller1.inputs.fire_right") + else if (var == "controller1.button.fire_right") { options->controller[0].buttons[2] = (SDL_GameControllerButton)std::stoi(value); } - else if (var == "controller1.inputs.start") + else if (var == "controller1.button.start") { options->controller[0].buttons[3] = (SDL_GameControllerButton)std::stoi(value); } - else if (var == "controller1.inputs.service") + else if (var == "controller1.button.service") { options->controller[0].buttons[4] = (SDL_GameControllerButton)std::stoi(value); } @@ -1108,27 +1132,27 @@ bool Director::setOptions(options_t *options, std::string var, std::string value options->controller[1].playerId = std::max(1, std::min(2, std::stoi(value))); } - else if (var == "controller2.inputs.fire_left") + else if (var == "controller2.button.fire_left") { options->controller[1].buttons[0] = (SDL_GameControllerButton)std::stoi(value); } - else if (var == "controller2.inputs.fire_center") + else if (var == "controller2.button.fire_center") { options->controller[1].buttons[1] = (SDL_GameControllerButton)std::stoi(value); } - else if (var == "controller2.inputs.fire_right") + else if (var == "controller2.button.fire_right") { options->controller[1].buttons[2] = (SDL_GameControllerButton)std::stoi(value); } - else if (var == "controller2.inputs.start") + else if (var == "controller2.button.start") { options->controller[1].buttons[3] = (SDL_GameControllerButton)std::stoi(value); } - else if (var == "controller2.inputs.service") + else if (var == "controller2.button.service") { options->controller[1].buttons[4] = (SDL_GameControllerButton)std::stoi(value); } diff --git a/source/game.cpp b/source/game.cpp index 1ee9fa2..478a83a 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1,5 +1,4 @@ #include "game.h" -#include "service.h" #define GAME_OVER_COUNTER 350 @@ -2038,35 +2037,40 @@ void Game::updateMenace() // Gestiona la entrada durante el juego void Game::checkInput() { - // Comprueba las teclas que afectan al programa (solo para el primer jugador) - if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba si se sale con el teclado + if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { section->name = SECTION_PROG_QUIT; section->options = SECTION_OPTIONS_QUIT_NORMAL; return; } - // Comprueba el botón de SERVICE - switch (checkServiceButton(input)) + for (int i = 0; i < input->getNumControllers(); ++i) { - case SERVICE_RESET: - section->name = SECTION_PROG_LOGO; - screen->showNotification("Reset"); - break; + // Comprueba si se va a resetear el juego + if (input->checkModInput(input_service, input_reset, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + section->name = SECTION_PROG_LOGO; + screen->showNotification("Reset"); + return; + } - case SERVICE_MUTE: - options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; - JA_EnableMusic(options->audio.music.enabled); - JA_EnableSound(options->audio.sound.enabled); - screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); - break; + // Comprueba si se va a activar o desactivar el audio + if (input->checkModInput(input_service, input_mute, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; + JA_EnableMusic(options->audio.music.enabled); + JA_EnableSound(options->audio.sound.enabled); + screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); + return; + } - case SERVICE_PAUSE: - pause(!paused); - break; - - default: - break; + // Comprueba si se va a pausar el juego + if (input->checkModInput(input_service, input_pause, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + pause(!paused); + return; + } } // Modo Demo activo diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index 302343f..d3b26a9 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -1,5 +1,4 @@ #include "hiscore_table.h" -#include "service.h" #include // Constructor @@ -195,36 +194,42 @@ void HiScoreTable::checkEvents() // Comprueba las entradas void HiScoreTable::checkInput() { - if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba si se sale con el teclado + if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { section->name = SECTION_PROG_QUIT; section->options = SECTION_OPTIONS_QUIT_NORMAL; + return; } - else if (input->checkAnyButtonPressed()) + // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) + if (input->checkAnyButtonPressed()) { JA_StopMusic(); section->name = SECTION_PROG_TITLE; section->options = SECTION_OPTIONS_TITLE_1; + return; } - // Comprueba el botón de SERVICE - switch (checkServiceButton(input)) + for (int i = 0; i < input->getNumControllers(); ++i) { - case SERVICE_RESET: - section->name = SECTION_PROG_LOGO; - screen->showNotification("Reset"); - break; + // Comprueba si se va a resetear el juego + if (input->checkModInput(input_service, input_reset, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + section->name = SECTION_PROG_LOGO; + screen->showNotification("Reset"); + return; + } - case SERVICE_MUTE: - options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; - JA_EnableMusic(options->audio.music.enabled); - JA_EnableSound(options->audio.sound.enabled); - screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); - break; - - default: - break; + // Comprueba si se va a activar o desactivar el audio + if (input->checkModInput(input_service, input_mute, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; + JA_EnableMusic(options->audio.music.enabled); + JA_EnableSound(options->audio.sound.enabled); + screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); + return; + } } // Comprueba el input para el resto de objetos diff --git a/source/instructions.cpp b/source/instructions.cpp index ec98ebf..2f6277a 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -1,5 +1,4 @@ #include "instructions.h" -#include "service.h" #include // Constructor @@ -325,36 +324,42 @@ void Instructions::checkEvents() // Comprueba las entradas void Instructions::checkInput() { - if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba si se sale con el teclado + if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { section->name = SECTION_PROG_QUIT; section->options = SECTION_OPTIONS_QUIT_NORMAL; + return; } - else if (input->checkAnyButtonPressed()) + // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) + if (input->checkAnyButtonPressed()) { JA_StopMusic(); section->name = SECTION_PROG_TITLE; section->options = SECTION_OPTIONS_TITLE_1; + return; } - // Comprueba el botón de SERVICE - switch (checkServiceButton(input)) + for (int i = 0; i < input->getNumControllers(); ++i) { - case SERVICE_RESET: - section->name = SECTION_PROG_LOGO; - screen->showNotification("Reset"); - break; + // Comprueba si se va a resetear el juego + if (input->checkModInput(input_service, input_reset, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + section->name = SECTION_PROG_LOGO; + screen->showNotification("Reset"); + return; + } - case SERVICE_MUTE: - options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; - JA_EnableMusic(options->audio.music.enabled); - JA_EnableSound(options->audio.sound.enabled); - screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); - break; - - default: - break; + // Comprueba si se va a activar o desactivar el audio + if (input->checkModInput(input_service, input_mute, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; + JA_EnableMusic(options->audio.music.enabled); + JA_EnableSound(options->audio.sound.enabled); + screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); + return; + } } // Comprueba el input para el resto de objetos diff --git a/source/intro.cpp b/source/intro.cpp index 8e316fd..adb2596 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -1,5 +1,4 @@ #include "intro.h" -#include "service.h" // Constructor Intro::Intro(Screen *screen, Asset *asset, Input *input, Lang *lang, options_t *options, param_t *param, section_t *section, JA_Music_t *music) @@ -194,36 +193,42 @@ void Intro::checkEvents() // Comprueba las entradas void Intro::checkInput() { - if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba si se sale con el teclado + if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { section->name = SECTION_PROG_QUIT; section->options = SECTION_OPTIONS_QUIT_NORMAL; + return; } - else if (input->checkAnyButtonPressed()) + // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) + if (input->checkAnyButtonPressed()) { JA_StopMusic(); section->name = SECTION_PROG_TITLE; section->options = SECTION_OPTIONS_TITLE_1; + return; } + + for (int i = 0; i < input->getNumControllers(); ++i) + { + // Comprueba si se va a resetear el juego + if (input->checkModInput(input_service, input_reset, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + section->name = SECTION_PROG_LOGO; + screen->showNotification("Reset"); + return; + } - // Comprueba el botón de SERVICE - switch (checkServiceButton(input)) - { - case SERVICE_RESET: - section->name = SECTION_PROG_LOGO; - screen->showNotification("Reset"); - break; - - case SERVICE_MUTE: - options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; - JA_EnableMusic(options->audio.music.enabled); - JA_EnableSound(options->audio.sound.enabled); - screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); - break; - - default: - break; + // Comprueba si se va a activar o desactivar el audio + if (input->checkModInput(input_service, input_mute, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; + JA_EnableMusic(options->audio.music.enabled); + JA_EnableSound(options->audio.sound.enabled); + screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); + return; + } } // Comprueba el input para el resto de objetos diff --git a/source/logo.cpp b/source/logo.cpp index 047041a..1bdd92b 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -1,5 +1,4 @@ #include "logo.h" -#include "service.h" #include // Constructor @@ -105,37 +104,42 @@ void Logo::checkEvents() // Comprueba las entradas void Logo::checkInput() { - if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT)) + // Comprueba si se sale con el teclado + if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { section->name = SECTION_PROG_QUIT; section->options = SECTION_OPTIONS_QUIT_NORMAL; return; } - else if (input->checkAnyButtonPressed()) + // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) + if (input->checkAnyButtonPressed()) { + JA_StopMusic(); section->name = SECTION_PROG_TITLE; section->options = SECTION_OPTIONS_TITLE_1; + return; } - // Comprueba el botón de SERVICE - switch (checkServiceButton(input)) + for (int i = 0; i < input->getNumControllers(); ++i) { - case SERVICE_EXIT: - section->name = SECTION_PROG_QUIT; - section->options = SECTION_OPTIONS_QUIT_SHUTDOWN; - return; - break; + // Comprueba si se sale con el mando + if (input->checkModInput(input_service, input_exit, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + section->name = SECTION_PROG_QUIT; + section->options = SECTION_OPTIONS_QUIT_SHUTDOWN; + return; + } - case SERVICE_MUTE: - options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; - JA_EnableMusic(options->audio.music.enabled); - JA_EnableSound(options->audio.sound.enabled); - screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); - break; - - default: - break; + // Comprueba si se va a activar o desactivar el audio + if (input->checkModInput(input_service, input_mute, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; + JA_EnableMusic(options->audio.music.enabled); + JA_EnableSound(options->audio.sound.enabled); + screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); + return; + } } // Comprueba el input para el resto de objetos diff --git a/source/service.cpp b/source/service.cpp deleted file mode 100644 index 80be531..0000000 --- a/source/service.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "service.h" - -// Comprueba el botón de servicio del controlador "index" -int checkServiceButton(Input *input, int index) -{ - if (index == -1) - { - for (int i = 0; i < input->getNumControllers(); ++i) - { - if (input->checkInput(input_service, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) - { - if (input->checkInput(input_start, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) - { - return SERVICE_EXIT; - } - - else if (input->checkInput(input_fire_left, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) - { - return SERVICE_SHADERS; - } - - else if (input->checkInput(input_fire_center, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) - { - return SERVICE_RESET; - } - - else if (input->checkInput(input_fire_right, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) - { - return SERVICE_PAUSE; - } - - else if (input->checkInput(input_up, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) - { - return SERVICE_SWAP_CONTROLLERS; - } - - else if (input->checkInput(input_down, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) - { - return SERVICE_CONFIG; - } - - else if (input->checkInput(input_left, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) - { - return SERVICE_MUTE; - } - } - } - } - else - { - if (input->checkInput(input_service, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, index)) - { - if (input->checkInput(input_start, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, index)) - { - return SERVICE_EXIT; - } - - else if (input->checkInput(input_fire_left, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, index)) - { - return SERVICE_SHADERS; - } - - else if (input->checkInput(input_fire_center, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, index)) - { - return SERVICE_RESET; - } - - else if (input->checkInput(input_fire_right, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, index)) - { - return SERVICE_PAUSE; - } - - else if (input->checkInput(input_up, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, index)) - { - return SERVICE_SWAP_CONTROLLERS; - } - - else if (input->checkInput(input_down, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, index)) - { - return SERVICE_CONFIG; - } - - else if (input->checkInput(input_left, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, index)) - { - return SERVICE_MUTE; - } - } - } - - return SERVICE_NULL; -} \ No newline at end of file diff --git a/source/service.h b/source/service.h deleted file mode 100644 index f7d917e..0000000 --- a/source/service.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include "common/utils.h" -#include "common/input.h" -#include "const.h" - -enum service_e -{ - SERVICE_NULL, - SERVICE_EXIT, - SERVICE_CONFIG, - SERVICE_SHADERS, - SERVICE_PAUSE, - SERVICE_SWAP_CONTROLLERS, - SERVICE_RESET, - SERVICE_MUTE -}; - -// Comprueba el botón de servicio del controlador "index" -int checkServiceButton(Input *input, int index = -1); \ No newline at end of file diff --git a/source/title.cpp b/source/title.cpp index 61d114d..aa6cb8c 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -1,5 +1,4 @@ #include "title.h" -#include "service.h" // Constructor Title::Title(Screen *screen, Asset *asset, Input *input, Lang *lang, options_t *options, param_t *param, section_t *section, JA_Music_t *music) @@ -122,6 +121,10 @@ void Title::update() { counter++; } + else + { + counter = 0; + } // Reproduce la música if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) @@ -246,11 +249,12 @@ void Title::checkInput() // TECLADO // ////////////////////////////////////////////////// - // Comprueba el teclado para salir + // Comprueba si se sale con el teclado if (input->checkInput(input_exit, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { section->name = SECTION_PROG_QUIT; section->options = SECTION_OPTIONS_QUIT_NORMAL; + return; } // Comprueba el teclado para empezar a jugar @@ -264,50 +268,51 @@ void Title::checkInput() } ////////////////////////////////////////////////// - // MANDO // + // MANDOS // ////////////////////////////////////////////////// - // Comprueba el botón de SERVICE - switch (checkServiceButton(input)) - { - case SERVICE_RESET: - section->name = SECTION_PROG_LOGO; - screen->showNotification("Reset"); - break; - - case SERVICE_MUTE: - options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; - JA_EnableMusic(options->audio.music.enabled); - JA_EnableSound(options->audio.sound.enabled); - screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); - break; - - case SERVICE_SWAP_CONTROLLERS: - swapControllers(); - break; - - default: - break; - } - - // Comprueba si algun mando quiere ser configurado for (int i = 0; i < input->getNumControllers(); ++i) { - if (checkServiceButton(input, i) == SERVICE_CONFIG) + // Comprueba si se va a resetear el juego + if (input->checkModInput(input_service, input_reset, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + section->name = SECTION_PROG_LOGO; + screen->showNotification("Reset"); + return; + } + + // Comprueba si se va a activar o desactivar el audio + if (input->checkModInput(input_service, input_mute, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + options->audio.sound.enabled = options->audio.music.enabled = !options->audio.music.enabled; + JA_EnableMusic(options->audio.music.enabled); + JA_EnableSound(options->audio.sound.enabled); + screen->showNotification("Audio " + boolToOnOff(options->audio.music.enabled)); + return; + } + + // Comprueba si se va a intercambiar la asignación de mandos a jugadores + if (input->checkModInput(input_service, input_swap_controllers, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) + { + swapControllers(); + return; + } + + // Comprueba si algun mando quiere ser configurado + if (input->checkModInput(input_service, input_config, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { defineButtons->enable(i); + return; } - } - // Comprueba el botón de START de los mandos - for (int i = 0; i < input->getNumControllers(); ++i) - { + // Comprueba el botón de START de los mandos if (input->checkInput(input_start, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { if (section->options == SECTION_OPTIONS_TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) { fade->activate(); postFade = options->controller[i].playerId; + return; } } } @@ -358,20 +363,27 @@ void Title::swapControllers() // Crea cadenas de texto vacias para un numero máximo de mandos const int MAX_CONTROLLERS = 2; std::string text[MAX_CONTROLLERS]; + int playerControllerIndex[MAX_CONTROLLERS]; for (int i = 0; i < MAX_CONTROLLERS; ++i) + { text[i] = ""; + playerControllerIndex[i] = -1; + } // Obtiene para cada jugador el índice del mando correspondiente - int playerControllerIndex[numControllers]; - for (int i = 0; i < numControllers; ++i) + for (int i = 0; i < MAX_CONTROLLERS; ++i) { playerControllerIndex[options->controller[i].playerId - 1] = i; } // Genera el texto correspondiente - for (int i = 0; i < numControllers; ++i) + for (int i = 0; i < MAX_CONTROLLERS; ++i) { - text[i] = "Jugador " + std::to_string(i + 1) + ": " + options->controller[playerControllerIndex[i]].name; + const int index = playerControllerIndex[i]; + if (options->controller[index].name != "NO NAME") + { + text[i] = "Jugador " + std::to_string(i + 1) + ": " + options->controller[index].name; + } } screen->showNotification(text[0], text[1]);