Skip to content

Commit a5932a9

Browse files
committed
fix: Mutex fixes when animating
1 parent 9bb9346 commit a5932a9

1 file changed

Lines changed: 29 additions & 26 deletions

File tree

src/animate.cc

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,44 @@ float EaseInOut(float t)
4545
return t < 0.5f ? 2 * t * t : -1 + (4 - 2 * t) * t;
4646
}
4747

48-
float EaseInOutFloat(const std::string& id, float lowerBound, float upperBound, bool currentState, float duration)
48+
namespace
4949
{
50-
struct FloatAnimationState
51-
{
52-
bool wasHovered = false;
53-
bool isAnimating = false;
54-
bool isReversing = false;
50+
struct FloatAnimationState
51+
{
52+
bool wasHovered = false;
53+
bool isAnimating = false;
54+
bool isReversing = false;
5555

56-
float elapsedTime = 0.0f;
57-
float currentValue = 0.0f;
58-
float minValue = 0.0f;
59-
float maxValue = 1.0f;
56+
float elapsedTime = 0.0f;
57+
float currentValue = 0.0f;
58+
float minValue = 0.0f;
59+
float maxValue = 1.0f;
6060

61-
float lastXDPI = XDPI, lastYDPI = YDPI;
61+
float lastXDPI = XDPI, lastYDPI = YDPI;
6262

63-
FloatAnimationState() = default;
64-
FloatAnimationState(float lower, float upper)
65-
: wasHovered(false), isAnimating(false), isReversing(false), elapsedTime(0.0f), currentValue(lower), minValue(lower), maxValue(upper)
66-
{
67-
}
68-
};
63+
FloatAnimationState() = default;
64+
FloatAnimationState(float lower, float upper)
65+
: wasHovered(false), isAnimating(false), isReversing(false), elapsedTime(0.0f), currentValue(lower), minValue(lower), maxValue(upper)
66+
{
67+
}
68+
};
6969

70-
static std::unordered_map<std::string, FloatAnimationState> animations;
71-
static std::mutex animationsMutex;
70+
std::unordered_map<std::string, FloatAnimationState> g_floatAnimations;
71+
std::mutex g_floatAnimationsMutex;
72+
} // namespace
7273

73-
std::lock_guard<std::mutex> lock(animationsMutex);
74+
float EaseInOutFloat(const std::string& id, float lowerBound, float upperBound, bool currentState, float duration)
75+
{
76+
std::lock_guard<std::mutex> lock(g_floatAnimationsMutex);
7477

75-
auto it = animations.find(id);
76-
if (it == animations.end()) {
77-
animations[id] = FloatAnimationState(lowerBound, upperBound);
78-
animations[id].currentValue = currentState ? upperBound : lowerBound;
79-
animations[id].wasHovered = currentState;
78+
auto it = g_floatAnimations.find(id);
79+
if (it == g_floatAnimations.end()) {
80+
g_floatAnimations[id] = FloatAnimationState(lowerBound, upperBound);
81+
g_floatAnimations[id].currentValue = currentState ? upperBound : lowerBound;
82+
g_floatAnimations[id].wasHovered = currentState;
8083
}
8184

82-
FloatAnimationState& state = animations[id];
85+
FloatAnimationState& state = g_floatAnimations[id];
8386

8487
float deltaTime = ImGui::GetIO().DeltaTime;
8588

0 commit comments

Comments
 (0)