diff --git a/data/lang/ba_BA.txt b/data/lang/ba_BA.txt index c8a76a5..0dabd8d 100644 --- a/data/lang/ba_BA.txt +++ b/data/lang/ba_BA.txt @@ -124,8 +124,8 @@ Enrere ## 41 - MENU DE PAUSA Menu de pausa -## 42 - -- +## 42 - TABLA DE RECORDS +Millors puntuacions ## 43 - PANTALLA DE GAME OVER FI DEL JOC diff --git a/data/lang/en_UK.txt b/data/lang/en_UK.txt index 3329a6c..00f4b6a 100644 --- a/data/lang/en_UK.txt +++ b/data/lang/en_UK.txt @@ -124,8 +124,8 @@ Back ## 41 - MENU DE PAUSA Pause Menu -## 42 - -- +## 42 - TABLA DE RECORDS +Best scores ## 43 - PANTALLA DE GAME OVER GAME OVER diff --git a/data/lang/es_ES.txt b/data/lang/es_ES.txt index 84b7b87..fd9cfb4 100644 --- a/data/lang/es_ES.txt +++ b/data/lang/es_ES.txt @@ -124,8 +124,8 @@ Volver ## 41 - MENU DE PAUSA Menu de pausa -## 42 - -- +## 42 - TABLA DE RECORDS +Mejores puntuaciones ## 43 - PANTALLA DE GAME OVER FIN DE JUEGO diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp new file mode 100644 index 0000000..11e7a32 --- /dev/null +++ b/source/hiscore_table.cpp @@ -0,0 +1,179 @@ +#include "hiscore_table.h" +#include "common/jscore.h" + +const Uint8 SELF = 0; + +// Constructor +HiScoreTable::HiScoreTable(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; + + // Reserva memoria para los punteros + eventHandler = new SDL_Event(); + + text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); + + // Crea un backbuffer para el renderizador + backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAME_WIDTH, GAME_HEIGHT); + if (backbuffer == nullptr) + { + printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError()); + } + + // Inicializa variables + section.name = SELF; + ticks = 0; + ticksSpeed = 15; + manualQuit = false; + counter = 0; + counterEnd = 600; +} + +// Destructor +HiScoreTable::~HiScoreTable() +{ + delete eventHandler; + delete text; + + SDL_DestroyTexture(backbuffer); +} + +// Actualiza las variables +void HiScoreTable::update() +{ + // Comprueba los eventos + checkEventHandler(); + + // Actualiza las variables + if (SDL_GetTicks() - ticks > ticksSpeed) + { + // Actualiza el contador de ticks + ticks = SDL_GetTicks(); + + if (mode == mhst_auto) + { // Modo automático + counter++; + + if (counter == counterEnd) + { + section.name = PROG_SECTION_TITLE; + section.subsection = TITLE_SECTION_1; + } + } + else + { // Modo manual + ++counter %= 60000; + + if (manualQuit) + { + section.name = PROG_SECTION_TITLE; + section.subsection = TITLE_SECTION_3; + } + } + } +} + +// Pinta en pantalla +void HiScoreTable::render() +{ + // Pinta en pantalla + SDL_Rect window = {0, 0, GAME_WIDTH, GAME_HEIGHT}; + SDL_Rect srcRect = {0, 0, 16, 16}; + + const color_t orangeColor = {0xFF, 0x7A, 0x00}; + + // Pinta en el backbuffer el texto y los sprites + SDL_SetRenderTarget(renderer, backbuffer); + SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255); + SDL_RenderClear(renderer); + + // Escribe el texto + text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, SCREEN_CENTER_X, 8, lang->getText(42), 1, orangeColor, 1, shdwTxtColor); + + int numUsers = jscore::getNumUsers(); + for (int i = 0; i < numUsers; ++i) + { + text->writeShadowed(0, (i * text->getCharacterSize())+20, jscore::getUserName(i),shdwTxtColor); + text->writeShadowed(100, (i * text->getCharacterSize())+20, std::to_string(jscore::getPoints(i)),shdwTxtColor); + } + + if ((mode == mhst_manual) && (counter % 50 > 14)) + { + text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, SCREEN_CENTER_X, GAME_HEIGHT - 12, lang->getText(22), 1, orangeColor, 1, shdwTxtColor); + } + + // Cambia el destino de renderizado + SDL_SetRenderTarget(renderer, nullptr); + + // Prepara para empezar a dibujar en la textura de juego + screen->start(); + + // Limpia la pantalla + screen->clean(bgColor); + + // Establece la ventana del backbuffer + if (mode == mhst_auto) + { + window.y = std::max(8, GAME_HEIGHT - counter + 100); + } + else + { + window.y = 0; + } + + // Copia el backbuffer al renderizador + SDL_RenderCopy(renderer, backbuffer, nullptr, &window); + + // Vuelca el contenido del renderizador en pantalla + screen->blit(); +} + +// Comprueba los eventos +void HiScoreTable::checkEventHandler() +{ + // 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) + { + section.name = PROG_SECTION_QUIT; + break; + } + + if ((eventHandler->type == SDL_KEYUP) || (eventHandler->type == SDL_JOYBUTTONUP)) + { + if (mode == mhst_auto) + { + JA_StopMusic(); + section.name = PROG_SECTION_TITLE; + section.subsection = TITLE_SECTION_1; + } + else + { + if (counter > 30) + { + manualQuit = true; + } + } + } + } +} + +// Bucle para la pantalla de instrucciones +section_t HiScoreTable::run(mode_hiScoreTable_e mode) +{ + this->mode = mode; + + while (section.name == SELF) + { + update(); + render(); + } + + return section; +} diff --git a/source/hiscore_table.h b/source/hiscore_table.h new file mode 100644 index 0000000..ae46deb --- /dev/null +++ b/source/hiscore_table.h @@ -0,0 +1,63 @@ +#pragma once + +#include +#include "common/asset.h" +#include "common/jail_audio.h" +#include "common/screen.h" +#include "common/sprite.h" +#include "common/text.h" +#include "common/utils.h" +#include "const.h" + +#ifndef HISCORE_TABLE_H +#define HISCORE_TABLE_H + +enum mode_hiScoreTable_e +{ + mhst_manual, + mhst_auto +}; + +class HiScoreTable +{ +private: + // Objetos y punteros + SDL_Renderer *renderer; // El renderizador de la ventana + Screen *screen; // Objeto encargado de dibujar en pantalla + SDL_Event *eventHandler; // Manejador de eventos + SDL_Texture *backbuffer; // Textura para usar como backbuffer + Asset *asset; // Objeto que gestiona todos los ficheros de recursos + Lang *lang; // Objeto para gestionar los textos en diferentes idiomas + Text *text; // Objeto para escribir texto + options_t *options; // Opciones y parametyros del programa + + // Variables + Uint16 counter; // Contador + Uint16 counterEnd; // Valor final para el contador + section_t section; // Estado del bucle principal para saber si continua o se sale + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa + bool manualQuit; // Indica si se quiere salir del modo manual + mode_hiScoreTable_e mode; // Modo en el que se van a ejecutar las instrucciones + + // Actualiza las variables + void update(); + + // Pinta en pantalla + void render(); + + // Comprueba los eventos + void checkEventHandler(); + +public: + // Constructor + HiScoreTable(SDL_Renderer *renderer, Screen *screen, Asset *mAsset, Lang *lang, options_t *options); + + // Destructor + ~HiScoreTable(); + + // Bucle principal + section_t run(mode_hiScoreTable_e mode); +}; + +#endif diff --git a/source/title.cpp b/source/title.cpp index 010273f..40f24b3 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -309,6 +309,10 @@ void Title::update() { runInstructions(m_auto); } + if (section.name != PROG_SECTION_QUIT) + { + runHiScoreTable(mhst_auto); + } } else section.name = PROG_SECTION_LOGO; @@ -494,6 +498,10 @@ void Title::update() { runInstructions(m_auto); } + if (section.name != PROG_SECTION_QUIT) + { + runHiScoreTable(mhst_auto); + } init(); demo = false; counter = TITLE_COUNTER; @@ -665,7 +673,7 @@ void Title::checkEventHandler() case SDL_SCANCODE_ESCAPE: section.name = PROG_SECTION_QUIT; break; - + case SDL_SCANCODE_F: screen->switchVideoMode(); reloadTextures(); @@ -962,6 +970,16 @@ section_t Title::runInstructions(mode_e mode) return section; } +// Ejecuta la parte donde se muestra la tabla de puntuaciones +section_t Title::runHiScoreTable(mode_hiScoreTable_e mode) +{ + hiScoreTable = new HiScoreTable(renderer, screen, asset, lang, options); + section = hiScoreTable->run(mode); + delete hiScoreTable; + + return section; +} + // Ejecuta el juego en modo demo section_t Title::runDemoGame() { diff --git a/source/title.h b/source/title.h index 925b612..134857d 100644 --- a/source/title.h +++ b/source/title.h @@ -14,6 +14,7 @@ #include "const.h" #include "fade.h" #include "game.h" +#include "hiscore_table.h" #include "instructions.h" #include "item.h" @@ -48,12 +49,13 @@ private: Input *input; // Objeto para leer las entradas de teclado o mando Lang *lang; // Objeto para gestionar los textos en diferentes idiomas Instructions *instructions; // Objeto para la sección de las instrucciones + HiScoreTable *hiScoreTable; // Objeto para mostrar las mejores puntuaciones online Game *demoGame; // Objeto para lanzar la demo del juego SDL_Event *eventHandler; // Manejador de eventos - Texture *dustTexture; // Textura con los graficos del polvo - Texture *coffeeTexture; // Textura con los graficos de la palabra coffee - Texture *crisisTexture; // Textura con los graficos de la plabra crisis + Texture *dustTexture; // Textura con los graficos del polvo + Texture *coffeeTexture; // Textura con los graficos de la palabra coffee + Texture *crisisTexture; // Textura con los graficos de la plabra crisis Texture *gradientTexture; // Textura con los graficos para el degradado del fondo del titulo SDL_Rect backgroundWindow; // Ventana visible para la textura de fondo del titulo @@ -118,6 +120,9 @@ private: // Ejecuta la parte donde se muestran las instrucciones section_t runInstructions(mode_e mode); + // Ejecuta la parte donde se muestra la tabla de puntuaciones + section_t runHiScoreTable(mode_hiScoreTable_e mode); + // Ejecuta el juego en modo demo section_t runDemoGame();