added get data function to inline and finished basic entity (still needs testing though

This commit is contained in:
herbglitch 2025-03-16 01:35:09 -06:00
parent c078ce907f
commit 6085d22df4
4 changed files with 71 additions and 11 deletions

View file

@ -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;
}

View file

@ -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 <stdint.h>
#include <stdlib.h>
#include <string.h>
@ -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
};
}