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": {
"author": "Jane Doe",
"description": "A simple hello world flecs application",
"use": [
"flecs"
]
}
}

View File

@@ -0,0 +1,46 @@
#include <basics.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_TAG(ecs, Eats);
// Entity used for Grows relationship
ecs_entity_t grows = ecs_new_entity(ecs, "Grows");
// Relationship objects
ecs_entity_t apples = ecs_new_entity(ecs, "Apples");
ecs_entity_t pears = ecs_new_entity(ecs, "Pears");
// Create an entity with 3 relationships. Relationships are like regular components,
// but combine two types/identifiers into an (relationship, object) pair.
ecs_entity_t bob = ecs_new_entity(ecs, "Bob");
// Pairs can be constructed from a type and entity
ecs_add_pair(ecs, bob, Eats, apples);
ecs_add_pair(ecs, bob, Eats, pears);
// Pairs can also be constructed from two entity ids
ecs_add_pair(ecs, bob, grows, pears);
// Has can be used with relationships as well
printf("Bob eats apples? %d\n", ecs_has_pair(ecs, bob, Eats, apples));
// Wildcards can be used to match relationships
printf("Bob grows food? %d\n", ecs_has_pair(ecs, bob, grows, EcsWildcard));
// Print the type of the entity. Should output:
// (Identifier,Name),(Eats,Apples),(Eats,Pears),(Grows,Pears)
char *type_str = ecs_type_str(ecs, ecs_get_type(ecs, bob));
printf("Bob's type: [%s]\n", type_str);
ecs_os_free(type_str);
// Get first target of relationship
printf("Bob eats %s\n",
ecs_get_name(ecs, ecs_get_target(ecs, bob, Eats, 0)));
// Get second target of relationship
printf("Bob also eats %s\n",
ecs_get_name(ecs, ecs_get_target(ecs, bob, Eats, 1)));
return ecs_fini(ecs);
}

View File

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

View File

@@ -0,0 +1,9 @@
{
"id": "exclusive_relations",
"type": "application",
"value": {
"use": [
"flecs"
]
}
}

View File

@@ -0,0 +1,47 @@
#include <exclusive_relations.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_TAG(ecs, Platoon);
// Register Platoon as exclusive relationship. This ensures that an entity
// can only belong to a single Platoon.
ecs_add_id(ecs, Platoon, EcsExclusive);
// Create two platoons
ecs_entity_t platoon_1 = ecs_new_id(ecs);
ecs_entity_t platoon_2 = ecs_new_id(ecs);
// Create a unit
ecs_entity_t unit = ecs_new_id(ecs);
// Add unit to platoon 1
ecs_add_pair(ecs, unit, Platoon, platoon_1);
// Log platoon of unit
printf("Unit in platoon 1: %s\n",
ecs_has_pair(ecs, unit, Platoon, platoon_1) ? "true" : "false");
printf("Unit in platoon 2: %s\n\n",
ecs_has_pair(ecs, unit, Platoon, platoon_2) ? "true" : "false");
// Add unit to platoon 2. Because Platoon is an exclusive relationship, this
// both removes (Platoon, platoon_1) and adds (Platoon, platoon_2) in a
// single operation.
ecs_add_pair(ecs, unit, Platoon, platoon_2);
printf("Unit in platoon 1: %s\n",
ecs_has_pair(ecs, unit, Platoon, platoon_1) ? "true" : "false");
printf("Unit in platoon 2: %s\n",
ecs_has_pair(ecs, unit, Platoon, platoon_2) ? "true" : "false");
// Output:
// Unit in platoon 1: true
// Unit in platoon 2: false
//
// Unit in platoon 1: false
// Unit in platoon 2: true
return ecs_fini(ecs);
}

View File

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

View File

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

View File

@@ -0,0 +1,112 @@
#include <relation_component.h>
#include <stdio.h>
// This example shows how relationships can be combined with components to attach
// data to a relationship.
// Some demo components:
typedef struct {
double amount;
} Requires;
typedef struct {
double timeout;
} Expires;
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, Requires);
ECS_COMPONENT(ecs, Expires);
ECS_COMPONENT(ecs, Position);
ECS_TAG(ecs, Gigawatts);
ECS_TAG(ecs, MustHave);
// When one element of a pair is a component and the other element is a tag,
// the pair assumes the type of the component.
ecs_entity_t e1 = ecs_new_id(ecs);
ecs_set_pair(ecs, e1, Requires, Gigawatts, {1.21});
const Requires *r = ecs_get_pair(ecs, e1, Requires, Gigawatts);
printf("requires: %.2f\n", r->amount);
// The component can be either the first or second part of a pair:
ecs_entity_t e2 = ecs_new_id(ecs);
ecs_set_pair_second(ecs, e2, Gigawatts, Requires, {1.21});
r = ecs_get_pair_second(ecs, e2, Gigawatts, Requires);
printf("requires: %.2f\n", r->amount);
// Note that <Requires, Gigawatts> and <Gigawatts, Requires> are two
// different pairs, and can be added to an entity at the same time.
// If both parts of a pair are components, the pair assumes the type of
// the first element:
ecs_entity_t e3 = ecs_new_id(ecs);
ecs_set_pair(ecs, e3, Expires, ecs_id(Position), {0.5});
const Expires *e = ecs_get_pair(ecs, e3, Expires, ecs_id(Position));
printf("expires: %.1f\n", e->timeout);
// You can prevent a pair from assuming the type of a component by adding
// the Tag property to a relationship:
ecs_add_id(ecs, MustHave, EcsTag);
// Even though Position is a component, <MustHave, Position> contains no
// data because MustHave has the Tag property.
ecs_new_w_pair(ecs, MustHave, ecs_id(Position));
// The id::type_id method can be used to find the component type for a pair:
ecs_entity_t type_id;
char *type_str;
type_id = ecs_get_typeid(ecs, ecs_pair(ecs_id(Requires), Gigawatts));
type_str = ecs_get_fullpath(ecs, type_id);
printf("%s\n", type_str);
ecs_os_free(type_str);
type_id = ecs_get_typeid(ecs, ecs_pair(Gigawatts, ecs_id(Requires)));
type_str = ecs_get_fullpath(ecs, type_id);
printf("%s\n", type_str);
ecs_os_free(type_str);
type_id = ecs_get_typeid(ecs, ecs_pair(ecs_id(Expires), ecs_id(Position)));
type_str = ecs_get_fullpath(ecs, type_id);
printf("%s\n", type_str);
ecs_os_free(type_str);
type_id = ecs_get_typeid(ecs, ecs_pair(MustHave, ecs_id(Position)));
type_str = ecs_get_fullpath(ecs, type_id);
printf("%s\n", type_str);
ecs_os_free(type_str);
// When querying for a relationship, provide both parts of the pair:
ecs_query_t *q = ecs_query(ecs, {
.filter.terms = {
{ .id = ecs_pair(ecs_id(Requires), Gigawatts) }
}
});
// When iterating, always use the pair type:
ecs_iter_t it = ecs_query_iter(ecs, q);
while (ecs_query_next(&it)) {
r = ecs_field(&it, Requires, 1);
for (int i = 0; i < it.count; i ++) {
printf("requires %.2f gigawatts\n", r[i].amount);
}
}
// Output:
// requires: 1.21
// requires: 1.21
// expires: 0.5
// Requires
// Requires
// Expires
// 0
// requires 1.21 gigawatts
}

View File

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

View File

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

View File

@@ -0,0 +1,32 @@
#include <symmetric_relations.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_TAG(ecs, TradesWith);
// Register TradesWith as symmetric relationship. Symmetric relationships
// go both ways, adding (R, B) to A will also add (R, A) to B.
ecs_add_id(ecs, TradesWith, EcsSymmetric);
// Create two players
ecs_entity_t player_1 = ecs_new_id(ecs);
ecs_entity_t player_2 = ecs_new_id(ecs);
// Add (TradesWith, player_2) to player_1. This also adds
// (TradesWith, player_1) to player_2.
ecs_add_pair(ecs, player_1, TradesWith, player_2);
// Log platoon of unit
printf("Player 1 trades with Player 2: %s\n",
(ecs_has_pair(ecs, player_1, TradesWith, player_2) ? "true" : "false"));
printf("Player 2 trades with Player 1: %s\n",
(ecs_has_pair(ecs, player_2, TradesWith, player_1) ? "true" : "false"));
// Output:
// Player 1 trades with Player 2: true
// Player 2 trades with Player 1: true
return ecs_fini(ecs);
}

View File

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

View File

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

View File

@@ -0,0 +1,77 @@
#include <union.h>
#include <stdio.h>
// This example shows how to use union relationships. Union relationships behave
// much like exclusive relationships in that entities can have only one instance
// and that adding an instance removes the previous instance.
//
// What makes union relationships stand out is that changing the relationship
// target doesn't change the archetype of an entity. This allows for quick
// switching of tags, which can be useful when encoding state machines in ECS.
//
// There is a tradeoff, and that is that because a single archetype can contain
// entities with multiple targets, queries need to do a bit of extra work to
// only return the requested target.
int main(int argc, char *argv[]) {
ecs_world_t *world = ecs_init_w_args(argc, argv);
// Create a Movement state machine with 3 states
ECS_ENTITY(world, Movement, Union);
ECS_TAG(world, Standing);
ECS_TAG(world, Walking);
ECS_TAG(world, Running);
// Create a Direction state machine with 4 states
ECS_ENTITY(world, Direction, Union);
ECS_TAG(world, Front);
ECS_TAG(world, Back);
ECS_TAG(world, Left);
ECS_TAG(world, Right);
// Create a query that subscribers for all entities that have a Direction
// and that are walking.
ecs_query_t *q = ecs_query(world, {
.filter.terms = {
{ .id = ecs_pair(Movement, Walking) },
{ .id = ecs_pair(Direction, EcsWildcard) }
}
});
// Create a few entities with various state combinations
ecs_entity_t e1 = ecs_new_entity(world, "e1");
ecs_add_pair(world, e1, Movement, Walking);
ecs_add_pair(world, e1, Direction, Front);
ecs_entity_t e2 = ecs_new_entity(world, "e2");
ecs_add_pair(world, e2, Movement, Running);
ecs_add_pair(world, e2, Direction, Left);
ecs_entity_t e3 = ecs_new_entity(world, "e3");
ecs_add_pair(world, e3, Movement, Running);
ecs_add_pair(world, e3, Direction, Right);
// Add Walking to e3. This will remove the Running case
ecs_add_pair(world, e3, Movement, Walking);
// Iterate query as usual
ecs_iter_t it = ecs_query_iter(world, q);
while (ecs_iter_next(&it)) {
ecs_entity_t *movement = ecs_field(&it, ecs_entity_t, 1);
ecs_entity_t *direction = ecs_field(&it, ecs_entity_t, 2);
for (int i = 0; i < it.count; i ++) {
printf("%s: Movement: %s, Direction: %s\n",
ecs_get_name(world, it.entities[i]),
ecs_get_name(world, movement[i]),
ecs_get_name(world, direction[i]));
}
}
// Output:
// e3: Movement: Walking, Direction: Back
// e1: Movement: Walking, Direction: Front
// Cleanup
return ecs_fini(world);
}