From 6085d22df4f63d6a7bdd2fb959fb445b2e9dccd1 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Sun, 16 Mar 2025 01:35:09 -0600 Subject: [PATCH] added get data function to inline and finished basic entity (still needs testing though --- include/arc/std/entity.h | 2 +- include/arc/std/vector/inline.h | 12 +++++++ src/std/entity.c | 59 +++++++++++++++++++++++++++------ src/std/vector/inline.c | 9 +++++ 4 files changed, 71 insertions(+), 11 deletions(-) diff --git a/include/arc/std/entity.h b/include/arc/std/entity.h index 011b49b..10d9112 100644 --- a/include/arc/std/entity.h +++ b/include/arc/std/entity.h @@ -89,7 +89,7 @@ void *ARC_EntitySystem_GetComponentData(ARC_EntitySystem *entitySystem, ARC_Enti /** * @brief */ -ARC_Array ARC_EntitySystem_QueryComponentsData(ARC_EntitySystem *entitySystem, uint32_t components); +ARC_Array ARC_EntitySystem_QueryComponentsData(ARC_EntitySystem *entitySystem, ARC_Array components); #ifdef __cplusplus } diff --git a/include/arc/std/vector/inline.h b/include/arc/std/vector/inline.h index cb5a466..3c6a409 100644 --- a/include/arc/std/vector/inline.h +++ b/include/arc/std/vector/inline.h @@ -5,6 +5,7 @@ extern "C" { #endif +#include "arc/std/array.h" #include "arc/std/vector.h" #include @@ -102,6 +103,17 @@ uint32_t ARC_VectorInline_GetSize(ARC_VectorInline *vectorInline); */ void *ARC_VectorInline_Get(ARC_VectorInline *vectorInline, uint32_t index); +/** + * @brief gets the data from the vector as an ARC_Array + * + * @note this function will does not copy the data it returns, so changes will affect the contents of the vector inline + * + * @param[in] vectorInline ARC_VectorInline to get data from + * + * @return an ARC_Array that holds the current contents of the vector inline +*/ +ARC_Array ARC_VectorInline_GetData(ARC_VectorInline *vectorInline); + #ifdef __cplusplus } #endif diff --git a/src/std/entity.c b/src/std/entity.c index ebe1414..2673f9b 100644 --- a/src/std/entity.c +++ b/src/std/entity.c @@ -21,18 +21,14 @@ struct ARC_EntitySystem { //TODO: this would probs be better to use a queue ARC_VectorInline *freeEntities; -}; -ARC_Bool ARC_EntitySystem_VectorCompareDataFn(void *dataA, void *dataB){ - return (ARC_Bool)(*(uint32_t *)dataA == *(uint32_t *)dataB); -} + //temporary array for the query function + ARC_Entity *query; +}; void ARC_EntitySystem_Create(ARC_EntitySystem **entitySystem){ *entitySystem = (ARC_EntitySystem *)malloc(sizeof(ARC_EntitySystem)); - //ARC_Vector_CompareDataFn compareDataFn = ARC_EntitySystem_VectorCompareDataFn; - //ARC_Vector_DestroyDataFn destroyDataFn = ARC_EntitySystem_VectorDestroyDataFn; - ARC_VectorInline_Create(&((*entitySystem)->flagVector) , sizeof(uint8_t) , NULL, NULL); ARC_VectorInline_Create(&((*entitySystem)->maskVector) , sizeof(uint32_t), NULL, NULL); ARC_VectorInline_Create(&((*entitySystem)->offsetVector), sizeof(uint32_t), NULL, NULL); @@ -43,9 +39,16 @@ void ARC_EntitySystem_Create(ARC_EntitySystem **entitySystem){ //TODO: this would probs be better to use a queue ARC_VectorInline_Create(&((*entitySystem)->freeEntities), sizeof(ARC_Entity), NULL, NULL); + + //init an empty query + (*entitySystem)->query = NULL; } void ARC_EntitySystem_Destroy(ARC_EntitySystem *entitySystem){ + if(entitySystem->query != NULL){ + free(entitySystem->query); + } + if(entitySystem->data != NULL){ ARC_VectorInline_Destroy(entitySystem->data); } @@ -164,11 +167,47 @@ void *ARC_EntitySystem_GetComponentData(ARC_EntitySystem *entitySystem, ARC_Enti return data + *(int32_t *)ARC_VectorInline_Get(entitySystem->offsetVector, (uint32_t)component); } +ARC_Array ARC_EntitySystem_QueryComponentsData(ARC_EntitySystem *entitySystem, ARC_Array components){ + //get the mask for all the components + ARC_EntityComponent componentsMask = 0; + for(uint32_t index = 0; index < components.size; index++){ + componentsMask |= (1 << ((ARC_EntityComponent *)components.data)[index]); + } -ARC_Array ARC_EntitySystem_QueryComponentsData(ARC_EntitySystem *entitySystem, uint32_t components){ - ARC_Array componentsData = { 0, NULL }; + //setup teh components data and get a return size + uint32_t maxEntitySize = ARC_VectorInline_GetSize(entitySystem->data) - ARC_VectorInline_GetSize(entitySystem->freeEntities); - componentsData.size = ARC_VectorInline_GetSize(entitySystem->data) - ARC_VectorInline_GetSize(entitySystem->freeEntities); + //clear the query if it already exists + if(entitySystem->query != NULL){ + free(entitySystem->query); + } + //reserve space for the query + entitySystem->query = (ARC_Entity *)malloc(sizeof(ARC_Entity) * maxEntitySize); + + //get all the alive entities that match the components + uint32_t queryIndex = 0; + for(uint32_t index = 0; index < ARC_VectorInline_GetSize(entitySystem->data); index++){ + uint8_t entityFlag = *(uint8_t *)ARC_VectorInline_Get(entitySystem->flagVector, index); + if((entityFlag & ARC_ENTITY_DEAD) != 0){ + continue; + } + + //if the entity's component mask matches the components passed in + uint32_t entityComponentMask = *(uint32_t *)ARC_VectorInline_Get(entitySystem->maskVector, index); + if((entityComponentMask & componentsMask) == componentsMask){ + //set the value at the queryIndex + entitySystem->query[queryIndex] = index; + queryIndex++; + } + } + + //set components data to the query array + ARC_Array componentsData = { + queryIndex, + entitySystem->query + }; + + //return the ids that hold all the components return componentsData; } diff --git a/src/std/vector/inline.c b/src/std/vector/inline.c index add84e5..fb7129e 100644 --- a/src/std/vector/inline.c +++ b/src/std/vector/inline.c @@ -1,7 +1,9 @@ #include "arc/std/vector/inline.h" +#include "arc/std/array.h" #include "arc/std/bool.h" #include "arc/std/errno.h" +#include "arc/std/vector.h" #include #include #include @@ -165,3 +167,10 @@ void *ARC_VectorInline_Get(ARC_VectorInline *vectorInline, uint32_t index){ return (vectorInline->data + (vectorInline->typeSize * index)); } + +ARC_Array ARC_VectorInline_GetData(ARC_VectorInline *vectorInline){ + return (ARC_Array){ + vectorInline->currentSize, + vectorInline->data + }; +}