Properly link flecs library

This commit is contained in:
2023-11-09 11:38:29 +01:00
parent dc585396c3
commit 8edcf9305c
1392 changed files with 390081 additions and 164 deletions

View File

@@ -0,0 +1,16 @@
#ifndef BASICS_H
#define BASICS_H
/* This generated file contains includes for project dependencies */
#include "basics/bake_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef BASICS_BAKE_CONFIG_H
#define BASICS_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

@@ -0,0 +1,10 @@
{
"id": "basics",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,100 @@
#include <basics.h>
#include <stdio.h>
// Observers provide a mechanism for responding to builtin and user defined
// events. They are similar to systems, in that they have the same callback
// signature and use the same query interface to match with entities, but
// instead of a phase they have an event kind.
//
// The most commonly used builtin events are:
// - EcsOnAdd: a component was added
// - EcsOnRemove: a component was removed
// - EcsOnSet: a component's value was changed
//
// The EcsOnAdd and EcsOnRemove events are only thrown when a component is
// actually added or removed. If an application invokes ecs_add and the entity
// already has the component, no event is emitted. Similarly, if an application
// invokes ecs_remove for a component the entity doesn't have, no event is
// emitted. That is in contrast to EcsOnSet, which is invoked each time ecs_set
// or ecs_modified is invoked.
//
// Observers are different from component hooks in a number of ways:
// - A component can only have one hook, whereas it can match many observers
// - A hook matches one component, whereas observers can match complex queries
// - Hooks are for add/set/remove events, observers can match custom events.
typedef struct {
double x, y;
} Position;
void Observer(ecs_iter_t *it) {
ecs_world_t *ecs = it->world;
// The event kind
ecs_entity_t event = it->event;
// The (component) id for which the event was emitted
ecs_entity_t event_id = it->event_id;
// Get component values as usual
Position *p = ecs_field(it, Position, 1);
for (int i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
if (event == EcsOnAdd) {
// No assumptions about the component value should be made here. If
// a ctor for the component was registered it will be called before
// the EcsOnAdd event, but a value assigned by set won't be visible.
printf(" - OnAdd: %s: %s\n",
ecs_get_name(ecs, event_id),
ecs_get_name(ecs, e));
} else {
// EcsOnSet or EcsOnRemove event
printf(" - %s: %s: %s: {%f, %f}\n",
ecs_get_name(ecs, event),
ecs_get_name(ecs, event_id),
ecs_get_name(ecs, e),
p[i].x, p[i].y);
}
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
// The ecs_observer macro (which calls the ecs_observer_init function) can
// be used to create an observer.
ecs_observer(ecs, {
// Observer filter. Uses same ecs_filter_desc_t as systems/queries
.filter = { .terms = {{ .id = ecs_id(Position) }}},
// Events the observer will listen for. Can contain multiple events
.events = { EcsOnAdd, EcsOnRemove },
// Observer callback
.callback = Observer
});
// Alternatively an observer can be created with the ECS_OBSERVER macro:
ECS_OBSERVER(ecs, Observer, EcsOnSet, Position);
// Create entity
ecs_entity_t e = ecs_new_entity(ecs, "e");
// Set Position (emits EcsOnAdd and EcsOnSet)
ecs_set(ecs, e, Position, {10, 20});
// Remove component (emits EcsOnRemove)
ecs_remove(ecs, e, Position);
// Remove component again (no event is emitted)
ecs_remove(ecs, e, Position);
ecs_fini(ecs);
// Output
// - OnAdd: Position: e
// - OnSet: Position: e: {10.000000, 20.000000}
// - OnRemove: Position: e: {10.000000, 20.000000}
}

View File

@@ -0,0 +1,16 @@
#ifndef CUSTOM_EVENT_H
#define CUSTOM_EVENT_H
/* This generated file contains includes for project dependencies */
#include "custom_event/bake_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef CUSTOM_EVENT_BAKE_CONFIG_H
#define CUSTOM_EVENT_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

@@ -0,0 +1,10 @@
{
"id": "custom_event",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,66 @@
#include <custom_event.h>
#include <stdio.h>
// Observers can be used to match custom events. Custom events can be emitted
// using the ecs_emit function. This function is also used by builtin events,
// so builtin and custom events use the same rules for matching with observers.
//
// An event consists out of three pieces of data used to match with observers:
// - An single event kind (EcsOnAdd, EcsOnRemove, ...)
// - One or more event ids (Position, Velocity, ...)
// - A source (either an entity or a table)
typedef struct {
double x, y;
} Position;
void Observer(ecs_iter_t *it) {
ecs_world_t *ecs = it->world;
// The event kind
ecs_entity_t event = it->event;
// The (component) id for which the event was emitted
ecs_entity_t event_id = it->event_id;
for (int i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
printf(" - %s: %s: %s\n",
ecs_get_name(ecs, event),
ecs_get_name(ecs, event_id),
ecs_get_name(ecs, e));
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
// Create custom event
ecs_entity_t MyEvent = ecs_new_entity(ecs, "MyEvent");
// Create observer for custom event
ECS_OBSERVER(ecs, Observer, MyEvent, Position);
// Create entity
ecs_entity_t e = ecs_new_entity(ecs, "e");
// The observer filter can be matched against the entity, so make sure it
// has the Position component before emitting the event. This does not
// trigger the observer yet.
ecs_set(ecs, e, Position, {10, 20});
// Emit the custom event
ecs_emit(ecs, &(ecs_event_desc_t) {
.event = MyEvent,
.ids = &(ecs_type_t){ (ecs_id_t[]){ ecs_id(Position) }, 1 }, // 1 id
.entity = e
});
ecs_fini(ecs);
// Output
// - MyEvent: Position: e
}

View File

@@ -0,0 +1,16 @@
#ifndef ENTITY_EVENT_H
#define ENTITY_EVENT_H
/* This generated file contains includes for project dependencies */
#include "entity_event/bake_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef ENTITY_EVENT_BAKE_CONFIG_H
#define ENTITY_EVENT_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

@@ -0,0 +1,10 @@
{
"id": "entity_event",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,84 @@
#include <entity_event.h>
#include <stdio.h>
// Entity events are events that are emitted and observed for a specific entity.
// They are a thin wrapper around regular observers, which match against queries
// instead of single entities. While they work similarly under the hood, entity
// events provide a much simpler API.
//
// An entity event only needs two pieces of data:
// - The entity on which to emit the event
// - The event to emit
//
// Any (_) is provided as component, indicating that we're not emitting an event
// for a specific component but rather for the entity itself.
// Event without payload
ECS_TAG_DECLARE(Click);
// Event with payload
typedef struct {
double width, height;
} Resize;
ECS_COMPONENT_DECLARE(Resize);
void OnClick(ecs_iter_t *it) {
// The event source can be obtained with ecs_field_src(1). This allows the
// same event function to be used for different entities.
char *path = ecs_get_fullpath(it->world, ecs_field_src(it, 1));
printf("clicked on %s!\n", path);
ecs_os_free(path);
}
void OnResize(ecs_iter_t *it) {
// Event payload can be obtained from the it->param member
Resize *p = it->param;
char *path = ecs_get_fullpath(it->world, ecs_field_src(it, 1));
printf("resized %s to {%.0f, %.0f}!\n", path, p->width, p->height);
ecs_os_free(path);
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_TAG_DEFINE(ecs, Click);
ECS_COMPONENT_DEFINE(ecs, Resize);
// Create a widget entity
ecs_entity_t widget = ecs_new_entity(ecs, "MyWidget");
// Create entity observer. Use EcsAny to indicate we're not interested in
// matching specific components.
ecs_observer(ecs, {
.filter.terms = {{ .id = EcsAny, .src.id = widget }},
.events = { Click },
.callback = OnClick
});
// Create another one for the Resize event
ecs_observer(ecs, {
.filter.terms = {{ .id = EcsAny, .src.id = widget }},
.events = { ecs_id(Resize) },
.callback = OnResize
});
// Emit the Click event
ecs_emit(ecs, &(ecs_event_desc_t) {
.event = Click,
.entity = widget
});
// Emit the Resize event
ecs_emit(ecs, &(ecs_event_desc_t) {
.event = ecs_id(Resize),
.entity = widget,
.param = &(Resize){100, 200} // pass payload
});
ecs_fini(ecs);
// Output
// clicked on MyWidget!
// resized MyWidget to {100, 200}!
}

View File

@@ -0,0 +1,16 @@
#ifndef MONITOR_H
#define MONITOR_H
/* This generated file contains includes for project dependencies */
#include "monitor/bake_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef MONITOR_BAKE_CONFIG_H
#define MONITOR_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

@@ -0,0 +1,10 @@
{
"id": "monitor",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,67 @@
#include <monitor.h>
#include <stdio.h>
// A monitor observer triggers when an entity starts/stop matching the observer
// filter. The observer communicates whether an entity is "entering/leaving" the
// monitor by setting ecs_iter_t::event to EcsOnAdd (for entering) or
// EcsOnRemove (for leaving).
//
// To specify that an observer is a monitor observer, the EcsMonitor tag must be
// provided as event. No additional event kinds should be provided for a monitor
// observer.
typedef struct {
double x, y;
} Position, Velocity;
void Observer(ecs_iter_t *it) {
ecs_world_t *ecs = it->world;
ecs_entity_t event = it->event;
ecs_entity_t event_id = it->event_id;
for (int i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
if (event == EcsOnAdd) {
printf(" - Enter: %s: %s\n",
ecs_get_name(ecs, event_id),
ecs_get_name(ecs, e));
} else if (event == EcsOnRemove) {
printf(" - Leave: %s: %s\n",
ecs_get_name(ecs, event_id),
ecs_get_name(ecs, e));
}
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Velocity);
ecs_observer(ecs, {
.filter = { .terms = {{ .id = ecs_id(Position) }, { .id = ecs_id(Velocity) }}},
.events = { EcsMonitor }, // Monitor entities entering/leaving the query
.callback = Observer,
});
// Create entity
ecs_entity_t e = ecs_new_entity(ecs, "e");
// This does not yet trigger the monitor, as the entity does not yet match.
ecs_set(ecs, e, Position, {10, 20});
// This triggers the monitor with EcsOnAdd, as the entity now matches.
ecs_set(ecs, e, Velocity, {1, 2});
// This triggers the monitor with EcsOnRemove, as the entity no longer matches.
ecs_remove(ecs, e, Position);
ecs_fini(ecs);
// Output
// - Enter: Velocity: e
// - Leave: Position: e
}

View File

@@ -0,0 +1,16 @@
#ifndef PROPAGATE_H
#define PROPAGATE_H
/* This generated file contains includes for project dependencies */
#include "propagate/bake_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef PROPAGATE_BAKE_CONFIG_H
#define PROPAGATE_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

@@ -0,0 +1,10 @@
{
"id": "propagate",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,77 @@
#include <propagate.h>
#include <stdio.h>
// Events are propagated along relationship edges. This means that observers can
// listen for events from a parent or prefab, like triggering when a component
// inherited from a prefab was set.
//
// Event propagation happens automatically when an observer contains a filter
// with the EcsUp flag set (indicating upwards traversal). Observers use the
// same matching logic as queries: if a query with upwards traversal matches an
// entity, so will an observer.
//
// Events are only propagated along traversable relationship edges.
typedef struct {
double x, y;
} Position;
void Observer(ecs_iter_t *it) {
ecs_world_t *ecs = it->world;
// The event kind
ecs_entity_t event = it->event;
// The (component) id for which the event was emitted
ecs_entity_t event_id = it->event_id;
// Grab Position from self and parent
Position *p_self = ecs_field(it, Position, 1);
Position *p_parent = ecs_field(it, Position, 2);
for (int i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
printf(" - %s: %s: %s: self: {%f, %f}, parent: {%f, %f}\n",
ecs_get_name(ecs, event),
ecs_get_name(ecs, event_id),
ecs_get_name(ecs, e),
p_self[i].x, p_self[i].y,
p_parent[i].x, p_parent[i].y);
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
// Create observer that listens for events from both self and parent
ecs_observer(ecs, {
.filter = { .terms = {
// Listen for Position events from self
{ .id = ecs_id(Position) },
// Listen for Position events from parent
{ .id = ecs_id(Position), .src.flags = EcsUp, .src.trav = EcsChildOf }
}},
.events = { EcsOnSet },
.callback = Observer
});
// Create entity and parent
ecs_entity_t p = ecs_new_entity(ecs, "p");
ecs_entity_t e = ecs_new_entity(ecs, "p.e"); // Create as child of p
// Set Position on entity. This doesn't trigger the observer yet, since the
// parent doesn't have Position yet.
ecs_set(ecs, e, Position, {10, 20});
// Set Position on parent. This event will be propagated and trigger the
// observer, as the observer query now matches.
ecs_set(ecs, p, Position, {1, 2});
ecs_fini(ecs);
// Output
// - OnSet: Position: e: self: {10.000000, 20.000000}, parent: {1.000000, 2.000000}
}

View File

@@ -0,0 +1,16 @@
#ifndef TWO_COMPONENTS_H
#define TWO_COMPONENTS_H
/* This generated file contains includes for project dependencies */
#include "two_components/bake_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef TWO_COMPONENTS_BAKE_CONFIG_H
#define TWO_COMPONENTS_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

@@ -0,0 +1,10 @@
{
"id": "two_components",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,53 @@
#include <two_components.h>
#include <stdio.h>
// An observer can match multiple components/tags. Only entities that match the
// entire observer filter will be forwarded to the callback. For example, an
// observer for Position,Velocity won't match an entity that only has Position.
typedef struct {
double x, y;
} Position, Velocity;
void Observer(ecs_iter_t *it) {
ecs_world_t *ecs = it->world;
ecs_entity_t event = it->event;
ecs_entity_t event_id = it->event_id;
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
for (int i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
printf("%s: %s: %s: p: {%f, %f} v: {%f, %f}\n",
ecs_get_name(ecs, event),
ecs_get_name(ecs, event_id),
ecs_get_name(ecs, e),
p[i].x, p[i].y,
v[i].x, v[i].y);
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Velocity);
ECS_OBSERVER(ecs, Observer, EcsOnSet, Position, Velocity);
// Create entity
ecs_entity_t e = ecs_new_entity(ecs, "e");
// Set Position (emits EcsOnSet, does not yet match observer)
ecs_set(ecs, e, Position, {10, 20});
// Set Velocity (emits EcsOnSet, matches observer)
ecs_set(ecs, e, Velocity, {1, 2});
return ecs_fini(ecs);
// Output:
// OnSet: Velocity: e: p: {10.000000, 20.000000} v: {1.000000, 2.000000}
}

View File

@@ -0,0 +1,16 @@
#ifndef YIELD_EXISTING_H
#define YIELD_EXISTING_H
/* This generated file contains includes for project dependencies */
#include "yield_existing/bake_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef YIELD_EXISTING_BAKE_CONFIG_H
#define YIELD_EXISTING_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

@@ -0,0 +1,10 @@
{
"id": "yield_existing",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,55 @@
#include <yield_existing.h>
#include <stdio.h>
// Observers can enable a "yield_existing" feature that upon creation of the
// observer produces events for all entities that match the observer query. The
// feature is only implemented for the builtin EcsOnAdd and EcsOnSet events.
//
// Custom events can also implement behavior for yield_existing by adding the
// Iterable component to the event (see EcsIterable for more details).
typedef struct {
double x, y;
} Position;
void Observer(ecs_iter_t *it) {
ecs_world_t *ecs = it->world;
ecs_entity_t event = it->event;
ecs_entity_t event_id = it->event_id;
Position *p = ecs_field(it, Position, 1);
for (int i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
printf(" - %s: %s: %s: {%f, %f}\n",
ecs_get_name(ecs, event),
ecs_get_name(ecs, event_id),
ecs_get_name(ecs, e),
p[i].x, p[i].y);
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
// Create existing entities with Position component
ecs_entity_t e1 = ecs_new_entity(ecs, "e1");
ecs_set(ecs, e1, Position, {10, 20});
ecs_entity_t e2 = ecs_new_entity(ecs, "e2");
ecs_set(ecs, e2, Position, {20, 30});
ecs_observer(ecs, {
.filter = { .terms = {{ .id = ecs_id(Position) }}},
.events = { EcsOnSet },
.callback = Observer,
.yield_existing = true // Trigger for existing matching entities
});
ecs_fini(ecs);
// Output
// - OnSet: Position: e1: {10.000000, 20.000000}
// - OnSet: Position: e2: {20.000000, 30.000000}
}