From 52d0c182f3e67d44c2e838f6b9e495bd620ff778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Thu, 22 Sep 2022 22:22:27 +0200 Subject: [PATCH] Trabajando en el marcador --- source/game.cpp | 4 ++++ source/input.cpp | 39 ++++++++++++++++++++++++++++++++++++++- source/input.h | 9 ++++++--- source/scoreboard.cpp | 21 ++++++++++++++++++++- source/scoreboard.h | 12 ++++++++++++ 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/source/game.cpp b/source/game.cpp index bf39d44..248ed9f 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -89,6 +89,10 @@ void Game::update() // Comprueba los eventos de la cola checkEventHandler(); + // Comprueba si se ha pulsado alguna tecla + if (input->checkAnyInput()) + scoreboard->reset(); + board.diamonds = player->diamonds; // Actualiza los objetos diff --git a/source/input.cpp b/source/input.cpp index f79f641..912c309 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -130,6 +130,43 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) return (successKeyboard || successGameController); } +// Comprueba si hay almenos un input activo +bool Input::checkAnyInput(int device, int index) +{ + bool successKeyboard = false; + bool successGameController = false; + + if (device == INPUT_USE_ANY) + index = 0; + + if ((device == INPUT_USE_KEYBOARD) || (device == INPUT_USE_ANY)) + { + const Uint8 *mKeystates = SDL_GetKeyboardState(NULL); + + for (int i = 0; i < 17; i++) + { + if (mKeystates[keyBindings[i].scancode] != 0) + { + successKeyboard |= true; + } + } + } + + if (gameControllerFound()) + if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) + { + for (int i = 0; i < 17; i++) + { + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0) + { + successGameController |= true; + } + } + } + + return (successKeyboard || successGameController); +} + // Comprueba si hay un mando conectado bool Input::discoverGameController() { @@ -174,7 +211,7 @@ bool Input::discoverGameController() std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl; } - //mGameController = connectedControllers[0]; + // mGameController = connectedControllers[0]; SDL_GameControllerEventState(SDL_ENABLE); } diff --git a/source/input.h b/source/input.h index 9cc7af8..d459e49 100644 --- a/source/input.h +++ b/source/input.h @@ -51,9 +51,9 @@ private: GameControllerBindings_t gameControllerBindings[17]; // Vector con las teclas asociadas a los inputs predefinidos std::vector connectedControllers; // Vector con todos los mandos conectados - std::vector controllerNames; // Vector con los nombres de los mandos - int numGamepads; // Numero de mandos conectados - std::string dbPath; // Ruta al archivo gamecontrollerdb.txt + std::vector controllerNames; // Vector con los nombres de los mandos + int numGamepads; // Numero de mandos conectados + std::string dbPath; // Ruta al archivo gamecontrollerdb.txt // Comprueba si hay un mando conectado bool discoverGameController(); @@ -74,6 +74,9 @@ public: // Comprueba si un input esta activo bool checkInput(Uint8 input, bool repeat, 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); + // Comprueba si hay algun mando conectado bool gameControllerFound(); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 3d03475..978db32 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -23,6 +23,7 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board) // Inicializa las variables counter = 0; + state = sb_hide; } // Destructor @@ -67,6 +68,10 @@ void ScoreBoard::fillTexture() // Pinta el objeto en pantalla void ScoreBoard::render() { + if (state == sb_hide) + { + return; + } // Dibuja la textura con el marcador en pantalla SDL_Rect rect = {SCOREBOARD_X, SCOREBOARD_Y, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT}; SDL_RenderCopy(renderer, layer, NULL, &rect); @@ -75,7 +80,14 @@ void ScoreBoard::render() // Actualiza las variables del objeto void ScoreBoard::update() { - counter++; + if (counter == 200) + { + state = sb_show; + } + else + { + counter++; + } } // Recarga la textura @@ -83,4 +95,11 @@ void ScoreBoard::reLoadTexture() { texture->reLoad(); text->reLoadTexture(); +} + +// Resetea el tiempo de aparición del marcador +void ScoreBoard::reset() +{ + counter = 0; + state = sb_hide; } \ No newline at end of file diff --git a/source/scoreboard.h b/source/scoreboard.h index fa441ae..b2b37f3 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -17,6 +17,14 @@ struct board_t int lives; // Lleva la cuenta de ls vidas restantes del jugador }; +enum sb_state_e +{ + sb_show, + sb_hide, + sb_showing, + sb_hiding +}; + // Clase ScoreBoard class ScoreBoard { @@ -29,6 +37,7 @@ private: Text *text; // Objeto para escribir texto int counter; // Contador interno board_t *board; // Contiene las variables a mostrar en el marcador + sb_state_e state; // Estado en el que se encuentra el marcador public: // Constructor @@ -48,6 +57,9 @@ public: // Recarga la textura void reLoadTexture(); + + // Resetea el tiempo de aparición del marcador + void reset(); }; #endif