-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
45 lines (33 loc) · 1003 Bytes
/
example.cpp
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
#include <iostream>
#define ECS_IMPL
#include "ecs.hpp"
struct Transform {
float x, y;
};
struct Tag {
const char* name;
};
static void on_tag_create(ecs::World& world, const ecs::Entity& e) {
std::cout << "Tag create: " << e.get<Tag>().name << "\n";
}
static void on_tag_destroy(ecs::World& world, const ecs::Entity& e) {
std::cout << "Tag destroy: " << e.get<Tag>().name << "\n";
}
ecs::i32 main() {
ecs::World world;
world.set_create_func<Tag>(on_tag_create);
world.set_destroy_func<Tag>(on_tag_destroy);
auto e = world.new_entity();
e.add<Transform>(Transform { 5, 3 });
e.add<Tag>(Tag { "Bob" });
e = world.new_entity();
e.add<Transform>(Transform { 3, 55 });
e.add<Tag>(Tag { "Alice" });
for (auto view = world.new_view<Tag, Transform>(); view.valid(); view.next()) {
auto e = view.get_entity();
auto& trans = view.get<Transform>();
auto& tag = view.get<Tag>();
std::cout << tag.name << ": " << trans.x << ", " << trans.y << "\n";
}
world.collect_garbage();
}