123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #include "core/Components.h"
- void coreInitComponents(CoreComponents* c, size_t componentSize) {
- coreInitHashMap(&c->entityToIndex, sizeof(CoreEntity), sizeof(size_t));
- coreInitList(&c->indexToEntity, sizeof(CoreEntity));
- coreInitList(&c->components, componentSize);
- }
- void coreDestroyComponents(CoreComponents* c) {
- coreDestroyHashMap(&c->entityToIndex);
- coreDestroyList(&c->indexToEntity);
- coreDestroyList(&c->components);
- }
- void* coreComponentsGetOrAdd(CoreComponents* c, CoreEntity e) {
- void* component = coreComponentsSearch(c, e);
- if(component != nullptr) {
- return component;
- }
- size_t index = c->components.length;
- coreHashMapPutPointer(&c->entityToIndex, &e, &index);
- coreAddListData(&c->indexToEntity, &e);
- return coreAddEmptyListData(&c->components);
- }
- void* coreComponentsSearch(CoreComponents* c, CoreEntity e) {
- size_t* index = coreHashMapSearchPointer(&c->entityToIndex, &e);
- if(index == nullptr) {
- return nullptr;
- }
- return coreGetListIndex(&c->components, *index);
- }
- bool coreComponentsRemove(CoreComponents* c, CoreEntity e) {
- size_t* indexP = coreHashMapSearchPointer(&c->entityToIndex, &e);
- if(indexP == nullptr) {
- return false;
- }
- size_t lastIndex = c->components.length - 1;
- size_t index = *indexP;
- coreHashMapRemovePointer(&c->entityToIndex, &e);
- coreRemoveListIndexBySwap(&c->components, index);
- if(index == lastIndex) {
- coreRemoveListIndexBySwap(&c->indexToEntity, index);
- return true;
- }
- CoreEntity other =
- coreGetTypedListIndex(&c->indexToEntity, lastIndex, CoreEntity);
- coreRemoveListIndexBySwap(&c->indexToEntity, index);
- coreHashMapPutPointer(&c->entityToIndex, &other, &index);
- return true;
- }
- void coreInitComponentsIterator(CoreComponentIterator* ci, CoreComponents* c) {
- ci->indexToEntity = coreGetListStart(&c->indexToEntity);
- ci->indexToEntityEnd = coreGetListEnd(&c->indexToEntity);
- ci->component = coreGetListStart(&c->components);
- ci->componentEnd = coreGetListEnd(&c->components);
- ci->componentSize = c->components.dataSize;
- ci->node = (CoreComponentNode){0};
- }
- bool coreComponentsHasNext(CoreComponentIterator* ci) {
- return ci->indexToEntity != ci->indexToEntityEnd;
- }
- CoreComponentNode* coreComponentsNext(CoreComponentIterator* ci) {
- ci->node.component = ci->component;
- ci->node.entity = *ci->indexToEntity;
- ci->indexToEntity++;
- ci->component = (char*)ci->component + ci->componentSize;
- return &ci->node;
- }
- void* coreComponentsBegin(CoreComponents* c) {
- return coreGetListStart(&c->components);
- }
- void* coreComponentsEnd(CoreComponents* c) {
- return coreGetListEnd(&c->components);
- }
|