summaryrefslogtreecommitdiff
path: root/src/storage.c
blob: 2fd260144f28ec655e5c435cf2d02e221df26e03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
}