Components.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "core/Components.h"
  2. void coreInitComponents(CoreComponents* c, size_t componentSize) {
  3. coreInitHashMap(&c->entityToIndex, sizeof(CoreEntity), sizeof(size_t));
  4. coreInitList(&c->indexToEntity, sizeof(CoreEntity));
  5. coreInitList(&c->components, componentSize);
  6. }
  7. void coreDestroyComponents(CoreComponents* c) {
  8. coreDestroyHashMap(&c->entityToIndex);
  9. coreDestroyList(&c->indexToEntity);
  10. coreDestroyList(&c->components);
  11. }
  12. void* coreComponentsGetOrAdd(CoreComponents* c, CoreEntity e) {
  13. void* component = coreComponentsSearch(c, e);
  14. if(component != nullptr) {
  15. return component;
  16. }
  17. size_t index = c->components.length;
  18. coreHashMapPutPointer(&c->entityToIndex, &e, &index);
  19. coreAddListData(&c->indexToEntity, &e);
  20. return coreAddEmptyListData(&c->components);
  21. }
  22. void* coreComponentsSearch(CoreComponents* c, CoreEntity e) {
  23. size_t* index = coreHashMapSearchPointer(&c->entityToIndex, &e);
  24. if(index == nullptr) {
  25. return nullptr;
  26. }
  27. return coreGetListIndex(&c->components, *index);
  28. }
  29. bool coreComponentsRemove(CoreComponents* c, CoreEntity e) {
  30. size_t* indexP = coreHashMapSearchPointer(&c->entityToIndex, &e);
  31. if(indexP == nullptr) {
  32. return false;
  33. }
  34. size_t lastIndex = c->components.length - 1;
  35. size_t index = *indexP;
  36. coreHashMapRemovePointer(&c->entityToIndex, &e);
  37. coreRemoveListIndexBySwap(&c->components, index);
  38. if(index == lastIndex) {
  39. coreRemoveListIndexBySwap(&c->indexToEntity, index);
  40. return true;
  41. }
  42. CoreEntity other =
  43. coreGetTypedListIndex(&c->indexToEntity, lastIndex, CoreEntity);
  44. coreRemoveListIndexBySwap(&c->indexToEntity, index);
  45. coreHashMapPutPointer(&c->entityToIndex, &other, &index);
  46. return true;
  47. }
  48. void coreInitComponentsIterator(CoreComponentIterator* ci, CoreComponents* c) {
  49. ci->indexToEntity = coreGetListStart(&c->indexToEntity);
  50. ci->indexToEntityEnd = coreGetListEnd(&c->indexToEntity);
  51. ci->component = coreGetListStart(&c->components);
  52. ci->componentEnd = coreGetListEnd(&c->components);
  53. ci->componentSize = c->components.dataSize;
  54. ci->node = (CoreComponentNode){0};
  55. }
  56. bool coreComponentsHasNext(CoreComponentIterator* ci) {
  57. return ci->indexToEntity != ci->indexToEntityEnd;
  58. }
  59. CoreComponentNode* coreComponentsNext(CoreComponentIterator* ci) {
  60. ci->node.component = ci->component;
  61. ci->node.entity = *ci->indexToEntity;
  62. ci->indexToEntity++;
  63. ci->component = (char*)ci->component + ci->componentSize;
  64. return &ci->node;
  65. }
  66. void* coreComponentsBegin(CoreComponents* c) {
  67. return coreGetListStart(&c->components);
  68. }
  69. void* coreComponentsEnd(CoreComponents* c) {
  70. return coreGetListEnd(&c->components);
  71. }