summaryrefslogtreecommitdiff
path: root/src/storage.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/storage.c')
-rw-r--r--src/storage.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/storage.c b/src/storage.c
new file mode 100644
index 0000000..2fd2601
--- /dev/null
+++ b/src/storage.c
@@ -0,0 +1,69 @@
+#include "storage.h"
+
+component_pair *
+findPair(storage *this, const char *name)
+{
+ for (int i = 0; i < this->componentsStored; ++i)
+ {
+ component_pair *pair = &this->objects[i];
+ if (strcmp(name, pair->name) == 0)
+ {
+ return pair;
+ }
+ }
+ return NULL;
+}
+
+Entity
+createEntity(storage *this)
+{
+ Entity idx = this->objectsStored++;
+ for (int i = 0; i < this->componentsStored; ++i)
+ {
+ this->objects[i].objects[idx] = NULL;
+ }
+ return idx;
+}
+
+// Not intended for use outside of macros
+void
+internal_registerComponent(storage *storage, char *name, size_t size)
+{
+ unsigned int componentIdx = storage->componentsStored++;
+ storage->objects[componentIdx] = (component_pair) {
+ .name = name,
+ .objectSize = size,
+ .objects = malloc(sizeof(void*) * MAX_OBJECTS)
+ };
+}
+
+void * // NULL if component with such name is not found
+internal_addComponent(storage *this, Entity idx, const char *name)
+{
+ component_pair *pair = findPair(this, name);
+ if (pair == NULL)
+ {
+ return NULL;
+ }
+
+ pair->objects[idx] = (void *) malloc(pair->objectSize);
+ void *component = pair->objects[idx];
+ // component = (void *) malloc(pair->objectSize);
+
+ return component;
+}
+
+// Not intended for use outside of macros
+void * // NULL if component is not found
+internal_getComponent(storage *this, Entity idx, const char *name)
+{
+ for (int i = 0; i < this->componentsStored; ++i)
+ {
+ component_pair *pair = &this->objects[i];
+ if (strcmp(name, pair->name) == 0)
+ {
+ return pair->objects[idx];
+ }
+ }
+ return NULL;
+}