Skip to content

Commit

Permalink
Optimize the Tween implementation by remove the queueAction.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottcgi committed Aug 16, 2021
1 parent 97201bd commit e760ed2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
1 change: 0 additions & 1 deletion Engine/Toolkit/Math/TweenEase.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ static float Linear(float time)

static float Smooth(float time)
{
ALog_E("Smooth");
return time * time * (3.0f - 2.0f * time);
}

Expand Down
62 changes: 25 additions & 37 deletions Engine/Toolkit/Utils/Tween.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ typedef struct
* Target's current running actions.
*/
ArrayList(TweenAction*) current[1];

/**
* A queue action currently is running in current list.
*/
TweenAction* queueAction;
}
Tween;

Expand Down Expand Up @@ -66,8 +61,6 @@ static inline Tween* GetTween()
AArrayList ->Clear(tween->current);
}

tween->queueAction = NULL;

return tween;
}

Expand Down Expand Up @@ -139,6 +132,20 @@ static inline void SetActionValue(TweenAction* action)
}


static void PopQueueActionToRun(Tween* tween)
{
// get a queue action to run
TweenAction* action = AArrayQueue_Pop(tween->queue, TweenAction*);

if (action != NULL)
{
// add the running queue action into current list
AArrayList_Add(tween->current, action);
SetActionValue(action);
}
}


static void* RunActions(Array(TweenAction*)* actions, void* tweenID)
{
Tween* tween;
Expand Down Expand Up @@ -183,17 +190,14 @@ static void* RunActions(Array(TweenAction*)* actions, void* tweenID)
}
}

PopQueueActionToRun(tween);

return tweenID;
}


static void RemoveActionByIndex(Tween* tween, TweenAction* action, int index)
static void RemoveCurrentActionByIndex(Tween* tween, TweenAction* action, int index)
{
if (action == tween->queueAction)
{
tween->queueAction = NULL;
}

AArrayList->RemoveByLast(tween->current, index);
AArrayList_Add(actionCacheList, action);
}
Expand All @@ -211,7 +215,7 @@ static bool TryRemoveAction(void* tweenID, TweenAction* action)

if (action == tweenAction)
{
RemoveActionByIndex(tween, action, i);
RemoveCurrentActionByIndex(tween, action, i);
return true;
}
}
Expand Down Expand Up @@ -254,10 +258,6 @@ static bool TryRemoveAllActions(void* tweenID)
AArrayList_Add(actionCacheList, action);
}

// if queueAction not NULL it must be in tweenData->current
// so just set NULL
tween->queueAction = NULL;

return true;
}

Expand Down Expand Up @@ -308,10 +308,6 @@ static bool TryCompleteAllActions(void* tweenID, bool isFireOnComplete)
AArrayList_Add(actionCacheList, action);
}

// if queueAction not NULL it must be in tweenData->current
// so just set NULL
tween->queueAction = NULL;

return true;
}

Expand Down Expand Up @@ -344,20 +340,7 @@ static void Update(float deltaSeconds)
for (int i = tweenRunningMap->elementList->size - 1; i > -1; --i)
{
Tween* tween = AArrayIntMap_GetAt(tweenRunningMap, i, Tween*);

// get a queue action to run
if (tween->queueAction == NULL)
{
tween->queueAction = AArrayQueue_Pop(tween->queue, TweenAction*);

if (tween->queueAction != NULL)
{
// add the running queue action into current list
AArrayList_Add(tween->current, tween->queueAction);
SetActionValue(tween->queueAction);
}
}


if (tween->current->size == 0)
{
// all actions complete so remove tweenData and push to cache
Expand Down Expand Up @@ -399,8 +382,13 @@ static void Update(float deltaSeconds)
{
action->OnComplete(action);
}

RemoveCurrentActionByIndex(tween, action, j);

RemoveActionByIndex(tween, action, j);
if (action->isQueue)
{
PopQueueActionToRun(tween);
}
}
}
}
Expand Down

0 comments on commit e760ed2

Please sign in to comment.