added get data function to inline and finished basic entity (still needs testing though
This commit is contained in:
parent
c078ce907f
commit
6085d22df4
4 changed files with 71 additions and 11 deletions
|
|
@ -89,7 +89,7 @@ void *ARC_EntitySystem_GetComponentData(ARC_EntitySystem *entitySystem, ARC_Enti
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "arc/std/array.h"
|
||||||
#include "arc/std/vector.h"
|
#include "arc/std/vector.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
@ -102,6 +103,17 @@ uint32_t ARC_VectorInline_GetSize(ARC_VectorInline *vectorInline);
|
||||||
*/
|
*/
|
||||||
void *ARC_VectorInline_Get(ARC_VectorInline *vectorInline, uint32_t index);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -21,18 +21,14 @@ struct ARC_EntitySystem {
|
||||||
|
|
||||||
//TODO: this would probs be better to use a queue
|
//TODO: this would probs be better to use a queue
|
||||||
ARC_VectorInline *freeEntities;
|
ARC_VectorInline *freeEntities;
|
||||||
};
|
|
||||||
|
|
||||||
ARC_Bool ARC_EntitySystem_VectorCompareDataFn(void *dataA, void *dataB){
|
//temporary array for the query function
|
||||||
return (ARC_Bool)(*(uint32_t *)dataA == *(uint32_t *)dataB);
|
ARC_Entity *query;
|
||||||
}
|
};
|
||||||
|
|
||||||
void ARC_EntitySystem_Create(ARC_EntitySystem **entitySystem){
|
void ARC_EntitySystem_Create(ARC_EntitySystem **entitySystem){
|
||||||
*entitySystem = (ARC_EntitySystem *)malloc(sizeof(ARC_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)->flagVector) , sizeof(uint8_t) , NULL, NULL);
|
||||||
ARC_VectorInline_Create(&((*entitySystem)->maskVector) , sizeof(uint32_t), NULL, NULL);
|
ARC_VectorInline_Create(&((*entitySystem)->maskVector) , sizeof(uint32_t), NULL, NULL);
|
||||||
ARC_VectorInline_Create(&((*entitySystem)->offsetVector), 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
|
//TODO: this would probs be better to use a queue
|
||||||
ARC_VectorInline_Create(&((*entitySystem)->freeEntities), sizeof(ARC_Entity), NULL, NULL);
|
ARC_VectorInline_Create(&((*entitySystem)->freeEntities), sizeof(ARC_Entity), NULL, NULL);
|
||||||
|
|
||||||
|
//init an empty query
|
||||||
|
(*entitySystem)->query = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ARC_EntitySystem_Destroy(ARC_EntitySystem *entitySystem){
|
void ARC_EntitySystem_Destroy(ARC_EntitySystem *entitySystem){
|
||||||
|
if(entitySystem->query != NULL){
|
||||||
|
free(entitySystem->query);
|
||||||
|
}
|
||||||
|
|
||||||
if(entitySystem->data != NULL){
|
if(entitySystem->data != NULL){
|
||||||
ARC_VectorInline_Destroy(entitySystem->data);
|
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);
|
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){
|
//setup teh components data and get a return size
|
||||||
ARC_Array componentsData = { 0, NULL };
|
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;
|
return componentsData;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
#include "arc/std/vector/inline.h"
|
#include "arc/std/vector/inline.h"
|
||||||
|
|
||||||
|
#include "arc/std/array.h"
|
||||||
#include "arc/std/bool.h"
|
#include "arc/std/bool.h"
|
||||||
#include "arc/std/errno.h"
|
#include "arc/std/errno.h"
|
||||||
|
#include "arc/std/vector.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
@ -165,3 +167,10 @@ void *ARC_VectorInline_Get(ARC_VectorInline *vectorInline, uint32_t index){
|
||||||
|
|
||||||
return (vectorInline->data + (vectorInline->typeSize * index));
|
return (vectorInline->data + (vectorInline->typeSize * index));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ARC_Array ARC_VectorInline_GetData(ARC_VectorInline *vectorInline){
|
||||||
|
return (ARC_Array){
|
||||||
|
vectorInline->currentSize,
|
||||||
|
vectorInline->data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue