use map instead of linear search to find texture indices for animation

This commit is contained in:
Ricardo Luís Vaz Silva 2024-01-09 03:51:01 -03:00 committed by Rachael Alexanderson
parent 3bd80ab8f6
commit 8ae93fb87f
4 changed files with 42 additions and 22 deletions

View file

@ -1,4 +1,5 @@
#pragma once
#define TEXTUREID_H
#include <cstddef>
@ -66,3 +67,13 @@ public:
constexpr FSetTextureID(int v) : FTextureID(v) {}
};
#ifdef TARRAY_H
template<> struct THashTraits<FTextureID>
{
hash_t Hash(const FTextureID key) { return (hash_t)key.GetIndex(); }
// Compares two keys, returning zero if they are the same.
int Compare(const FTextureID left, const FTextureID right) { return left != right; }
};
#endif

View file

@ -1,4 +1,5 @@
#pragma once
#define TARRAY_H
/*
** tarray.h
** Templated, automatically resizing array
@ -943,6 +944,17 @@ template<class KT> struct THashTraits
int Compare(const KT left, const KT right) { return left != right; }
};
#ifdef TEXTUREID_H
template<> struct THashTraits<FTextureID>
{
hash_t Hash(const FTextureID key) { return (hash_t)key.GetIndex(); }
// Compares two keys, returning zero if they are the same.
int Compare(const FTextureID left, const FTextureID right) { return left != right; }
};
#endif
template<> struct THashTraits<float>
{
// Use all bits when hashing singles instead of converting them to ints.

View file

@ -88,18 +88,19 @@ void FTextureAnimator::DeleteAll()
FAnimDef *FTextureAnimator::AddAnim (FAnimDef& anim)
{
// Search for existing duplicate.
for (unsigned int i = 0; i < mAnimations.Size(); ++i)
{
if (mAnimations[i].BasePic == anim.BasePic)
{
// Found one!
mAnimations[i] = anim;
return &mAnimations[i];
}
uint16_t * index = mAnimationIndices.CheckKey(anim.BasePic);
if(index)
{ // Found one!
mAnimations[*index] = anim;
return &mAnimations[*index];
}
else
{ // Didn't find one, so add it at the end.
mAnimationIndices.Insert(anim.BasePic, mAnimations.Size());
mAnimations.Push (anim);
return &mAnimations.Last();
}
// Didn't find one, so add it at the end.
mAnimations.Push (anim);
return &mAnimations.Last();
}
//==========================================================================
@ -982,18 +983,13 @@ constexpr double msPerTic = 1'000.0 / TICRATE;
bool FTextureAnimator::InitStandaloneAnimation(FStandaloneAnimation &animInfo, FTextureID tex, uint32_t curTic)
{
FAnimDef * anim;
animInfo.ok = false;
for(unsigned i = 0; i < mAnimations.Size(); i++)
{
if(mAnimations[i].BasePic == tex)
{
animInfo.ok = true;
animInfo.AnimIndex = i;
anim = &mAnimations[i];
}
}
if(!animInfo.ok) return false;
uint16_t * index = mAnimationIndices.CheckKey(tex);
if(!index) return false;
FAnimDef * anim = &mAnimations[*index];
animInfo.ok = true;
animInfo.AnimIndex = *index;
animInfo.CurFrame = 0;
animInfo.SwitchTic = curTic;
animInfo.AnimType = (anim->AnimType == FAnimDef::ANIM_OscillateDown) ? FAnimDef::ANIM_OscillateUp : anim->AnimType;

View file

@ -73,6 +73,7 @@ struct FDoorAnimation
class FTextureAnimator
{
TMap<FTextureID, uint16_t> mAnimationIndices;
TArray<FAnimDef> mAnimations;
TArray<FSwitchDef*> mSwitchDefs;
TArray<FDoorAnimation> mAnimatedDoors;