Posant make_uniques, s'ha quedat tot enmerdat per culpa d'un struct

This commit is contained in:
2024-10-08 13:53:24 +02:00
parent 9d41d14d68
commit 06a4f439c1
11 changed files with 157 additions and 304 deletions

View File

@@ -1,34 +1,30 @@
#include "notify.h"
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <string> // for basic_string, char_traits, string
#include "jail_audio.h" // for JA_DeleteSound, JA_LoadSound, JA_Pla...
#include "options.h" // for options
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <string> // 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<Texture>(renderer, iconFile);
}
textTexture = new Texture(renderer, bitmapFile);
text = new Text(textFile, textTexture, renderer);
textTexture = std::make_unique<Texture>(renderer, bitmapFile);
text = std::make_unique<Text>(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<Texture>(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<Sprite>((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<Sprite>(n.rect, n.texture);
// Deja la notificación invisible
n.texture->setAlpha(0);