AnimatedSprite::getIndex() ara gasta un std::unordered_map

This commit is contained in:
2025-07-11 23:10:50 +02:00
parent 66d55061dd
commit 06815d4f72
2 changed files with 14 additions and 8 deletions

View File

@@ -55,20 +55,19 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const Animation
// Obtiene el índice de la animación a partir del nombre
int AnimatedSprite::getIndex(const std::string &name)
{
auto index = -1;
for (const auto &a : animations_)
auto it = animation_indices_.find(name);
if (it != animation_indices_.end())
{
index++;
if (a.name == name)
{
return index;
}
// Si se encuentra la animación en el mapa, devuelve su índice
return it->second;
}
// Si no se encuentra, muestra una advertencia y devuelve -1
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "** Warning: could not find \"%s\" animation", name.c_str());
return -1;
}
// Calcula el frame correspondiente a la animación
void AnimatedSprite::animate()
{
@@ -257,6 +256,9 @@ void AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so
// Añade la animación al vector de animaciones
animations_.emplace_back(animation);
// Rellena el mapa con el nombre y el nuevo índice
animation_indices_[animation.name] = animations_.size() - 1;
}
// Una vez procesada la línea, aumenta el índice para pasar a la siguiente