1#include <hierarchies.h>
2#include <stdio.h>
3
4
5typedef struct {
6 double x;
7 double y;
8} Transform;
9
10
11typedef Transform WorldTransform;
12typedef Transform LocalTransform;
13
14int main(int argc, char *argv[]) {
16
19
20
22
23
25 ecs_set(ecs, sun, LocalTransform, {1, 1});
26
28 ecs_set(ecs, mercury, LocalTransform, {1, 1});
29
31 ecs_set(ecs, venus, LocalTransform, {2, 2});
32
34 ecs_set(ecs, earth, LocalTransform, {3, 3});
35
37 ecs_set(ecs, moon, LocalTransform, {0.1, 0.1});
38
39
40
42 .terms = {
43
45
47
48
49 {
50 .id =
ecs_id(WorldTransform),
52
54
56 }
57 }
58 });
59
60
63 const LocalTransform *p =
ecs_field(&it, LocalTransform, 0);
64 WorldTransform *p_out =
ecs_field(&it, WorldTransform, 1);
65 const WorldTransform *p_parent =
ecs_field(&it, WorldTransform, 2);
66
67
68 for (int i = 0; i < it.count; i ++) {
69 p_out[i].x = p[i].x;
70 p_out[i].y = p[i].y;
71 if (p_parent) {
72 p_out[i].x += p_parent->x;
73 p_out[i].y += p_parent->y;
74 }
75 }
76 }
77
78
81 WorldTransform *p =
ecs_field(&it, WorldTransform, 0);
82 for (int i = 0; i < it.count; i ++) {
83 printf(
"%s: {%f, %f}\n",
ecs_get_name(ecs, it.entities[i]),
84 p[i].x, p[i].y);
85 }
86 }
87
89}
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
ecs_id_t ecs_entity_t
An entity identifier.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
bool ecs_each_next(ecs_iter_t *it)
Progress an iterator created with ecs_each_id().
#define ecs_add_pair(world, subject, first, second)
Add a pair to an entity.
#define ecs_entity(world,...)
Shorthand for creating an entity with ecs_entity_init().
#define ECS_COMPONENT(world, id)
Declare and define a component.
#define ecs_query(world,...)
Shorthand for creating a query with ecs_query_init().
#define ecs_set(world, entity, component,...)
Set a component value.
#define ecs_field(it, T, index)
Get field data for a component.
#define ecs_each(world, id)
Iterate entities with a component.
const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
Get the name of an entity.
bool ecs_query_next(ecs_iter_t *it)
Progress a query iterator.
#define EcsCascade
Sort results breadth-first.
ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
Create a query iterator.
@ EcsOut
Term is only written.
@ EcsIn
Term is only read.
@ EcsOptional
The term may match.
int ecs_fini(ecs_world_t *world)
Delete a world.
ecs_world_t * ecs_init_w_args(int argc, char *argv[])
Create a new world with arguments.
Queries are lists of constraints (terms) that match entities.