diff options
Diffstat (limited to 'src/events.c')
-rw-r--r-- | src/events.c | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/events.c b/src/events.c new file mode 100644 index 0000000..95be9e9 --- /dev/null +++ b/src/events.c @@ -0,0 +1,149 @@ +#include <stdlib.h> // calloc +#include <string.h> //memcpy + +#include "events.h" + +inline int eventSerializedSize() { + //FIXME : A rendre portable + return sizeof(event_t); +} + +void eventSerialize(const event_t *e, char *buf) { + //FIXME : A rendre portable + memcpy(buf,e,sizeof(event_t)); +} + +int eventUnserialize(event_t *e, char *buf) { + //FIXME : A rendre portable et vĂ©rifier les champs + memcpy(e,buf,sizeof(event_t)); + return 0; +} + +void eventListInit(eventList_t *elist) { + elist->first=NULL; + elist->last=NULL; + elist->lock= SDL_CreateMutex(); +} + +void eventListFree(eventList_t *elist) { + event_t *next; + event_t *curr = elist->first; + while(curr!=NULL) { + next = curr->next; + free(curr); + curr=next; + } +} +int eventListLock(eventList_t *elist) { + return SDL_mutexP(elist->lock); +} + +int eventListUnlock(eventList_t *elist) { + return SDL_mutexV(elist->lock); +} + +void eventListAdd(eventList_t *elist, event_t *event) { + if (elist->first==NULL) { + elist->first=elist->last=event; + event->next=event->prev=NULL; + } else { + elist->last->next = event; + event->prev = elist->last; + event->next = NULL; + elist->last = event; + } +} + +event_t * eventListPop(eventList_t *elist) { + event_t *res = elist->first; + + if (res!=NULL) { + elist->first=res->next; + } + + return res; +} + +int eventListItemCount(eventList_t *elist) { + int i=0; + event_t *cur = elist->first; + while(cur!=NULL) { ++i, cur=cur->next; } + return i; +} + +// Private methods below (not present in .h file) +void _eventListCopyItemValues(event_t *dst, const event_t *src) { + dst->playerId = src->playerId; + dst->eventTick = src->eventTick; + dst->serverTick = src->serverTick; + dst->type = src->type; + dst->lemId = src->lemId; + dst->newRole = src->newRole; +} + + +void _eventList_sort(event_t *left, event_t *right) { + + event_t *start, *curr, copy; + + // If the left and right pointers are the same, then return + if (left == right) return; + + // Set the Start and the Current item pointers + start = left; + curr = start->next; + + // Loop until we get to the right + while (1) + { + // If the start item is less then the right + if ( + (start->eventTick < curr->eventTick) || + ( + (start->eventTick == curr->eventTick) && + (start->playerId < curr->playerId) + ) + ) + { + // Swap the items TODO : change references + _eventListCopyItemValues(©, curr); + _eventListCopyItemValues(curr, start); + _eventListCopyItemValues(start, ©); + } + + // Check if we have reached the right end + if (curr == right) break; + + // Move to the next item in the list + curr = curr->next; + } + + // Swap the First and Current items + _eventListCopyItemValues(©, left); + _eventListCopyItemValues(left, curr); + _eventListCopyItemValues(curr, ©); + + // Save this Current item + const event_t *oldCurr = curr; + + // Check if we need to sort the left hand size of the Current point + curr = curr->prev; + if (curr != NULL) + { + if ((left->prev != curr) && (curr->next != left)) + _eventList_sort(left, curr); + } + + // Check if we need to sort the right hand size of the Current point + curr = oldCurr->next; + if (curr != NULL) + { + if ((curr->prev != right) && (right->next != curr)) + _eventList_sort(curr, right); + } +} + +void eventList_sort(eventList_t *elist) { + _eventList_sort(elist->first,elist->last); +} + |