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

View File

@@ -0,0 +1,62 @@
#include <basics.h>
#include <stdio.h>
typedef struct {
double x, y;
} Position, Velocity;
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
const Velocity *v = ecs_field(it, Velocity, 2);
for (int i = 0; i < it->count; i ++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
printf("%s: {%f, %f}\n", ecs_get_name(it->world, it->entities[i]),
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);
ECS_COMPONENT(ecs, Velocity);
// Create a system for Position, Velocity.
ecs_entity_t move = ecs_system(ecs, {
// Systems are entities, and by initializing the .entity field we can
// set some additional properties for the system like a name. While this
// is not mandatory, it makes a system easier to find in tools like the
// explorer (https://www.flecs.dev/explorer/).
.entity = ecs_entity(ecs, {
.name = "Move"
}),
.query.filter.terms = {
{ .id = ecs_id(Position) },
{ .id = ecs_id(Velocity), .inout = EcsIn }
},
.callback = Move
});
// C applications can also use the ECS_SYSTEM convenience macro:
// ECS_SYSTEM(ecs, Move, 0, Position, [in] Velocity);
// Create a few test entities for a Position, Velocity query
ecs_entity_t e1 = ecs_new_entity(ecs, "e1");
ecs_set(ecs, e1, Position, {10, 20});
ecs_set(ecs, e1, Velocity, {1, 2});
ecs_entity_t e2 = ecs_new_entity(ecs, "e2");
ecs_set(ecs, e2, Position, {10, 20});
ecs_set(ecs, e2, Velocity, {3, 4});
// This entity will not match as it does not have Position, Velocity
ecs_entity_t e3 = ecs_new_entity(ecs, "e3");
ecs_set(ecs, e3, Position, {10, 20});
// Run the system
ecs_run(ecs, move, 0.0f, NULL);
return ecs_fini(ecs);
}

View File

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

View File

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

View File

@@ -0,0 +1,58 @@
#include <custom_phases.h>
#include <stdio.h>
// This application demonstrates how to use custom phases for systems. The
// default pipeline will automatically run systems for custom phases as long as
// they have the EcsPhase tag.
// Dummy system
void Sys(ecs_iter_t *it) {
printf("system %s\n", ecs_get_name(it->world, it->system));
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// Create two custom phases that branch off of EcsOnUpdate. Note that the
// phases have the Phase tag, which is necessary for the builtin pipeline
// to discover which systems it should run.
ecs_entity_t Physics = ecs_new_w_id(ecs, EcsPhase);
ecs_entity_t Collisions = ecs_new_w_id(ecs, EcsPhase);
ecs_add_pair(ecs, Physics, EcsDependsOn, EcsOnUpdate);
ecs_add_pair(ecs, Collisions, EcsDependsOn, Physics);
// Create 3 dummy systems.
ecs_system(ecs, {
.entity = ecs_entity(ecs, {
.name = "CollisionSystem",
.add = { ecs_dependson(Collisions) }
}),
.callback = Sys
});
ecs_system(ecs, {
.entity = ecs_entity(ecs, {
.name = "PhysicsSystem",
.add = { ecs_dependson(Physics) }
}),
.callback = Sys
});
ecs_system(ecs, {
.entity = ecs_entity(ecs, {
.name = "GameSystem",
.add = { ecs_dependson(EcsOnUpdate) }
}),
.callback = Sys
});
// Run pipeline
ecs_progress(ecs, 0);
return ecs_fini(ecs);
// Output
// system GameSystem
// system PhysicsSystem
// system CollisionSystem
}

View File

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

View File

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

View File

@@ -0,0 +1,59 @@
#include <custom_phases_no_builtin.h>
#include <stdio.h>
// This application demonstrates how to use custom phases for systems, without
// using or branching off of the builtin pipelines.
// Dummy system
void Sys(ecs_iter_t *it) {
printf("system %s\n", ecs_get_name(it->world, it->system));
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// Create three custom phases. Note that the two phases have the Phase tag,
// which is necessary for the builtin pipeline to discover which systems it
// should run.
ecs_entity_t Update = ecs_new_w_id(ecs, EcsPhase);
ecs_entity_t Physics = ecs_new_w_id(ecs, EcsPhase);
ecs_entity_t Collisions = ecs_new_w_id(ecs, EcsPhase);
ecs_add_pair(ecs, Physics, EcsDependsOn, Update);
ecs_add_pair(ecs, Collisions, EcsDependsOn, Physics);
// Create 3 dummy systems.
ecs_system(ecs, {
.entity = ecs_entity(ecs, {
.name = "CollisionSystem",
.add = { ecs_dependson(Collisions) }
}),
.callback = Sys
});
ecs_system(ecs, {
.entity = ecs_entity(ecs, {
.name = "PhysicsSystem",
.add = { ecs_dependson(Physics) }
}),
.callback = Sys
});
ecs_system(ecs, {
.entity = ecs_entity(ecs, {
.name = "GameSystem",
.add = { ecs_dependson(Update) }
}),
.callback = Sys
});
// Run pipeline
ecs_progress(ecs, 0);
return ecs_fini(ecs);
// Output
// system GameSystem
// system PhysicsSystem
// system CollisionSystem
}

View File

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

View File

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

View File

@@ -0,0 +1,47 @@
#include <custom_pipeline.h>
#include <stdio.h>
// Custom pipelines make it possible for applications to override which systems
// are ran by a pipeline and how they are ordered. Pipelines are queries under
// the hood, and custom pipelines override the query used for system matching.
// If you only want to use custom phases in addition or in place of the builtin
// phases see the custom_phases and custom_phases_no_builtin examples, as this
// does not require using a custom pipeline.
// Dummy system
void Sys(ecs_iter_t *it) {
(void)it;
printf("System ran!\n");
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_TAG(ecs, Physics);
// Create a pipeline that matches systems with Physics. Note that this
// pipeline does not require the use of phases (see custom_phases) or of the
// DependsOn relationship.
ecs_entity_t pipeline = ecs_pipeline(ecs, {
.query.filter.terms = {
{ .id = EcsSystem }, // Mandatory, must always match systems
{ .id = Physics }
}
});
// Configure the world to use the custom pipeline
ecs_set_pipeline(ecs, pipeline);
// Create system with Physics tag
ECS_SYSTEM(ecs, Sys, Physics, 0);
// Runs the pipeline & system
ecs_progress(ecs, 0);
return ecs_fini(ecs);
// Output
// System ran!
}

View File

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

View File

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

View File

@@ -0,0 +1,77 @@
#include <custom_runner.h>
#include <stdio.h>
// Systems can be created with a custom run function that takes control over the
// entire iteration. By default, a system is invoked once per matched table,
// which means the function can be called multiple times per frame. In some
// cases that's inconvenient, like when a system has things it needs to do only
// once per frame. For these use cases, the run callback can be used which is
// called once per frame per system.
typedef struct {
double x, y;
} Position, Velocity;
void Move(ecs_iter_t *it) {
printf("Move begin\n");
// In a run function we have to manually iterate through the query results.
// Note that this code looks the same as iterating a query.
while (ecs_query_next(it)) {
// Inside the loop the code looks the same as for a regular system
Position *p = ecs_field(it, Position, 1);
const Velocity *v = ecs_field(it, Velocity, 2);
for (int i = 0; i < it->count; i ++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
printf("%s: {%f, %f}\n", ecs_get_name(it->world, it->entities[i]),
p[i].x, p[i].y);
}
}
printf("Move end\n");
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Velocity);
// Create a system for Position, Velocity
ecs_entity_t move = ecs_system(ecs, {
.query.filter.terms = {
{ .id = ecs_id(Position) },
{ .id = ecs_id(Velocity), .inout = EcsIn }
},
.run = Move // use .run instead of .callback
});
// C applications can also use the ECS_SYSTEM convenience macro:
// ECS_SYSTEM(ecs, Move, 0, Position, [in] Velocity);
// Create a few test entities for a Position, Velocity query
ecs_entity_t e1 = ecs_new_entity(ecs, "e1");
ecs_set(ecs, e1, Position, {10, 20});
ecs_set(ecs, e1, Velocity, {1, 2});
ecs_entity_t e2 = ecs_new_entity(ecs, "e2");
ecs_set(ecs, e2, Position, {10, 20});
ecs_set(ecs, e2, Velocity, {3, 4});
// This entity will not match as it does not have Position, Velocity
ecs_entity_t e3 = ecs_new_entity(ecs, "e3");
ecs_set(ecs, e3, Position, {10, 20});
// Run the system
ecs_run(ecs, move, 0.0f, NULL);
return ecs_fini(ecs);
// Output
// Move begin
// e1: {11.000000, 22.000000}
// e2: {13.000000, 24.000000}
// Move end
}

View File

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

View File

@@ -0,0 +1,12 @@
{
"id": "delta_time",
"type": "application",
"value": {
"author": "Jane Doe",
"description": "A simple hello world flecs application",
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,31 @@
#include <delta_time.h>
#include <stdio.h>
void PrintDeltaTime(ecs_iter_t *it) {
// Print delta_time. The same value is passed to all systems.
printf("delta_time: %f\n", (double)it->delta_time);
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// Create system that prints delta_time. This system doesn't query for any
// components which means it won't match any entities, but will still be ran
// once for each call to ecs_progress.
ECS_SYSTEM(ecs, PrintDeltaTime, EcsOnUpdate, 0);
// Call progress with 0.0f for the delta_time parameter. This will cause
// ecs_progress to measure the time passed since the last frame. The
// delta_time of the first frame is a best guess (16ms).
ecs_progress(ecs, 0.0f);
// The following calls should print a delta_time of approximately 100ms
ecs_os_sleep(0, 100 * 1000 * 1000);
ecs_progress(ecs, 0.0f);
ecs_os_sleep(0, 100 * 1000 * 1000);
ecs_progress(ecs, 0.0f);
return ecs_fini(ecs);
}

View File

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

View File

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

View File

@@ -0,0 +1,83 @@
#include <mutate_entity.h>
#include <stdio.h>
typedef struct {
double value;
} Timeout;
// System that deletes an entity after a timeout expires
void Expire(ecs_iter_t *it) {
Timeout *t = ecs_field(it, Timeout, 1);
for (int i = 0; i < it->count; i ++) {
t[i].value -= (double)it->delta_time;
if (t[i].value <= 0) {
// When deleting the entity, use the world provided by the iterator.
// To make sure that the storage doesn't change while a system is
// iterating entities, and multiple threads can safely access the
// data, mutations (like a delete) are added to a command queue and
// executed when it's safe to do so.
// A system should not use the world pointer that is provided by the
// ecs_init function, as this will throw an error that the world is
// in readonly mode (try replacing it->world with it->real_world).
ecs_delete(it->world, it->entities[i]);
printf("Expire: %s deleted!\n", ecs_get_name(
it->world, it->entities[i]));
}
}
}
// System that prints remaining expiry time
void PrintExpire(ecs_iter_t *it) {
Timeout *t = ecs_field(it, Timeout, 1);
for (int i = 0; i < it->count; i ++) {
printf("PrintExpire: %s has %.2f seconds left\n", ecs_get_name(
it->world, it->entities[i]), t[i].value);
}
}
// Observer that triggers when the component is actually removed
void Expired(ecs_iter_t *it) {
for (int i = 0; i < it->count; i ++) {
printf("Expired: %s actually deleted\n", ecs_get_name(
it->world, it->entities[i]));
}
}
int main(int argc, char *argv[]) {
ecs_world_t *world = ecs_init_w_args(argc, argv);
ECS_COMPONENT(world, Timeout);
ECS_SYSTEM(world, Expire, EcsOnUpdate, Timeout);
ECS_SYSTEM(world, PrintExpire, EcsOnUpdate, [in] Timeout);
ECS_OBSERVER(world, Expired, EcsOnRemove, Timeout);
// Create entity that expires after 3 seconds
ecs_entity_t e = ecs_set_name(world, 0, "MyEntity");
ecs_set(world, e, Timeout, { 3.0 });
ecs_set_target_fps(world, 1);
while ( ecs_progress(world, 0)) {
// If entity is no longer alive, exit
if (!ecs_is_alive(world, e)) {
break;
}
printf("Tick...\n");
}
// Output:
// PrintExpire: MyEntity has 2.00 seconds left
// Tick...
// PrintExpire: MyEntity has 0.98 seconds left
// Tick...
// Expire: MyEntity deleted!
// PrintExpire: MyEntity has -0.02 seconds left
// Expired: MyEntity actually deleted
return ecs_fini(world);
}

View File

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

View File

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

View File

@@ -0,0 +1,133 @@
#include <no_readonly.h>
#include <stdio.h>
// When an application calls ecs_progress(), the world is put in readonly mode.
// This ensures that systems (on multiple threads) can safely iterate
// components, without having to worry about components moving around while
// they're being read. This has as side effect that any operations (like adding
// or removing components) are not visible until the end of the frame (see the
// sync_point example for more details).
// Sometimes this is not what you want, and you need a change to be visible
// immediately. For these use cases, applications can use a no_readonly system.
// This temporarily takes the world out of readonly mode, so a system can make
// changes that are directly visible.
// Because they mutate the world directly, no_readonly systems are never ran on
// more than one thread, and no other systems are ran at the same time.
ECS_DECLARE(Waiter);
ECS_DECLARE(Plate);
// System that assigns plates to waiter. By making this system no_readonly
// plate assignments are assigned directly (not deferred) to waiters, which
// ensures that we won't assign plates to the same waiter more than once.
void AssignPlate(ecs_iter_t *it) {
ecs_world_t *ecs = it->world;
ecs_query_t *q_waiter = it->ctx; // Get query from system context
// Iterate unassigned plates
for (int i = 0; i < it->count; i ++) {
ecs_entity_t plate = it->entities[i];
// Find an available waiter
ecs_iter_t qit = ecs_query_iter(ecs, q_waiter);
ecs_entity_t waiter = ecs_iter_first(&qit); // Get first matching entity
if (waiter) {
// An available waiter was found, assign a plate to it so that the
// next plate will no longer find it.
// The defer_suspend function temporarily suspends deferring
// operations, which ensures that our plate is assigned immediately.
// Even though this is a no_readonly system, defering is still
// enabled by default, as adding/removing components to the entities
// being iterated would intefere with the system iterator.
ecs_defer_suspend(ecs);
ecs_add_pair(ecs, waiter, Plate, plate);
ecs_defer_resume(ecs);
// Now that defering is resumed, we can safely also add the waiter
// to the plate. We can't do this while defering is suspended,
// because the plate is the entity we're currently iterating, and
// we don't want to move it to a different table while we're
// iterating it.
ecs_add_pair(ecs, plate, Waiter, waiter);
printf("Assigned %s to %s!\n",
ecs_get_name(ecs, waiter),
ecs_get_name(ecs, plate));
// Note that we use defer suspend/resume here instead of end/begin.
// This ensures that we don't merge the enqueued commands for the
// plates, which shouldn't be merged before we're done iterating.
} else {
// No available waiters, can't assign the plate
}
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_TAG_DEFINE(ecs, Waiter);
ECS_TAG_DEFINE(ecs, Plate);
// Create query to find all waiters without a plate
ecs_query_t *q_waiter = ecs_query(ecs, {
.filter.terms = {
{ .id = Waiter },
{ .id = ecs_pair(Plate, EcsWildcard), .oper = EcsNot }
}
});
// Find all plates without a Waiter
ecs_system(ecs, {
.entity = ecs_entity(ecs, {
.name = "AssignPlate",
.add = { ecs_dependson(EcsOnUpdate) }
}),
.query.filter.terms = {
{ .id = Plate },
{ .id = ecs_pair(Waiter, EcsWildcard), .oper = EcsNot }
},
.callback = AssignPlate,
.ctx = q_waiter,
.no_readonly = true // disable readonly mode for this system
});
// Create a few plates and waiters
ecs_entity_t waiter_1 = ecs_entity(ecs, {
.name = "waiter_1",
.add = { Waiter }
});
/* ecs_entity_t waiter_2 = */ ecs_entity(ecs, { // silence unused warning
.name = "waiter_2",
.add = { Waiter }
});
/* ecs_entity_t waiter_3 = */ ecs_entity(ecs, {
.name = "waiter_3",
.add = { Waiter }
});
/* ecs_entity_t plate_1 = */ ecs_entity(ecs, {
.name = "plate_1",
.add = { Plate }
});
ecs_entity_t plate_2 = ecs_entity(ecs, {
.name = "plate_2",
.add = { Plate, ecs_pair(Waiter, waiter_1) } // already assigned
});
/* ecs_entity_t plate_3 = */ ecs_entity(ecs, {
.name = "plate_3",
.add = { Plate }
});
// Also assign plate_2 to waiter_1
ecs_add_pair(ecs, waiter_1, Plate, plate_2);
// Run the system
ecs_progress(ecs, 0);
return ecs_fini(ecs);
// Output:
// Assigned waiter_3 to plate_1!
// Assigned waiter_2 to plate_3!
}

View File

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

View File

@@ -0,0 +1,12 @@
{
"id": "pipeline",
"type": "application",
"value": {
"author": "Jane Doe",
"description": "A simple hello world flecs application",
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,54 @@
#include <pipeline.h>
#include <stdio.h>
typedef struct {
double x, y;
} Position, Velocity;
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
const Velocity *v = ecs_field(it, const Velocity, 2);
for (int i = 0; i < it->count; i ++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
}
}
void PrintPosition(ecs_iter_t *it) {
const Position *p = ecs_field(it, const Position, 1);
for (int i = 0; i < it->count; i ++) {
printf("%s: {%f, %f}\n", ecs_get_name(it->world, it->entities[i]),
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);
ECS_COMPONENT(ecs, Velocity);
// Create system for moving an entity in the EcsOnUpdate phase
ECS_SYSTEM(ecs, Move, EcsOnUpdate, Position, [in] Velocity);
// Create system for printing a Position in the EcsPostUpdate phase
ECS_SYSTEM(ecs, PrintPosition, EcsPostUpdate, [in] Position);
// Create a few test entities for a Position, Velocity query
ecs_entity_t e1 = ecs_new_entity(ecs, "e1");
ecs_set(ecs, e1, Position, {10, 20});
ecs_set(ecs, e1, Velocity, {1, 2});
ecs_entity_t e2 = ecs_new_entity(ecs, "e2");
ecs_set(ecs, e2, Position, {10, 20});
ecs_set(ecs, e2, Velocity, {3, 4});
// Run the default pipeline. This will run all systems ordered by their
// phase. Systems within the same phase are ran in declaration order. This
// function is usually called in a loop.
ecs_progress(ecs, 0.0f);
return ecs_fini(ecs);
}

View File

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

View File

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

View File

@@ -0,0 +1,42 @@
#include <startup_system.h>
#include <stdio.h>
// Startup systems are systems registered with the EcsOnStart phase, and are
// only ran during the first frame. Just like with regular phases, custom phases
// can depend on the EcsOnStart phase (see custom_phases example). Phases that
// depend on EcsOnStart are also only ran during the first frame.
//
// Other than that, startup systems behave just like regular systems (they can
// match components, can introduce merge points), with as only exception that
// they are guaranteed to always run on the main thread.
void Startup(ecs_iter_t *it) {
printf("%s\n", ecs_get_name(it->world, it->system));
}
void Update(ecs_iter_t *it) {
printf("%s\n", ecs_get_name(it->world, it->system));
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// Startup system
ECS_SYSTEM(ecs, Startup, EcsOnStart, 0);
// Regular system
ECS_SYSTEM(ecs, Update, EcsOnUpdate, 0);
// First frame. This runs both the Startup and Update systems
ecs_progress(ecs, 0.0f);
// Second frame. This runs only the Update system
ecs_progress(ecs, 0.0f);
return ecs_fini(ecs);
// Output
// Startup
// Update
// Update
}

View File

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

View File

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

View File

@@ -0,0 +1,121 @@
#include <sync_point.h>
#include <stdio.h>
typedef struct {
double x, y;
} Position, Velocity;
// Forward declare components so they can be accessed from systems
ECS_COMPONENT_DECLARE(Position);
ECS_COMPONENT_DECLARE(Velocity);
void SetVelocity(ecs_iter_t *it) {
for (int i = 0; i < it->count; i ++) {
ecs_set(it->world, it->entities[i], Velocity, {1, 2});
}
}
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
const Velocity *v = ecs_field(it, const Velocity, 2);
for (int i = 0; i < it->count; i ++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
}
}
void PrintPosition(ecs_iter_t *it) {
const Position *p = ecs_field(it, const Position, 1);
for (int i = 0; i < it->count; i ++) {
printf("%s: {%f, %f}\n", ecs_get_name(it->world, it->entities[i]),
p[i].x, p[i].y);
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT_DEFINE(ecs, Position);
ECS_COMPONENT_DEFINE(ecs, Velocity);
// System that sets velocity using set<T> for entities with Position.
// While systems are progressing, operations like set<T> are deferred until
// it is safe to merge. By default this merge happens at the end of the
// frame, but we can annotate systems to give the scheduler more information
// about what it's doing, which allows it to insert sync points earlier.
//
// The parentheses after Velocity indicate that the component is not used
// to match entities, while [out] indicates that it is written. A subsequent
// system that accesses the component will cause the scheduler to insert a
// sync point.
//
// Note that sync points are never necessary/inserted for systems that write
// components provided by their signature, as these writes directly happen
// in the ECS storage and are never deferred.
//
// The [none] annotation for Position tells the scheduler that while we
// want to match entities with Position, we're not interested in reading or
// writing the component value.
ECS_SYSTEM(ecs, SetVelocity, EcsOnUpdate, [none] Position, [out] Velocity());
// This system reads Velocity, which causes the insertion of a sync point.
ECS_SYSTEM(ecs, Move, EcsOnUpdate, Position, [in] Velocity);
// Print resulting Position
ECS_SYSTEM(ecs, PrintPosition, EcsPostUpdate, [in] Position);
// Create a few test entities for a Position, Velocity query
ecs_entity_t e1 = ecs_new_entity(ecs, "e1");
ecs_set(ecs, e1, Position, {10, 20});
ecs_set(ecs, e1, Velocity, {1, 2});
ecs_entity_t e2 = ecs_new_entity(ecs, "e2");
ecs_set(ecs, e2, Position, {10, 20});
ecs_set(ecs, e2, Velocity, {3, 4});
// Run systems. Debug logging enables us to see the generated schedule
ecs_log_set_level(1);
ecs_progress(ecs, 0.0f);
ecs_log_set_level(-1); // Restore so we don't get world cleanup logs
// Output:
// info: pipeline rebuild:
// info: | schedule: threading: 0, staging: 1:
// info: | | system SetVelocity
// info: | | merge
// info: | schedule: threading: 0, staging: 1:
// info: | | system Move
// info: | | system PrintPosition
// info: | | merge
// e1: {11.000000, 22.000000}
// e2: {11.000000, 22.000000}
//
// The "merge" lines indicate sync points.
//
// Removing the '[out] Velocity()' annotation from the system will remove
// the first sync point from the schedule.
//
// To create the same system with ecs_system_init, do:
// ecs_system_init(ecs, &(ecs_system_desc_t){
// .query.filter.terms = {
// {
// .id = ecs_id(Position),
// .inout = EcsInOutNone
// },
// {
// .id = ecs_id(Velocity),
// .inout = EcsOut,
// .src.flags = EcsIsEntity
// }
// },
// .entity = {
// .name = "SetVelocity",
// .add = {ecs_dependson(EcsOnUpdate)}
// },
// .callback = SetVelocity
// });
return ecs_fini(ecs);
}

View File

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

View File

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

View File

@@ -0,0 +1,136 @@
#include <sync_point_delete.h>
#include <stdio.h>
typedef struct {
double x, y;
} Position, Velocity;
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
const Velocity *v = ecs_field(it, const Velocity, 2);
for (int i = 0; i < it->count; i ++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
}
}
void DeleteEntity(ecs_iter_t *it) {
const Position *p = ecs_field(it, Position, 1);
for (int i = 0; i < it->count; i ++) {
if (p[i].x >= 3) {
ecs_entity_t e = it->entities[i];
printf("Delete entity %s\n", ecs_get_name(it->world, e));
ecs_delete(it->world, e);
}
}
}
void PrintPosition(ecs_iter_t *it) {
const Position *p = ecs_field(it, const Position, 1);
for (int i = 0; i < it->count; i ++) {
printf("%s: {%f, %f}\n", ecs_get_name(it->world, it->entities[i]),
p[i].x, p[i].y);
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// This example shows how to annotate systems that delete entities, in a way
// that allows the scheduler to correctly insert sync points. See the
// sync_point example for more details on sync points.
//
// While annotating a system for a delete operation follows the same
// design as other operations, one key difference is that a system often
// does not know which components a to be deleted entity has. This makes it
// impossible to annotate the system in advance for specific components.
//
// To ensure the scheduler is still able to insert the correct sync points,
// a system can use a wildcard to indicate that any component could be
// modified by the system, which forces the scheduler to insert a sync.
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Velocity);
// Basic move system.
ECS_SYSTEM(ecs, Move, EcsOnUpdate, Position, [in] Velocity);
// Delete entities when p.x >= 3. Add wildcard annotation to indicate any
// component could be written by the system. Position itself is added as
// const, since inside the system we're only reading it.
//
// Note how the system uses the same annotations as the sync_point example.
ECS_SYSTEM(ecs, DeleteEntity, EcsOnUpdate, [in] Position, [out] *());
// Print resulting Position
ECS_SYSTEM(ecs, PrintPosition, EcsPostUpdate, [in] Position);
// Create a few test entities for a Position, Velocity query
ecs_entity_t e1 = ecs_new_entity(ecs, "e1");
ecs_set(ecs, e1, Position, {0, 0});
ecs_set(ecs, e1, Velocity, {1, 2});
ecs_entity_t e2 = ecs_new_entity(ecs, "e2");
ecs_set(ecs, e2, Position, {1, 2});
ecs_set(ecs, e2, Velocity, {1, 2});
// Run systems. Debug logging enables us to see the generated schedule
ecs_log_set_level(1);
while (ecs_progress(ecs, 0.0f)) {
if (!ecs_count(ecs, Position)) {
break; // No more entities left with Position
}
}
ecs_log_set_level(-1); // Restore so we don't get world cleanup logs
// Output:
// info: pipeline rebuild:
// info: | schedule: threading: 0, staging: 1:
// info: | | system Move
// info: | | system DeleteEntity
// info: | | merge
// info: | schedule: threading: 0, staging: 1:
// info: | | system PrintPosition
// info: | | merge
// e1: {1, 2}
// e2: {2, 4}
// Delete entity e2
// e1: {2, 4}
// Delete entity e1
// info: system Move deactivated
// info: system DeleteEntity deactivated
// info: system PrintPosition deactivated
//
// Removing the wildcard annotation from the DeleteEntity system will
// remove the first sync point.
//
// Note how after both entities are deleted, all three systems are
// deactivated. This happens when there are no matching entities for a
// system. A deactivated system is not ran by the scheduler, which reduces
// overhead.
//
// To create the same system with ecs_system_init, do:
// ecs_system_init(ecs, &(ecs_system_desc_t){
// .query.filter.terms = {
// {
// .id = ecs_id(Position),
// .inout = EcsIn
// },
// {
// .id = EcsWildcard,
// .inout = EcsOut,
// .src.flags = EcsIsEntity
// }
// },
// .entity = {
// .name = "DeleteEntity",
// .add = {ecs_dependson(EcsOnUpdate)}
// },
// .callback = DeleteEntity
// });
return ecs_fini(ecs);
}

View File

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

View File

@@ -0,0 +1,15 @@
{
"id": "system_ctx",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false
},
"lang.c": {
"${os linux}": {
"lib": ["m"]
}
}
}

View File

@@ -0,0 +1,106 @@
#include <system_ctx.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Applications can pass context data to a system. A common use case where this
// comes in handy is when a system needs to iterate more than one query. The
// following example shows how to pass a custom query into a system for a simple
// collision detection example.
typedef struct {
double x, y;
} Position;
typedef struct {
double value;
} Radius;
double sqr(double value) {
return value * value;
}
double distance_sqr(const Position *p1, const Position *p2) {
return sqr(p2->x - p1->x) + sqr(p2->y - p1->y);
}
void Collide(ecs_iter_t *it) {
ecs_query_t *q_collide = it->ctx; // Get query from system context
const Position *p1 = ecs_field(it, Position, 1);
const Radius *r1 = ecs_field(it, Radius, 2);
for (int i = 0; i < it->count; i ++) {
ecs_entity_t e1 = it->entities[i];
// For each matching entity, iterate the query
ecs_iter_t qit = ecs_query_iter(it->world, q_collide);
while (ecs_query_next(&qit)) {
const Position *p2 = ecs_field(&qit, Position, 1);
const Radius *r2 = ecs_field(&qit, Radius, 2);
for (int j = 0; j < qit.count; j ++) {
ecs_entity_t e2 = qit.entities[j];
if (e1 == e2) {
// don't collide with self
continue;
}
if (e2 > e1) {
// Simple trick to prevent collisions from being detected
// twice with the entities reversed.
continue;
}
// Check for collision
double d_sqr = distance_sqr(&p1[i], &p2[j]);
double r_sqr = sqr(r1[i].value + r2[j].value);
if (r_sqr > d_sqr) {
printf("%u and %u collided!\n", (uint32_t)e1, (uint32_t)e2);
}
}
}
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Radius);
// Create a query for Position that we can use inside the collide system to
// check each entity with each other entity.
ecs_query_t *q_position = ecs_query(ecs, {
.filter.terms = {
{ ecs_id(Position), .inout = EcsIn },
{ ecs_id(Radius), .inout = EcsIn }
}
});
// Create collide system that passes query as context
ecs_entity_t collide = ecs_system(ecs, {
.query.filter.terms = {
{ .id = ecs_id(Position), .inout = EcsIn },
{ ecs_id(Radius), .inout = EcsIn }
},
.callback = Collide,
.ctx = q_position
});
// Create a few test entities
for (int i = 0; i < 10; i ++) {
ecs_entity_t e = ecs_new_id(ecs);
ecs_set(ecs, e, Position, { .x = rand() % 100, .y = rand() % 100 });
ecs_set(ecs, e, Radius, { rand() % 10 + 1 });
}
// Run the system
ecs_run(ecs, collide, 0.0f, NULL);
return ecs_fini(ecs);
// Output
// 508 and 505 collided!
// 510 and 508 collided!
// 513 and 506 collided!
// 514 and 512 collided!
}

View File

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

View File

@@ -0,0 +1,12 @@
{
"id": "target_fps",
"type": "application",
"value": {
"author": "Jane Doe",
"description": "A simple hello world flecs application",
"use": [
"flecs"
],
"public": false
}
}

View File

@@ -0,0 +1,37 @@
#include <target_fps.h>
#include <stdio.h>
void PrintDeltaTime(ecs_iter_t *it) {
// Print delta_time. The same value is passed to all systems.
printf("delta_time: %f\n", (double)it->delta_time);
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// Create system that prints delta_time. This system doesn't query for any
// components which means it won't match any entities, but will still be ran
// once for each call to ecs_progress.
ECS_SYSTEM(ecs, PrintDeltaTime, EcsOnUpdate, 0);
// Set target FPS to 1 frame per second
ecs_set_target_fps(ecs, 1);
// Run 5 frames
for (int i = 0; i < 5; i ++) {
// To make sure the frame doesn't run faster than the specified target
// FPS ecs_progress will insert a sleep if the measured delta_time is
// smaller than 1 / target_fps.
//
// By default ecs_progress uses the sleep function provided by the OS
// which is not always very accurate. If more accuracy is required the
// sleep function of the OS API can be overridden with a custom one.
//
// If a value other than 0 is provided to the delta_time argument of
// ecs_progress, this value will be used to calculate the length of
// the sleep to insert.
ecs_progress(ecs, 0);
}
return ecs_fini(ecs);
}

View File

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

View File

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

View File

@@ -0,0 +1,51 @@
#include <time_interval.h>
#include <stdio.h>
// This example shows how to run a system at a specified time interval.
void Tick(ecs_iter_t *it) {
printf("%s\n", ecs_get_name(it->world, it->system));
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// Create two systems that are running at different time intervals
ecs_system(ecs, {
.entity = ecs_entity(ecs, {
.name = "Tick",
.add = { ecs_dependson(EcsOnUpdate) } // run in OnUpdate phase
}),
.callback = Tick,
.interval = 1.0 // time in seconds
});
ecs_system(ecs, {
.entity = ecs_entity(ecs, {
.name = "FastTick",
.add = { ecs_dependson(EcsOnUpdate) }
}),
.callback = Tick,
.interval = 0.5
});
// Run the main loop at 60 FPS
ecs_set_target_fps(ecs, 60);
while (ecs_progress(ecs, 0)) { }
return ecs_fini(ecs);
// Output
// FastTick
// Tick
// FastTick
// FastTick
// Tick
// FastTick
// FastTick
// Tick
// FastTick
// FastTick
// ...
}