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,11 @@
{
"id": "basics",
"type": "application",
"value": {
"description": "A simple hello world flecs application",
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,72 @@
#include <basics.h>
#include <stdio.h>
// Prefabs are entities that can be used as templates for other entities. They
// are created with a builtin Prefab tag, which by default excludes them from
// queries and systems.
//
// Prefab instances are entities that have an IsA relationship to the prefab.
// The IsA relationship causes instances to inherit the components from the
// prefab. By default all instances for a prefab share its components.
//
// Inherited components save memory as they only need to be stored once for all
// prefab instances. They also speed up the creation of prefabs, as inherited
// components don't need to be copied to the instances.
//
// To get a private copy of a component, an instance can add it which is called
// an override. Overrides can be manual (by using add) or automatic (see the
// override example).
//
// If a prefab has children, adding the IsA relationship instantiates the prefab
// children for the instance (see hierarchy example).
typedef struct { double value; } Defense;
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Defense);
// Create a SpaceShip prefab with a Defense component.
ecs_entity_t SpaceShip = ecs_new_prefab(ecs, "SpaceShip");
ecs_set(ecs, SpaceShip, Defense, {50});
// Create a prefab instance
ecs_entity_t inst = ecs_new_entity(ecs, "my_spaceship");
ecs_add_pair(ecs, inst, EcsIsA, SpaceShip);
// Because of the IsA relationship, the instance now shares the Defense
// component with the prefab, and can be retrieved as a regular component:
const Defense *d_inst = ecs_get(ecs, inst, Defense);
printf("defense: %f\n", d_inst->value);
// Because the component is shared, changing the value on the prefab will
// also change the value for the instance:
ecs_set(ecs, SpaceShip, Defense, { 100 });
printf("defense after set: %f\n", d_inst->value); // now prints 100
// Prefab components can be iterated like regular components:
ecs_query_t *q = ecs_query(ecs, {
.filter.terms = {
{ .id = ecs_id(Defense) }
}
});
ecs_iter_t it = ecs_query_iter(ecs, q);
while (ecs_query_next(&it)) {
Defense *d = ecs_field(&it, Defense, 1);
for (int i = 0; i < it.count; i ++) {
printf("%s: defense: %f\n",
ecs_get_name(ecs, it.entities[i]), d[i].value);
}
}
// Output:
// defense: 50.000000
// defense after set: 100.000000
// my_spaceship: defense: 100.000000
return ecs_fini(ecs);
}

View File

@@ -0,0 +1,16 @@
#ifndef HIERARCHY_H
#define HIERARCHY_H
/* This generated file contains includes for project dependencies */
#include "hierarchy/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 HIERARCHY_BAKE_CONFIG_H
#define HIERARCHY_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

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

View File

@@ -0,0 +1,38 @@
#include <hierarchy.h>
#include <stdio.h>
// When a prefab has children, they are instantiated for an instance when the
// IsA relationship to the prefab is added.
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// Create a prefab hierarchy.
ecs_entity_t SpaceShip = ecs_new_prefab(ecs, "SpaceShip");
ecs_entity_t SpaceShipEngine = ecs_new_prefab(ecs, "Engine");
ecs_add_pair(ecs, SpaceShipEngine, EcsChildOf, SpaceShip);
ecs_entity_t SpaceShipCockpit = ecs_new_prefab(ecs, "Cockpit");
ecs_add_pair(ecs, SpaceShipCockpit, EcsChildOf, SpaceShip);
// Instantiate the prefab. This also creates an Engine and Cockpit child
// for the instance.
ecs_entity_t inst = ecs_new_entity(ecs, "my_spaceship");
ecs_add_pair(ecs, inst, EcsIsA, SpaceShip);
ecs_entity_t inst_engine = ecs_lookup_child(ecs, inst, "Engine");
ecs_entity_t inst_cockpit = ecs_lookup_child(ecs, inst, "Cockpit");
char *path = ecs_get_fullpath(ecs, inst_engine);
printf("instance engine: %s\n", path);
ecs_os_free(path);
path = ecs_get_fullpath(ecs, inst_cockpit);
printf("instance cockpit: %s\n", path);
ecs_os_free(path);
// Output:
// instance engine: my_spaceship.Engine
// instance cockpit: my_spaceship.Cockpit
return ecs_fini(ecs);
}

View File

@@ -0,0 +1,16 @@
#ifndef NESTED_PREFABS_H
#define NESTED_PREFABS_H
/* This generated file contains includes for project dependencies */
#include "nested_prefabs/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 NESTED_PREFABS_BAKE_CONFIG_H
#define NESTED_PREFABS_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

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

View File

@@ -0,0 +1,75 @@
#include <nested_prefabs.h>
#include <stdio.h>
// Nested prefabs make it possible to reuse an existing prefab inside another
// prefab. An example of where this could be useful is a car with four wheels:
// instead of defining four times what a wheel is a Car prefab can reference an
// existing Wheel prefab.
//
// Nested prefabs can be created by adding a child that is a variant (inherits
// from) another prefab. For more information on variants, see the variants
// example.
//
// Instantiated children from a nested prefab still inherit from the original
// prefab. The reason for this is that an instantiated child is an exact copy
// of the prefab child, and the prefab child only has an IsA relationship to the
// nested prefab.
//
// This example shows how auto overriding (see the auto override example) can be
// used to give instantiated children from a nested prefab a private copy of an
// inherited component.
typedef struct {
double value;
} TirePressure;
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, TirePressure);
// Create a Wheel prefab, make sure each instantiated wheel has a private
// copy of the TirePressure component.
ecs_entity_t Wheel = ecs_new_prefab(ecs, "Wheel");
ecs_set(ecs, Wheel, TirePressure, { 32 });
ecs_override(ecs, Wheel, TirePressure);
// Create a Car prefab with four wheels. Note how the wheel names are
// prefixed with 'Car.', this is has the same effect as adding the
// (ChildOf, Car) relationship.
ecs_entity_t Car = ecs_new_prefab(ecs, "Car");
ecs_entity_t WheelFrontLeft = ecs_new_prefab(ecs, "Car.FrontLeft");
ecs_add_pair(ecs, WheelFrontLeft, EcsIsA, Wheel);
ecs_entity_t WheelFrontRight = ecs_new_prefab(ecs, "Car.FrontRight");
ecs_add_pair(ecs, WheelFrontRight, EcsIsA, Wheel);
ecs_entity_t WheelBackLeft = ecs_new_prefab(ecs, "Car.BackLeft");
ecs_add_pair(ecs, WheelBackLeft, EcsIsA, Wheel);
ecs_entity_t WheelBackRight = ecs_new_prefab(ecs, "Car.BackRight");
ecs_add_pair(ecs, WheelBackRight, EcsIsA, Wheel);
// Create a prefab instance.
ecs_entity_t inst = ecs_new_entity(ecs, "my_car");
ecs_add_pair(ecs, inst, EcsIsA, Car);
// Lookup one of the wheels
ecs_entity_t inst_front_left = ecs_lookup_child(ecs, inst, "FrontLeft");
// The type shows that the child has a private copy of the TirePressure
// component, and an IsA relationship to the Wheel prefab.
char *type = ecs_type_str(ecs, ecs_get_type(ecs, inst_front_left));
printf("type: [%s]\n", type);
ecs_os_free(type);
// Get the TirePressure component & print its value
const TirePressure *p = ecs_get(ecs, inst_front_left, TirePressure);
printf("pressure: %f\n", p->value);
// Output
// type: [TirePressure, (Identifier,Name), (ChildOf,my_car), (IsA,Wheel)]
// pressure: 32.000000
return ecs_fini(ecs);
}

View File

@@ -0,0 +1,16 @@
#ifndef OVERRIDE_H
#define OVERRIDE_H
/* This generated file contains includes for project dependencies */
#include "override/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 OVERRIDE_BAKE_CONFIG_H
#define OVERRIDE_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

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

View File

@@ -0,0 +1,78 @@
#include <override.h>
#include <stdio.h>
// Overriding makes it possible for a prefab instance to obtain a private copy
// of an inherited component. To override a component the regular add operation
// is used. The overridden component will be initialized with the value of the
// inherited component.
//
// In some cases a prefab instance should always have a private copy of an
// inherited component. This can be achieved with an auto override which can be
// added to a prefab. Components with an auto override are automatically
// overridden when the prefab is instantiated.
typedef struct {
double value;
} Attack, Defense, Damage;
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Attack);
ECS_COMPONENT(ecs, Defense);
ECS_COMPONENT(ecs, Damage);
ecs_entity_t SpaceShip = ecs_new_prefab(ecs, "SpaceShip");
// Attack and Defense are properties that can be shared across many
// spaceships. This saves memory, and speeds up prefab creation as we don't
// have to copy the values of Attack and Defense to private components.
ecs_set(ecs, SpaceShip, Attack, { 75 });
ecs_set(ecs, SpaceShip, Defense, { 100 });
// Damage is a property that is private to a spaceship, so add an auto
// override for it. This ensures that each prefab instance will have a
// private copy of the component.
ecs_set(ecs, SpaceShip, Damage, { 0 });
ecs_override(ecs, SpaceShip, Damage);
// Create a prefab instance.
ecs_entity_t inst = ecs_new_entity(ecs, "my_instance");
ecs_add_pair(ecs, inst, EcsIsA, SpaceShip);
// The entity will now have a private copy of the Damage component, but not
// of the Attack and Defense components. We can see this when we look at the
// type of the instance:
char *type = ecs_type_str(ecs, ecs_get_type(ecs, inst));
printf("instance type = [%s]\n", type);
ecs_os_free(type);
// Even though Attack was not automatically overridden, we can always
// override it manually afterwards by adding it:
ecs_add(ecs, inst, Attack);
// The Attack component now shows up in the entity type:
type = ecs_type_str(ecs, ecs_get_type(ecs, inst));
printf("instance type = [%s]\n", type);
ecs_os_free(type);
// We can get all components on the instance, regardless of whether they
// are overridden or not. Note that the overridden components (Attack and
// Damage) are initialized with the values from the prefab component:
const Attack *attack = ecs_get(ecs, inst, Attack);
const Defense *defense = ecs_get(ecs, inst, Defense);
const Damage *damage = ecs_get(ecs, inst, Damage);
printf("attack: %f\n", attack->value);
printf("defense: %f\n", defense->value);
printf("damage: %f\n", damage->value);
// Output:
// instance type = [Damage, (Identifier,Name), (IsA,SpaceShip)]
// instance type = [Attack, Damage, (Identifier,Name), (IsA,SpaceShip)]
// attack: 75.000000
// defense: 100.000000
// damage: 0.000000
return ecs_fini(ecs);
}

View File

@@ -0,0 +1,16 @@
#ifndef SLOTS_H
#define SLOTS_H
/* This generated file contains includes for project dependencies */
#include "slots/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 SLOTS_BAKE_CONFIG_H
#define SLOTS_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

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

View File

@@ -0,0 +1,76 @@
#include <slots.h>
#include <stdio.h>
// Slots can be combined with prefab hierarchies to make it easier to access
// the child entities created for an instance.
//
// To create a slot, the SlotOf relationship is added to the child of a prefab,
// with as relationship target the prefab for which to register the slot. When
// the prefab is instantiated, each slot will be added as a relationship pair
// to the instance that looks like this:
// (PrefabChild, InstanceChild)
//
// For a SpaceShip prefab and an Engine child, that pair would look like this:
// (SpaceShip.Engine, Instance.Engine)
//
// To get the entity for a slot, an application can use the regular functions
// to inspect relationships and relationship targets (see code).
//
// Slots can be added to any level of a prefab hierarchy, as long as it is above
// (a parent of) the slot itself. When the prefab tree is instantiated, the
// slots are added to the entities that correspond with the prefab children.
//
// Without slots, an application would have to rely on manually looking up
// entities by name to get access to the instantiated children, like what the
// hierarchy example does.
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// Create the same prefab hierarchy as from the hierarchy example, but now
// with the SlotOf relationship.
ecs_entity_t SpaceShip = ecs_new_prefab(ecs, "SpaceShip");
ecs_entity_t Engine = ecs_new_prefab(ecs, "Engine");
ecs_add_pair(ecs, Engine, EcsChildOf, SpaceShip);
ecs_add_pair(ecs, Engine, EcsSlotOf, SpaceShip);
ecs_entity_t Cockpit = ecs_new_prefab(ecs, "Cockpit");
ecs_add_pair(ecs, Cockpit, EcsChildOf, SpaceShip);
ecs_add_pair(ecs, Cockpit, EcsSlotOf, SpaceShip);
// Add an additional child to the Cockpit prefab to demonstrate how
// slots can be different from the parent. This slot could have been
// added to the Cockpit prefab, but instead we register it on the top
// level SpaceShip prefab.
ecs_entity_t PilotSeat = ecs_new_prefab(ecs, "PilotSeat");
ecs_add_pair(ecs, PilotSeat, EcsChildOf, Cockpit);
ecs_add_pair(ecs, PilotSeat, EcsSlotOf, SpaceShip);
// Create a prefab instance.
ecs_entity_t inst = ecs_new_entity(ecs, "my_spaceship");
ecs_add_pair(ecs, inst, EcsIsA, SpaceShip);
// Get the instantiated entities for the prefab slots
ecs_entity_t inst_engine = ecs_get_target(ecs, inst, Engine, 0);
ecs_entity_t inst_cockpit = ecs_get_target(ecs, inst, Cockpit, 0);
ecs_entity_t inst_seat = ecs_get_target(ecs, inst, PilotSeat, 0);
char *path = ecs_get_fullpath(ecs, inst_engine);
printf("instance engine: %s\n", path);
ecs_os_free(path);
path = ecs_get_fullpath(ecs, inst_cockpit);
printf("instance cockpit: %s\n", path);
ecs_os_free(path);
path = ecs_get_fullpath(ecs, inst_seat);
printf("instance seat: %s\n", path);
ecs_os_free(path);
// Output:
// instance engine: my_spaceship.Engine
// instance cockpit: my_spaceship.Cockpit
// instance seat: my_spaceship.Cockpit.PilotSeat
return ecs_fini(ecs);
}

View File

@@ -0,0 +1,16 @@
#ifndef VARIANT_H
#define VARIANT_H
/* This generated file contains includes for project dependencies */
#include "variant/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 VARIANT_BAKE_CONFIG_H
#define VARIANT_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

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

View File

@@ -0,0 +1,101 @@
#include <variant.h>
#include <stdio.h>
/* Prefabs can inherit from each other, which creates prefab variants. With
* variants applications can reuse a commmon set of components and specialize it
* by adding or overriding components on the variant. */
typedef struct { double value; } Attack;
typedef struct { double value; } Defense;
typedef struct { double value; } FreightCapacity;
typedef struct { double value; } ImpulseSpeed;
typedef struct {
double x;
double y;
} Position;
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Attack);
ECS_COMPONENT(ecs, Defense);
ECS_COMPONENT(ecs, FreightCapacity);
ECS_COMPONENT(ecs, ImpulseSpeed);
// Create a base prefab for SpaceShips.
ecs_entity_t SpaceShip = ecs_new_prefab(ecs, "SpaceShip");
ecs_set(ecs, SpaceShip, ImpulseSpeed, {50});
ecs_set(ecs, SpaceShip, Defense, {25});
// Create a Freighter variant which inherits from SpaceShip
ecs_entity_t Freighter = ecs_new_prefab(ecs, "Freighter");
ecs_add_pair(ecs, Freighter, EcsIsA, SpaceShip);
ecs_set(ecs, Freighter, FreightCapacity, {100});
ecs_set(ecs, Freighter, Defense, {50});
// Create a MammothFreighter variant which inherits from Freighter
ecs_entity_t MammothFreighter = ecs_new_prefab(ecs, "MammothFreighter");
ecs_add_pair(ecs, MammothFreighter, EcsIsA, Freighter);
ecs_set(ecs, MammothFreighter, FreightCapacity, {500});
// Create a Frigate variant which inherits from SpaceShip
ecs_entity_t Frigate = ecs_new_prefab(ecs, "Frigate");
ecs_add_pair(ecs, Frigate, EcsIsA, SpaceShip);
ecs_set(ecs, Frigate, Attack, {100});
ecs_set(ecs, Frigate, Defense, {75});
ecs_set(ecs, Frigate, ImpulseSpeed, {125});
// Create an instance of the MammothFreighter. This entity will inherit the
// ImpulseSpeed from SpaceShip, Defense from Freighter and FreightCapacity
// from MammothFreighter.
ecs_entity_t inst = ecs_new_entity(ecs, "my_freighter");
ecs_add_pair(ecs, inst, EcsIsA, MammothFreighter);
// Add a private Position component.
ecs_set(ecs, inst, Position, {10, 20});
// Instances can override inherited components to give them a private copy
// of the component. This freighter got an armor upgrade:
ecs_set(ecs, inst, Defense, {100});
// Queries can match components from multiple levels of inheritance
ecs_query_t *q = ecs_query(ecs, {
.filter.terms = {
{ .id = ecs_id(Position) },
{ .id = ecs_id(ImpulseSpeed) },
{ .id = ecs_id(Defense) },
{ .id = ecs_id(FreightCapacity) }
}
});
ecs_iter_t it = ecs_query_iter(ecs, q);
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
ImpulseSpeed *s = ecs_field(&it, ImpulseSpeed, 2);
Defense *d = ecs_field(&it, Defense, 3);
FreightCapacity *c = ecs_field(&it, FreightCapacity, 4);
for (int i = 0; i < it.count; i ++) {
printf("%s:\n", ecs_get_name(ecs, it.entities[i]));
printf(" - position: %f, %f\n", p[i].x, p[i].y);
printf(" - impulse speed: %f\n", s[i].value);
printf(" - defense: %f\n", d[i].value);
printf(" - capacity: %f\n", c[i].value);
}
}
// Note that when matching tables with shared components, entities are
// returned one by one. See the queries/instancing example for more details.
// Output:
// my_freighter:
// - position: 10.000000, 20.000000
// - impulse speed: 50.000000
// - defense: 100.000000
// - capacity: 500.000000
return ecs_fini(ecs);
}