Skip to content

Commit

Permalink
Optimize|Animation|libcore: Faster animation time checking
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Feb 26, 2017
1 parent 07c5a99 commit 65e11e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 2 additions & 0 deletions doomsday/sdk/libcore/include/de/data/time.h
Expand Up @@ -301,6 +301,8 @@ class DENG2_PUBLIC Time : public ISerializable
*/
dint asBuildNumber() const;

Delta highPerformanceTime() const;

// Implements ISerializable.
void operator >> (Writer &to) const;
void operator << (Reader &from);
Expand Down
12 changes: 12 additions & 0 deletions doomsday/sdk/libcore/src/data/time.cpp
Expand Up @@ -219,6 +219,12 @@ DENG2_PIMPL_NOREF(Time)
DENG2_ASSERT(false);
return 0;
}

void setDateTimeFromHighPerf()
{
dateTime = (highPerfTimer.startedAt() + highPerfElapsed).asDateTime();
flags |= DateTime;
}
};

Time::Time() : d(new Impl)
Expand Down Expand Up @@ -539,6 +545,12 @@ void Time::operator << (Reader &from)
}
}

TimeDelta Time::highPerformanceTime() const
{
DENG2_ASSERT(d->flags & Impl::HighPerformance);
return d->highPerfElapsed;
}

Time Time::currentHighPerformanceTime()
{
return Time(highPerfTimer.elapsed());
Expand Down
24 changes: 10 additions & 14 deletions doomsday/sdk/libcore/src/widgets/animation.cpp
Expand Up @@ -62,10 +62,9 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(AnimationFlags)

/// Thread-safe current time for animations.
struct AnimationTime : DENG2_OBSERVES(Clock, TimeChange) {
LockableT<Time> now;
double now;
void timeChanged(Clock const &clock) override {
DENG2_GUARD(now);
now.value = clock.time();
now = clock.time().highPerformanceTime();
}
};
static AnimationTime theTime;
Expand Down Expand Up @@ -103,7 +102,7 @@ DENG2_PIMPL_NOREF(Animation)
* @param now Point of time at which to evaluate.
* @return Value of the animation when the frame time is @a now.
*/
float valueAt(Time const &now) const
float valueAt(TimeDelta const &now) const
{
TimeDelta span = targetTime - setTime;

Expand All @@ -119,14 +118,14 @@ DENG2_PIMPL_NOREF(Animation)
peak2 = 2.f/3;
}

if (now >= targetTime || span <= 0)
if (now >= targetTime.highPerformanceTime() || span <= 0)
{
return target;
}
else
{
span -= startDelay;
TimeDelta const elapsed = now - setTime - startDelay;
TimeDelta const elapsed = now - setTime.highPerformanceTime() - startDelay;
TimeDelta const t = clamp(0.0, elapsed/span, 1.0);
float const delta = target - value;
switch (style)
Expand Down Expand Up @@ -172,7 +171,7 @@ DENG2_PIMPL_NOREF(Animation)

Time currentTime() const
{
if (flags.testFlag(Paused)) return pauseTime;
if (flags & Paused) return pauseTime;
return Animation::currentTime();
}
};
Expand Down Expand Up @@ -229,7 +228,7 @@ void Animation::setValue(float v, TimeDelta transitionSpan, TimeDelta startDelay
}
else
{
d->value = d->valueAt(now);
d->value = d->valueAt(now.highPerformanceTime());
d->target = v;
d->setTime = now;
d->targetTime = d->setTime + transitionSpan;
Expand All @@ -252,9 +251,8 @@ float Animation::value() const
{
if (d->flags & Paused)
{
return d->valueAt(d->pauseTime);
return d->valueAt(d->pauseTime.highPerformanceTime());
}
DENG2_GUARD_FOR(theTime.now, G);
return d->valueAt(theTime.now);
}

Expand All @@ -264,8 +262,7 @@ bool Animation::done() const
{
return d->pauseTime >= d->targetTime;
}
DENG2_GUARD_FOR(theTime.now, G);
return theTime.now.value >= d->targetTime;
return theTime.now >= d->targetTime.highPerformanceTime();
}

float Animation::target() const
Expand Down Expand Up @@ -385,8 +382,7 @@ Time Animation::currentTime() // static
{
throw ClockMissingError("Animation::clock", "Animation has no clock");
}
DENG2_GUARD_FOR(theTime.now, G);
return theTime.now;
return Time(theTime.now);
}

Animation Animation::range(Style style, float from, float to, TimeDelta span, TimeDelta delay)
Expand Down

0 comments on commit 65e11e7

Please sign in to comment.