diff --git a/source/game.cpp b/source/game.cpp index 3825039..5499047 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -148,11 +148,11 @@ void Game::init(int playerID) scoreboard->setPos({param->scoreboard.x, param->scoreboard.y, param->scoreboard.w, param->scoreboard.h}); if (!players[0]->isEnabled()) { - scoreboard->setMode(SCOREBOARD_LEFT_SIDE, SCOREBOARD_MODE_GAME_OVER); + scoreboard->setMode(SCOREBOARD_LEFT_PANEL, SCOREBOARD_MODE_GAME_OVER); } if (!players[1]->isEnabled()) { - scoreboard->setMode(SCOREBOARD_RIGHT_SIDE, SCOREBOARD_MODE_GAME_OVER); + scoreboard->setMode(SCOREBOARD_RIGHT_PANEL, SCOREBOARD_MODE_GAME_OVER); } // Resto de variables @@ -226,8 +226,8 @@ void Game::init(int playerID) JA_EnableSound(false); // Configura los marcadores - scoreboard->setMode(SCOREBOARD_LEFT_SIDE, SCOREBOARD_MODE_DEMO); - scoreboard->setMode(SCOREBOARD_RIGHT_SIDE, SCOREBOARD_MODE_DEMO); + scoreboard->setMode(SCOREBOARD_LEFT_PANEL, SCOREBOARD_MODE_DEMO); + scoreboard->setMode(SCOREBOARD_RIGHT_PANEL, SCOREBOARD_MODE_DEMO); } initPaths(); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 21ea5df..a89ba51 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -21,8 +21,9 @@ Scoreboard::Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lan hiScoreName = ""; color = {0, 0, 0}; rect = {0, 0, 320, 40}; - mode[0] = SCOREBOARD_MODE_PLAYING; - mode[1] = SCOREBOARD_MODE_PLAYING; + panel[SCOREBOARD_LEFT_PANEL].mode = SCOREBOARD_MODE_SCORE; + panel[SCOREBOARD_RIGHT_PANEL].mode = SCOREBOARD_MODE_SCORE; + panel[SCOREBOARD_CENTER_PANEL].mode = SCOREBOARD_MODE_STAGE_INFO; ticks = SDL_GetTicks(); counter = 0; @@ -34,9 +35,11 @@ Scoreboard::Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lan powerMeterSprite = new Sprite(offsetPowerMeter.x - 20, offsetPowerMeter.y, 40, 7, gamePowerMeterTexture); textScoreBoard = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); - // Crea la textura para dibujar el marcador - background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h); - SDL_SetTextureBlendMode(background, SDL_BLENDMODE_BLEND); + // Crea la textura de fondo + createBackgroundTexture(); + + // Crea las texturas de los paneles + createPanelTextures(); // Rellena la textura de fondo fillBackgroundTexture(); @@ -49,6 +52,13 @@ Scoreboard::~Scoreboard() delete powerMeterSprite; delete textScoreBoard; SDL_DestroyTexture(background); + for (auto texture : panelTexture) + { + if (texture) + { + SDL_DestroyTexture(texture); + } + } } // Transforma un valor numérico en una cadena de 6 cifras @@ -181,19 +191,92 @@ void Scoreboard::setPos(SDL_Rect rect) powerMeterSprite->setPosX(offsetPowerMeter.x - 20); powerMeterSprite->setPosY(offsetPowerMeter.y); - // Recrea la textura de fondo - if (background) - { - SDL_DestroyTexture(background); - } - background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h); - SDL_SetTextureBlendMode(background, SDL_BLENDMODE_BLEND); + // Crea la textura de fondo + createBackgroundTexture(); + + // Crea las texturas de los paneles + createPanelTextures(); + + // Rellena la textura de fondo fillBackgroundTexture(); } +// Rellena los diferentes paneles del marcador +void Scoreboard::fillPanelTextures() +{ + // Guarda a donde apunta actualmente el renderizador + SDL_Texture *temp = SDL_GetRenderTarget(renderer); + + // Genera el contenidoi de cada panel + for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i) + { + // Cambia el destino del renderizador + SDL_SetRenderTarget(renderer, panelTexture[i]); + + // Dibuja el fondo de la textura + SDL_SetRenderDrawColor(renderer, i * 64, 0, 0, 255); + SDL_RenderClear(renderer); + + switch (panel[i].mode) + { + case SCOREBOARD_MODE_SCORE: + // SCORE + textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y, lang->getText(53)); + textScoreBoard->writeCentered(offsetScoreP1.x, offsetScoreP1.y, updateScoreText(score1)); + + // MULT + textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y, lang->getText(55)); + textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y, std::to_string(mult1).substr(0, 3)); + break; + + case SCOREBOARD_MODE_DEMO: + textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y + 4, "Mode demostracio"); + if (counter % 10 < 8) + { + textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y - 2, "Pulsa START"); + textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y - 2, "per jugar"); + } + break; + + case SCOREBOARD_MODE_GAME_OVER: + textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y + 4, "Game Over"); + if (counter % 10 < 8) + { + textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y - 2, "Pulsa START"); + textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y - 2, "per jugar"); + } + break; + + case SCOREBOARD_MODE_STAGE_INFO: + // STAGE + textScoreBoard->writeCentered(offsetStage.x, offsetStage.y, lang->getText(57) + std::to_string(stage)); + + // POWERMETER + powerMeterSprite->setSpriteClip(0, 0, 40, 7); + powerMeterSprite->render(); + powerMeterSprite->setSpriteClip(40, 0, int(power * 40.0f), 7); + powerMeterSprite->render(); + + // HI-SCORE + textScoreBoard->writeCentered(offsetHiScoreLabel.x, offsetHiScoreLabel.y, lang->getText(56)); + textScoreBoard->writeCentered(offsetHiScore.x, offsetHiScore.y, hiScoreName + updateScoreText(hiScore)); + break; + + default: + break; + } + } + + // Deja el renderizador apuntando donde estaba + SDL_SetRenderTarget(renderer, temp); +} + // Rellena la textura de fondo void Scoreboard::fillBackgroundTexture() { + // Rellena los diferentes paneles del marcador + fillPanelTextures(); + // Cambia el destino del renderizador SDL_Texture *temp = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer, background); @@ -202,83 +285,12 @@ void Scoreboard::fillBackgroundTexture() SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255); SDL_RenderFillRect(renderer, nullptr); - // PARTE IZQUIERDA - if (mode[SCOREBOARD_LEFT_SIDE] == SCOREBOARD_MODE_PLAYING) - { // PLAYER1 - SCORE - textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y, lang->getText(53)); - textScoreBoard->writeCentered(offsetScoreP1.x, offsetScoreP1.y, updateScoreText(score1)); - - // PLAYER1 - MULT - textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y, lang->getText(55)); - textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y, std::to_string(mult1).substr(0, 3)); - } - - else if (mode[SCOREBOARD_LEFT_SIDE] == SCOREBOARD_MODE_DEMO) + // Copia las texturas de los paneles + for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i) { - textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y + 4, "Mode demostracio"); - if (counter % 10 < 8) - { - textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y - 2, "Pulsa START"); - textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y - 2, "per jugar"); - } + SDL_RenderCopy(renderer, panelTexture[i], nullptr, &panel[i].pos); } - else if (mode[SCOREBOARD_LEFT_SIDE] == SCOREBOARD_MODE_GAME_OVER) - { - textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y + 4, "Game Over"); - if (counter % 10 < 8) - { - textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y - 2, "Pulsa START"); - textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y - 2, "per jugar"); - } - } - - // PARTE DERECHA - if (mode[SCOREBOARD_RIGHT_SIDE] == SCOREBOARD_MODE_PLAYING) - { // PLAYER2 - SCORE - textScoreBoard->writeCentered(offsetScoreP2Label.x, offsetScoreP2Label.y, lang->getText(54)); - textScoreBoard->writeCentered(offsetScoreP2.x, offsetScoreP2.y, updateScoreText(score2)); - - // PLAYER2 - MULT - textScoreBoard->writeCentered(offsetMultP2Label.x, offsetMultP2Label.y, lang->getText(55)); - textScoreBoard->writeCentered(offsetMultP2.x, offsetMultP2.y, std::to_string(mult2).substr(0, 3)); - } - - else if (mode[SCOREBOARD_RIGHT_SIDE] == SCOREBOARD_MODE_DEMO) - { - textScoreBoard->writeCentered(offsetScoreP2Label.x, offsetScoreP2Label.y + 4, "Mode demostracio"); - if (counter % 10 < 8) - { - textScoreBoard->writeCentered(offsetMultP2Label.x, offsetMultP2Label.y - 2, "Pulsa START"); - textScoreBoard->writeCentered(offsetMultP2.x, offsetMultP2.y - 2, "per jugar"); - } - } - - else if (mode[SCOREBOARD_RIGHT_SIDE] == SCOREBOARD_MODE_GAME_OVER) - { - textScoreBoard->writeCentered(offsetScoreP2Label.x, offsetScoreP2Label.y + 4, "Game Over"); - if (counter % 10 < 8) - { - textScoreBoard->writeCentered(offsetMultP2Label.x, offsetMultP2Label.y - 2, "Pulsa START"); - textScoreBoard->writeCentered(offsetMultP2.x, offsetMultP2.y - 2, "per jugar"); - } - } - - // PARTE CENTRAL - - // STAGE - textScoreBoard->writeCentered(offsetStage.x, offsetStage.y, lang->getText(57) + std::to_string(stage)); - - // POWERMETER - powerMeterSprite->setSpriteClip(0, 0, 40, 7); - powerMeterSprite->render(); - powerMeterSprite->setSpriteClip(40, 0, int(power * 40.0f), 7); - powerMeterSprite->render(); - - // HI-SCORE - textScoreBoard->writeCentered(offsetHiScoreLabel.x, offsetHiScoreLabel.y, lang->getText(56)); - textScoreBoard->writeCentered(offsetHiScore.x, offsetHiScore.y, hiScoreName + updateScoreText(hiScore)); - // Deja el renderizador apuntando donde estaba SDL_SetRenderTarget(renderer, temp); } @@ -286,42 +298,87 @@ void Scoreboard::fillBackgroundTexture() // Recalcula las anclas de los elementos void Scoreboard::recalculateAnchors() { - // Constantes para definir las zonas del marcador: 4 filas y 3 columnas + const int panelWidth = rect.w / SCOREBOARD_MAX_PANELS; + for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i) + { + panel[i].pos.x = panelWidth * i; + panel[i].pos.y = 0; + panel[i].pos.w = panelWidth; + panel[i].pos.h = rect.h; + } + + // Constantes para definir las zonas del panel: 4 filas y 1 columna const int rowSize = rect.h / 4; const int textHeight = 7; + + // Filas const int row1 = (rowSize * 0) + (textHeight / 2); const int row2 = (rowSize * 1) + (textHeight / 2) - 1; const int row3 = (rowSize * 2) + (textHeight / 2) - 2; const int row4 = (rowSize * 3) + (textHeight / 2) - 3; - const int halfColSize = rect.w / 6; - const int col1 = halfColSize; - const int col2 = halfColSize * 3; - const int col3 = halfColSize * 5; + // Columna + const int col = panelWidth / 2; // Primera fila - offsetScoreP1Label = {col1, row1}; - offsetStage = {col2, row1}; - offsetScoreP2Label = {col3, row1}; + offsetScoreP1Label = {col, row1}; + offsetStage = {col, row1}; + offsetScoreP2Label = {col, row1}; // Segunda fila - offsetScoreP1 = {col1, row2}; - offsetPowerMeter = {col2, row2}; - offsetScoreP2 = {col3, row2}; + offsetScoreP1 = {col, row2}; + offsetPowerMeter = {col, row2}; + offsetScoreP2 = {col, row2}; // Tercera fila - offsetMultP1Label = {col1, row3}; - offsetHiScoreLabel = {col2, row3}; - offsetMultP2Label = {col3, row3}; + offsetMultP1Label = {col, row3}; + offsetHiScoreLabel = {col, row3}; + offsetMultP2Label = {col, row3}; // Cuarta fila - offsetMultP1 = {col1, row4}; - offsetHiScore = {col2, row4}; - offsetMultP2 = {col3, row4}; + offsetMultP1 = {col, row4}; + offsetHiScore = {col, row4}; + offsetMultP2 = {col, row4}; } // Establece el modo del marcador void Scoreboard::setMode(int index, scoreboard_modes_e mode) { - this->mode[index] = mode; + panel[index].mode = mode; +} + +// Crea la textura de fondo +void Scoreboard::createBackgroundTexture() +{ + // Elimina la textura en caso de existir + if (background) + { + SDL_DestroyTexture(background); + } + + // Recrea la textura de fondo + background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h); + SDL_SetTextureBlendMode(background, SDL_BLENDMODE_BLEND); +} + +// Crea las texturas de los paneles +void Scoreboard::createPanelTextures() +{ + // Elimina las texturas en caso de existir + for (auto texture : panelTexture) + { + if (texture) + { + SDL_DestroyTexture(texture); + } + } + panelTexture.clear(); + + // Crea las texturas para cada panel + for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i) + { + SDL_Texture *tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w / SCOREBOARD_MAX_PANELS, rect.h); + SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); + panelTexture.push_back(tex); + } } \ No newline at end of file diff --git a/source/scoreboard.h b/source/scoreboard.h index ee792e0..3c9d314 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -9,47 +9,63 @@ #include "const.h" #include "lang.h" -enum scoreboard_modes_e -{ - SCOREBOARD_MODE_PLAYING, - SCOREBOARD_MODE_GAME_OVER, - SCOREBOARD_MODE_DEMO, -}; +// Defines +#define SCOREBOARD_MAX_PANELS 3 -#define SCOREBOARD_LEFT_SIDE 0 -#define SCOREBOARD_RIGHT_SIDE 1 +#define SCOREBOARD_LEFT_PANEL 0 +#define SCOREBOARD_CENTER_PANEL 1 +#define SCOREBOARD_RIGHT_PANEL 2 #define SCOREBOARD_TICK_SPEED 100 +// Enums +enum scoreboard_modes_e +{ + SCOREBOARD_MODE_SCORE, + SCOREBOARD_MODE_STAGE_INFO, + SCOREBOARD_MODE_CONTINUE, + SCOREBOARD_MODE_GAME_OVER, + SCOREBOARD_MODE_DEMO, + SCOREBOARD_MODE_NUM_MODES, +}; + +// Structs +struct panel_t +{ + scoreboard_modes_e mode; // Modo en el que se encuentra el panel + SDL_Rect pos; // Posición donde dibujar el panel dentro del marcador +}; + // 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 - SDL_Texture *background; // Textura para dibujar el marcador + 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 + SDL_Texture *background; // Textura para dibujar el marcador + std::vector panelTexture; // Texturas para dibujar cada panel; // Variables - struct options_t *options; // Variable con todas las variables de las opciones del programa - int stage; // Número de fase actual - int score1; // Puntuación del jugador 1 - int score2; // Puntuación del jugador 2 - float mult1; // Multiplicador del jugador 1 - float mult2; // MUltiplicador del jugador 2 - int hiScore; // Mäxima puntuación - float power; // Poder actual de la fase - std::string hiScoreName; // Nombre del jugador con la máxima puntuación - color_t color; // Color del marcador - SDL_Rect rect; // Posición y dimensiones del marcador - scoreboard_modes_e mode[2]; // Modos en los que se puede encontrar el marcador - Uint32 ticks; // Variable donde almacenar el valor de SDL_GetTiks() - int counter; // Contador + struct options_t *options; // Variable con todas las variables de las opciones del programa + int stage; // Número de fase actual + int score1; // Puntuación del jugador 1 + int score2; // Puntuación del jugador 2 + float mult1; // Multiplicador del jugador 1 + float mult2; // Multiplicador del jugador 2 + int hiScore; // Máxima puntuación + float power; // Poder actual de la fase + std::string hiScoreName; // Nombre del jugador con la máxima puntuación + color_t color; // Color del marcador + SDL_Rect rect; // Posición y dimensiones del marcador + panel_t panel[SCOREBOARD_MAX_PANELS]; // Lista con todos los paneles del marcador + Uint32 ticks; // Variable donde almacenar el valor de SDL_GetTiks() + int counter; // Contador SDL_Point offsetScoreP1Label; SDL_Point offsetScoreP1; @@ -73,6 +89,15 @@ private: // Transforma un valor numérico en una cadena de 6 cifras std::string updateScoreText(Uint32 num); + // Crea la textura de fondo + void createBackgroundTexture(); + + // Crea las texturas de los paneles + void createPanelTextures(); + + // Rellena los diferentes paneles del marcador + void fillPanelTextures(); + // Rellena la textura de fondo void fillBackgroundTexture(); diff --git a/source/title.h b/source/title.h index c1cd7d0..396c48f 100644 --- a/source/title.h +++ b/source/title.h @@ -25,7 +25,7 @@ #define TEXT_COPYRIGHT "@2020,2024 JailDesigner" // Parámetros -#define ALLOW_TITLE_ANIMATION_SKIP false +#define ALLOW_TITLE_ANIMATION_SKIP true /* Esta clase gestiona un estado del programa. Se encarga de la parte del titulo o menu