diff --git a/source/director.cpp b/source/director.cpp index f948e01..961bd7e 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -16,7 +16,7 @@ Director::Director(int argc, char *argv[]) { // Inicializa variables section = new section_t(); - section->name = SECTION_PROG_LOGO; + section->name = SECTION_PROG_GAME; // Inicializa las opciones del programa initOptions(); diff --git a/source/game.cpp b/source/game.cpp index fd502e4..7b9725c 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -27,6 +27,7 @@ Game::Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *scr // Crea los objetos fade = new Fade(renderer); eventHandler = new SDL_Event(); + scoreboard = new Scoreboard(renderer, screen, asset, lang, options); // Carga los recursos loadMedia(); @@ -147,6 +148,8 @@ Game::~Game() } balloonTextures.clear(); + delete scoreboard; + delete text; delete textBig; delete textScoreBoard; @@ -401,7 +404,6 @@ void Game::loadMedia() gameBuildingsTexture = new Texture(renderer, asset->get("game_buildings.png")); gameCloudsTexture = new Texture(renderer, asset->get("game_clouds.png")); gameGrassTexture = new Texture(renderer, asset->get("game_grass.png")); - gamePowerMeterTexture = new Texture(renderer, asset->get("game_power_meter.png")); gameSkyColorsTexture = new Texture(renderer, asset->get("game_sky_colors.png")); gameTextTexture = new Texture(renderer, asset->get("game_text.png")); gameOverTexture = new Texture(renderer, asset->get("menu_game_over.png")); @@ -1599,120 +1601,6 @@ void Game::updateHiScore() } } -// Transforma un valor numérico en una cadena de 6 cifras -std::string Game::updateScoreText(Uint32 num) -{ - if ((num >= 0) && (num <= 9)) - { - return ("000000" + std::to_string(num)); - } - - if ((num >= 10) && (num <= 99)) - { - return ("00000" + std::to_string(num)); - } - - if ((num >= 100) && (num <= 999)) - { - return ("0000" + std::to_string(num)); - } - - if ((num >= 1000) && (num <= 9999)) - { - return ("000" + std::to_string(num)); - } - - if ((num >= 010000) && (num <= 99999)) - { - return ("00" + std::to_string(num)); - } - - if ((num >= 100000) && (num <= 999999)) - { - return ("0" + std::to_string(num)); - } - - if ((num >= 1000000) && (num <= 9999999)) - { - return (std::to_string(num)); - } - - return (std::to_string(num)); -} - -// Pinta el marcador en pantalla usando un objeto texto -void Game::renderScoreBoard() -{ - // Dibuja el fondo del marcador - if (difficulty == DIFFICULTY_NORMAL) - { // Pone el color gris de siempre - SDL_SetRenderDrawColor(renderer, 46, 63, 71, 255); - } - else - { // Pinta el fondo del marcador del color de la dificultad - SDL_SetRenderDrawColor(renderer, difficultyColor.r, difficultyColor.g, difficultyColor.b, 255); - } - SDL_Rect rect = {0, 160, 256, 32}; - SDL_RenderFillRect(renderer, &rect); - - // Dibuja la linea que separa el marcador de la zona de juego - SDL_SetRenderDrawColor(renderer, 13, 26, 43, 255); - SDL_RenderDrawLine(renderer, 0, 160, 255, 160); - - // Anclas para los elementos - const int offset1 = 162; - const int offset2 = offset1 + 7; - const int offset3 = offset2 + 7; - const int offset4 = offset3 + 7; - - const int offsetLeft = PLAY_AREA_LEFT + 45; - const int offsetRight = PLAY_AREA_RIGHT - 45; - - // PLAYER1 - SCORE - textScoreBoard->writeCentered(offsetLeft, offset1, lang->getText(53)); - textScoreBoard->writeCentered(offsetLeft, offset2, updateScoreText(players[0]->getScore())); - - // PLAYER1 - MULT - textScoreBoard->writeCentered(offsetLeft, offset3, lang->getText(55)); - textScoreBoard->writeCentered(offsetLeft, offset4, std::to_string(players[0]->getScoreMultiplier()).substr(0, 3)); - - if (numPlayers == 2) - { - // PLAYER2 - SCORE - textScoreBoard->writeCentered(offsetRight, offset1, lang->getText(54)); - textScoreBoard->writeCentered(offsetRight, offset2, updateScoreText(players[1]->getScore())); - - // PLAYER2 - MULT - textScoreBoard->writeCentered(offsetRight, offset3, lang->getText(55)); - textScoreBoard->writeCentered(offsetRight, offset4, std::to_string(players[1]->getScoreMultiplier()).substr(0, 3)); - } - else - { - // PLAYER2 - SCORE - textScoreBoard->writeCentered(offsetRight, offset1, lang->getText(54)); - textScoreBoard->writeCentered(offsetRight, offset2, "0000000"); - - // PLAYER2 - MULT - textScoreBoard->writeCentered(offsetRight, offset3, lang->getText(55)); - textScoreBoard->writeCentered(offsetRight, offset4, "1.0"); - } - - // STAGE - textScoreBoard->writeCentered(PLAY_AREA_CENTER_X, offset1, lang->getText(57) + std::to_string(stage[currentStage].number)); - - // POWERMETER - powerMeterSprite->setPosY(offset2); - powerMeterSprite->setSpriteClip(0, 0, 40, 7); - powerMeterSprite->render(); - const float percent = (stage[currentStage].currentPower * 40.0f) / stage[currentStage].powerToComplete; - powerMeterSprite->setSpriteClip(40, 0, (int)percent, 7); - powerMeterSprite->render(); - - // HI-SCORE - textScoreBoard->writeCentered(PLAY_AREA_CENTER_X, offset3, lang->getText(56)); - textScoreBoard->writeCentered(PLAY_AREA_CENTER_X, offset4, hiScoreName + updateScoreText(hiScore)); -} - // Actualiza las variables del jugador void Game::updatePlayers() { @@ -2928,7 +2816,7 @@ void Game::render() renderMessages(); renderItems(); renderSmartSprites(); - renderScoreBoard(); + scoreboard->render(); renderPlayers(); if ((deathCounter <= 150) && !players[0]->isAlive()) @@ -3292,7 +3180,7 @@ void Game::shakeScreen() renderBullets(); renderItems(); renderPlayers(); - renderScoreBoard(); + scoreboard->render(); // Vuelca el contenido del renderizador en pantalla screen->blit(); @@ -3433,7 +3321,7 @@ void Game::renderPausedGame() renderMessages(); renderItems(); renderSmartSprites(); - renderScoreBoard(); + scoreboard->render(); renderPlayers(); if ((deathCounter <= 150) && !players[0]->isAlive()) diff --git a/source/game.h b/source/game.h index ce02863..5479dfb 100644 --- a/source/game.h +++ b/source/game.h @@ -18,6 +18,7 @@ #include "fade.h" #include "item.h" #include "player.h" +#include "scoreboard.h" #include #ifndef GAME_H @@ -121,6 +122,7 @@ private: Lang *lang; // Objeto para gestionar los textos en diferentes idiomas Input *input; // Manejador de entrada section_t *section; // Seccion actual dentro del juego + Scoreboard *scoreboard; // Objeto para dibujar el marcador std::vector players; // Vector con los jugadores std::vector balloons; // Vector con los globos @@ -138,7 +140,6 @@ private: Texture *gameBuildingsTexture; // Textura con los edificios de fondo Texture *gameCloudsTexture; // Textura con las nubes de fondo Texture *gameGrassTexture; // Textura con la hierba del suelo - Texture *gamePowerMeterTexture; // Textura con el marcador de poder de la fase Texture *gameSkyColorsTexture; // Textura con los diferentes colores de fondo del juego Texture *gameTextTexture; // Textura para los sprites con textos Texture *gameOverTexture; // Textura para la pantalla de game over @@ -150,7 +151,6 @@ private: Text *text; // Fuente para los textos del juego Text *textBig; // Fuente de texto grande - Text *textScoreBoard; // Fuente para el marcador del juego Text *textNokia2; // Otra fuente de texto para mensajes Text *textNokiaBig2; // Y la versión en grande @@ -171,7 +171,6 @@ private: Sprite *buildingsSprite; // Sprite con los edificios de fondo Sprite *skyColorsSprite; // Sprite con los graficos del degradado de color de fondo Sprite *grassSprite; // Sprite para la hierba - Sprite *powerMeterSprite; // Sprite para el medidor de poder de la fase Sprite *gameOverSprite; // Sprite para dibujar los graficos del game over Sprite *gameOverEndSprite; // Sprite para dibujar los graficos del game over de acabar el juego @@ -287,12 +286,6 @@ private: // Actualiza el valor de HiScore en caso necesario void updateHiScore(); - // Transforma un valor numérico en una cadena de 6 cifras - std::string updateScoreText(Uint32 num); - - // Pinta el marcador en pantalla usando un objeto texto - void renderScoreBoard(); - // Actualiza las variables del jugador void updatePlayers(); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp new file mode 100644 index 0000000..4b6a2ed --- /dev/null +++ b/source/scoreboard.cpp @@ -0,0 +1,131 @@ +#include "scoreboard.h" + +// Constructor +Scoreboard::Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, options_t *options) +{ + // Copia los punteros + this->renderer = renderer; + this->screen = screen; + this->asset = asset; + this->lang = lang; + this->options = options; + + // Crea objetos + gamePowerMeterTexture = new Texture(renderer, asset->get("game_power_meter.png")); + powerMeterSprite = new Sprite(PLAY_AREA_CENTER_X - 20, 170, 40, 7, gamePowerMeterTexture, renderer); + textScoreBoard = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); + + // Inicializa variables + stage = 0; + score1 = 0; + score2 = 0; + mult1 = 0; + mult2 = 0; + hiScore = 0; + power = 0; + hiScoreName = ""; + color = {255, 0, 0}; +} + +Scoreboard::~Scoreboard() +{ + gamePowerMeterTexture->unload(); + delete gamePowerMeterTexture; + delete powerMeterSprite; + delete textScoreBoard; +} + +// Transforma un valor numérico en una cadena de 6 cifras +std::string Scoreboard::updateScoreText(Uint32 num) +{ + if ((num >= 0) && (num <= 9)) + { + return ("000000" + std::to_string(num)); + } + + if ((num >= 10) && (num <= 99)) + { + return ("00000" + std::to_string(num)); + } + + if ((num >= 100) && (num <= 999)) + { + return ("0000" + std::to_string(num)); + } + + if ((num >= 1000) && (num <= 9999)) + { + return ("000" + std::to_string(num)); + } + + if ((num >= 010000) && (num <= 99999)) + { + return ("00" + std::to_string(num)); + } + + if ((num >= 100000) && (num <= 999999)) + { + return ("0" + std::to_string(num)); + } + + if ((num >= 1000000) && (num <= 9999999)) + { + return (std::to_string(num)); + } + + return (std::to_string(num)); +} + +// Pinta el marcador +void Scoreboard::render() +{ + // Dibuja el fondo del marcador + SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255); + SDL_Rect rect = {0, 160, 256, 32}; + SDL_RenderFillRect(renderer, &rect); + + // Dibuja la linea que separa el marcador de la zona de juego + SDL_SetRenderDrawColor(renderer, 13, 26, 43, 255); + SDL_RenderDrawLine(renderer, 0, 160, 255, 160); + + // Anclas para los elementos + const int offset1 = 162; + const int offset2 = offset1 + 7; + const int offset3 = offset2 + 7; + const int offset4 = offset3 + 7; + + const int offsetLeft = PLAY_AREA_LEFT + 45; + const int offsetRight = PLAY_AREA_RIGHT - 45; + + // PLAYER1 - SCORE + textScoreBoard->writeCentered(offsetLeft, offset1, lang->getText(53)); + textScoreBoard->writeCentered(offsetLeft, offset2, updateScoreText(score1)); + + // PLAYER1 - MULT + textScoreBoard->writeCentered(offsetLeft, offset3, lang->getText(55)); + textScoreBoard->writeCentered(offsetLeft, offset4, std::to_string(mult1).substr(0, 3)); + + // PLAYER2 - SCORE + textScoreBoard->writeCentered(offsetRight, offset1, lang->getText(54)); + textScoreBoard->writeCentered(offsetRight, offset2, updateScoreText(score2)); + + // PLAYER2 - MULT + textScoreBoard->writeCentered(offsetRight, offset3, lang->getText(55)); + textScoreBoard->writeCentered(offsetRight, offset4, std::to_string(mult2).substr(0, 3)); + + // STAGE + textScoreBoard->writeCentered(PLAY_AREA_CENTER_X, offset1, lang->getText(57) + std::to_string(stage)); + + // POWERMETER + powerMeterSprite->setPosY(offset2); + powerMeterSprite->setSpriteClip(0, 0, 40, 7); + powerMeterSprite->render(); + //const float percent = (stage[currentStage].currentPower * 40.0f) / stage[currentStage].powerToComplete; + const float percent = 0.5; + powerMeterSprite->setSpriteClip(40, 0, (int)percent, 7); + powerMeterSprite->render(); + + // HI-SCORE + textScoreBoard->writeCentered(PLAY_AREA_CENTER_X, offset3, lang->getText(56)); + textScoreBoard->writeCentered(PLAY_AREA_CENTER_X, offset4, hiScoreName + updateScoreText(hiScore)); +} \ No newline at end of file diff --git a/source/scoreboard.h b/source/scoreboard.h new file mode 100644 index 0000000..46215d4 --- /dev/null +++ b/source/scoreboard.h @@ -0,0 +1,61 @@ +#pragma once + +#include +#include "common/asset.h" +#include "common/input.h" +#include "common/jail_audio.h" +#include "common/menu.h" +#include "common/movingsprite.h" +#include "common/screen.h" +#include "common/smartsprite.h" +#include "common/sprite.h" +#include "common/text.h" +#include "common/utils.h" +#include "common/writer.h" +#include "const.h" + +#ifndef SCOREBOARD_H +#define SCOREBOARD_H + +// Clase Scoreboard +class Scoreboard +{ +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 + Lang *lang; // Objeto para gestionar los textos en diferentes idiomas + + Texture *gamePowerMeterTexture; // Textura con el marcador de poder de la fase + Sprite *powerMeterSprite; // Sprite para el medidor de poder de la fase + Text *textScoreBoard; // Fuente para el marcador del juego + + // Variables + struct options_t *options; // Variable con todas las variables de las opciones del programa + int stage; // Numero de fase actual + int score1; // Puntuación del jugador 1 + int score2; // Puntuación del jugador 2 + int mult1; // Multiplicador del jugador 1 + int mult2; // MUltiplicador del jugador 2 + int hiScore; // Mäxima puntuación + int power; // Poder actual de la fase + std::string hiScoreName; // Nombre del jugador con la máxima puntuación + color_t color; // Color del marcador + +public: + // Constructor + Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, options_t *options); + + // Destructor + ~Scoreboard(); + + // Pinta el marcador + void render(); + +private: + // Transforma un valor numérico en una cadena de 6 cifras + std::string updateScoreText(Uint32 num); +}; + +#endif