- La clase input ya admite inputs personalizados

- El juego ya utiliza el objeto input para comprobar las teclas de cambio de tamaño de ventana, pausa, etc.
This commit is contained in:
2023-02-10 18:28:53 +01:00
parent 5007bea835
commit 9c6924d7bb
6 changed files with 137 additions and 80 deletions

View File

@@ -11,12 +11,12 @@ Input::Input(std::string file)
keyBindings_t kb;
kb.scancode = 0;
kb.active = false;
keyBindings.resize(17, kb);
keyBindings.resize(input_number_of_inputs, kb);
GameControllerBindings_t gcb;
gcb.button = SDL_CONTROLLER_BUTTON_INVALID;
gcb.active = false;
gameControllerBindings.resize(17, gcb);
gameControllerBindings.resize(input_number_of_inputs, gcb);
verbose = true;
enabled = true;

View File

@@ -25,6 +25,35 @@
#define INPUT_BUTTON_PAUSE 15
#define INPUT_BUTTON_ESCAPE 16
enum inputs_e
{
// Inputs obligatorios
input_null,
input_up,
input_down,
input_left,
input_right,
input_pause,
input_exit,
input_accept,
input_cancel,
// Inputs personalizados
input_jump,
input_switch_music,
input_video_mode,
input_swap_palette,
input_window_size_1,
input_window_size_2,
input_window_size_3,
input_window_size_4,
input_change_windows_size,
input_toggle_border,
// Input obligatorio
input_number_of_inputs
};
#define REPEAT_TRUE true
#define REPEAT_FALSE false
@@ -81,7 +110,7 @@ public:
void bindGameControllerButton(Uint8 input, SDL_GameControllerButton button);
// Comprueba si un input esta activo
bool checkInput(Uint8 input, bool repeat, int device = INPUT_USE_ANY, int index = 0);
bool checkInput(Uint8 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);

View File

@@ -1209,43 +1209,51 @@ void Director::initInput()
// Busca si hay un mando conectado
input->discoverGameController();
// Asigna inputs a teclas
// Teclado - Movimiento
if (options->keys == ctrl_cursor)
{
input->bindKey(INPUT_UP, SDL_SCANCODE_UP);
input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
input->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT);
input->bindKey(input_jump, SDL_SCANCODE_UP);
input->bindKey(input_left, SDL_SCANCODE_LEFT);
input->bindKey(input_right, SDL_SCANCODE_RIGHT);
}
else if (options->keys == ctrl_opqa)
{
input->bindKey(INPUT_UP, SDL_SCANCODE_Q);
input->bindKey(INPUT_LEFT, SDL_SCANCODE_O);
input->bindKey(INPUT_RIGHT, SDL_SCANCODE_P);
input->bindKey(input_jump, SDL_SCANCODE_Q);
input->bindKey(input_left, SDL_SCANCODE_O);
input->bindKey(input_right, SDL_SCANCODE_P);
}
else if (options->keys == ctrl_wasd)
{
input->bindKey(INPUT_UP, SDL_SCANCODE_W);
input->bindKey(INPUT_LEFT, SDL_SCANCODE_A);
input->bindKey(INPUT_RIGHT, SDL_SCANCODE_D);
input->bindKey(input_jump, SDL_SCANCODE_W);
input->bindKey(input_left, SDL_SCANCODE_A);
input->bindKey(input_right, SDL_SCANCODE_D);
}
input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
input->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN);
input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE);
input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D);
input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE);
input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE);
// Teclado - Otros
input->bindKey(input_accept, SDL_SCANCODE_RETURN);
input->bindKey(input_cancel, SDL_SCANCODE_ESCAPE);
input->bindKey(input_pause, SDL_SCANCODE_H);
input->bindKey(input_exit, SDL_SCANCODE_ESCAPE);
input->bindKey(input_window_size_1, SDL_SCANCODE_F1);
input->bindKey(input_window_size_2, SDL_SCANCODE_F2);
input->bindKey(input_window_size_3, SDL_SCANCODE_F3);
input->bindKey(input_window_size_4, SDL_SCANCODE_F4);
input->bindKey(input_swap_palette, SDL_SCANCODE_F5);
input->bindKey(input_switch_music, SDL_SCANCODE_M);
input->bindKey(input_video_mode, SDL_SCANCODE_F);
input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_B);
input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
input->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
input->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B);
input->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A);
input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_B);
input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_START);
input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE);
// Mando - Movimiento
input->bindGameControllerButton(input_jump, SDL_CONTROLLER_BUTTON_B);
input->bindGameControllerButton(input_left, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
input->bindGameControllerButton(input_right, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
// Mando - Otros
input->bindGameControllerButton(input_accept, SDL_CONTROLLER_BUTTON_B);
input->bindGameControllerButton(input_cancel, SDL_CONTROLLER_BUTTON_A);
input->bindGameControllerButton(input_pause, SDL_CONTROLLER_BUTTON_START);
input->bindGameControllerButton(input_exit, SDL_CONTROLLER_BUTTON_GUIDE);
input->bindGameControllerButton(input_swap_palette, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
input->bindGameControllerButton(input_switch_music, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
}
// Inicializa JailAudio
@@ -1368,7 +1376,7 @@ bool Director::setFileList()
// Notificaciones
asset->add(prefix + "/data/notifications/notify.png", t_bitmap);
// Habitaciones
asset->add(prefix + "/data/room/01.room", t_room);
asset->add(prefix + "/data/room/02.room", t_room);

View File

@@ -131,9 +131,6 @@ void Game::checkEventHandler()
{
switch (eventHandler->key.keysym.scancode)
{
case SDL_SCANCODE_ESCAPE:
section->name = SECTION_PROG_TITLE;
break;
#ifdef DEBUG
case SDL_SCANCODE_G:
debug->switchEnabled();
@@ -178,50 +175,6 @@ void Game::checkEventHandler()
screen->showNotification("JAILDESIGNER IS LOGGED IN", "", 5);
break;
#endif
case SDL_SCANCODE_M:
board.music = !board.music;
board.music ? JA_ResumeMusic() : JA_PauseMusic();
break;
case SDL_SCANCODE_H:
switchPause();
break;
case SDL_SCANCODE_B:
screen->switchBorder();
reLoadTextures();
break;
case SDL_SCANCODE_F:
screen->switchVideoMode();
reLoadTextures();
break;
case SDL_SCANCODE_F1:
screen->setWindowSize(1);
reLoadTextures();
break;
case SDL_SCANCODE_F2:
screen->setWindowSize(2);
reLoadTextures();
break;
case SDL_SCANCODE_F3:
screen->setWindowSize(3);
reLoadTextures();
break;
case SDL_SCANCODE_F4:
screen->setWindowSize(4);
reLoadTextures();
break;
case SDL_SCANCODE_F5:
switchPalette();
break;
default:
break;
}
@@ -229,6 +182,67 @@ void Game::checkEventHandler()
}
}
// Comprueba el teclado
void Game::checkInput()
{
if (input->checkInput(input_exit, REPEAT_FALSE))
{
section->name = SECTION_PROG_TITLE;
}
if (input->checkInput(input_switch_music, REPEAT_FALSE))
{
board.music = !board.music;
board.music ? JA_ResumeMusic() : JA_PauseMusic();
}
if (input->checkInput(input_pause, REPEAT_FALSE))
{
switchPause();
}
if (input->checkInput(input_toggle_border, REPEAT_FALSE))
{
screen->switchBorder();
reLoadTextures();
}
if (input->checkInput(input_video_mode, REPEAT_FALSE))
{
screen->switchVideoMode();
reLoadTextures();
}
if (input->checkInput(input_window_size_1, REPEAT_FALSE))
{
screen->setWindowSize(1);
reLoadTextures();
}
if (input->checkInput(input_window_size_2, REPEAT_FALSE))
{
screen->setWindowSize(2);
reLoadTextures();
}
if (input->checkInput(input_window_size_3, REPEAT_FALSE))
{
screen->setWindowSize(3);
reLoadTextures();
}
if (input->checkInput(input_window_size_4, REPEAT_FALSE))
{
screen->setWindowSize(4);
reLoadTextures();
}
if (input->checkInput(input_swap_palette, REPEAT_FALSE))
{
switchPalette();
}
}
// Bucle para el juego
void Game::run()
{
@@ -259,6 +273,9 @@ void Game::update()
// Comprueba los eventos de la cola
checkEventHandler();
// Comprueba el teclado
checkInput();
#ifdef DEBUG
debug->clear();
#endif

View File

@@ -83,6 +83,9 @@ private:
// Cambia de habitación
bool changeRoom(std::string file);
// Comprueba el teclado
void checkInput();
// Comprueba si el jugador esta en el borde de la pantalla y actua
void checkPlayerOnBorder();

View File

@@ -181,13 +181,13 @@ void Player::checkInput()
if (!autoMovement)
{ // Comprueba las entradas de desplazamiento lateral solo en el caso de no estar enganchado a una superficie automatica
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
if (input->checkInput(input_left))
{
vx = -0.6f;
sprite->setFlipH(true);
}
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
else if (input->checkInput(input_right))
{
vx = 0.6f;
sprite->setFlipH(false);
@@ -216,7 +216,7 @@ void Player::checkInput()
}
}
if (input->checkInput(INPUT_UP, REPEAT_TRUE))
if (input->checkInput(input_jump))
{
// Solo puede saltar si ademas de estar (state == s_standing)
// Esta sobre el suelo, rampa o suelo que se mueve