From c00f4326ae59b37c1f73d0512046e8d35ea6c41b Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 8 Oct 2024 18:07:54 +0200 Subject: [PATCH] Commit de "guardar partida" --- source/notify.cpp | 57 +++++++++++++++++++++-------------------------- source/notify.h | 48 ++++++++++++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/source/notify.cpp b/source/notify.cpp index 93f5dc1..1e20e67 100644 --- a/source/notify.cpp +++ b/source/notify.cpp @@ -10,7 +10,8 @@ #include "texture.h" // for Texture // Constructor -Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile) : renderer(renderer) +Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile) + : renderer(renderer) { // Inicializa variables bgColor = options.notification.color; @@ -23,8 +24,7 @@ Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapF { iconTexture = std::make_unique(renderer, iconFile); } - textTexture = std::make_unique(renderer, bitmapFile); - text = std::make_unique(textFile, textTexture.get()); + text = std::make_unique(bitmapFile, textFile, renderer); sound = JA_LoadSound(soundFile.c_str()); } @@ -227,35 +227,15 @@ void Notify::showText(std::string text1, std::string text2, int icon) offset = (int)notifications.size() > 0 ? notifications.back().y - travelDist : despV; } - // Crea la notificacion - notification_t n; - - // Inicializa variables - n.y = offset; - n.travelDist = travelDist; - n.counter = 0; - n.state = ns_rising; - n.text1 = text1; - n.text2 = text2; - n.shape = shape; - if (options.notification.posV == pos_top) - { - n.rect = {despH, offset - travelDist, width, height}; - } - else - { - n.rect = {despH, offset + travelDist, width, height}; - } - - // Crea la textura - n.texture = std::make_unique(renderer); - n.texture->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); - n.texture->setBlendMode(SDL_BLENDMODE_BLEND); + // Crea la textura de fondo de la notificación + auto texture = std::make_unique(renderer); + texture->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); + texture->setBlendMode(SDL_BLENDMODE_BLEND); // Prepara para dibujar en la textura - n.texture->setAsRenderTarget(renderer); + texture->setAsRenderTarget(renderer); - // Dibuja el fondo de la notificación + // Dibuja fondo de la notificación sobre la textura SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255); SDL_Rect rect; if (shape == notification_shape_rounded) @@ -303,13 +283,28 @@ void Notify::showText(std::string text1, std::string text2, int icon) SDL_SetRenderTarget(renderer, nullptr); // Crea el sprite de la notificación - n.sprite = std::make_unique(n.rect, n.texture); + auto sprite = std::make_unique((SDL_Rect){despH, 50, width, height}, texture.get()); + + // Crea la notificacion + notification_t n(std::move(texture), std::move(sprite)); + + // Inicializa variables + n.y = offset; + n.travelDist = travelDist; + n.counter = 0; + n.state = ns_rising; + n.text1 = text1; + n.text2 = text2; + n.shape = shape; + const int yPos = offset + (options.notification.posV == pos_top ? -travelDist : travelDist); + n.rect = {despH, yPos, width, height}; + n.sprite->setPos(n.rect); // Deja la notificación invisible n.texture->setAlpha(0); // Añade la notificación a la lista - notifications.push_back(n); + notifications.push_back(std::move(n)); } // Indica si hay notificaciones activas diff --git a/source/notify.h b/source/notify.h index 3a671c5..3350c89 100644 --- a/source/notify.h +++ b/source/notify.h @@ -1,11 +1,11 @@ #pragma once -#include // for SDL_Rect -#include // for SDL_Renderer -#include // for basic_string, string -#include // for vector +#include // for SDL_Rect +#include // for SDL_Renderer +#include // for basic_string, string +#include // for vector #include -#include "utils.h" // for color_t +#include "utils.h" // for color_t #include "text.h" #include "texture.h" #include "sprite.h" @@ -42,25 +42,51 @@ private: struct notification_t { + std::unique_ptr texture; + std::unique_ptr sprite; std::string text1; std::string text2; int counter; notification_state_e state; notification_position_e position; - std::unique_ptr texture; - std::unique_ptr sprite; SDL_Rect rect; int y; int travelDist; notification_shape_t shape; + + // Constructor + notification_t(std::unique_ptr texture, std::unique_ptr sprite) + : texture(std::move(texture)), sprite(std::move(sprite)) {} + + // Constructor de movimiento + notification_t(notification_t &&other) noexcept + : texture(std::move(other.texture)), sprite(std::move(other.sprite)) + { + // Mover otros miembros si es necesario + } + + // Operador de asignación por movimiento + notification_t &operator=(notification_t &&other) noexcept + { + if (this != &other) + { + texture = std::move(other.texture); + sprite = std::move(other.sprite); + // Mover otros miembros si es necesario + } + return *this; + } + + // Deshabilitar el constructor de copia y operador de asignación por copia + notification_t(const notification_t &) = delete; + notification_t &operator=(const notification_t &) = delete; }; // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana - - 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 + + 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