From c8cd375d818cbed2cad737e7a718d501357643cb Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 6 Jul 2024 13:25:15 +0200 Subject: [PATCH] empezando a trabajar en la clase DefineButtons --- source/define_buttons.cpp | 172 ++++++++++++++++++++++++++++++++++++++ source/define_buttons.h | 64 ++++++++++++++ source/title.cpp | 82 +++++++++++++----- source/title.h | 26 +++--- 4 files changed, 309 insertions(+), 35 deletions(-) create mode 100644 source/define_buttons.cpp create mode 100644 source/define_buttons.h diff --git a/source/define_buttons.cpp b/source/define_buttons.cpp new file mode 100644 index 0000000..0296ded --- /dev/null +++ b/source/define_buttons.cpp @@ -0,0 +1,172 @@ +#include "define_buttons.h" + +// Constructor +DefineButtons::DefineButtons(SDL_Renderer *renderer, Input *input, Text *text, param_t *param, options_t *options) +{ + // Copia punteros a los objetos + this->renderer = renderer; + this->input = input; + this->text = text; + this->param = param; + this->options = options; + + // Inicializa variables + enabled = false; + x = param->gameWidth / 2; + y = param->pressStart; + indexController = 0; + indexButton = 0; + + buttons.clear(); + db_button_t button; + + button.label = "DISPARO IZQUIERDA"; + button.input = input_fire_left; + button.button = SDL_CONTROLLER_BUTTON_X; + buttons.push_back(button); + + button.label = "DISPARO CENTRO"; + button.input = input_fire_center; + button.button = SDL_CONTROLLER_BUTTON_Y; + buttons.push_back(button); + + button.label = "DISPARO DERECHA"; + button.input = input_fire_right; + button.button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER; + buttons.push_back(button); + + button.label = "START"; + button.input = input_start; + button.button = SDL_CONTROLLER_BUTTON_START; + buttons.push_back(button); + + button.label = "QUIT"; + button.input = input_exit; + button.button = SDL_CONTROLLER_BUTTON_BACK; + buttons.push_back(button); +} + +// Destructor +DefineButtons::~DefineButtons() +{ +} + +// Actualiza las variables del objeto +void DefineButtons::update() +{ + if (enabled) + { + } +} + +// Dibuja el objeto en pantalla +void DefineButtons::render() +{ + if (enabled) + { + text->writeCentered(x, y - 10, "PLAYER " + std::to_string(indexController + 1)); + text->writeCentered(x, y, buttons[indexButton].label); + } +} + +int getJoyIndex(int id); + +void DefineButtons::doJoystickButtonDown(SDL_JoyButtonEvent *event) +{ + //int i = getJoyIndex(event->which); + + buttons[indexButton].button = (SDL_GameControllerButton)event->button; + incIndexButton(); +} + +// Comprueba las entradas +void DefineButtons::checkInput() +{ + if (enabled) + { + SDL_Event event; + + // Comprueba los eventos que hay en la cola + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_JOYBUTTONDOWN) + doJoystickButtonDown(&event.jbutton); + + /*if (eventHandler->type == SDL_JOYBUTTONDOWN) + { + if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_A) + { + buttons[indexButton].button = SDL_CONTROLLER_BUTTON_A; + incIndexButton(); + } + + if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_B) + { + buttons[indexButton].button = SDL_CONTROLLER_BUTTON_B; + incIndexButton(); + } + + if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_X) + { + buttons[indexButton].button = SDL_CONTROLLER_BUTTON_X; + incIndexButton(); + } + + if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_Y) + { + buttons[indexButton].button = SDL_CONTROLLER_BUTTON_Y; + incIndexButton(); + } + + if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_BACK) + { + buttons[indexButton].button = SDL_CONTROLLER_BUTTON_BACK; + incIndexButton(); + } + + if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_START) + { + buttons[indexButton].button = SDL_CONTROLLER_BUTTON_START; + incIndexButton(); + } + + if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER) + { + buttons[indexButton].button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER; + incIndexButton(); + } + + if (eventHandler->jbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) + { + buttons[indexButton].button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER; + incIndexButton(); + } + }*/ + } + } +} + +// Habilita el objeto +void DefineButtons::enable(int index) +{ + enabled = true; + indexController = index - 1; + indexButton = 0; +} + +// Comprueba si está habilitado +bool DefineButtons::isEnabled() +{ + return enabled; +} + +// Incrementa el indice de los botones +void DefineButtons::incIndexButton() +{ + indexButton++; + + if (indexButton == (int)buttons.size()) + { + enabled = false; + } +} \ No newline at end of file diff --git a/source/define_buttons.h b/source/define_buttons.h new file mode 100644 index 0000000..838f9cb --- /dev/null +++ b/source/define_buttons.h @@ -0,0 +1,64 @@ +#pragma once + +#include +#include "common/input.h" +#include "common/text.h" + +#ifndef DEFINE_BUTTONS_H +#define DEFINE_BUTTONS_H + +struct db_button_t +{ + std::string label; // Texto en pantalla para el botón + inputs_e input; // Input asociado + SDL_GameControllerButton button; // Botón del mando correspondiente +}; + +// Clase Bullet +class DefineButtons +{ +private: + // Objetos + SDL_Renderer *renderer; // El renderizador de la ventana + Input *input; // Objeto pata gestionar la entrada + Text *text; // Objeto para escribir texto + + // Variables + options_t *options; // Variable con todas las variables de las opciones del programa + param_t *param; // Puntero con todos los parametros del programa + bool enabled; // Indica si el objeto está habilitado + int x; // Posición donde dibujar el texto + int y; // Posición donde dibujar el texto + std::vector buttons; // Vector con las nuevas definiciones de botones/acciones + int indexController; // Indice del controlador a reasignar + int indexButton; // Indice para saber qué bot´çon se está definiendo + + // Incrementa el indice de los botones + void incIndexButton(); + + void doJoystickButtonDown(SDL_JoyButtonEvent *event); + +public: + // Constructor + DefineButtons(SDL_Renderer *renderer, Input *input, Text *text, param_t *param, options_t *options); + + // Destructor + ~DefineButtons(); + + // Actualiza las variables del objeto + void update(); + + // Dibuja el objeto en pantalla + void render(); + + // Comprueba las entradas + void checkInput(); + + // Habilita el objeto + void enable(int index); + + // Comprueba si está habilitado + bool isEnabled(); +}; + +#endif \ No newline at end of file diff --git a/source/title.cpp b/source/title.cpp index 944c06f..e42311e 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -36,6 +36,8 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, gameLogo = new GameLogo(renderer, screen, asset, param, GAMECANVAS_CENTER_X, GAMECANVAS_FIRST_QUARTER_Y + 20); gameLogo->enable(); + defineButtons = new DefineButtons(renderer, input, text2, param, options); + // Inicializa los valores init(); } @@ -57,6 +59,7 @@ Title::~Title() delete background; delete tiledbg; delete gameLogo; + delete defineButtons; } // Inicializa los valores de las variables @@ -88,6 +91,8 @@ void Title::update() // Actualiza el objeto 'background' background->update(); + defineButtons->update(); + // Comprueba el fade y si se ha acabado fade->update(); if (fade->hasEnded()) @@ -130,7 +135,11 @@ void Title::update() { if (counter < param->titleCounter) { - counter++; + // El contador solo sube si no estamos definiendo botones + if (!defineButtons->isEnabled()) + { + counter++; + } // Reproduce la música if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) @@ -165,13 +174,13 @@ void Title::render() tiledbg->render(); // background->render(); - // Dinuja el logo con el título del juego + // Dibuja el logo con el título del juego gameLogo->render(); if (section->subsection == SUBSECTION_TITLE_2) { // 'PULSA 1P o 2P PARA JUGAR' - if (counter % 50 > 14) + if (counter % 50 > 14 && !defineButtons->isEnabled()) { text1->writeDX(TXT_CENTER | TXT_SHADOW, GAMECANVAS_CENTER_X, param->pressStart, lang->getText(23), 1, noColor, 1, shdwTxtColor); } @@ -186,6 +195,8 @@ void Title::render() text1->writeDX(TXT_CENTER | TXT_SHADOW, GAMECANVAS_CENTER_X, pos2, TEXT_COPYRIGHT, 1, noColor, 1, shdwTxtColor); } + defineButtons->render(); + // Fade fade->render(); @@ -196,19 +207,23 @@ void Title::render() // Comprueba los eventos void Title::checkEvents() { - // Comprueba los eventos que hay en la cola - while (SDL_PollEvent(eventHandler) != 0) - { - // Evento de salida de la aplicación - if (eventHandler->type == SDL_QUIT) + // Si defineButtons está habilitado, es él quien gestiona los eventos + if (!defineButtons->isEnabled()) + { + // Comprueba los eventos que hay en la cola + while (SDL_PollEvent(eventHandler) != 0) { - section->name = SECTION_PROG_QUIT; - break; - } + // Evento de salida de la aplicación + if (eventHandler->type == SDL_QUIT) + { + section->name = SECTION_PROG_QUIT; + break; + } - else if (eventHandler->type == SDL_RENDER_DEVICE_RESET || eventHandler->type == SDL_RENDER_TARGETS_RESET) - { - reLoadTextures(); + else if (eventHandler->type == SDL_RENDER_DEVICE_RESET || eventHandler->type == SDL_RENDER_TARGETS_RESET) + { + reLoadTextures(); + } } } } @@ -216,12 +231,24 @@ void Title::checkEvents() // Comprueba las entradas void Title::checkInput() { - // Comprueba todos los controladores para salir - for (int i = 0; i < numControllers; ++i) + // Comprueba los controladores solo si no se estan definiendo los botones + if (!defineButtons->isEnabled()) { - if (input->checkInput(input_exit, REPEAT_FALSE, options->game.input[i].deviceType, options->game.input[i].id)) + // Comprueba todos los controladores para salir + for (int i = 0; i < numControllers; ++i) { - section->name = SECTION_PROG_QUIT; + if (input->checkInput(input_exit, REPEAT_FALSE, options->game.input[i].deviceType, options->game.input[i].id)) + { + section->name = SECTION_PROG_QUIT; + } + } + + // Comprueba si se ha pulsado algún botón para empezar a jugar + const int index = input->checkAnyButtonPressed(); + if (index) + { + fade->activate(); + postFade = index; } } @@ -231,16 +258,25 @@ void Title::checkInput() section->name = SECTION_PROG_QUIT; } - // Comprueba si se ha pulsado algún botón para empezar a jugar - const int index = input->checkAnyButtonPressed(); - if (index) + // Comprueba si se ha pulsado la tecla 1 o 2 para definir los controladores + if (!defineButtons->isEnabled()) { - fade->activate(); - postFade = index; + const Uint8 *keyStates = SDL_GetKeyboardState(nullptr); + + if (keyStates[SDL_SCANCODE_1] != 0) + { + defineButtons->enable(1); + } + + else if (keyStates[SDL_SCANCODE_2] != 0) + { + defineButtons->enable(2); + } } // Comprueba el input para el resto de objetos screen->checkInput(); + defineButtons->checkInput(); } // Bucle para el titulo del juego diff --git a/source/title.h b/source/title.h index 3ceaf68..efbf29f 100644 --- a/source/title.h +++ b/source/title.h @@ -20,6 +20,7 @@ #include "background.h" #include "tiledbg.h" #include "game_logo.h" +#include "define_buttons.h" #ifndef TITLE_H #define TITLE_H @@ -32,18 +33,19 @@ class Title { private: // Objetos y punteros - SDL_Renderer *renderer; // El renderizador de la ventana - Screen *screen; // Objeto encargado de dibujar en pantalla - Asset *asset; // Objeto que gestiona todos los ficheros de recursos - Input *input; // Objeto para leer las entradas de teclado o mando - Lang *lang; // Objeto para gestionar los textos en diferentes idiomas - SDL_Event *eventHandler; // Manejador de eventos - section_t *section; // Indicador para el bucle del titulo - Background *background; // Objeto para dibujar el fondo del juego - Tiledbg *tiledbg; // Objeto para dibujar el mosaico animado de fondo - GameLogo *gameLogo; // Objeto para dibujar el logo con el título del juego - Texture *miniLogoTexture; // Textura con el logo de JailGames mini - Sprite *miniLogoSprite; // Sprite con el logo de JailGames mini + SDL_Renderer *renderer; // El renderizador de la ventana + Screen *screen; // Objeto encargado de dibujar en pantalla + Asset *asset; // Objeto que gestiona todos los ficheros de recursos + Input *input; // Objeto para leer las entradas de teclado o mando + Lang *lang; // Objeto para gestionar los textos en diferentes idiomas + SDL_Event *eventHandler; // Manejador de eventos + section_t *section; // Indicador para el bucle del titulo + Background *background; // Objeto para dibujar el fondo del juego + Tiledbg *tiledbg; // Objeto para dibujar el mosaico animado de fondo + GameLogo *gameLogo; // Objeto para dibujar el logo con el título del juego + Texture *miniLogoTexture; // Textura con el logo de JailGames mini + Sprite *miniLogoSprite; // Sprite con el logo de JailGames mini + DefineButtons *defineButtons; // Objeto para definir los botones del joystic Text *text1; // Objeto de texto para poder escribir textos en pantalla Text *text2; // Objeto de texto para poder escribir textos en pantalla