Adding linked list insertion sort and sorting entities by draw priority.
This commit is contained in:
parent
50dc383e2d
commit
ea7f745313
11
src/ecs.c
11
src/ecs.c
|
|
@ -114,7 +114,8 @@ static inline int action_draw_component(elem_t *elem, va_list args __attribute__
|
|||
{
|
||||
component_t *component = elem->data;
|
||||
|
||||
if(component->draw) if(component->draw(component)) return EXIT_FAILURE;
|
||||
if(component->draw)
|
||||
if(component->draw(component)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -172,10 +173,18 @@ static inline int action_draw_entity(elem_t *elem, va_list args __attribute__((u
|
|||
return draw_entity((entity_t *)elem->data);
|
||||
}
|
||||
|
||||
// No args
|
||||
static inline bool condition_is_draw_priority_higher(elem_t *elem, va_list args)
|
||||
{
|
||||
return ((entity_t *)elem->data)->draw_priority > ((entity_t *)elem->next->data)->draw_priority;
|
||||
}
|
||||
|
||||
inline int entity_manager_draw(entity_manager_t *entity_manager)
|
||||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
linked_list_insertion_sort(&entity_manager->entities, condition_is_draw_priority_higher);
|
||||
|
||||
if(linked_list_for_each(&entity_manager->entities, action_draw_entity)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
|
|||
|
|
@ -286,4 +286,15 @@ int linked_list_for_each(const linked_list_t *linked_list, const action_t action
|
|||
*/
|
||||
int linked_list_for_eachv(const linked_list_t *linked_list, const action_t action, va_list args);
|
||||
|
||||
/**
|
||||
* @brief Do an insertion sort data of the given linked list.
|
||||
* The condition return true to swap element's data with next one.
|
||||
*
|
||||
* @param linked_list Pointer to an linked list
|
||||
* @param action Action to apply
|
||||
*
|
||||
* @return EXIT_SUCCESS, EXIT_FAILURE on error.
|
||||
*/
|
||||
void linked_list_insertion_sort(linked_list_t *linked_list, const condition_t condition, ...);
|
||||
|
||||
#endif // LINKED_LIST_H
|
||||
|
|
|
|||
|
|
@ -275,13 +275,6 @@ elem_t *linked_list_pop_front(linked_list_t *linked_list)
|
|||
return elem;
|
||||
}
|
||||
|
||||
inline bool linked_list_is_empty(const linked_list_t *linked_list)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
return !linked_list->first;
|
||||
}
|
||||
|
||||
elem_t *linked_list_get(const linked_list_t *linked_list, size_t index)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
|
@ -506,3 +499,41 @@ int linked_list_for_eachv(const linked_list_t *linked_list, const action_t actio
|
|||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static inline void swap_elems_data(elem_t *elem_a, elem_t *elem_b)
|
||||
{
|
||||
void *tmp_data = elem_a->data;
|
||||
elem_a->data = elem_b->data;
|
||||
elem_b->data = tmp_data;
|
||||
}
|
||||
|
||||
void linked_list_insertion_sort(linked_list_t *linked_list, const condition_t condition, ...)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
assert("Condition cannot be NULL" && condition);
|
||||
|
||||
va_list args;
|
||||
va_start(args, condition);
|
||||
|
||||
elem_t *actual_elem = linked_list->first;
|
||||
elem_t *next_elem;
|
||||
|
||||
while(actual_elem->next)
|
||||
{
|
||||
next_elem = actual_elem->next;
|
||||
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
|
||||
if(condition(actual_elem, args_copy))
|
||||
{
|
||||
swap_elems_data(actual_elem, next_elem);
|
||||
}
|
||||
|
||||
va_end(args_copy);
|
||||
|
||||
actual_elem = next_elem;
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue