Optimitzats alguns bucles en les notificacions

This commit is contained in:
2025-03-08 11:38:17 +01:00
parent 195d812d2a
commit 3a36bf6530

View File

@@ -45,46 +45,47 @@ Notifier::Notifier(const std::string &icon_file, const std::string &text)
// Dibuja las notificaciones por pantalla // Dibuja las notificaciones por pantalla
void Notifier::render() void Notifier::render()
{ {
for (int i = (int)notifications_.size() - 1; i >= 0; --i) for (auto it = notifications_.rbegin(); it != notifications_.rend(); ++it)
{ {
notifications_[i].sprite->render(); it->sprite->render();
} }
} }
// Actualiza el estado de las notificaiones // Actualiza el estado de las notificaiones
void Notifier::update() void Notifier::update()
{ {
for (int i = 0; i < (int)notifications_.size(); ++i) for (auto &notification : notifications_)
{ {
// Si la notificación anterior está "saliendo", no hagas nada // Si la notificación anterior está "saliendo", no hagas nada
if (i > 0) if (!notifications_.empty() && &notification != &notifications_.front())
{ {
if (notifications_[i - 1].state == NotificationStatus::RISING) auto &previous_notification = *(std::prev(&notification));
if (previous_notification.state == NotificationStatus::RISING)
{ {
break; break;
} }
} }
switch (notifications_[i].state) switch (notification.state)
{ {
case NotificationStatus::RISING: case NotificationStatus::RISING:
{ {
const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1; const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1;
notifications_[i].rect.y += DIRECTION; notification.rect.y += DIRECTION;
if (notifications_[i].rect.y == notifications_[i].y) if (notification.rect.y == notification.y)
{ {
notifications_[i].state = NotificationStatus::STAY; notification.state = NotificationStatus::STAY;
notifications_[i].start_time = SDL_GetTicks(); notification.start_time = SDL_GetTicks();
} }
break; break;
} }
case NotificationStatus::STAY: case NotificationStatus::STAY:
{ {
notifications_[i].elapsed_time = SDL_GetTicks() - notifications_[i].start_time; notification.elapsed_time = SDL_GetTicks() - notification.start_time;
if (notifications_[i].elapsed_time >= notifications_[i].display_duration) if (notification.elapsed_time >= notification.display_duration)
{ {
notifications_[i].state = NotificationStatus::VANISHING; notification.state = NotificationStatus::VANISHING;
} }
break; break;
} }
@@ -92,11 +93,11 @@ void Notifier::update()
case NotificationStatus::VANISHING: case NotificationStatus::VANISHING:
{ {
const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -1 : 1; const int DIRECTION = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? -1 : 1;
notifications_[i].rect.y += DIRECTION; notification.rect.y += DIRECTION;
if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist) if (notification.rect.y == notification.y - notification.travel_dist)
{ {
notifications_[i].state = NotificationStatus::FINISHED; notification.state = NotificationStatus::FINISHED;
} }
break; break;
} }
@@ -108,7 +109,7 @@ void Notifier::update()
break; break;
} }
notifications_[i].sprite->setPosition(notifications_[i].rect); notification.sprite->setPosition(notification.rect);
} }
clearFinishedNotifications(); clearFinishedNotifications();
@@ -117,13 +118,13 @@ void Notifier::update()
// Elimina las notificaciones finalizadas // Elimina las notificaciones finalizadas
void Notifier::clearFinishedNotifications() void Notifier::clearFinishedNotifications()
{ {
for (int i = (int)notifications_.size() - 1; i >= 0; --i) notifications_.erase(
std::remove_if(notifications_.begin(), notifications_.end(),
[](const Notification &notification)
{ {
if (notifications_[i].state == NotificationStatus::FINISHED) return notification.state == NotificationStatus::FINISHED;
{ }),
notifications_.erase(notifications_.begin() + i); notifications_.end());
}
}
} }
void Notifier::show(std::vector<std::string> texts, NotificationText text_is, Uint32 display_duration, int icon, bool can_be_removed, const std::string &code) void Notifier::show(std::vector<std::string> texts, NotificationText text_is, Uint32 display_duration, int icon, bool can_be_removed, const std::string &code)
@@ -194,7 +195,6 @@ void Notifier::show(std::vector<std::string> texts, NotificationText text_is, Ui
const int TRAVEL_MOD = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1; const int TRAVEL_MOD = (options.notifications.getVerticalPosition() == NotificationPosition::TOP) ? 1 : -1;
const int OFFSET = !notifications_.empty() ? notifications_.back().y + TRAVEL_MOD * notifications_.back().travel_dist : DESP_V; const int OFFSET = !notifications_.empty() ? notifications_.back().y + TRAVEL_MOD * notifications_.back().travel_dist : DESP_V;
// Crea la notificacion // Crea la notificacion
Notification n; Notification n;