Notifier no gastava Resource per als sons

Resource no alliberava correctament els elements de JailAudio
This commit is contained in:
2024-11-27 18:20:33 +01:00
parent 6ed37425bf
commit eed45bdbc6
4 changed files with 49 additions and 19 deletions

View File

@@ -10,14 +10,15 @@
#include "sprite.h" // Para Sprite #include "sprite.h" // Para Sprite
#include "text.h" // Para Text #include "text.h" // Para Text
#include "texture.h" // Para Texture #include "texture.h" // Para Texture
#include "resource.h"
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado // [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Notifier *Notifier::notifier_ = nullptr; Notifier *Notifier::notifier_ = nullptr;
// [SINGLETON] Crearemos el objeto screen con esta función estática // [SINGLETON] Crearemos el objeto screen con esta función estática
void Notifier::init(const std::string &icon_file, std::shared_ptr<Text> text, const std::string &sound_file) void Notifier::init(const std::string &icon_file, std::shared_ptr<Text> text)
{ {
Notifier::notifier_ = new Notifier(icon_file, text, sound_file); Notifier::notifier_ = new Notifier(icon_file, text);
} }
// [SINGLETON] Destruiremos el objeto screen con esta función estática // [SINGLETON] Destruiremos el objeto screen con esta función estática
@@ -33,21 +34,14 @@ Notifier *Notifier::get()
} }
// Constructor // Constructor
Notifier::Notifier(std::string icon_file, std::shared_ptr<Text> text, const std::string &sound_file) Notifier::Notifier(std::string icon_file, std::shared_ptr<Text> text)
: renderer_(Screen::get()->getRenderer()), : renderer_(Screen::get()->getRenderer()),
icon_texture_(!icon_file.empty() ? std::make_unique<Texture>(renderer_, icon_file) : nullptr), icon_texture_(!icon_file.empty() ? std::make_unique<Texture>(renderer_, icon_file) : nullptr),
text_(text), text_(text),
bg_color_(param.notification.color), bg_color_(param.notification.color),
wait_time_(150), wait_time_(150),
stack_(false), stack_(false),
has_icons_(!icon_file.empty()), has_icons_(!icon_file.empty()) {}
sound_(JA_LoadSound(sound_file.c_str())) {}
// Destructor
Notifier::~Notifier()
{
JA_DeleteSound(sound_);
}
// Dibuja las notificaciones por pantalla // Dibuja las notificaciones por pantalla
void Notifier::render() void Notifier::render()
@@ -80,8 +74,9 @@ void Notifier::update()
if (param.notification.sound) if (param.notification.sound)
{ {
if (notifications_[i].status == NotificationStatus::RISING) if (notifications_[i].status == NotificationStatus::RISING)
{ // Reproduce el sonido de la notificación {
JA_PlaySound(sound_); // Reproduce el sonido de la notificación
JA_PlaySound(Resource::get()->getSound("notify.wav"));
} }
} }
} }

View File

@@ -62,7 +62,6 @@ private:
std::vector<Notification> notifications_; // La lista de notificaciones activas std::vector<Notification> notifications_; // La lista de notificaciones activas
bool stack_; // Indica si las notificaciones se apilan bool stack_; // Indica si las notificaciones se apilan
bool has_icons_; // Indica si el notificador tiene textura para iconos bool has_icons_; // Indica si el notificador tiene textura para iconos
JA_Sound_t *sound_; // Sonido a reproducir cuando suena la notificación
// Elimina las notificaciones finalizadas // Elimina las notificaciones finalizadas
void clearFinishedNotifications(); void clearFinishedNotifications();
@@ -73,14 +72,14 @@ private:
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos notifier desde fuera // [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos notifier desde fuera
// Constructor // Constructor
Notifier(std::string icon_file, std::shared_ptr<Text> text, const std::string &sound_file); Notifier(std::string icon_file, std::shared_ptr<Text> text);
// Destructor // Destructor
~Notifier(); ~Notifier() = default;
public: public:
// [SINGLETON] Crearemos el objeto notifier con esta función estática // [SINGLETON] Crearemos el objeto notifier con esta función estática
static void init(const std::string &icon_file, std::shared_ptr<Text> text, const std::string &sound_file); static void init(const std::string &icon_file, std::shared_ptr<Text> text);
// [SINGLETON] Destruiremos el objeto notifier con esta función estática // [SINGLETON] Destruiremos el objeto notifier con esta función estática
static void destroy(); static void destroy();

View File

@@ -41,8 +41,8 @@ Resource::Resource()
// Vacia todos los vectores de recursos // Vacia todos los vectores de recursos
void Resource::clear() void Resource::clear()
{ {
sounds_.clear(); clearSounds();
musics_.clear(); clearMusics();
textures_.clear(); textures_.clear();
text_files_.clear(); text_files_.clear();
texts_.clear(); texts_.clear();
@@ -332,3 +332,33 @@ void Resource::createText()
printWithDots("Text : ", resource.first, "[ DONE ]"); printWithDots("Text : ", resource.first, "[ DONE ]");
} }
} }
// Vacía el vector de sonidos
void Resource::clearSounds()
{
// Itera sobre el vector y libera los recursos asociados a cada JA_Sound_t
for (auto &sound : sounds_)
{
if (sound.sound)
{
JA_DeleteSound(sound.sound);
sound.sound = nullptr;
}
}
sounds_.clear(); // Limpia el vector después de liberar todos los recursos
}
// Vacía el vector de musicas
void Resource::clearMusics()
{
// Itera sobre el vector y libera los recursos asociados a cada JA_Music_t
for (auto &music : musics_)
{
if (music.music)
{
JA_DeleteMusic(music.music);
music.music = nullptr;
}
}
musics_.clear(); // Limpia el vector después de liberar todos los recursos
}

View File

@@ -123,6 +123,12 @@ private:
// Carga todos los recursos // Carga todos los recursos
void load(); void load();
// Vacía el vector de sonidos
void clearSounds();
// Vacía el vector de musicas
void clearMusics();
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos resource desde fuera // [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos resource desde fuera
// Constructor // Constructor