Skip to content

Commit

Permalink
Refactor the ArrayQueue, rename [Push] to [Enqueue] and [Pop] to [Deq…
Browse files Browse the repository at this point in the history
…ueue].
  • Loading branch information
scottcgi committed Aug 18, 2021
1 parent 0907ef0 commit f9e5e50
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 55 deletions.
5 changes: 4 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## Developing
## v0.7.0

_`2021.8.18 UTC+8 10:42`_

* Fix case spelling error in `ArrayList.c`.
* Update the `Vibrator` API of Android.
* Add the `Smooth` function and Set the `Smooth` as the default ease.
* Optimize the `Tween` implementation by remove the `queueAction`.
* Refactor the `ArrayQueue`, rename `Push` to `Enqueue` and `Pop` to `Dequeue`. **(Break Compatibility)**


## v0.6.4
Expand Down
4 changes: 2 additions & 2 deletions Engine/Graphics/OpenGL/Mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,14 @@ static void Render(Drawable* drawable)
fromChild = AArrayList_Get
(
mesh->childList,
AArrayQueue_PopWithDefault(mesh->drawRangeQueue, int, mesh->fromIndex),
AArrayQueue_DequeueWithDefault(mesh->drawRangeQueue, int, mesh->fromIndex),
SubMesh*
);

toChild = AArrayList_Get
(
mesh->childList,
AArrayQueue_PopWithDefault(mesh->drawRangeQueue, int, mesh->toIndex),
AArrayQueue_DequeueWithDefault(mesh->drawRangeQueue, int, mesh->toIndex),
SubMesh*
);
}
Expand Down
4 changes: 2 additions & 2 deletions Engine/Graphics/OpenGL/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ static inline void AMesh_Draw(Mesh* mesh)
*/
static inline void AMesh_DrawByIndex(Mesh* mesh, int fromIndex, int toIndex)
{
AArrayQueue_Push(mesh->drawRangeQueue, fromIndex);
AArrayQueue_Push(mesh->drawRangeQueue, toIndex);
AArrayQueue_Enqueue(mesh->drawRangeQueue, fromIndex);
AArrayQueue_Enqueue(mesh->drawRangeQueue, toIndex);
ADrawable->Draw (mesh->drawable);
}

Expand Down
28 changes: 14 additions & 14 deletions Engine/Toolkit/Utils/ArrayQueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,38 @@
#include "Engine/Toolkit/Platform/Log.h"


static void* Push(ArrayQueue* arrayQueue, void* elementPtr)
static void* Enqueue(ArrayQueue* arrayQueue, void* elementPtr)
{
if (arrayQueue->topIndex > 0 && arrayQueue->topIndex == arrayQueue->elementList->elementArr->length)
if (arrayQueue->headIndex > 0 && arrayQueue->headIndex == arrayQueue->elementList->elementArr->length)
{
AArrayList->RemoveRange(arrayQueue->elementList, 0, arrayQueue->topIndex - 1);
arrayQueue->topIndex = 0;
AArrayList->RemoveRange(arrayQueue->elementList, 0, arrayQueue->headIndex - 1);
arrayQueue->headIndex = 0;
}

return AArrayList->Add(arrayQueue->elementList, elementPtr);
}


static void* Pop(ArrayQueue* arrayQueue, void* defaultElementPtr)
static void* Dequeue(ArrayQueue* arrayQueue, void* defaultElementPtr)
{
if (arrayQueue->topIndex == arrayQueue->elementList->size)
if (arrayQueue->headIndex == arrayQueue->elementList->size)
{
return defaultElementPtr;
}

return (char*) arrayQueue->elementList->elementArr->data +
arrayQueue->elementList->elementTypeSize *
(arrayQueue->topIndex++);
(arrayQueue->headIndex++);
}


static void RemoveAt(ArrayQueue* arrayQueue, int index)
{
ALog_A
(
index >= arrayQueue->topIndex && index < arrayQueue->elementList->size,
index >= arrayQueue->headIndex && index < arrayQueue->elementList->size,
"AArrayQueue RemoveAt index = %d, out of range [%d, %d]",
index, arrayQueue->topIndex, arrayQueue->elementList->size - 1
index, arrayQueue->headIndex, arrayQueue->elementList->size - 1
);

AArrayList->Remove(arrayQueue->elementList, index);
Expand All @@ -59,7 +59,7 @@ static void RemoveAt(ArrayQueue* arrayQueue, int index)

static void Release(ArrayQueue* arrayQueue)
{
arrayQueue->topIndex = 0;
arrayQueue->headIndex = 0;
AArrayList->Release(arrayQueue->elementList);
}

Expand All @@ -75,7 +75,7 @@ static void InitWithCapacity(int elementTypeSize, int capacity, ArrayQueue* outA
AArrayList->InitWithCapacity(elementTypeSize, capacity, outArrayQueue->elementList);
}

outArrayQueue->topIndex = 0;
outArrayQueue->headIndex = 0;
}


Expand All @@ -102,7 +102,7 @@ static ArrayQueue* Create(int elementTypeSize)

static void Clear(ArrayQueue* arrayQueue)
{
arrayQueue->topIndex = 0;
arrayQueue->headIndex = 0;
AArrayList->Clear(arrayQueue->elementList);
}

Expand All @@ -115,8 +115,8 @@ struct AArrayQueue AArrayQueue[1] =
InitWithCapacity,
Release,

Push,
Pop,
Enqueue,
Dequeue,
RemoveAt,
Clear,
}};
54 changes: 27 additions & 27 deletions Engine/Toolkit/Utils/ArrayQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
typedef struct
{
/**
* ArrayQueue top element index.
* ArrayQueue head element index.
*/
int topIndex;
int headIndex;

/**
* Store all elements.
Expand All @@ -53,26 +53,26 @@ struct AArrayQueue
void (*Release) (ArrayQueue* arrayQueue);

/**
* Push the element from elementPtr to the tail of ArrayQueue.
* Add the element from elementPtr to the tail of ArrayQueue.
* elementPtr: point to element
*
* return the elementPtr in ArrayQueue.
*/
void* (*Push) (ArrayQueue* arrayQueue, void* elementPtr);
void* (*Enqueue) (ArrayQueue* arrayQueue, void* elementPtr);

/**
* Pop the element from the head of ArrayQueue.
* return the top elementPtr of the ArrayQueue, if no element return defaultElementPtr.
* Remove the element from the head of ArrayQueue.
* return the head elementPtr of the ArrayQueue, if no element return defaultElementPtr.
*/
void* (*Pop) (ArrayQueue* arrayQueue, void* defaultElementPtr);
void* (*Dequeue) (ArrayQueue* arrayQueue, void* defaultElementPtr);

/**
* Remove the element at index that range in [topIndex, ArrayQueue size - 1].
* Remove the element at index that range in [headIndex, ArrayQueue size - 1].
*/
void (*RemoveAt) (ArrayQueue* arrayQueue, int index);

/**
* Clear all elements, and reset topIndex to 0.
* Clear all elements, and reset headIndex to 0.
*/
void (*Clear) (ArrayQueue* arrayQueue);
};
Expand All @@ -93,49 +93,49 @@ extern struct AArrayQueue AArrayQueue[1];
* example: ArrayQueue queue[1] = AArrayQueue_Init(ElementType, increase)
*/
#define AArrayQueue_Init(ElementType, increase) \
{ \
0, \
AArrayList_Init(ElementType, increase), \
{ \
0, \
AArrayList_Init(ElementType, increase), \
}


/**
* Shortcut of AArrayQueue->Push.
* Shortcut of AArrayQueue->Enqueue.
*/
#define AArrayQueue_Push(arrayQueue, element) \
AArrayQueue->Push(arrayQueue, &(element))
#define AArrayQueue_Enqueue(arrayQueue, element) \
AArrayQueue->Enqueue(arrayQueue, &(element))


/**
* Shortcut of AArrayQueue->Pop.
* Shortcut of AArrayQueue->Dequeue.
* return element.
*/
#define AArrayQueue_Pop(arrayQueue, ElementType) \
(*(ElementType*) AArrayQueue->Pop(arrayQueue, NULL_PTR))
#define AArrayQueue_Dequeue(arrayQueue, ElementType) \
(*(ElementType*) AArrayQueue->Dequeue(arrayQueue, NULL_PTR))


/**
* Shortcut of AArrayQueue->Pop.
* Shortcut of AArrayQueue->Dequeue.
* return element.
*/
#define AArrayQueue_PopWithDefault(arrayQueue, ElementType, defaultValue) \
(*(ElementType*) AArrayQueue->Pop(arrayQueue, &(defaultValue)))
#define AArrayQueue_DequeueWithDefault(arrayQueue, ElementType, defaultValue) \
(*(ElementType*) AArrayQueue->Dequeue(arrayQueue, &(defaultValue)))


/**
* Shortcut of AArrayQueue->Pop.
* Shortcut of AArrayQueue->Dequeue.
* return elementPtr.
*/
#define AArrayQueue_PopPtr(arrayQueue, ElementType) \
((ElementType*) AArrayQueue->Pop(arrayQueue, NULL))
#define AArrayQueue_DequeuePtr(arrayQueue, ElementType) \
((ElementType*) AArrayQueue->Dequeue(arrayQueue, NULL))


/**
* Shortcut of AArrayQueue->Pop.
* Shortcut of AArrayQueue->Dequeue.
* return elementPtr.
*/
#define AArrayQueue_PopPtrWithDefault(arrayQueue, ElementType, defaultValue) \
((ElementType*) AArrayQueue->Pop(arrayQueue, &(defaultValue)))
#define AArrayQueue_DequeuePtrWithDefault(arrayQueue, ElementType, defaultValue) \
((ElementType*) AArrayQueue->Dequeue(arrayQueue, &(defaultValue)))


#endif
16 changes: 8 additions & 8 deletions Engine/Toolkit/Utils/Tween.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ static inline void SetActionValue(TweenAction* action)
}


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

if (action != NULL)
{
Expand Down Expand Up @@ -181,7 +181,7 @@ static void* RunActions(Array(TweenAction*)* actions, void* tweenID)

if (action->isQueue)
{
AArrayQueue_Push(tween->queue, action);
AArrayQueue_Enqueue(tween->queue, action);
}
else
{
Expand All @@ -190,7 +190,7 @@ static void* RunActions(Array(TweenAction*)* actions, void* tweenID)
}
}

PopQueueActionToRun(tween);
DequeueAction(tween);

return tweenID;
}
Expand Down Expand Up @@ -220,7 +220,7 @@ static bool TryRemoveAction(void* tweenID, TweenAction* action)
}
}

for (int i = tween->queue->topIndex; i < tween->queue->elementList->size; ++i)
for (int i = tween->queue->headIndex; i < tween->queue->elementList->size; ++i)
{
TweenAction* tweenAction = AArrayList_Get(tween->queue->elementList, i, TweenAction*);

Expand Down Expand Up @@ -253,7 +253,7 @@ static bool TryRemoveAllActions(void* tweenID)
AArrayList->Clear(tween->current);

TweenAction* action;
while ((action = AArrayQueue_Pop(tween->queue, TweenAction*)))
while ((action = AArrayQueue_Dequeue(tween->queue, TweenAction*)))
{
AArrayList_Add(actionCacheList, action);
}
Expand Down Expand Up @@ -302,7 +302,7 @@ static bool TryCompleteAllActions(void* tweenID, bool isFireOnComplete)
AArrayList->Clear(tween->current);

TweenAction* action;
while ((action = AArrayQueue_Pop(tween->queue, TweenAction*)))
while ((action = AArrayQueue_Dequeue(tween->queue, TweenAction*)))
{
SetActionComplete(action, isFireOnComplete);
AArrayList_Add(actionCacheList, action);
Expand Down Expand Up @@ -387,7 +387,7 @@ static void Update(float deltaSeconds)

if (action->isQueue)
{
PopQueueActionToRun(tween);
DequeueAction(tween);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img src="./Docs/Images/Logo.png" width="176" height="228" alt="Mojoc Logo" title="Mojoc Logo" />


## Mojoc v0.6.4
## Mojoc v0.7.0

Mojoc is an open-source, cross-platform, pure C game engine. It is based on OpenGLES3 and written in C99. It currently works on IOS and Android, but can easily be extended to other platforms, and will support more platforms in the future.

Expand Down

0 comments on commit f9e5e50

Please sign in to comment.