summaryrefslogtreecommitdiff
path: root/src/storage.h
blob: 2601ef78f341609e123147e98847b16d07670193 (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
70
71
72
#ifndef storage_h_INCLUDED
#define storage_h_INCLUDED

#include <stdlib.h>
#include <string.h>

#ifndef MAX_OBJECTS
#define MAX_OBJECTS 4096
#endif

#ifndef MAX_COMPONENTS
#define MAX_COMPONENTS 32
#endif

#define BITSET_BYTES (MAX_COMPONENTS / 8)
#include "bitset.h"

#define GET_COMPONENT(storage, componentType, idx) (componentType *) internal_getComponent(storage, idx, #componentType)
#define REGISTER_COMPONENT(storage, componentType) internal_registerComponent(storage, #componentType, sizeof(componentType))
#define ADD_COMPONENT(storage, idx, componentType) internal_addComponent(storage, idx, #componentType)

typedef struct storage_t storage_t;
typedef unsigned int entity_t;
typedef void (*system_t)(storage_t *storage, entity_t eid);

typedef struct
{
	int idx;
	const char *name;
	size_t objectSize;
	char *objects;
} component_pair_t;

struct storage_t
{
	unsigned int componentsStored;

	size_t objectsStored;
	component_pair_t *objects;
	bitarena_t objectSignatures;

	size_t systemsStored;
	system_t *systems;
	bitarena_t systemSignatures;
};

component_pair_t * // NULL if pair is not found
findPair(storage_t *this, const char *name);

entity_t
createEntity(storage_t *this);

void
registerSystem(storage_t *storage, system_t system, bitset_t components);

bitset_t
createSignature(storage_t *storage, char **components);

// Not intended for use outside of macros
void
internal_registerComponent(storage_t *storage, const char *name, size_t size);

// Not intended for use outside of macros
void * // NULL if component with such name is not found
internal_addComponent(storage_t *this, entity_t idx, const char *name);

// Not intended for use outside of macros
void * // NULL if component is not found
internal_getComponent(storage_t *this, entity_t idx, const char *name);

#endif // storage_h_INCLUDED