diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index 7d3a49d..0d2c632 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -21,16 +21,13 @@ HiScoreTable::HiScoreTable(JA_Music_t *music) : music(music) { // Copia punteros - asset = Asset::get(); - input = Input::get(); - screen = Screen::get(); - renderer = screen->getRenderer(); + renderer = Screen::get()->getRenderer(); // Objetos eventHandler = std::make_unique(); fade = std::make_unique(renderer); background = std::make_unique(renderer); - text = std::make_unique(asset->get("smb2.gif"), asset->get("smb2.txt"), renderer); + text = std::make_unique(Asset::get()->get("smb2.gif"), Asset::get()->get("smb2.txt"), renderer); // Crea un backbuffer para el renderizador backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height); @@ -80,7 +77,7 @@ void HiScoreTable::update() JA_PlayMusic(music); // Actualiza el objeto screen - screen->update(); + Screen::get()->update(); // Actualiza el fondo background->update(); @@ -147,10 +144,10 @@ void HiScoreTable::fillTexture() void HiScoreTable::render() { // Prepara para empezar a dibujar en la textura de juego - screen->start(); + Screen::get()->start(); // Limpia la pantalla - screen->clean(bgColor); + Screen::get()->clean(bgColor); // Pinta el fondo background->render(); @@ -164,7 +161,7 @@ void HiScoreTable::render() fade->render(); // Vuelca el contenido del renderizador en pantalla - screen->blit(); + Screen::get()->blit(); } // Recarga todas las texturas @@ -202,7 +199,7 @@ void HiScoreTable::checkEvents() void HiScoreTable::checkInput() { // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) - if (input->checkAnyButtonPressed()) + if (Input::get()->checkAnyButtonPressed()) { JA_StopMusic(); section::name = section::NAME_TITLE; @@ -211,7 +208,7 @@ void HiScoreTable::checkInput() } // Comprueba el input para el resto de objetos - screen->checkInput(); + Screen::get()->checkInput(); // Comprueba los inputs que se pueden introducir en cualquier sección del juego globalInputs::check(); diff --git a/source/hiscore_table.h b/source/hiscore_table.h index 18db8b0..83c6bfd 100644 --- a/source/hiscore_table.h +++ b/source/hiscore_table.h @@ -10,10 +10,6 @@ #include "section.h" // for options_e #include "background.h" #include "text.h" -class Asset; - -class Input; -class Screen; struct JA_Music_t; @@ -33,10 +29,7 @@ class HiScoreTable private: // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana - Screen *screen; // Objeto encargado de dibujar en pantalla SDL_Texture *backbuffer; // Textura para usar como backbuffer - Asset *asset; // Objeto que gestiona todos los ficheros de recursos - Input *input; // Objeto pata gestionar la entrada JA_Music_t *music; // Musica de fondo std::unique_ptr fade; // Objeto para renderizar fades diff --git a/source/instructions.cpp b/source/instructions.cpp index 9f4d31d..4970bfb 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -22,20 +22,16 @@ struct JA_Music_t; // Constructor -Instructions::Instructions(JA_Music_t *music) +Instructions::Instructions(JA_Music_t *music) : music(music) { // Copia los punteros - this->music = music; - input = Input::get(); - screen = Screen::get(); - asset = Asset::get(); - renderer = screen->getRenderer(); + renderer = Screen::get()->getRenderer(); // Crea objetos - eventHandler = new SDL_Event(); - text = new Text(asset->get("smb2.gif"), asset->get("smb2.txt"), renderer); - tiledbg = new Tiledbg(asset->get("title_bg_tile.png"), {0, 0, param.game.width, param.game.height}, TILED_MODE_STATIC); - fade = new Fade(renderer); + eventHandler = std::make_unique(); + text = std::make_unique(Asset::get()->get("smb2.gif"), Asset::get()->get("smb2.txt"), renderer); + tiledbg = std::make_unique(Asset::get()->get("title_bg_tile.png"), (SDL_Rect){0, 0, param.game.width, param.game.height}, TILED_MODE_STATIC); + fade = std::make_unique(renderer); // Crea un backbuffer para el renderizador backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height); @@ -72,24 +68,9 @@ Instructions::Instructions(JA_Music_t *music) // Destructor Instructions::~Instructions() { - for (auto texture : itemTextures) - { - texture->unload(); - delete texture; - } itemTextures.clear(); - - for (auto sprite : sprites) - { - delete sprite; - } sprites.clear(); - delete eventHandler; - delete text; - delete tiledbg; - delete fade; - SDL_DestroyTexture(backbuffer); SDL_DestroyTexture(texture); } @@ -98,27 +79,27 @@ Instructions::~Instructions() void Instructions::iniSprites() { // Inicializa las texturas - Texture *item1 = new Texture(renderer, asset->get("item_points1_disk.png")); - itemTextures.push_back(item1); + auto item1 = std::make_unique(renderer, Asset::get()->get("item_points1_disk.png")); + itemTextures.push_back(std::move(item1)); - Texture *item2 = new Texture(renderer, asset->get("item_points2_gavina.png")); - itemTextures.push_back(item2); + auto item2 = std::make_unique(renderer, Asset::get()->get("item_points2_gavina.png")); + itemTextures.push_back(std::move(item2)); - Texture *item3 = new Texture(renderer, asset->get("item_points3_pacmar.png")); - itemTextures.push_back(item3); + auto item3 = std::make_unique(renderer, Asset::get()->get("item_points3_pacmar.png")); + itemTextures.push_back(std::move(item3)); - Texture *item4 = new Texture(renderer, asset->get("item_clock.png")); - itemTextures.push_back(item4); + auto item4 = std::make_unique(renderer, Asset::get()->get("item_clock.png")); + itemTextures.push_back(std::move(item4)); - Texture *item5 = new Texture(renderer, asset->get("item_coffee.png")); - itemTextures.push_back(item5); + auto item5 = std::make_unique(renderer, Asset::get()->get("item_coffee.png")); + itemTextures.push_back(std::move(item5)); // Inicializa los sprites for (int i = 0; i < (int)itemTextures.size(); ++i) { - Sprite *sprite = new Sprite(0, 0, param.game.itemSize, param.game.itemSize, itemTextures[i]); + auto sprite = std::make_unique(0, 0, param.game.itemSize, param.game.itemSize, itemTextures[i].get()); sprite->setPos((SDL_Point){spritePos.x, spritePos.y + ((param.game.itemSize + itemSpace) * i)}); - sprites.push_back(sprite); + sprites.push_back(std::move(sprite)); } } @@ -229,7 +210,7 @@ void Instructions::fillBackbuffer() SDL_RenderCopy(renderer, texture, nullptr, nullptr); // Dibuja los sprites - for (auto sprite : sprites) + for (auto &sprite : sprites) { sprite->render(); } @@ -252,7 +233,7 @@ void Instructions::update() JA_PlayMusic(music); // Actualiza el objeto screen - screen->update(); + Screen::get()->update(); // Incrementa el contador counter++; @@ -282,10 +263,10 @@ void Instructions::render() fillBackbuffer(); // Prepara para empezar a dibujar en la textura de juego - screen->start(); + Screen::get()->start(); // Limpia la pantalla - screen->clean(bgColor); + Screen::get()->clean(bgColor); // Dibuja el mosacico de fondo tiledbg->render(); @@ -299,13 +280,13 @@ void Instructions::render() fade->render(); // Vuelca el contenido del renderizador en pantalla - screen->blit(); + Screen::get()->blit(); } // Recarga todas las texturas void Instructions::reloadTextures() { - for (auto tex : itemTextures) + for (auto &tex : itemTextures) { tex->reLoad(); } @@ -317,7 +298,7 @@ void Instructions::reloadTextures() void Instructions::checkEvents() { // 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) @@ -341,7 +322,7 @@ void Instructions::checkEvents() void Instructions::checkInput() { // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) - if (input->checkAnyButtonPressed()) + if (Input::get()->checkAnyButtonPressed()) { JA_StopMusic(); section::name = section::NAME_TITLE; @@ -350,7 +331,7 @@ void Instructions::checkInput() } // Comprueba el input para el resto de objetos - screen->checkInput(); + Screen::get()->checkInput(); // Comprueba los inputs que se pueden introducir en cualquier sección del juego globalInputs::check(); diff --git a/source/instructions.h b/source/instructions.h index ae04170..049a13a 100644 --- a/source/instructions.h +++ b/source/instructions.h @@ -1,18 +1,16 @@ #pragma once -#include // for SDL_Event -#include // for SDL_Point, SDL_Rect -#include // for SDL_Texture, SDL_Renderer -#include // for Uint32 -#include // for vector -class Asset; -class Fade; -class Input; -class Screen; -class Sprite; -class Text; -class Texture; -class Tiledbg; +#include // for SDL_Event +#include // for SDL_Point, SDL_Rect +#include // for SDL_Texture, SDL_Renderer +#include // for Uint32 +#include // for vector +#include +#include "fade.h" +#include "sprite.h" +#include "text.h" +#include "texture.h" +#include "tiled_bg.h" struct JA_Music_t; /* @@ -33,19 +31,17 @@ class Instructions { private: // Objetos y punteros - SDL_Renderer *renderer; // El renderizador de la ventana - Screen *screen; // Objeto encargado de dibujar en pantalla - std::vector itemTextures; // Vector con las texturas de los items - std::vector sprites; // Vector con los sprites de los items - SDL_Event *eventHandler; // Manejador de eventos - SDL_Texture *backbuffer; // Textura para usar como backbuffer - SDL_Texture *texture; // Textura fija con el texto - Asset *asset; // Objeto que gestiona todos los ficheros de recursos - Input *input; // Objeto pata gestionar la entrada - Text *text; // Objeto para escribir texto - Tiledbg *tiledbg; // Objeto para dibujar el mosaico animado de fondo - Fade *fade; // Objeto para renderizar fades - JA_Music_t *music; // Musica de fondo + std::vector> itemTextures; // Vector con las texturas de los items + std::vector> sprites; // Vector con los sprites de los items + std::unique_ptr eventHandler; // Manejador de eventos + std::unique_ptr text; // Objeto para escribir texto + std::unique_ptr tiledbg; // Objeto para dibujar el mosaico animado de fondo + std::unique_ptr fade; // Objeto para renderizar fades + + SDL_Renderer *renderer; // El renderizador de la ventana + JA_Music_t *music; // Musica de fondo + SDL_Texture *texture; // Textura fija con el texto + SDL_Texture *backbuffer; // Textura para usar como backbuffer // Variables int counter; // Contador diff --git a/source/logo.cpp b/source/logo.cpp index 87f062a..19345c4 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -17,16 +17,13 @@ Logo::Logo() { // Copia la dirección de los objetos - input = Input::get(); - screen = Screen::get(); - asset = Asset::get(); - SDL_Renderer *renderer = screen->getRenderer(); + SDL_Renderer *renderer = Screen::get()->getRenderer(); // Reserva memoria para los punteros - eventHandler = new SDL_Event(); - jailTexture = new Texture(renderer, asset->get("logo_jailgames.png")); - sinceTexture = new Texture(renderer, asset->get("logo_since_1998.png")); - sinceSprite = new Sprite((param.game.width - sinceTexture->getWidth()) / 2, 83 + jailTexture->getHeight() + 5, sinceTexture->getWidth(), sinceTexture->getHeight(), sinceTexture); + eventHandler = std::make_unique(); + jailTexture = std::make_unique(renderer, Asset::get()->get("logo_jailgames.png")); + sinceTexture = std::make_unique(renderer, Asset::get()->get("logo_since_1998.png")); + sinceSprite = std::make_unique((param.game.width - sinceTexture->getWidth()) / 2, 83 + jailTexture->getHeight() + 5, sinceTexture->getWidth(), sinceTexture->getHeight(), sinceTexture.get()); // Inicializa variables counter = 0; @@ -48,12 +45,12 @@ Logo::Logo() // Crea los sprites de cada linea for (int i = 0; i < jailTexture->getHeight(); ++i) { - Sprite *temp = new Sprite(0, i, jailTexture->getWidth(), 1, jailTexture); + auto temp = std::make_unique(0, i, jailTexture->getWidth(), 1, jailTexture.get()); temp->setSpriteClip(0, i, jailTexture->getWidth(), 1); const int posX = (i % 2 == 0) ? param.game.width + (i * 3) : -jailTexture->getWidth() - (i * 3); temp->setPosX(posX); temp->setPosY(dest.y + i); - jailSprite.push_back(temp); + jailSprite.push_back(std::move(temp)); } // Inicializa el vector de colores @@ -67,18 +64,6 @@ Logo::Logo() color.push_back({0xFF, 0xFF, 0xFF}); // Bright white } -// Destructor -Logo::~Logo() -{ - for (auto sprite : jailSprite) - { - delete sprite; - } - - delete sinceSprite; - delete eventHandler; -} - // Recarga todas las texturas void Logo::reloadTextures() { @@ -90,7 +75,7 @@ void Logo::reloadTextures() void Logo::checkEvents() { // 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) @@ -114,7 +99,7 @@ void Logo::checkEvents() void Logo::checkInput() { // Comprueba si se ha pulsado cualquier botón (de los usados para jugar) - if (input->checkAnyButtonPressed()) + if (Input::get()->checkAnyButtonPressed()) { JA_StopMusic(); section::name = section::NAME_TITLE; @@ -123,7 +108,7 @@ void Logo::checkInput() } // Comprueba el input para el resto de objetos - screen->checkInput(); + Screen::get()->checkInput(); // Comprueba los inputs que se pueden introducir en cualquier sección del juego globalInputs::check(); @@ -162,91 +147,29 @@ void Logo::updateJAILGAMES() // Gestiona el color de las texturas void Logo::updateTextureColors() { - const int inc = 4; + constexpr int inc = 4; - if (counter <= showSinceSprite_cm + inc * 0) - { - sinceTexture->setColor(color[0].r, color[0].g, color[0].b); - } + // Manejo de 'sinceTexture' + for (int i = 0; i <= 7; ++i) + { + if (counter == showSinceSprite_cm + inc * i) + { + sinceTexture->setColor(color[i].r, color[i].g, color[i].b); + } + } - else if (counter == showSinceSprite_cm + inc * 1) - { - sinceTexture->setColor(color[1].r, color[1].g, color[1].b); - } - - else if (counter == showSinceSprite_cm + inc * 2) - { - sinceTexture->setColor(color[2].r, color[2].g, color[2].b); - } - - else if (counter == showSinceSprite_cm + inc * 3) - { - sinceTexture->setColor(color[3].r, color[3].g, color[3].b); - } - - else if (counter == showSinceSprite_cm + inc * 4) - { - sinceTexture->setColor(color[4].r, color[4].g, color[4].b); - } - - else if (counter == showSinceSprite_cm + inc * 5) - { - sinceTexture->setColor(color[5].r, color[5].g, color[5].b); - } - - else if (counter == showSinceSprite_cm + inc * 6) - { - sinceTexture->setColor(color[6].r, color[6].g, color[6].b); - } - - else if (counter == showSinceSprite_cm + inc * 7) - { - sinceTexture->setColor(color[7].r, color[7].g, color[7].b); - } - - else if (counter == initFade_cm + inc * 0) - { - jailTexture->setColor(color[6].r, color[6].g, color[6].b); - sinceTexture->setColor(color[6].r, color[6].g, color[6].b); - } - - else if (counter == initFade_cm + inc * 1) - { - jailTexture->setColor(color[5].r, color[5].g, color[5].b); - sinceTexture->setColor(color[5].r, color[5].g, color[5].b); - } - - else if (counter == initFade_cm + inc * 2) - { - jailTexture->setColor(color[4].r, color[4].g, color[4].b); - sinceTexture->setColor(color[4].r, color[4].g, color[4].b); - } - - else if (counter == initFade_cm + inc * 3) - { - jailTexture->setColor(color[3].r, color[3].g, color[3].b); - sinceTexture->setColor(color[3].r, color[3].g, color[3].b); - } - - else if (counter == initFade_cm + inc * 4) - { - jailTexture->setColor(color[2].r, color[2].g, color[2].b); - sinceTexture->setColor(color[2].r, color[2].g, color[2].b); - } - - else if (counter == initFade_cm + inc * 5) - { - jailTexture->setColor(color[1].r, color[1].g, color[1].b); - sinceTexture->setColor(color[1].r, color[1].g, color[1].b); - } - - else if (counter == initFade_cm + inc * 6) - { - jailTexture->setColor(color[0].r, color[0].g, color[0].b); - sinceTexture->setColor(color[0].r, color[0].g, color[0].b); - } + // Manejo de 'jailTexture' y 'sinceTexture' en el fade + for (int i = 0; i <= 6; ++i) + { + if (counter == initFade_cm + inc * i) + { + jailTexture->setColor(color[6 - i].r, color[6 - i].g, color[6 - i].b); + sinceTexture->setColor(color[6 - i].r, color[6 - i].g, color[6 - i].b); + } + } } + // Actualiza las variables void Logo::update() { @@ -257,7 +180,7 @@ void Logo::update() ticks = SDL_GetTicks(); // Actualiza el objeto screen - screen->update(); + Screen::get()->update(); // Comprueba las entradas checkInput(); @@ -289,20 +212,20 @@ void Logo::update() void Logo::render() { // Prepara para empezar a dibujar en la textura de juego - screen->start(); + Screen::get()->start(); // Limpia la pantalla - screen->clean(); + Screen::get()->clean(); // Dibuja los sprites - for (auto sprite : jailSprite) + for (auto &sprite : jailSprite) { sprite->render(); } sinceSprite->render(); // Vuelca el contenido del renderizador en pantalla - screen->blit(); + Screen::get()->blit(); } // Bucle para el logo del juego diff --git a/source/logo.h b/source/logo.h index 7b6324e..469d260 100644 --- a/source/logo.h +++ b/source/logo.h @@ -1,15 +1,13 @@ #pragma once -#include // for SDL_Event -#include // for SDL_Point -#include // for Uint32 -#include // for vector -#include "utils.h" // for color_t -class Asset; -class Input; -class Screen; -class Sprite; -class Texture; +#include // for SDL_Event +#include // for SDL_Point +#include // for Uint32 +#include // for vector +#include +#include "utils.h" // for color_t +#include "sprite.h" +#include "texture.h" /* Esta clase gestiona un estado del programa. Se encarga de dibujar por pantalla el @@ -24,14 +22,11 @@ class Logo { private: // Objetos y punteros - Screen *screen; // Objeto encargado de dibujar en pantalla - Asset *asset; // Objeto con los ficheros de recursos - Input *input; // Objeto pata gestionar la entrada - Texture *jailTexture; // Textura con los graficos "JAILGAMES" - Texture *sinceTexture; // Textura con los graficos "Since 1998" - SDL_Event *eventHandler; // Manejador de eventos - std::vector jailSprite; // Vector con los sprites de cada linea que forman el bitmap JAILGAMES - Sprite *sinceSprite; // Sprite para manejar la sinceTexture + std::unique_ptr eventHandler; // Manejador de eventos + std::unique_ptr sinceTexture; // Textura con los graficos "Since 1998" + std::unique_ptr sinceSprite; // Sprite para manejar la sinceTexture + std::unique_ptr jailTexture; // Textura con los graficos "JAILGAMES" + std::vector> jailSprite; // Vector con los sprites de cada linea que forman el bitmap JAILGAMES // Variables std::vector color; // Vector con los colores para el fade @@ -71,7 +66,7 @@ public: Logo(); // Destructor - ~Logo(); + ~Logo() = default; // Bucle principal void run(); diff --git a/source/notify.cpp b/source/notify.cpp index 4202d23..93f5dc1 100644 --- a/source/notify.cpp +++ b/source/notify.cpp @@ -1,34 +1,30 @@ #include "notify.h" -#include // for SDL_BLENDMODE_BLEND -#include // for SDL_PIXELFORMAT_RGBA8888 -#include // for basic_string, char_traits, string -#include "jail_audio.h" // for JA_DeleteSound, JA_LoadSound, JA_Pla... -#include "options.h" // for options +#include // for SDL_BLENDMODE_BLEND +#include // for SDL_PIXELFORMAT_RGBA8888 +#include // for basic_string, char_traits, string +#include "jail_audio.h" // for JA_DeleteSound, JA_LoadSound, JA_Pla... +#include "options.h" // for options #include "param.h" -#include "sprite.h" // for Sprite -#include "text.h" // for Text -#include "texture.h" // for Texture +#include "sprite.h" // for Sprite +#include "text.h" // for Text +#include "texture.h" // for Texture // Constructor -Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile) +Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile) : renderer(renderer) { // Inicializa variables - this->renderer = renderer; bgColor = options.notification.color; waitTime = 150; stack = false; hasIcons = iconFile == "" ? false : true; - iconTexture = nullptr; - textTexture = nullptr; - text = nullptr; // Crea objetos if (hasIcons) { - iconTexture = new Texture(renderer, iconFile); + iconTexture = std::make_unique(renderer, iconFile); } - textTexture = new Texture(renderer, bitmapFile); - text = new Text(textFile, textTexture, renderer); + textTexture = std::make_unique(renderer, bitmapFile); + text = std::make_unique(textFile, textTexture.get()); sound = JA_LoadSound(soundFile.c_str()); } @@ -36,22 +32,9 @@ Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapF Notify::~Notify() { // Libera la memoria de los objetos - if (textTexture) - delete textTexture; - - if (iconTexture) - delete iconTexture; - - if (text) - delete text; - JA_DeleteSound(sound); - for (auto notification : notifications) - { - delete notification.sprite; - delete notification.texture; - } + notifications.clear(); } // Dibuja las notificaciones por pantalla @@ -158,8 +141,6 @@ void Notify::clearFinishedNotifications() { if (notifications[i].state == ns_finished) { - delete notifications[i].sprite; - delete notifications[i].texture; notifications.erase(notifications.begin() + i); } } @@ -171,9 +152,13 @@ void Notify::showText(std::string text1, std::string text2, int icon) // Cuenta el número de textos a mostrar int numTexts = 0; if (text1 != "") + { numTexts++; + } if (text2 != "") + { numTexts++; + } // Si no hay texto, acaba if (numTexts == 0) @@ -194,8 +179,8 @@ void Notify::showText(std::string text1, std::string text2, int icon) } // Inicializa variables - const int iconSize = 16; - const int paddingOut = 1; + constexpr int iconSize = 16; + constexpr int paddingOut = 1; const int paddingIn = text->getCharacterSize() / 2; const int iconSpace = icon >= 0 ? iconSize + paddingIn : 0; const std::string txt = text1.length() > text2.length() ? text1 : text2; @@ -263,7 +248,7 @@ void Notify::showText(std::string text1, std::string text2, int icon) } // Crea la textura - n.texture = new Texture(renderer); + n.texture = std::make_unique(renderer); n.texture->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); n.texture->setBlendMode(SDL_BLENDMODE_BLEND); @@ -296,11 +281,10 @@ void Notify::showText(std::string text1, std::string text2, int icon) // Dibuja el icono de la notificación if (hasIcons && icon >= 0 && numTexts == 2) { - Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, iconTexture); + auto sp = std::make_unique((SDL_Rect){0, 0, iconSize, iconSize}, iconTexture.get()); sp->setPos({paddingIn, paddingIn, iconSize, iconSize}); sp->setSpriteClip({iconSize * (icon % 10), iconSize * (icon / 10), iconSize, iconSize}); sp->render(); - delete sp; } // Escribe el texto de la notificación @@ -319,7 +303,7 @@ void Notify::showText(std::string text1, std::string text2, int icon) SDL_SetRenderTarget(renderer, nullptr); // Crea el sprite de la notificación - n.sprite = new Sprite(n.rect, n.texture); + n.sprite = std::make_unique(n.rect, n.texture); // Deja la notificación invisible n.texture->setAlpha(0); diff --git a/source/notify.h b/source/notify.h index 45c2f0a..3a671c5 100644 --- a/source/notify.h +++ b/source/notify.h @@ -4,15 +4,13 @@ #include // for SDL_Renderer #include // for basic_string, string #include // for vector +#include #include "utils.h" // for color_t -class Sprite; -class Text; -class Texture; +#include "text.h" +#include "texture.h" +#include "sprite.h" struct JA_Sound_t; -#ifndef NOTIFY_H -#define NOTIFY_H - class Notify { private: @@ -49,8 +47,8 @@ private: int counter; notification_state_e state; notification_position_e position; - Texture *texture; - Sprite *sprite; + std::unique_ptr texture; + std::unique_ptr sprite; SDL_Rect rect; int y; int travelDist; @@ -59,9 +57,10 @@ private: // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana - Texture *textTexture; // Textura para la fuente de las notificaciones - Texture *iconTexture; // Textura para los iconos de las notificaciones - Text *text; // Objeto para dibujar texto + + std::unique_ptr textTexture; // Textura para la fuente de las notificaciones + std::unique_ptr iconTexture; // Textura para los iconos de las notificaciones + std::unique_ptr text; // Objeto para dibujar texto // Variables color_t bgColor; // Color de fondo de las notificaciones @@ -96,5 +95,3 @@ public: // Indica si hay notificaciones activas bool active(); }; - -#endif diff --git a/source/player.cpp b/source/player.cpp index 9b88aa6..193fab0 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -14,10 +14,10 @@ Player::Player(int id, float x, int y, bool demo, SDL_Rect *playArea, std::vector texture, std::vector *> animations) { // Reserva memoria para los objetos - playerSprite = std::unique_ptr(new AnimatedSprite(texture[0], "", animations[0])); - powerSprite = std::unique_ptr(new AnimatedSprite(texture[1], "", animations[1])); + playerSprite = std::make_unique(texture[0], "", animations[0]); + powerSprite = std::make_unique(texture[1], "", animations[1]); powerSprite->getTexture()->setAlpha(224); - enterName = std::unique_ptr(new EnterName()); + enterName = std::make_unique(); // Rectangulo con la zona de juego this->playArea = playArea; diff --git a/source/text.cpp b/source/text.cpp index 9ec9182..e2b9a6d 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -95,15 +95,15 @@ Text::Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer) } // Crea los objetos - texture = new Texture(renderer, bitmapFile); - sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture); + texture = std::make_unique(renderer, bitmapFile); + sprite = std::make_unique((SDL_Rect){0, 0, boxWidth, boxHeight}, texture.get()); // Inicializa variables fixedWidth = false; } // Constructor -Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer) +Text::Text(std::string textFile, Texture *texture) { // Carga los offsets desde el fichero textFile_t tf = LoadTextFile(textFile); @@ -119,15 +119,14 @@ Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer) } // Crea los objetos - this->texture = nullptr; - sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture); + sprite = std::make_unique((SDL_Rect){0, 0, boxWidth, boxHeight}); // Inicializa variables fixedWidth = false; } // Constructor -Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer) +Text::Text(textFile_t *textFile, Texture *texture) { // Inicializa variables desde la estructura boxHeight = textFile->boxHeight; @@ -140,23 +139,12 @@ Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer) } // Crea los objetos - this->texture = nullptr; - sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture); + sprite = std::make_unique((SDL_Rect){0, 0, boxWidth, boxHeight}); // Inicializa variables fixedWidth = false; } -// Destructor -Text::~Text() -{ - delete sprite; - if (texture != nullptr) - { - delete texture; - } -} - // Escribe texto en pantalla void Text::write(int x, int y, std::string text, int kerning, int lenght) { @@ -259,7 +247,7 @@ int Text::lenght(std::string text, int kerning) } // Devuelve el valor de la variable -int Text::getCharacterSize() +int Text::getCharacterSize() const { return boxWidth; } diff --git a/source/text.h b/source/text.h index 9c6859d..10dffbe 100644 --- a/source/text.h +++ b/source/text.h @@ -3,9 +3,10 @@ #include // for SDL_Renderer #include // for Uint8 #include // for string +#include #include "utils.h" -class Sprite; // lines 6-6 -class Texture; // lines 7-7 +#include "sprite.h" +#include "texture.h" #define TXT_COLOR 1 #define TXT_SHADOW 2 @@ -14,9 +15,7 @@ class Texture; // lines 7-7 struct offset_t { - int x; - int y; - int w; + int x, y, w; }; struct textFile_t @@ -34,8 +33,8 @@ class Text { private: // Objetos y punteros - Sprite *sprite; // Objeto con los graficos para el texto - Texture *texture; // Textura con los bitmaps del texto + std::unique_ptr sprite; // Objeto con los graficos para el texto + std::unique_ptr texture; // Textura con los bitmaps del texto // Variables int boxWidth; // Anchura de la caja de cada caracter en el png @@ -46,11 +45,11 @@ private: public: // Constructor Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer); - Text(std::string textFile, Texture *texture, SDL_Renderer *renderer); - Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer); + Text(std::string textFile, Texture *texture); + Text(textFile_t *textFile, Texture *texture); // Destructor - ~Text(); + ~Text() = default; // Escribe el texto en pantalla void write(int x, int y, std::string text, int kerning = 1, int lenght = -1); @@ -71,7 +70,7 @@ public: int lenght(std::string text, int kerning = 1); // Devuelve el valor de la variable - int getCharacterSize(); + int getCharacterSize() const; // Recarga la textura void reLoadTexture();