Input: mogudes structs, enums i consts a la part publica

This commit is contained in:
2025-07-30 08:53:57 +02:00
parent 677feb3afe
commit 989f081a25
14 changed files with 302 additions and 275 deletions

View File

@@ -51,12 +51,12 @@ void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event)
// Asigna los botones definidos al input_
void DefineButtons::bindButtons() {
for (const auto &button : buttons_) {
input_->bindGameControllerButton(index_controller_, button.input, button.button);
input_->bindGameControllerButton(index_controller_, button.action, button.button);
}
// Remapea los inputs a inputs
input_->bindGameControllerButton(index_controller_, InputAction::SM_SELECT, InputAction::FIRE_LEFT);
input_->bindGameControllerButton(index_controller_, InputAction::SM_BACK, InputAction::FIRE_CENTER);
input_->bindGameControllerButton(index_controller_, Input::Action::SM_SELECT, Input::Action::FIRE_LEFT);
input_->bindGameControllerButton(index_controller_, Input::Action::SM_BACK, Input::Action::FIRE_CENTER);
}
// Comprueba los eventos
@@ -117,11 +117,11 @@ auto DefineButtons::checkButtonNotInUse(SDL_GamepadButton button) -> bool {
// Limpia la asignación de botones
void DefineButtons::clearButtons() {
buttons_.clear();
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_LEFT"), InputAction::FIRE_LEFT, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_UP"), InputAction::FIRE_CENTER, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_RIGHT"), InputAction::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] START"), InputAction::START, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] SERVICE_MENU"), InputAction::SERVICE, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_LEFT"), Input::Action::FIRE_LEFT, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_UP"), Input::Action::FIRE_CENTER, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] FIRE_RIGHT"), Input::Action::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] START"), Input::Action::START, SDL_GAMEPAD_BUTTON_INVALID);
buttons_.emplace_back(Lang::getText("[DEFINE_BUTTONS] SERVICE_MENU"), Input::Action::SERVICE, SDL_GAMEPAD_BUTTON_INVALID);
}
// Comprueba si ha finalizado

View File

@@ -8,24 +8,22 @@
#include <utility>
#include <vector> // Para vector
// Declaraciones adelantadas
class Input;
class Text;
enum class InputAction : int;
// Estructura para definir botones
struct DefineButtonsButton {
std::string label; // Texto en pantalla
InputAction input; // Acción asociada
SDL_GamepadButton button; // Botón del mando
DefineButtonsButton(std::string lbl, InputAction inp, SDL_GamepadButton btn)
: label(std::move(lbl)), input(inp), button(btn) {}
};
#include "input.h"
#include "text.h"
// Clase DefineButtons
class DefineButtons {
public:
// Estructura para definir botones
struct Button {
std::string label; // Texto en pantalla
Input::Action action; // Acción asociada
SDL_GamepadButton button; // Botón del mando
Button(std::string label, Input::Action action, SDL_GamepadButton button)
: label(std::move(label)), action(action), button(button) {}
};
DefineButtons();
~DefineButtons() = default;
@@ -42,7 +40,7 @@ class DefineButtons {
// Variables
bool enabled_ = false; // Indica si está activo
int x_ = 0, y_ = 0; // Coordenadas de texto
std::vector<DefineButtonsButton> buttons_; // Definiciones de botones
std::vector<Button> buttons_; // Definiciones de botones
size_t index_controller_ = 0; // Índice del controlador asignado
size_t index_button_ = 0; // Índice del botón en proceso
std::vector<std::string> controller_names_; // Nombres de los mandos

View File

@@ -150,60 +150,60 @@ void Director::loadScoreFile() {
// Asigna los botones y teclas al objeto Input
void Director::bindInputs() {
// Teclado - Movimiento del jugador
Input::get()->bindKey(InputAction::UP, SDL_SCANCODE_UP);
Input::get()->bindKey(InputAction::DOWN, SDL_SCANCODE_DOWN);
Input::get()->bindKey(InputAction::LEFT, SDL_SCANCODE_LEFT);
Input::get()->bindKey(InputAction::RIGHT, SDL_SCANCODE_RIGHT);
Input::get()->bindKey(Input::Action::UP, SDL_SCANCODE_UP);
Input::get()->bindKey(Input::Action::DOWN, SDL_SCANCODE_DOWN);
Input::get()->bindKey(Input::Action::LEFT, SDL_SCANCODE_LEFT);
Input::get()->bindKey(Input::Action::RIGHT, SDL_SCANCODE_RIGHT);
// Teclado - Disparo del jugador
Input::get()->bindKey(InputAction::FIRE_LEFT, SDL_SCANCODE_Q);
Input::get()->bindKey(InputAction::FIRE_CENTER, SDL_SCANCODE_W);
Input::get()->bindKey(InputAction::FIRE_RIGHT, SDL_SCANCODE_E);
Input::get()->bindKey(Input::Action::FIRE_LEFT, SDL_SCANCODE_Q);
Input::get()->bindKey(Input::Action::FIRE_CENTER, SDL_SCANCODE_W);
Input::get()->bindKey(Input::Action::FIRE_RIGHT, SDL_SCANCODE_E);
// Teclado - Interfaz
Input::get()->bindKey(InputAction::START, SDL_SCANCODE_RETURN);
Input::get()->bindKey(Input::Action::START, SDL_SCANCODE_RETURN);
// Teclado - Menu de servicio
Input::get()->bindKey(InputAction::SERVICE, SDL_SCANCODE_0);
Input::get()->bindKey(InputAction::SM_SELECT, SDL_SCANCODE_RETURN);
Input::get()->bindKey(InputAction::SM_BACK, SDL_SCANCODE_BACKSPACE);
Input::get()->bindKey(Input::Action::SERVICE, SDL_SCANCODE_0);
Input::get()->bindKey(Input::Action::SM_SELECT, SDL_SCANCODE_RETURN);
Input::get()->bindKey(Input::Action::SM_BACK, SDL_SCANCODE_BACKSPACE);
// Teclado - Control del programa
Input::get()->bindKey(InputAction::EXIT, SDL_SCANCODE_ESCAPE);
Input::get()->bindKey(InputAction::PAUSE, SDL_SCANCODE_P);
Input::get()->bindKey(InputAction::BACK, SDL_SCANCODE_BACKSPACE);
Input::get()->bindKey(Input::Action::EXIT, SDL_SCANCODE_ESCAPE);
Input::get()->bindKey(Input::Action::PAUSE, SDL_SCANCODE_P);
Input::get()->bindKey(Input::Action::BACK, SDL_SCANCODE_BACKSPACE);
Input::get()->bindKey(InputAction::WINDOW_DEC_SIZE, SDL_SCANCODE_F1);
Input::get()->bindKey(InputAction::WINDOW_INC_SIZE, SDL_SCANCODE_F2);
Input::get()->bindKey(InputAction::WINDOW_FULLSCREEN, SDL_SCANCODE_F3);
Input::get()->bindKey(InputAction::TOGGLE_VIDEO_SHADERS, SDL_SCANCODE_F4);
Input::get()->bindKey(InputAction::TOGGLE_VIDEO_INTEGER_SCALE, SDL_SCANCODE_F5);
Input::get()->bindKey(InputAction::TOGGLE_VIDEO_VSYNC, SDL_SCANCODE_F6);
Input::get()->bindKey(Input::Action::WINDOW_DEC_SIZE, SDL_SCANCODE_F1);
Input::get()->bindKey(Input::Action::WINDOW_INC_SIZE, SDL_SCANCODE_F2);
Input::get()->bindKey(Input::Action::WINDOW_FULLSCREEN, SDL_SCANCODE_F3);
Input::get()->bindKey(Input::Action::TOGGLE_VIDEO_SHADERS, SDL_SCANCODE_F4);
Input::get()->bindKey(Input::Action::TOGGLE_VIDEO_INTEGER_SCALE, SDL_SCANCODE_F5);
Input::get()->bindKey(Input::Action::TOGGLE_VIDEO_VSYNC, SDL_SCANCODE_F6);
Input::get()->bindKey(InputAction::TOGGLE_AUDIO, SDL_SCANCODE_F7);
Input::get()->bindKey(InputAction::TOGGLE_AUTO_FIRE, SDL_SCANCODE_F8);
Input::get()->bindKey(InputAction::CHANGE_LANG, SDL_SCANCODE_F9);
Input::get()->bindKey(Input::Action::TOGGLE_AUDIO, SDL_SCANCODE_F7);
Input::get()->bindKey(Input::Action::TOGGLE_AUTO_FIRE, SDL_SCANCODE_F8);
Input::get()->bindKey(Input::Action::CHANGE_LANG, SDL_SCANCODE_F9);
Input::get()->bindKey(InputAction::RESET, SDL_SCANCODE_F10);
Input::get()->bindKey(InputAction::SHOW_INFO, SDL_SCANCODE_F12);
Input::get()->bindKey(Input::Action::RESET, SDL_SCANCODE_F10);
Input::get()->bindKey(Input::Action::SHOW_INFO, SDL_SCANCODE_F12);
// Asigna botones a inputs
const int NUM_GAMEPADS = Input::get()->getNumControllers();
for (int i = 0; i < NUM_GAMEPADS; ++i) {
// Mando - Movimiento del jugador
Input::get()->bindGameControllerButton(i, InputAction::UP, SDL_GAMEPAD_BUTTON_DPAD_UP);
Input::get()->bindGameControllerButton(i, InputAction::DOWN, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
Input::get()->bindGameControllerButton(i, InputAction::LEFT, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
Input::get()->bindGameControllerButton(i, InputAction::RIGHT, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
Input::get()->bindGameControllerButton(i, Input::Action::UP, SDL_GAMEPAD_BUTTON_DPAD_UP);
Input::get()->bindGameControllerButton(i, Input::Action::DOWN, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
Input::get()->bindGameControllerButton(i, Input::Action::LEFT, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
Input::get()->bindGameControllerButton(i, Input::Action::RIGHT, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
// Mando - Disparo del jugador
Input::get()->bindGameControllerButton(i, InputAction::FIRE_LEFT, SDL_GAMEPAD_BUTTON_WEST);
Input::get()->bindGameControllerButton(i, InputAction::FIRE_CENTER, SDL_GAMEPAD_BUTTON_NORTH);
Input::get()->bindGameControllerButton(i, InputAction::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_EAST);
Input::get()->bindGameControllerButton(i, Input::Action::FIRE_LEFT, SDL_GAMEPAD_BUTTON_WEST);
Input::get()->bindGameControllerButton(i, Input::Action::FIRE_CENTER, SDL_GAMEPAD_BUTTON_NORTH);
Input::get()->bindGameControllerButton(i, Input::Action::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_EAST);
// Mando - Interfaz
Input::get()->bindGameControllerButton(i, InputAction::START, SDL_GAMEPAD_BUTTON_START);
Input::get()->bindGameControllerButton(i, InputAction::SERVICE, SDL_GAMEPAD_BUTTON_BACK);
Input::get()->bindGameControllerButton(i, Input::Action::START, SDL_GAMEPAD_BUTTON_START);
Input::get()->bindGameControllerButton(i, Input::Action::SERVICE, SDL_GAMEPAD_BUTTON_BACK);
}
// Mapea las asignaciones a los botones desde el archivo de configuración, si se da el caso
@@ -221,8 +221,8 @@ void Director::bindInputs() {
// Asigna botones a inputs desde otros inputs
for (int i = 0; i < NUM_GAMEPADS; ++i) {
// Mando - Menu de servicio
Input::get()->bindGameControllerButton(i, InputAction::SM_SELECT, InputAction::FIRE_LEFT);
Input::get()->bindGameControllerButton(i, InputAction::SM_BACK, InputAction::FIRE_CENTER);
Input::get()->bindGameControllerButton(i, Input::Action::SM_SELECT, Input::Action::FIRE_LEFT);
Input::get()->bindGameControllerButton(i, Input::Action::SM_BACK, Input::Action::FIRE_CENTER);
}
// Guarda las asignaciones de botones en las opciones de los dos primeros mandos
@@ -402,11 +402,11 @@ void Director::setFileList() {
Asset::get()->add(PREFIX + "/data/gfx/player/player2_coffee2.pal", AssetType::PALETTE);
Asset::get()->add(PREFIX + "/data/gfx/player/player2_invencible.pal", AssetType::PALETTE);
Asset::get()->add(PREFIX + "/data/gfx/player/player2_power.png", AssetType::BITMAP);
// Animaciones del jugador
Asset::get()->add(PREFIX + "/data/gfx/player/player.ani", AssetType::ANIMATION);
Asset::get()->add(PREFIX + "/data/gfx/player/player_power.ani", AssetType::ANIMATION);
// Texturas - Golpe del jugador
Asset::get()->add(PREFIX + "/data/gfx/player/hit.png", AssetType::BITMAP);

View File

@@ -5,6 +5,7 @@
#include "mouse.h" // Para handleEvent
#include "screen.h"
#include "section.hpp" // Para Name, Options, name, options
#include "input.h"
namespace GlobalEvents {
// Comprueba los eventos que se pueden producir en cualquier sección del juego
@@ -29,5 +30,8 @@ void check(const SDL_Event &event) {
}
Mouse::handleEvent(event);
static auto input = Input::get();
input->handleEvent(event);
}
} // namespace GlobalEvents

View File

@@ -5,7 +5,7 @@
#include "asset.h" // Para Asset
#include "audio.h" // Para Audio
#include "input.h" // Para Input, INPUT_DO_NOT_ALLOW_REPEAT, InputAction, InputDevice
#include "input.h" // Para Input, Input::DO_NOT_ALLOW_REPEAT, InputAction, InputDevice
#include "lang.h" // Para getText, Code, getNextLangCode, loadFromFile
#include "options.h" // Para SettingsOptions, settings, VideoOptions, WindowOptions, video, window, AudioOptions, audio
#include "screen.h" // Para Screen
@@ -163,7 +163,7 @@ void incWindowSize() {
// Comprueba el boton de servicio
auto checkServiceButton() -> bool {
// Teclado
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::SERVICE, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
toggleServiceMenu();
return true;
}
@@ -171,7 +171,7 @@ auto checkServiceButton() -> bool {
// Mandos
{
for (int i = 0; i < Input::get()->getNumControllers(); ++i) {
if (Input::get()->checkInput(InputAction::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i)) {
if (Input::get()->checkInput(Input::Action::SERVICE, Input::DO_NOT_ALLOW_REPEAT, Input::Device::CONTROLLER, i)) {
toggleServiceMenu();
return true;
}
@@ -189,37 +189,37 @@ auto checkServiceInputs() -> bool {
// Teclado
{
// Arriba
if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::UP, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
ServiceMenu::get()->setSelectorUp();
return true;
}
// Abajo
if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::DOWN, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
ServiceMenu::get()->setSelectorDown();
return true;
}
// Derecha
if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::RIGHT, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
ServiceMenu::get()->adjustOption(true);
return true;
}
// Izquierda
if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::LEFT, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
ServiceMenu::get()->adjustOption(false);
return true;
}
// Aceptar
if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::SM_SELECT, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
ServiceMenu::get()->selectOption();
return true;
}
// Atras
if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::SM_BACK, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
ServiceMenu::get()->moveBack();
return true;
}
@@ -229,37 +229,37 @@ auto checkServiceInputs() -> bool {
{
for (int i = 0; i < Input::get()->getNumControllers(); ++i) {
// Arriba
if (Input::get()->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i)) {
if (Input::get()->checkInput(Input::Action::UP, Input::DO_NOT_ALLOW_REPEAT, Input::Device::CONTROLLER, i)) {
ServiceMenu::get()->setSelectorUp();
return true;
}
// Abajo
if (Input::get()->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i)) {
if (Input::get()->checkInput(Input::Action::DOWN, Input::DO_NOT_ALLOW_REPEAT, Input::Device::CONTROLLER, i)) {
ServiceMenu::get()->setSelectorDown();
return true;
}
// Derecha
if (Input::get()->checkInput(InputAction::RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i)) {
if (Input::get()->checkInput(Input::Action::RIGHT, Input::DO_NOT_ALLOW_REPEAT, Input::Device::CONTROLLER, i)) {
ServiceMenu::get()->adjustOption(true);
return true;
}
// Izquierda
if (Input::get()->checkInput(InputAction::LEFT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i)) {
if (Input::get()->checkInput(Input::Action::LEFT, Input::DO_NOT_ALLOW_REPEAT, Input::Device::CONTROLLER, i)) {
ServiceMenu::get()->adjustOption(false);
return true;
}
// Aceptar
if (Input::get()->checkInput(InputAction::SM_SELECT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i)) {
if (Input::get()->checkInput(Input::Action::SM_SELECT, Input::DO_NOT_ALLOW_REPEAT, Input::Device::CONTROLLER, i)) {
ServiceMenu::get()->selectOption();
return true;
}
// Atras
if (Input::get()->checkInput(InputAction::SM_BACK, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i)) {
if (Input::get()->checkInput(Input::Action::SM_BACK, Input::DO_NOT_ALLOW_REPEAT, Input::Device::CONTROLLER, i)) {
ServiceMenu::get()->moveBack();
return true;
}
@@ -273,7 +273,7 @@ auto checkInputs() -> bool {
// Teclado
{
// Comprueba el teclado para cambiar entre pantalla completa y ventana
if (Input::get()->checkInput(InputAction::WINDOW_FULLSCREEN, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::WINDOW_FULLSCREEN, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
Screen::get()->toggleFullscreen();
const std::string MODE = Options::video.fullscreen ? Lang::getText("[NOTIFICATIONS] 11") : Lang::getText("[NOTIFICATIONS] 10");
Notifier::get()->show({MODE});
@@ -281,7 +281,7 @@ auto checkInputs() -> bool {
}
// Comprueba el teclado para decrementar el tamaño de la ventana
if (Input::get()->checkInput(InputAction::WINDOW_DEC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::WINDOW_DEC_SIZE, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
if (Screen::get()->decWindowSize()) {
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.zoom)});
}
@@ -289,7 +289,7 @@ auto checkInputs() -> bool {
}
// Comprueba el teclado para incrementar el tamaño de la ventana
if (Input::get()->checkInput(InputAction::WINDOW_INC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::WINDOW_INC_SIZE, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
if (Screen::get()->incWindowSize()) {
Notifier::get()->show({Lang::getText("[NOTIFICATIONS] 09") + " x" + std::to_string(Options::window.zoom)});
}
@@ -297,7 +297,7 @@ auto checkInputs() -> bool {
}
// Salir
if (Input::get()->checkInput(InputAction::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::EXIT, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
quit();
return true;
}
@@ -309,50 +309,50 @@ auto checkInputs() -> bool {
}
// Reset
if (Input::get()->checkInput(InputAction::RESET, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::RESET, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
reset();
return true;
}
// Audio
if (Input::get()->checkInput(InputAction::TOGGLE_AUDIO, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::TOGGLE_AUDIO, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
toggleAudio();
return true;
}
// Autofire
if (Input::get()->checkInput(InputAction::TOGGLE_AUTO_FIRE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::TOGGLE_AUTO_FIRE, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
toggleFireMode();
return true;
}
// Idioma
if (Input::get()->checkInput(InputAction::CHANGE_LANG, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::CHANGE_LANG, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
changeLang();
return true;
}
// Shaders
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::TOGGLE_VIDEO_SHADERS, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
toggleShaders();
return true;
}
// Integer Scale
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_INTEGER_SCALE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::TOGGLE_VIDEO_INTEGER_SCALE, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
toggleIntegerScale();
return true;
}
// VSync
if (Input::get()->checkInput(InputAction::TOGGLE_VIDEO_VSYNC, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::TOGGLE_VIDEO_VSYNC, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
toggleVSync();
return true;
}
#ifdef _DEBUG
// Debug info
if (Input::get()->checkInput(InputAction::SHOW_INFO, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (Input::get()->checkInput(Input::Action::SHOW_INFO, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
Screen::get()->toggleDebugInfo();
return true;
}

View File

@@ -27,39 +27,39 @@ Input::Input(std::string game_controller_db_path)
initSDLGamePad();
// Inicializa los vectores
key_bindings_.resize(static_cast<int>(InputAction::SIZE), KeyBindings());
controller_bindings_.resize(num_gamepads_, std::vector<ControllerBindings>(static_cast<int>(InputAction::SIZE), ControllerBindings()));
key_bindings_.resize(static_cast<int>(Action::SIZE), KeyBindings());
controller_bindings_.resize(num_gamepads_, std::vector<ControllerBindings>(static_cast<int>(Action::SIZE), ControllerBindings()));
// Listado de los inputs para jugar que utilizan botones, ni palancas ni crucetas
button_inputs_ = {InputAction::FIRE_LEFT, InputAction::FIRE_CENTER, InputAction::FIRE_RIGHT, InputAction::START};
button_inputs_ = {Action::FIRE_LEFT, Action::FIRE_CENTER, Action::FIRE_RIGHT, Action::START};
}
// Asigna inputs a teclas
void Input::bindKey(InputAction input, SDL_Scancode code) {
void Input::bindKey(Action input, SDL_Scancode code) {
key_bindings_.at(static_cast<int>(input)).scancode = code;
}
// Asigna inputs a botones del mando
void Input::bindGameControllerButton(int controller_index, InputAction input, SDL_GamepadButton button) {
void Input::bindGameControllerButton(int controller_index, Action input, SDL_GamepadButton button) {
if (controller_index < num_gamepads_) {
controller_bindings_.at(controller_index).at(static_cast<int>(input)).button = button;
}
}
// Asigna inputs a botones del mando
void Input::bindGameControllerButton(int controller_index, InputAction input_target, InputAction input_source) {
void Input::bindGameControllerButton(int controller_index, Action input_target, Action input_source) {
if (controller_index < num_gamepads_) {
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
auto Input::checkInput(InputAction input, bool repeat, InputDevice device, int controller_index) -> bool {
auto Input::checkInput(Action input, bool repeat, Device device, int controller_index) -> bool {
bool success_keyboard = false;
bool success_controller = false;
const int INPUT_INDEX = static_cast<int>(input);
if (device == InputDevice::KEYBOARD || device == InputDevice::ANY) {
if (device == Device::KEYBOARD || device == Device::ANY) {
if (repeat) { // El usuario quiere saber si está pulsada (estado mantenido)
success_keyboard = key_bindings_[INPUT_INDEX].is_held;
} else { // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma)
@@ -68,7 +68,7 @@ auto Input::checkInput(InputAction input, bool repeat, InputDevice device, int c
}
if (gameControllerFound() && controller_index >= 0 && controller_index < num_gamepads_) {
if ((device == InputDevice::CONTROLLER) || (device == InputDevice::ANY)) {
if ((device == Device::CONTROLLER) || (device == Device::ANY)) {
success_controller = checkAxisInput(input, controller_index, repeat);
if (!success_controller) {
@@ -85,12 +85,12 @@ auto Input::checkInput(InputAction input, bool repeat, InputDevice device, int c
}
// Comprueba si hay almenos un input activo
auto Input::checkAnyInput(InputDevice device, int controller_index) -> bool {
auto Input::checkAnyInput(Device device, int controller_index) -> bool {
// Obtenemos el número total de acciones posibles para iterar sobre ellas.
const int NUM_ACTIONS = static_cast<int>(InputAction::SIZE);
const int NUM_ACTIONS = static_cast<int>(Action::SIZE);
// --- Comprobación del Teclado ---
if (device == InputDevice::KEYBOARD || device == InputDevice::ANY) {
if (device == Device::KEYBOARD || device == Device::ANY) {
for (int i = 0; i < NUM_ACTIONS; ++i) {
// Simplemente leemos el estado pre-calculado por Input::update().
// Ya no se llama a SDL_GetKeyboardState ni se modifica el estado '.active'.
@@ -103,7 +103,7 @@ auto Input::checkAnyInput(InputDevice device, int controller_index) -> bool {
// --- Comprobación del Mando ---
// Comprobamos si hay mandos y si el índice solicitado es válido.
if (gameControllerFound() && controller_index >= 0 && controller_index < num_gamepads_) {
if (device == InputDevice::CONTROLLER || device == InputDevice::ANY) {
if (device == Device::CONTROLLER || device == Device::ANY) {
// Bucle CORREGIDO: Iteramos sobre todas las acciones, no sobre el número de mandos.
for (int i = 0; i < NUM_ACTIONS; ++i) {
// Leemos el estado pre-calculado para el mando y la acción específicos.
@@ -123,13 +123,13 @@ auto Input::checkAnyButton(bool repeat) -> int {
// Solo comprueba los botones definidos previamente
for (auto bi : button_inputs_) {
// Comprueba el teclado
if (checkInput(bi, repeat, InputDevice::KEYBOARD)) {
if (checkInput(bi, repeat, Device::KEYBOARD)) {
return 1;
}
// Comprueba los mandos
for (int i = 0; i < num_gamepads_; ++i) {
if (checkInput(bi, repeat, InputDevice::CONTROLLER, i)) {
if (checkInput(bi, repeat, Device::CONTROLLER, i)) {
return i + 1;
}
}
@@ -226,12 +226,12 @@ auto Input::getJoyIndex(SDL_JoystickID id) const -> int {
}
// Muestra por consola los controles asignados
void Input::printBindings(InputDevice device, int controller_index) const {
if (device == InputDevice::ANY || device == InputDevice::KEYBOARD) {
void Input::printBindings(Device device, int controller_index) const {
if (device == Device::ANY || device == Device::KEYBOARD) {
return;
}
if (device == InputDevice::CONTROLLER) {
if (device == Device::CONTROLLER) {
if (controller_index >= num_gamepads_) {
return;
}
@@ -247,7 +247,7 @@ void Input::printBindings(InputDevice device, int controller_index) const {
}
// Obtiene el SDL_GamepadButton asignado a un input
auto Input::getControllerBinding(int controller_index, InputAction input) const -> SDL_GamepadButton {
auto Input::getControllerBinding(int controller_index, Action input) const -> SDL_GamepadButton {
return controller_bindings_[controller_index][static_cast<int>(input)].button;
}
@@ -258,17 +258,17 @@ auto Input::getIndexByName(const std::string &name) const -> int {
}
// Convierte un InputAction a std::string
auto Input::inputToString(InputAction input) -> std::string {
auto Input::inputToString(Action input) -> std::string {
switch (input) {
case InputAction::FIRE_LEFT:
case Action::FIRE_LEFT:
return "input_fire_left";
case InputAction::FIRE_CENTER:
case Action::FIRE_CENTER:
return "input_fire_center";
case InputAction::FIRE_RIGHT:
case Action::FIRE_RIGHT:
return "input_fire_right";
case InputAction::START:
case Action::START:
return "input_start";
case InputAction::SERVICE:
case Action::SERVICE:
return "input_service";
default:
return "";
@@ -276,34 +276,34 @@ auto Input::inputToString(InputAction input) -> std::string {
}
// Convierte un std::string a InputAction
auto Input::stringToInput(const std::string &name) -> InputAction {
static const std::unordered_map<std::string, InputAction> INPUT_MAP = {
{"input_fire_left", InputAction::FIRE_LEFT},
{"input_fire_center", InputAction::FIRE_CENTER},
{"input_fire_right", InputAction::FIRE_RIGHT},
{"input_start", InputAction::START},
{"input_service", InputAction::SERVICE}};
auto Input::stringToInput(const std::string &name) -> Action {
static const std::unordered_map<std::string, Action> INPUT_MAP = {
{"input_fire_left", Action::FIRE_LEFT},
{"input_fire_center", Action::FIRE_CENTER},
{"input_fire_right", Action::FIRE_RIGHT},
{"input_start", Action::START},
{"input_service", Action::SERVICE}};
auto it = INPUT_MAP.find(name);
return it != INPUT_MAP.end() ? it->second : InputAction::NONE;
return it != INPUT_MAP.end() ? it->second : Action::NONE;
}
// Comprueba el eje del mando
auto Input::checkAxisInput(InputAction input, int controller_index, bool repeat) -> bool {
auto Input::checkAxisInput(Action input, int controller_index, bool repeat) -> bool {
// Umbral para considerar el eje como activo
bool axis_active_now = false;
switch (input) {
case InputAction::LEFT:
case Action::LEFT:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) < -AXIS_THRESHOLD;
break;
case InputAction::RIGHT:
case Action::RIGHT:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) > AXIS_THRESHOLD;
break;
case InputAction::UP:
case Action::UP:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) < -AXIS_THRESHOLD;
break;
case InputAction::DOWN:
case Action::DOWN:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) > AXIS_THRESHOLD;
break;
default:
@@ -379,4 +379,25 @@ void Input::update() {
controller_bindings_[c][i].is_held = button_is_down_now;
}
}
}
void Input::handleEvent(const SDL_Event &event) {
switch (event.type) {
case SDL_EVENT_GAMEPAD_ADDED: {
printf("¡Mando conectado! ID: %d\n", event.gdevice.which);
SDL_Gamepad *gamepad = SDL_OpenGamepad(event.gdevice.which);
if (gamepad) {
printf("Gamepad abierto correctamente.\n");
} else {
printf("Error al abrir el gamepad: %s\n", SDL_GetError());
}
break;
}
case SDL_EVENT_GAMEPAD_REMOVED: {
printf("¡Mando desconectado! Instance ID: %d\n", event.gdevice.which);
// Aquí puedes cerrar el gamepad si lo tenías abierto
// SDL_CloseGamepad(gamepad); ← si tienes el puntero guardado
break;
}
}
}

View File

@@ -14,76 +14,77 @@ device contiene el tipo de dispositivo a comprobar:
InputDeviceToUse::ANY mirará tanto el teclado como el PRIMER controlador
*/
// Acciones de entrada posibles en el juego
enum class InputAction : int {
// Inputs de movimiento
UP,
DOWN,
LEFT,
RIGHT,
// Inputs personalizados
FIRE_LEFT,
FIRE_CENTER,
FIRE_RIGHT,
START,
// Service Menu
SM_SELECT,
SM_BACK,
// Inputs de control
BACK,
EXIT,
PAUSE,
SERVICE,
WINDOW_FULLSCREEN,
WINDOW_INC_SIZE,
WINDOW_DEC_SIZE,
TOGGLE_VIDEO_SHADERS,
TOGGLE_VIDEO_INTEGER_SCALE,
TOGGLE_VIDEO_VSYNC,
RESET,
TOGGLE_AUDIO,
CHANGE_LANG,
SHOW_INFO,
CONFIG,
SWAP_CONTROLLERS,
TOGGLE_AUTO_FIRE,
// Input obligatorio
NONE,
SIZE,
};
constexpr bool INPUT_ALLOW_REPEAT = true;
constexpr bool INPUT_DO_NOT_ALLOW_REPEAT = false;
// Tipos de dispositivos de entrada
enum class InputDevice : int {
KEYBOARD = 0,
CONTROLLER = 1,
ANY = 2,
};
// Clase Input: gestiona la entrada de teclado y mandos (singleton)
class Input {
public:
// --- Constantes ---
static constexpr bool ALLOW_REPEAT = true;
static constexpr bool DO_NOT_ALLOW_REPEAT = false;
// Acciones de entrada posibles en el juego
enum class Action : int {
// Inputs de movimiento
UP,
DOWN,
LEFT,
RIGHT,
// Inputs personalizados
FIRE_LEFT,
FIRE_CENTER,
FIRE_RIGHT,
START,
// Service Menu
SM_SELECT,
SM_BACK,
// Inputs de control
BACK,
EXIT,
PAUSE,
SERVICE,
WINDOW_FULLSCREEN,
WINDOW_INC_SIZE,
WINDOW_DEC_SIZE,
TOGGLE_VIDEO_SHADERS,
TOGGLE_VIDEO_INTEGER_SCALE,
TOGGLE_VIDEO_VSYNC,
RESET,
TOGGLE_AUDIO,
CHANGE_LANG,
SHOW_INFO,
CONFIG,
SWAP_CONTROLLERS,
TOGGLE_AUTO_FIRE,
// Input obligatorio
NONE,
SIZE,
};
// Tipos de dispositivos de entrada
enum class Device : int {
KEYBOARD = 0,
CONTROLLER = 1,
ANY = 2,
};
// --- Métodos de singleton ---
static void init(const std::string &game_controller_db_path); // Inicializa el singleton
static void destroy(); // Libera el singleton
static auto get() -> Input *; // Obtiene la instancia
// --- Métodos de configuración de controles ---
void bindKey(InputAction input, SDL_Scancode code); // Asigna inputs a teclas
void bindGameControllerButton(int controller_index, InputAction input, SDL_GamepadButton button); // Asigna inputs a botones del mando
void bindGameControllerButton(int controller_index, InputAction input_target, InputAction input_source); // Asigna inputs a otros inputs del mando
void bindKey(Action input, SDL_Scancode code); // Asigna inputs a teclas
void bindGameControllerButton(int controller_index, Action input, SDL_GamepadButton button); // Asigna inputs a botones del mando
void bindGameControllerButton(int controller_index, Action input_target, Action input_source); // Asigna inputs a otros inputs del mando
// --- Métodos de consulta de entrada ---
void update(); // Comprueba fisicamente los botones y teclas que se han pulsado
auto checkInput(InputAction input, bool repeat = true, InputDevice device = InputDevice::ANY, int controller_index = 0) -> bool; // Comprueba si un input está activo
auto checkAnyInput(InputDevice device = InputDevice::ANY, int controller_index = 0) -> bool; // Comprueba si hay al menos un input activo
auto checkAnyButton(bool repeat = INPUT_DO_NOT_ALLOW_REPEAT) -> int; // Comprueba si hay algún botón pulsado
void update(); // Comprueba fisicamente los botones y teclas que se han pulsado
auto checkInput(Action input, bool repeat = true, Device device = Device::ANY, int controller_index = 0) -> bool; // Comprueba si un input está activo
auto checkAnyInput(Device device = Device::ANY, int controller_index = 0) -> bool; // Comprueba si hay al menos un input activo
auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> int; // Comprueba si hay algún botón pulsado
// --- Métodos de gestión de mandos ---
auto discoverGameControllers() -> bool; // Busca si hay mandos conectados
@@ -93,15 +94,18 @@ class Input {
[[nodiscard]] auto getJoyIndex(SDL_JoystickID id) const -> int; // Obtiene el índice del controlador a partir de un event.id
// --- Métodos de consulta y utilidades ---
void printBindings(InputDevice device = InputDevice::KEYBOARD, int controller_index = 0) const; // Muestra por consola los controles asignados
[[nodiscard]] auto getControllerBinding(int controller_index, InputAction input) const -> SDL_GamepadButton; // Obtiene el SDL_GamepadButton asignado a un input
[[nodiscard]] static auto inputToString(InputAction input) -> std::string; // Convierte un InputAction a std::string
[[nodiscard]] static auto stringToInput(const std::string &name) -> InputAction; // Convierte un std::string a InputAction
[[nodiscard]] auto getIndexByName(const std::string &name) const -> int; // Obtiene el índice a partir del nombre del mando
void printBindings(Device device = Device::KEYBOARD, int controller_index = 0) const; // Muestra por consola los controles asignados
[[nodiscard]] auto getControllerBinding(int controller_index, Action input) const -> SDL_GamepadButton; // Obtiene el SDL_GamepadButton asignado a un input
[[nodiscard]] static auto inputToString(Action input) -> std::string; // Convierte un InputAction a std::string
[[nodiscard]] static auto stringToInput(const std::string &name) -> Action; // Convierte un std::string a InputAction
[[nodiscard]] auto getIndexByName(const std::string &name) const -> int; // Obtiene el índice a partir del nombre del mando
// --- Métodos de reseteo de estado de entrada ---
void resetInputStates(); // Pone todos los KeyBindings.active y ControllerBindings.active a false
// --- Eventos ---
void handleEvent(const SDL_Event &event); // Comprueba si se conecta algun mando
private:
// --- Estructuras internas ---
struct KeyBindings {
@@ -132,14 +136,14 @@ class Input {
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 asociados a los inputs predefinidos para cada mando
std::vector<std::string> controller_names_; // Vector con los nombres de los mandos
std::vector<InputAction> button_inputs_; // Inputs asignados al jugador y a botones, excluyendo direcciones
std::vector<Action> button_inputs_; // Inputs asignados al jugador y a botones, excluyendo direcciones
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
// --- Métodos internos ---
void initSDLGamePad(); // Inicializa SDL para la gestión de mandos
auto checkAxisInput(InputAction input, int controller_index, bool repeat) -> bool; // Comprueba el eje del mando
void initSDLGamePad(); // Inicializa SDL para la gestión de mandos
auto checkAxisInput(Action input, int controller_index, bool repeat) -> bool; // Comprueba el eje del mando
// --- Constructor y destructor ---
explicit Input(std::string game_controller_db_path); // Constructor privado

View File

@@ -189,7 +189,7 @@ void parseAndSetController(const std::string& var, const std::string& value) {
} else if (setting_key == "player") {
controller.player_id = std::clamp(std::stoi(value), 1, 2);
} else if (setting_key == "type") {
controller.type = static_cast<InputDevice>(std::stoi(value));
controller.type = static_cast<Input::Device>(std::stoi(value));
} else if (setting_key == "button.fire_left") {
controller.buttons.at(0) = static_cast<SDL_GamepadButton>(std::stoi(value));
} else if (setting_key == "button.fire_center") {
@@ -272,9 +272,9 @@ auto set(const std::string& var, const std::string& value) -> bool {
void setKeyboardToPlayer(int player_id) {
for (auto& controller : controllers) {
if (controller.player_id == player_id) {
controller.type = InputDevice::ANY;
controller.type = Input::Device::ANY;
} else {
controller.type = InputDevice::CONTROLLER;
controller.type = Input::Device::CONTROLLER;
}
}
}
@@ -293,7 +293,7 @@ void swapControllers() {
// Averigua quien está usando el teclado
auto getPlayerWhoUsesKeyboard() -> int {
for (const auto& controller : controllers) {
if (controller.type == InputDevice::ANY) {
if (controller.type == Input::Device::ANY) {
return controller.player_id;
}
}

View File

@@ -90,24 +90,24 @@ struct SettingsOptions {
// --- Opciones de mando ---
struct GamepadOptions {
int index; // Índice en el vector de mandos
int player_id; // Jugador asociado al mando
InputDevice type{InputDevice::CONTROLLER}; // Indica si se usará teclado, mando o ambos
std::string name; // Nombre del dispositivo
bool plugged{false}; // Indica si el mando está conectado
std::vector<InputAction> inputs; // Listado de acciones asignadas
std::vector<SDL_GamepadButton> buttons; // Listado de botones asignados a cada acción
int index; // Índice en el vector de mandos
int player_id; // Jugador asociado al mando
Input::Device type{Input::Device::CONTROLLER}; // Indica si se usará teclado, mando o ambos
std::string name; // Nombre del dispositivo
bool plugged{false}; // Indica si el mando está conectado
std::vector<Input::Action> inputs; // Listado de acciones asignadas
std::vector<SDL_GamepadButton> buttons; // Listado de botones asignados a cada acción
// Constructor por defecto
GamepadOptions()
: index(INVALID_INDEX),
player_id(INVALID_INDEX),
inputs{
InputAction::FIRE_LEFT,
InputAction::FIRE_CENTER,
InputAction::FIRE_RIGHT,
InputAction::START,
InputAction::SERVICE},
Input::Action::FIRE_LEFT,
Input::Action::FIRE_CENTER,
Input::Action::FIRE_RIGHT,
Input::Action::START,
Input::Action::SERVICE},
buttons{
SDL_GAMEPAD_BUTTON_WEST,
SDL_GAMEPAD_BUTTON_NORTH,

View File

@@ -66,19 +66,19 @@ void Player::init() {
shiftSprite();
// Selecciona un frame para pintar
//player_sprite_->setCurrentAnimation("stand");
// player_sprite_->setCurrentAnimation("stand");
}
// Actua en consecuencia de la entrada recibida
void Player::setInput(InputAction input) {
void Player::setInput(Input::Action action) {
switch (playing_state_) {
case State::PLAYING: {
setInputPlaying(input);
setInputPlaying(action);
break;
}
case State::ENTERING_NAME:
case State::ENTERING_NAME_GAME_COMPLETED: {
setInputEnteringName(input);
setInputEnteringName(action);
break;
}
default:
@@ -87,27 +87,27 @@ void Player::setInput(InputAction input) {
}
// Procesa inputs para cuando está jugando
void Player::setInputPlaying(InputAction input) {
switch (input) {
case InputAction::LEFT: {
void Player::setInputPlaying(Input::Action action) {
switch (action) {
case Input::Action::LEFT: {
vel_x_ = -BASE_SPEED;
setWalkingState(State::WALKING_LEFT);
break;
}
case InputAction::RIGHT: {
case Input::Action::RIGHT: {
vel_x_ = BASE_SPEED;
setWalkingState(State::WALKING_RIGHT);
break;
}
case InputAction::FIRE_CENTER: {
case Input::Action::FIRE_CENTER: {
setFiringState(State::FIRING_UP);
break;
}
case InputAction::FIRE_LEFT: {
case Input::Action::FIRE_LEFT: {
setFiringState(State::FIRING_LEFT);
break;
}
case InputAction::FIRE_RIGHT: {
case Input::Action::FIRE_RIGHT: {
setFiringState(State::FIRING_RIGHT);
break;
}
@@ -120,21 +120,21 @@ void Player::setInputPlaying(InputAction input) {
}
// Procesa inputs para cuando está introduciendo el nombre
void Player::setInputEnteringName(InputAction input) {
switch (input) {
case InputAction::LEFT:
void Player::setInputEnteringName(Input::Action action) {
switch (action) {
case Input::Action::LEFT:
enter_name_->decPosition();
break;
case InputAction::RIGHT:
case Input::Action::RIGHT:
enter_name_->incPosition();
break;
case InputAction::UP:
case Input::Action::UP:
enter_name_->incIndex();
break;
case InputAction::DOWN:
case Input::Action::DOWN:
enter_name_->decIndex();
break;
case InputAction::START:
case Input::Action::START:
last_enter_name_ = getRecordName();
break;
default:
@@ -288,7 +288,7 @@ void Player::handleEnteringScreen() {
}
void Player::handlePlayer1Entering() {
setInputPlaying(InputAction::RIGHT);
setInputPlaying(Input::Action::RIGHT);
pos_x_ += vel_x_;
if (pos_x_ > default_pos_x_) {
pos_x_ = default_pos_x_;
@@ -297,7 +297,7 @@ void Player::handlePlayer1Entering() {
}
void Player::handlePlayer2Entering() {
setInputPlaying(InputAction::LEFT);
setInputPlaying(Input::Action::LEFT);
pos_x_ += vel_x_;
if (pos_x_ < default_pos_x_) {
pos_x_ = default_pos_x_;
@@ -351,10 +351,10 @@ void Player::updateWalkingStateForCredits() {
void Player::setInputBasedOnPlayerId() {
switch (id_) {
case 1:
setInputPlaying(InputAction::LEFT);
setInputPlaying(Input::Action::LEFT);
break;
case 2:
setInputPlaying(InputAction::RIGHT);
setInputPlaying(Input::Action::RIGHT);
break;
default:
break;

View File

@@ -14,7 +14,7 @@
#include "utils.h" // Para Circle
class Texture;
enum class InputAction : int;
enum class Action : int;
enum class Mode;
// --- Clase Player ---
@@ -77,9 +77,9 @@ class Player {
void render(); // Dibuja el jugador en pantalla
// --- Entrada y control ---
void setInput(InputAction input); // Procesa entrada general
void setInputPlaying(InputAction input); // Procesa entrada en modo jugando
void setInputEnteringName(InputAction input); // Procesa entrada al introducir nombre
void setInput(Input::Action action); // Procesa entrada general
void setInputPlaying(Input::Action action); // Procesa entrada en modo jugando
void setInputEnteringName(Input::Action action); // Procesa entrada al introducir nombre
// --- Movimiento y animación ---
void move(); // Mueve el jugador

View File

@@ -16,7 +16,7 @@
#include "fade.h" // Para Fade, FadeType, FadeMode
#include "global_events.h" // Para check
#include "global_inputs.h" // Para check
#include "input.h" // Para Input, INPUT_ALLOW_REPEAT
#include "input.h" // Para Input, Input::ALLOW_REPEAT
#include "lang.h" // Para getText
#include "param.h" // Para Param, param, ParamGame, ParamFade
#include "player.h" // Para Player, PlayerState
@@ -131,7 +131,7 @@ void Credits::checkInput() {
if (!ServiceMenu::get()->isEnabled()) {
// Comprueba si se ha pulsado cualquier botón (de los usados para jugar)
if (Input::get()->checkAnyButton(INPUT_ALLOW_REPEAT) != 0) {
if (Input::get()->checkAnyButton(Input::ALLOW_REPEAT) != 0) {
want_to_pass_ = true;
fading_ = mini_logo_on_position_;
} else {

View File

@@ -22,7 +22,7 @@
#include "global_events.h" // Para check
#include "global_inputs.h" // Para check
#include "hit.h" // Para Hit
#include "input.h" // Para InputAction, Input, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_ALLOW_REPEAT, InputDevice
#include "input.h" // Para InputAction, Input, Input::DO_NOT_ALLOW_REPEAT, Input::ALLOW_REPEAT, InputDevice
#include "item.h" // Para Item, ItemType
#include "lang.h" // Para getText
#include "manage_hiscore_table.h" // Para ManageHiScoreTable, HiScoreEntry
@@ -1244,14 +1244,14 @@ void Game::checkInput() {
void Game::checkPauseInput() {
// Comprueba los mandos
for (int i = 0; i < input_->getNumControllers(); ++i) {
if (input_->checkInput(InputAction::PAUSE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::CONTROLLER, i)) {
if (input_->checkInput(Input::Action::PAUSE, Input::DO_NOT_ALLOW_REPEAT, Input::Device::CONTROLLER, i)) {
pause(!paused_);
return;
}
}
// Comprueba el teclado
if (input_->checkInput(InputAction::PAUSE, INPUT_DO_NOT_ALLOW_REPEAT, InputDevice::KEYBOARD)) {
if (input_->checkInput(Input::Action::PAUSE, Input::DO_NOT_ALLOW_REPEAT, Input::Device::KEYBOARD)) {
pause(!paused_);
return;
}
@@ -1283,11 +1283,11 @@ void Game::demoHandlePlayerInput(const std::shared_ptr<Player> &player, int inde
const auto &demo_data = demo_.data[index][demo_.counter];
if (demo_data.left == 1) {
player->setInput(InputAction::LEFT);
player->setInput(Input::Action::LEFT);
} else if (demo_data.right == 1) {
player->setInput(InputAction::RIGHT);
player->setInput(Input::Action::RIGHT);
} else if (demo_data.no_input == 1) {
player->setInput(InputAction::NONE);
player->setInput(Input::Action::NONE);
}
if (demo_data.fire == 1) {
@@ -1305,17 +1305,17 @@ void Game::handleFireInput(const std::shared_ptr<Player> &player, BulletType bul
SDL_Point bullet = {0, 0};
switch (bullet_type) {
case BulletType::UP:
player->setInput(InputAction::FIRE_CENTER);
player->setInput(Input::Action::FIRE_CENTER);
bullet.x = 2 + player->getPosX() + (player->getWidth() - Bullet::WIDTH) / 2;
bullet.y = player->getPosY() - (Bullet::HEIGHT / 2);
break;
case BulletType::LEFT:
player->setInput(InputAction::FIRE_LEFT);
player->setInput(Input::Action::FIRE_LEFT);
bullet.x = player->getPosX() - (Bullet::WIDTH / 2);
bullet.y = player->getPosY();
break;
case BulletType::RIGHT:
player->setInput(InputAction::FIRE_RIGHT);
player->setInput(Input::Action::FIRE_RIGHT);
bullet.x = player->getPosX() + player->getWidth() - (Bullet::WIDTH / 2);
bullet.y = player->getPosY();
break;
@@ -1362,18 +1362,18 @@ void Game::handlePlayersInput() {
void Game::handleNormalPlayerInput(const std::shared_ptr<Player> &player) {
const auto &controller = Options::controllers.at(player->getController());
if (input_->checkInput(InputAction::LEFT, INPUT_ALLOW_REPEAT, controller.type, controller.index)) {
player->setInput(InputAction::LEFT);
if (input_->checkInput(Input::Action::LEFT, Input::ALLOW_REPEAT, controller.type, controller.index)) {
player->setInput(Input::Action::LEFT);
#ifdef RECORDING
demo_.keys.left = 1;
#endif
} else if (input_->checkInput(InputAction::RIGHT, INPUT_ALLOW_REPEAT, controller.type, controller.index)) {
player->setInput(InputAction::RIGHT);
} else if (input_->checkInput(Input::Action::RIGHT, Input::ALLOW_REPEAT, controller.type, controller.index)) {
player->setInput(Input::Action::RIGHT);
#ifdef RECORDING
demo_.keys.right = 1;
#endif
} else {
player->setInput(InputAction::NONE);
player->setInput(Input::Action::NONE);
#ifdef RECORDING
demo_.keys.no_input = 1;
#endif
@@ -1386,17 +1386,17 @@ void Game::handleNormalPlayerInput(const std::shared_ptr<Player> &player) {
// Procesa las entradas de disparo del jugador, permitiendo disparos automáticos si está habilitado.
void Game::handleFireInputs(const std::shared_ptr<Player> &player, bool autofire, int controller_index) {
const auto CONTROLLER = Options::controllers.at(player->getController());
if (input_->checkInput(InputAction::FIRE_CENTER, autofire, CONTROLLER.type, CONTROLLER.index)) {
if (input_->checkInput(Input::Action::FIRE_CENTER, autofire, CONTROLLER.type, CONTROLLER.index)) {
handleFireInput(player, BulletType::UP);
#ifdef RECORDING
demo_.keys.fire = 1;
#endif
} else if (input_->checkInput(InputAction::FIRE_LEFT, autofire, CONTROLLER.type, CONTROLLER.index)) {
} else if (input_->checkInput(Input::Action::FIRE_LEFT, autofire, CONTROLLER.type, CONTROLLER.index)) {
handleFireInput(player, BulletType::LEFT);
#ifdef RECORDING
demo_.keys.fire_left = 1;
#endif
} else if (input_->checkInput(InputAction::FIRE_RIGHT, autofire, CONTROLLER.type, CONTROLLER.index)) {
} else if (input_->checkInput(Input::Action::FIRE_RIGHT, autofire, CONTROLLER.type, CONTROLLER.index)) {
handleFireInput(player, BulletType::RIGHT);
#ifdef RECORDING
demo_.keys.fire_right = 1;
@@ -1407,16 +1407,16 @@ void Game::handleFireInputs(const std::shared_ptr<Player> &player, bool autofire
// Maneja la continuación del jugador cuando no está jugando, permitiendo que continúe si se pulsa el botón de inicio.
void Game::handlePlayerContinueInput(const std::shared_ptr<Player> &player) {
const auto CONTROLLER = Options::controllers.at(player->getController());
if (input_->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
if (input_->checkInput(Input::Action::START, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
player->setPlayingState(Player::State::RESPAWNING);
player->addCredit();
sendPlayerToTheFront(player);
}
// Disminuye el contador de continuación si se presiona cualquier botón de disparo.
if (input_->checkInput(InputAction::FIRE_LEFT, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index) ||
input_->checkInput(InputAction::FIRE_CENTER, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index) ||
input_->checkInput(InputAction::FIRE_RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
if (input_->checkInput(Input::Action::FIRE_LEFT, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index) ||
input_->checkInput(Input::Action::FIRE_CENTER, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index) ||
input_->checkInput(Input::Action::FIRE_RIGHT, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
if (player->getContinueCounter() < param.scoreboard.skip_countdown_value) {
player->decContinueCounter();
}
@@ -1426,7 +1426,7 @@ void Game::handlePlayerContinueInput(const std::shared_ptr<Player> &player) {
// Maneja la continuación del jugador cuando no está jugando, permitiendo que continúe si se pulsa el botón de inicio.
void Game::handlePlayerWaitingInput(const std::shared_ptr<Player> &player) {
const auto CONTROLLER = Options::controllers.at(player->getController());
if (input_->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
if (input_->checkInput(Input::Action::START, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
player->setPlayingState(Player::State::ENTERING_SCREEN);
player->addCredit();
sendPlayerToTheFront(player);
@@ -1436,32 +1436,32 @@ void Game::handlePlayerWaitingInput(const std::shared_ptr<Player> &player) {
// Procesa las entradas para la introducción del nombre del jugador.
void Game::handleNameInput(const std::shared_ptr<Player> &player) {
const auto CONTROLLER = Options::controllers.at(player->getController());
if (input_->checkInput(InputAction::FIRE_LEFT, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
if (input_->checkInput(Input::Action::FIRE_LEFT, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
if (player->isShowingName()) {
player->setPlayingState(Player::State::CONTINUE);
} else if (player->getEnterNamePositionOverflow()) {
player->setInput(InputAction::START);
player->setInput(Input::Action::START);
addScoreToScoreBoard(player);
player->setPlayingState(Player::State::SHOWING_NAME);
} else {
player->setInput(InputAction::RIGHT);
player->setInput(Input::Action::RIGHT);
}
} else if (input_->checkInput(InputAction::FIRE_CENTER, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index) ||
input_->checkInput(InputAction::FIRE_RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
} else if (input_->checkInput(Input::Action::FIRE_CENTER, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index) ||
input_->checkInput(Input::Action::FIRE_RIGHT, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
if (player->isShowingName()) {
player->setPlayingState(Player::State::CONTINUE);
} else {
player->setInput(InputAction::LEFT);
player->setInput(Input::Action::LEFT);
}
} else if (input_->checkInput(InputAction::UP, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
player->setInput(InputAction::UP);
} else if (input_->checkInput(InputAction::DOWN, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
player->setInput(InputAction::DOWN);
} else if (input_->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
} else if (input_->checkInput(Input::Action::UP, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
player->setInput(Input::Action::UP);
} else if (input_->checkInput(Input::Action::DOWN, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
player->setInput(Input::Action::DOWN);
} else if (input_->checkInput(Input::Action::START, Input::DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index)) {
if (player->isShowingName()) {
player->setPlayingState(Player::State::CONTINUE);
} else {
player->setInput(InputAction::START);
player->setInput(Input::Action::START);
addScoreToScoreBoard(player);
player->setPlayingState(Player::State::SHOWING_NAME);
}
@@ -1882,7 +1882,7 @@ void Game::checkDebugEvents(const SDL_Event &event) {
case SDLK_1: // Crea una powerball
{
// balloon_manager_->createPowerBall();
//throwCoffee(players_.at(0)->getPosX() + (players_.at(0)->getWidth() / 2), players_.at(0)->getPosY() + (players_.at(0)->getHeight() / 2));
// throwCoffee(players_.at(0)->getPosX() + (players_.at(0)->getWidth() / 2), players_.at(0)->getPosY() + (players_.at(0)->getHeight() / 2));
break;
}
case SDLK_2: // Activa o desactiva la aparición de globos

View File

@@ -15,7 +15,7 @@
#include "game_logo.h" // Para GameLogo
#include "global_events.h" // Para check
#include "global_inputs.h" // Para check
#include "input.h" // Para Input, INPUT_DO_NOT_ALLOW_REPEAT, Input...
#include "input.h" // Para Input, Input::DO_NOT_ALLOW_REPEAT, Input...
#include "lang.h" // Para getText
#include "options.h" // Para GamepadOptions, controllers, getPlayerW...
#include "param.h" // Para Param, param, ParamGame, ParamTitle
@@ -271,8 +271,8 @@ void Title::processControllerInputs() {
auto Title::isStartButtonPressed(const Options::GamepadOptions& controller) -> bool {
return Input::get()->checkInput(
InputAction::START,
INPUT_DO_NOT_ALLOW_REPEAT,
Input::Action::START,
Input::DO_NOT_ALLOW_REPEAT,
controller.type,
controller.index);
}