From 4f0ea9dcf2f87fb96649143aa8babaee929e1f5a Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 7 Oct 2024 08:22:51 +0200 Subject: [PATCH] Make_unique en title.cpp --- source/title.cpp | 85 +++++++++++++++--------------------------------- source/title.h | 50 +++++++++++++++------------- 2 files changed, 54 insertions(+), 81 deletions(-) diff --git a/source/title.cpp b/source/title.cpp index 7408460..08e7e39 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -5,9 +5,6 @@ #include // for allocator, basic_string, char_traits #include // for vector #include "asset.h" // for Asset -#include "define_buttons.h" // for DefineButtons -#include "fade.h" // for Fade, FADE_RANDOM_SQUARE -#include "game_logo.h" // for GameLogo #include "global_inputs.h" // for globalInputs::check #include "input.h" // for Input, inputs_e, INPUT_DO_NOT_ALLOW_RE... #include "jail_audio.h" // for JA_GetMusicState, JA_Music_state, JA_P... @@ -16,63 +13,41 @@ #include "param.h" // for param #include "screen.h" // for Screen #include "section.h" // for options_e, options, name_e, name -#include "sprite.h" // for Sprite -#include "text.h" // for Text, TXT_CENTER, TXT_SHADOW -#include "texture.h" // for Texture -#include "tiled_bg.h" // for Tiledbg, TILED_MODE_RANDOM + struct JA_Music_t; // Constructor -Title::Title(JA_Music_t *music) +Title::Title(JA_Music_t *music) : music(music) { // Copia las direcciones de los punteros y objetos - this->music = music; input = Input::get(); asset = Asset::get(); screen = Screen::get(); SDL_Renderer *renderer = screen->getRenderer(); // Reserva memoria y crea los objetos - eventHandler = new SDL_Event(); - fade = new Fade(renderer); + eventHandler = std::make_unique(); + fade = std::make_unique(renderer); - text1 = new Text(asset->get("smb2.gif"), asset->get("smb2.txt"), renderer); + text1 = std::make_unique(asset->get("smb2.gif"), asset->get("smb2.txt"), renderer); text1->addPalette(asset->get("smb2_pal1.gif")); text1->setPalette(1); - text2 = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); + text2 = std::make_unique(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); - miniLogoTexture = new Texture(renderer, asset->get("logo_jailgames_mini.png")); - miniLogoSprite = new Sprite(param.game.gameArea.centerX - miniLogoTexture->getWidth() / 2, 0, miniLogoTexture->getWidth(), miniLogoTexture->getHeight(), miniLogoTexture); + miniLogoTexture = std::make_unique(renderer, asset->get("logo_jailgames_mini.png")); + miniLogoSprite = std::make_unique(param.game.gameArea.centerX - miniLogoTexture->getWidth() / 2, 0, miniLogoTexture->getWidth(), miniLogoTexture->getHeight(), miniLogoTexture.get()); - tiledbg = new Tiledbg(asset->get("title_bg_tile.png"), {0, 0, param.game.width, param.game.height}, TILED_MODE_RANDOM); + tiledbg = std::make_unique(asset->get("title_bg_tile.png"), (SDL_Rect){0, 0, param.game.width, param.game.height}, TILED_MODE_RANDOM); - gameLogo = new GameLogo(param.game.gameArea.centerX, param.title.titleCCPosition); + gameLogo = std::make_unique(param.game.gameArea.centerX, param.title.titleCCPosition); gameLogo->enable(); - defineButtons = new DefineButtons(text2); + defineButtons = std::make_unique(text2.get()); // Inicializa los valores init(); } -// Destructor -Title::~Title() -{ - // Destruye los objetos y libera la memoria - delete eventHandler; - delete fade; - - delete text1; - delete text2; - - delete miniLogoTexture; - delete miniLogoSprite; - - delete tiledbg; - delete gameLogo; - delete defineButtons; -} - // Inicializa los valores de las variables void Title::init() { @@ -135,14 +110,7 @@ void Title::update() else if (section::options == section::OPTIONS_TITLE_2) { // El contador solo sube si no estamos definiendo botones - if (!defineButtons->isEnabled()) - { - counter++; - } - else - { - counter = 0; - } + counter = defineButtons->isEnabled() ? 0 : counter + 1; // Reproduce la música if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) @@ -182,8 +150,9 @@ void Title::render() if (section::options == section::OPTIONS_TITLE_2) { - const color_t shadow = {0x14, 0x87, 0xc4}; - // 'PULSA 1P o 2P PARA JUGAR' + constexpr color_t shadow = {0x14, 0x87, 0xc4}; + + // 'PRESS TO PLAY' if (counter % 50 > 14 && !defineButtons->isEnabled()) { text1->writeDX(TXT_CENTER | TXT_SHADOW, param.game.gameArea.centerX, param.title.pressStartPosition, lang::getText(23), 1, noColor, 1, shadow); @@ -216,7 +185,7 @@ void Title::checkEvents() if (!defineButtons->isEnabled()) { // Comprueba los eventos que hay en la cola - while (SDL_PollEvent(eventHandler) != 0) + while (SDL_PollEvent(eventHandler.get()) != 0) { // Evento de salida de la aplicación if (eventHandler->type == SDL_QUIT) @@ -236,18 +205,24 @@ void Title::checkEvents() switch (eventHandler->key.keysym.sym) { case SDLK_1: + { if (defineButtons->enable(0)) resetCounter(); break; + } case SDLK_2: + { if (defineButtons->enable(1)) resetCounter(); break; + } case SDLK_3: + { swapControllers(); break; + } default: break; @@ -263,10 +238,6 @@ void Title::checkInput() // Comprueba los controladores solo si no se estan definiendo los botones if (!defineButtons->isEnabled()) { - ////////////////////////////////////////////////// - // TECLADO // - ////////////////////////////////////////////////// - // Comprueba el teclado para empezar a jugar if (input->checkInput(input_start, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) { @@ -277,10 +248,7 @@ void Title::checkInput() } } - ////////////////////////////////////////////////// - // MANDOS // - ////////////////////////////////////////////////// - + // Comprueba los mandos for (int i = 0; i < input->getNumControllers(); ++i) { // Comprueba si se va a intercambiar la asignación de mandos a jugadores @@ -299,7 +267,8 @@ void Title::checkInput() // Comprueba el botón de START de los mandos if (input->checkInput(input_start, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) - { // Si no está el botón de servicio activo + { + // Si no está el botón de servicio activo if (!input->checkInput(input_service, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) { if (section::options == section::OPTIONS_TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) @@ -359,7 +328,7 @@ void Title::swapControllers() defineButtons->swapControllers(); // Crea cadenas de texto vacias para un numero máximo de mandos - const int MAX_CONTROLLERS = 2; + constexpr int MAX_CONTROLLERS = 2; std::string text[MAX_CONTROLLERS]; int playerControllerIndex[MAX_CONTROLLERS]; for (int i = 0; i < MAX_CONTROLLERS; ++i) @@ -380,7 +349,7 @@ void Title::swapControllers() const int index = playerControllerIndex[i]; if (options.controller[index].plugged) { - text[i] = "Jugador " + std::to_string(i + 1) + ": " + options.controller[index].name; + text[i] = lang::getText(101) + std::to_string(i + 1) + ": " + options.controller[index].name; } } diff --git a/source/title.h b/source/title.h index b114d78..8c24691 100644 --- a/source/title.h +++ b/source/title.h @@ -1,25 +1,29 @@ #pragma once -#include // for SDL_Event -#include // for Uint32 -#include "utils.h" // for section_t +#include // for SDL_Event +#include // for Uint32 +#include +#include "define_buttons.h" +#include "fade.h" +#include "game_logo.h" +#include "text.h" +#include "tiled_bg.h" +#include "utils.h" // for section_t +#include "sprite.h" +#include "texture.h" + class Asset; -class DefineButtons; -class Fade; -class GameLogo; class Input; class Screen; class Sprite; -class Text; class Texture; -class Tiledbg; struct JA_Music_t; // Textos -#define TEXT_COPYRIGHT "@2020,2024 JailDesigner" +constexpr const char TEXT_COPYRIGHT[] = "@2020,2024 JailDesigner"; // Parámetros -#define ALLOW_TITLE_ANIMATION_SKIP true +constexpr bool ALLOW_TITLE_ANIMATION_SKIP = true; /* Esta clase gestiona un estado del programa. Se encarga de la parte del titulo o menu @@ -41,19 +45,19 @@ class Title { private: // Objetos y punteros - 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 - SDL_Event *eventHandler; // Manejador de eventos - Tiledbg *tiledbg; // Objeto para dibujar el mosaico animado de fondo - GameLogo *gameLogo; // Objeto para dibujar el logo con el título del juego - DefineButtons *defineButtons; // Objeto para definir los botones del joystic - Texture *miniLogoTexture; // Textura con el logo de JailGames mini - Sprite *miniLogoSprite; // Sprite con el logo de JailGames mini + 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 + std::unique_ptr eventHandler; // Manejador de eventos + std::unique_ptr tiledbg; // Objeto para dibujar el mosaico animado de fondo + std::unique_ptr gameLogo; // Objeto para dibujar el logo con el título del juego + std::unique_ptr defineButtons; // Objeto para definir los botones del joystic + std::unique_ptr miniLogoTexture; // Textura con el logo de JailGames mini + std::unique_ptr miniLogoSprite; // Sprite con el logo de JailGames mini - Text *text1; // Objeto de texto para poder escribir textos en pantalla - Text *text2; // Objeto de texto para poder escribir textos en pantalla - Fade *fade; // Objeto para realizar fundidos en pantalla + std::unique_ptr text1; // Objeto de texto para poder escribir textos en pantalla + std::unique_ptr text2; // Objeto de texto para poder escribir textos en pantalla + std::unique_ptr fade; // Objeto para realizar fundidos en pantalla JA_Music_t *music; // Musica para el titulo @@ -95,7 +99,7 @@ public: Title(JA_Music_t *music); // Destructor - ~Title(); + ~Title() = default; // Bucle para el titulo del juego void run();