1#include <group_by_callbacks.h>
2#include <stdio.h>
3#include <stdlib.h>
4
5
6
7
8
9
10typedef struct {
11 double x, y;
12} Position;
13
14
15typedef struct {
16 int32_t counter;
17} group_ctx;
18
19int group_counter = 0;
20
21
23 uint64_t group_id,
24 void *group_by_arg)
25{
26 (void)group_by_arg;
27 printf(
"Group %s created\n",
ecs_get_name(world, group_id));
28
29
30 group_ctx *ctx = malloc(sizeof(group_ctx));
31 ctx->counter = ++ group_counter;
32 return ctx;
33}
34
35
37 uint64_t group_id,
38 void *ctx,
39 void *group_by_arg)
40{
41 (void)group_by_arg;
42 printf(
"Group %s deleted\n",
ecs_get_name(world, group_id));
43
44
45 free(ctx);
46}
47
48int main(int argc, char *argv[]) {
50
53
54
55
57
58
62
63
65 .terms = {
67 },
68 .group_by = Group,
69 .on_group_create = on_group_create,
70 .on_group_delete = on_group_delete
71 });
72
73
75 ecs_set(ecs, e1, Position, {1, 1});
76
78 ecs_set(ecs, e2, Position, {2, 2});
79
81 ecs_set(ecs, e3, Position, {3, 3});
82
84 ecs_set(ecs, e4, Position, {4, 4});
86
88 ecs_set(ecs, e5, Position, {5, 5});
90
92 ecs_set(ecs, e6, Position, {6, 6});
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 printf("\n");
110
111
114 Position *p =
ecs_field(&it, Position, 0);
116
120
121 printf(" - group %s: table [%s]\n", group_str, table_str);
122 printf(" counter: %d\n", ctx->counter);
123
124 for (
int i = 0; i < it.
count; i ++) {
125 printf(" {%f, %f}\n", p[i].x, p[i].y);
126 }
127
128 ecs_os_free(group_str);
129 ecs_os_free(table_str);
130 printf("\n");
131 }
132
133
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
170}
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.
char * ecs_table_str(const ecs_world_t *world, const ecs_table_t *table)
Convert a table to a string.
#define ecs_add(world, entity, T)
Add a component to an entity.
#define ecs_new_w_pair(world, first, second)
Create a new entity with a pair.
#define ECS_COMPONENT(world, id)
Declare and define a component.
#define ECS_TAG(world, id)
Declare and define a tag.
#define ecs_query(world,...)
Shorthand for creating a query with ecs_query_init().
#define ecs_get_path(world, child)
Get path from root.
#define ecs_set(world, entity, component,...)
Set a component value.
#define ecs_field(it, T, index)
Get field data for a component.
uint64_t ecs_iter_get_group(const ecs_iter_t *it)
Return the group ID for the currently iterated result.
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.
void ecs_query_fini(ecs_query_t *query)
Delete a query.
ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
Create a query iterator.
void * ecs_query_get_group_ctx(const ecs_query_t *query, uint64_t group_id)
Get the context of a query group.
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.
ecs_table_t * table
Current table.
int32_t count
Number of entities to iterate.
Queries are lists of constraints (terms) that match entities.