aboutsummaryrefslogtreecommitdiff
path: root/ecs2.cpp
blob: 2b2ac666e28f414e853f3d0e4b0fba73dd00ca52 (plain) (blame)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <utility>

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

/* So it's like the other ECS example with a bunch of academic masturbation
 * See https://git.quou.xyz/samples/tree/ecs.c
 *
 * Areas for improvement:
 * - Should really respect alignment.
 * - Entity destruction
 */

#define max_entities 256
/* If you want more than 32 component types then you'll have to
 * use an unsigned long instead of an int for the component type
 * IDs. */
#define max_component_types 32

typedef int Entity;

unsigned get_new_component_id();

/* DO NOT make either of these functions static. That will break stuff. */
template <typename T>
unsigned get_component_id() {
  static unsigned id = get_new_component_id();
  return id;
}
unsigned get_new_component_id() {
  static unsigned id = 0;
  return id++;
}

struct Component_Pool {
	void* data;
	int element_size;

	template <typename T>
	T& get(int idx) {
		assert(sizeof(T) == element_size);
		return ((T*)data)[idx];
	}

	Component_Pool(): data(0) {}

	void init(int es) {
		element_size = es;
		data = malloc(element_size * max_entities);
	}

	void deinit() {
		free(data);
	}

	bool isinit() {
		return data != nullptr;
	}
};

struct World {
	Component_Pool pools[max_component_types];
	int entity_count;

	int bitset[max_entities];

	World(): entity_count(0) {}

	void deinit() {
		for (int i = 0; i < max_component_types; i++) {
			if (pools[i].isinit()) {
				pools[i].deinit();
			}
		}
	}

	template <typename T>
	void register_component_type() {
		pools[get_component_id<T>()].init(sizeof(T));
	}

	Entity create_entity() {
		auto e = entity_count++;
		bitset[e] = 0;
		return e;
	}

	template <typename T>
	T& add_component(Entity e, T&& c) {
		unsigned id = get_component_id<T>();
		Component_Pool& pool = pools[id];
		T& el = pool.get<T>(e);
		bitset[e] |= (1 << id);
		el = std::move(c);
		return el;
	}

	template <typename T>
	void remove_component(Entity e) {
		unsigned id = get_component_id<T>();
		bitset[e] &= ~(1 << id);
	}

	template <typename T>
	T& get_component(Entity e) {
		unsigned id = get_component_id<T>();
		Component_Pool& pool = pools[id];
		return pool.get<T>(e);
	}

	template <typename Fn>
	void iterate_with_mask(unsigned mask, Fn fn) {
		for (int i = 0; i < entity_count; i++) {
			if (bitset[i] & mask) {
				fn(i);
			}
		}
	}

	template <typename A, typename Fn>
	void iterate_with(Fn fn) {
		unsigned mask_a = 1 << get_component_id<A>();
		unsigned bits;
		for (int i = 0; i < entity_count; i++) {
			bits = bitset[i];
			if (bits & mask_a) {
				fn(i);
			}
		}
	}

	/* You need to make an overload for every number of components that you want to
	 * iterate at once. Just making two for example.
	 *
	 * There's probably a modern C++ way to not have to do this, but I don't know it.
	 * Please tell me if you know. */
	template <typename A, typename B, typename Fn>
	void iterate_with(Fn fn) {
		unsigned mask_a = 1 << get_component_id<A>();
		unsigned mask_b = 1 << get_component_id<B>();
		unsigned bits;
		for (int i = 0; i < entity_count; i++) {
			bits = bitset[i];
			if ((bits & mask_a) && (bits & mask_b)) {
				fn(i);
			}
		}
	}
};

/* Example */
struct Transform {
	int x, y;
	int scale_x, scale_y;
};

struct Enemy {
	int health, damage;
};

struct Sprite {
	int id;
};

void transform_system(World& world) {
	world.iterate_with<Transform>([&](Entity e) {
		Transform& t = world.get_component<Transform>(e);
		printf("Updating transform for %d\n", e);
		printf("The position is: %d, %d\n", t.x, t.y);
	});
}

void enemy_system(World& world) {
	world.iterate_with<Transform, Enemy>([&](Entity e) {
		Transform& t = world.get_component<Transform>(e);
		Enemy& enemy = world.get_component<Enemy>(e);

		printf("Updating enemy for %d\n", e);
		printf("The health is: %d\n", enemy.health);
	});
}

void sprite_system(World& world) {
	world.iterate_with<Transform, Sprite>([&](Entity e) {
		Transform& t = world.get_component<Transform>(e);
		Sprite& sprite = world.get_component<Sprite>(e);

		printf("Updating sprite for %d\n", e);
		printf("The id is: %d\n", sprite.id);
	});
}

int main() {
	World world;
	world.register_component_type<Transform>();
	world.register_component_type<Enemy>();
	world.register_component_type<Sprite>();

	Entity a = world.create_entity();
	world.add_component(a, Transform { 25, 25, 100, 100 });
	world.add_component(a, Enemy { 10, 5 });
	world.add_component(a, Sprite { 0 });

	Entity b = world.create_entity();
	world.add_component(b, Transform { 1, 3, 50, 4 });
	world.add_component(b, Enemy { 5, 20 });

	/* The game loop. */
	while (true) {
		enemy_system(world);
		transform_system(world);
		sprite_system(world);
	}

	world.deinit();
}