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,9 @@
cc_library(
name = "os-api",
visibility = ["//:__subpackages__"],
deps = ["//:flecs", "@bake//:util"],
srcs = glob(["os_api/flecs-os_api-bake/src/**/*.c", "os_api/flecs-os_api-bake/src/**/*.h"]),
hdrs = glob(["os_api/flecs-os_api-bake/include/**/*.h"]),
includes = ["os_api/flecs-os_api-bake/include"],
)

View File

@@ -0,0 +1,58 @@
# Examples
This folder contains code examples for the C and C++ APIs.
## Build with cmake
To build the examples with cmake, run the following commands from the root of the repository:
```
cd examples/c
mkdir cmake_build
cd cmake_build
cmake ..
cmake --build .
```
To build the C++ examples, run the same commands from examples/cpp:
```
cd examples/cpp
mkdir cmake_build
cd cmake_build
cmake ..
cmake --build .
```
This will produce two binaries for each example: one that links with a shared Flecs library, and one that is linked with static Flecs library. The binaries linked with the static library have the postfix _static.
To run a single example, execute it from the `cmake_build` directory:
```
./entities_basics_static
```
## Build with bake
To build the examples with bake, run bake once in the root of the repository, so it knows where to find Flecs:
```
bake
```
To run a single example, use the following command:
```
bake run examples/c/entities/basics
```
To run a single example with optimizations enabled, use the following command:
```
bake run examples/c/entities/basics --cfg release
```
To build all examples, run the following command:
```
bake examples
```
See the [bake repository](https://github.com/SanderMertens/bake) for instructions on how to install bake.

View File

@@ -0,0 +1,7 @@
package(default_visibility = ["//visibility:public"])
cc_binary(
name = "example",
srcs = ["main.cpp"],
deps = ["@flecs//:flecs"]
)

View File

@@ -0,0 +1,22 @@
You can include the flecs repository in your `WORKSPACE` with:
```bazel
git_repository(
name = "flecs",
remote = "https://github.com/SanderMertens/flecs",
commit = "f150d96ba9ea8be2b24dbf2217368c231cb17720", # v2.3.2+merge329
shallow_since = "1615075784 -0800",
)
```
And then add it to your `BUILD` with:
```bazel
deps = ["@flecs//:flecs"]
```
This directory contains a complete example of this usage. To try it you can run the following from your terminal:
```
bazel run example
```

View File

@@ -0,0 +1,8 @@
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "flecs",
remote = "https://github.com/SanderMertens/flecs",
commit = "f150d96ba9ea8be2b24dbf2217368c231cb17720", # v2.3.2+merge329
shallow_since = "1615075784 -0800",
)

View File

@@ -0,0 +1,43 @@
#include "flecs.h"
#include <iostream>
/* Component types */
struct Position {
double x, y;
};
struct Velocity {
double x, y;
};
int main(int argc, char *argv[]) {
flecs::world world(argc, argv);
flecs::component<Position>(world, "Position");
flecs::component<Velocity>(world, "Velocity");
flecs::system<Position, const Velocity>(world)
.iter([](const flecs::iter& it,
Position *p,
const Velocity *v)
{
for (auto row : it) {
p[row].x += v[row].x;
p[row].y += v[row].y;
std::cout << "Moved " << it.entity(row).name() << " to {" <<
p[row].x << ", " << p[row].y << "}" << std::endl;
}
});
flecs::entity(world, "MyEntity")
.set<Position>({0, 0})
.set<Velocity>({1, 1});
world.set_target_fps(1);
std::cout << "Application move_system is running, press CTRL-C to exit..." << std::endl;
/* Run systems */
while (world.progress()) { }
}

View File

@@ -0,0 +1,13 @@
cmake_minimum_required (VERSION 2.8.7)
project (cmake_example)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory (flecs)
add_executable(cmake_example main.cpp)
add_executable(cmake_example_static main.cpp)
target_link_libraries(cmake_example flecs)
target_link_libraries(cmake_example_static flecs_static)

View File

@@ -0,0 +1 @@
For this to work, flecs needs to be a subdirectory of the example.

View File

@@ -0,0 +1,40 @@
#include "flecs.h"
#include <iostream>
/* Component types */
struct Position {
double x, y;
};
struct Velocity {
double x, y;
};
int main(int argc, char *argv[]) {
flecs::world ecs(argc, argv);
ecs.system<Position, const Velocity>()
.iter([](const flecs::iter& it,
Position *p,
const Velocity *v)
{
for (auto row : it) {
p[row].x += v[row].x;
p[row].y += v[row].y;
std::cout << "Moved " << it.entity(row).name() << " to {" <<
p[row].x << ", " << p[row].y << "}" << std::endl;
}
});
ecs.entity("MyEntity")
.set<Position>({0, 0})
.set<Velocity>({1, 1});
ecs.set_target_fps(1);
std::cout << "Application move_system is running, press CTRL-C to exit..." << std::endl;
/* Run systems */
while (ecs.progress()) { }
}

View File

@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.5)
cmake_policy(SET CMP0063 NEW)
project(flecs_c_examples LANGUAGES C)
set(FLECS_DIR ../..)
set(CUR_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(${FLECS_DIR} ${CMAKE_CURRENT_BINARY_DIR}/flecs)
include(../../cmake/target_default_compile_options.cmake)
include(../../cmake/target_default_compile_warnings.cmake)
include(../../cmake/target_default_compile_functions.cmake)
list_targets(EXAMPLES)
foreach (EXAMPLE ${EXAMPLES})
create_target_c(${EXAMPLE} "")
create_target_c(${EXAMPLE} "static")
endforeach ()

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,61 @@
#include <basics.h>
#include <stdio.h>
typedef struct {
double x, y;
} Position;
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT(ecs, Position);
ECS_TAG(ecs, Walking);
// Create an entity with name Bob
ecs_entity_t bob = ecs_set_name(ecs, 0, "Bob");
// The set operation finds or creates a component, and sets it.
ecs_set(ecs, bob, Position, {10, 20});
// The add operation adds a component without setting a value. This is
// useful for tags, or when adding a component with its default value.
ecs_add(ecs, bob, Walking);
// Get the value for the Position component
const Position *ptr = ecs_get(ecs, bob, Position);
printf("{%f, %f}\n", ptr->x, ptr->y);
// Overwrite the value of the Position component
ecs_set(ecs, bob, Position, {20, 30});
// Create another named entity
ecs_entity_t alice = ecs_set_name(ecs, 0, "Alice");
ecs_set(ecs, alice, Position, {10, 20});
ecs_add(ecs, alice, Walking);
// Print all the components the entity has. This will output:
// Position, Walking, (Identifier,Name)
char *str = ecs_type_str(ecs, ecs_get_type(ecs, alice));
printf("[%s]\n", str);
ecs_os_free(str);
// Remove tag
ecs_remove(ecs, alice, Walking);
// Iterate all entities with Position
ecs_iter_t it = ecs_term_iter(ecs, &(ecs_term_t){ .id = ecs_id(Position) });
while (ecs_term_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
for (int i = 0; i < it.count; i ++) {
printf("%s: {%f, %f}\n", ecs_get_name(ecs, it.entities[i]),
p[i].x, p[i].y);
}
}
return ecs_fini(ecs);
// Output
// {10.000000, 20.000000}
// [Position, Walking, (Identifier,Name)]
// Alice: {10.000000, 20.000000}
// Bob: {20.000000, 30.000000}
}

View File

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

View File

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

View File

@@ -0,0 +1,81 @@
#include <fwd_declare_component.h>
#include <stdio.h>
// This example shows how to use utility macro's to forward declare and use a
// component. The ECS_COMPONENT automatically declares a variable that holds a
// component id, but this variable is scoped to the function only. When trying
// to access the component from another function, this typically causes errors
// that look like this:
// "FLECS_IDComponentNameID" is undefined
//
// Forward declaring a component will make the component id available from other
// functions, which fixes this error.
// The component type
typedef struct {
double x, y;
} Position;
// The forward declaration of the component id variable. This variable will have
// the name FLECS_IDPositionID, to ensure its name won't conflict with the type.
ECS_COMPONENT_DECLARE(Position);
// When you want forward declare a component from a header, make sure to use the
// extern keyword to prevent multiple definitions of the same variable:
//
// In the header:
// extern ECS_COMPONENT_DECLARE(Position);
//
// In *one* of the source files:
// ECS_COMPONENT_DECLARE(Position);
// To forward declare entities created with ECS_ENTITY, ECS_TAG or ECS_PREFAB,
// use ECS_DECLARE.
ECS_DECLARE(Wizard);
// Regular entity handles can also be forward declared
ecs_entity_t Platoon_1;
ecs_entity_t create_npc(ecs_world_t *world) {
ecs_entity_t result = ecs_new(world, 0);
// Without the forward declaration, this would have thrown a compiler error
ecs_set(world, result, Position, {10, 20});
ecs_add(world, result, Wizard);
// An entity that's not declared using the utility macro's can be added with
// ecs_add_id. This has the same effect, with as only difference that this
// function doesn't look for a variable named FLECS_EPlatoon_1
ecs_add_id(world, result, Platoon_1);
return result;
}
int main(int argc, char *argv[]) {
ecs_world_t *world = ecs_init_w_args(argc, argv);
// Register the component and assign the forward declared variable.
ECS_COMPONENT_DEFINE(world, Position);
// Same for the tag
ECS_TAG_DEFINE(world, Wizard);
// A forward declared entity can be assigned as any variable
Platoon_1 = ecs_new_id(world);
// Create new entity with Position
ecs_entity_t e = create_npc(world);
const Position *p = ecs_get(world, e, Position);
printf("{%f, %f}\n", p->x, p->y);
// To access the component id variable directly, use the ecs_id macro:
printf("Id of Position is %u\n",
(uint32_t)ecs_id(Position)); // the cast avoids formatting warnings
return ecs_fini(world);
// Output:
// {10.000000, 20.000000}
// Id of Position is 497
}

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

View File

@@ -0,0 +1,78 @@
#include <hierarchy.h>
#include <stdio.h>
typedef struct {
double x, y;
} Position;
// Forward declare component so we can use it from functions other than main
ECS_COMPONENT_DECLARE(Position);
void iterate_tree(ecs_world_t *ecs, ecs_entity_t e, Position p_parent) {
// Print hierarchical name of entity & the entity type
char *path_str = ecs_get_fullpath(ecs, e);
char *type_str = ecs_type_str(ecs, ecs_get_type(ecs, e));
printf("%s [%s]\n", path_str, type_str);
ecs_os_free(type_str);
ecs_os_free(path_str);
// Get entity position
const Position *ptr = ecs_get(ecs, e, Position);
// Calculate actual position
Position p_actual = {ptr->x + p_parent.x, ptr->y + p_parent.y};
printf("{%f, %f}\n\n", p_actual.x, p_actual.y);
// Iterate children recursively
ecs_iter_t it = ecs_children(ecs, e);
while (ecs_children_next(&it)) {
for (int i = 0; i < it.count; i ++) {
iterate_tree(ecs, it.entities[i], p_actual);
}
}
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
ECS_COMPONENT_DEFINE(ecs, Position);
ECS_TAG(ecs, Star);
ECS_TAG(ecs, Planet);
ECS_TAG(ecs, Moon);
// Create a simple hierarchy.
// Hierarchies use ECS relationships and the builtin flecs::ChildOf relationship to
// create entities as children of other entities.
ecs_entity_t sun = ecs_new_entity(ecs, "Sun");
ecs_add(ecs, sun, Star);
ecs_set(ecs, sun, Position, {1, 1});
ecs_entity_t mercury = ecs_new_entity(ecs, "Mercury");
ecs_add_pair(ecs, mercury, EcsChildOf, sun);
ecs_add(ecs, mercury, Planet);
ecs_set(ecs, mercury, Position, {1, 1});
ecs_entity_t venus = ecs_new_entity(ecs, "Venus");
ecs_add_pair(ecs, venus, EcsChildOf, sun);
ecs_add(ecs, venus, Planet);
ecs_set(ecs, venus, Position, {2, 2});
ecs_entity_t earth = ecs_new_entity(ecs, "Earth");
ecs_add_pair(ecs, earth, EcsChildOf, sun);
ecs_add(ecs, earth, Planet);
ecs_set(ecs, earth, Position, {3, 3});
ecs_entity_t moon = ecs_new_entity(ecs, "Moon");
ecs_add_pair(ecs, moon, EcsChildOf, earth);
ecs_add(ecs, moon, Moon);
ecs_set(ecs, moon, Position, {0.1, 0.1});
// Is the Moon a child of Earth?
printf("Child of Earth? %d\n", ecs_has_pair(ecs, moon, EcsChildOf, earth));
// Do a depth-first walk of the tree
iterate_tree(ecs, sun, (Position){0, 0});
return ecs_fini(ecs);
}

View File

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

View File

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

View File

@@ -0,0 +1,119 @@
#include <hooks.h>
#include <stdio.h>
// Component hooks are callbacks that can be registered for a type that are
// invoked during different parts of the component lifecycle.
typedef struct {
char *value; // Pointer to external memory
} String;
// Resource management hooks. The convenience macros hide details of
// the callback signature, while allowing hooks to be called on multiple
// entities.
// The constructor should initialize the component value.
ECS_CTOR(String, ptr, {
ecs_trace("Ctor");
ptr->value = NULL;
})
// The destructor should free resources.
ECS_DTOR(String, ptr, {
ecs_trace("Dtor");
ecs_os_free(ptr->value);
})
// The move hook should move resources from one location to another.
ECS_MOVE(String, dst, src, {
ecs_trace("Move");
ecs_os_free(dst->value);
dst->value = src->value;
src->value = NULL; // This makes sure the value doesn't get deleted twice,
// as the destructor is still invoked after a move.
})
// The copy hook should copy resources from one location to another.
ECS_COPY(String, dst, src, {
ecs_trace("Copy");
ecs_os_free(dst->value);
dst->value = ecs_os_strdup(src->value);
})
// This callback is used for the add, remove and set hooks. Note that the
// signature is the same as systems, triggers, observers.
void hook_callback(ecs_iter_t *it) {
ecs_world_t *world = it->world;
ecs_entity_t event = it->event;
for (int i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_trace("%s: %s",
ecs_get_name(world, event), ecs_get_name(world, e));
}
}
int main(int argc, char *argv[]) {
ecs_world_t *world = ecs_init_w_args(argc, argv);
ECS_COMPONENT(world, String);
ECS_TAG(world, Tag);
ecs_set_hooks(world, String, {
/* Resource management hooks. These hooks should primarily be used for
* managing memory used by the component. */
.ctor = ecs_ctor(String),
.move = ecs_move(String),
.copy = ecs_copy(String),
.dtor = ecs_dtor(String),
/* Lifecycle hooks. These hooks should be used for application logic. */
.on_add = hook_callback,
.on_remove = hook_callback,
.on_set = hook_callback
});
ecs_log_set_level(0);
ecs_entity_t e = ecs_new_entity(world, "Entity");
ecs_trace("ecs_add(world, e, String)");
ecs_log_push();
ecs_add(world, e, String);
ecs_log_pop();
ecs_trace("ecs_set(world, e, String, {\"Hello World\"})");
ecs_log_push();
ecs_set(world, e, String, {ECS_CONST_CAST(char*, "Hello World")});
ecs_log_pop();
// This operation changes the entity's archetype, which invokes a move
ecs_trace("ecs_add(world, e, Tag)");
ecs_log_push();
ecs_add(world, e, Tag);
ecs_log_pop();
ecs_trace("ecs_delete(world, e)");
ecs_log_push();
ecs_delete(world, e);
ecs_log_pop();
ecs_log_set_level(-1);
return ecs_fini(world);
// Output:
// info: ecs_add(world, e, String)
// info: | Ctor
// info: | OnAdd: Entity
// info: ecs_set(world, e, String, {"Hello World"})
// info: | Copy
// info: | OnSet: Entity
// info: ecs_add(world, e, Tag)
// info: | Ctor
// info: | Move
// info: | Dtor
// info: ecs_delete(world, e)
// info: | OnRemove: Entity
// info: | Dtor
}

View File

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

View File

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

View File

@@ -0,0 +1,114 @@
#include <iterate_components.h>
#include <stdio.h>
typedef struct {
double x, y;
} Position, Velocity;
void iterate_components(ecs_world_t *ecs, ecs_entity_t e) {
// First get the entity's type, which is a vector of (component) ids.
const ecs_type_t *type = ecs_get_type(ecs, e);
// 1. The easiest way to print the components is to use ecs_type_str
char *type_str = ecs_type_str(ecs, type);
printf("ecs_type_str: %s\n\n", type_str);
ecs_os_free(type_str);
// 2. To print individual ids, iterate the type array with ecs_id_str
const ecs_id_t *type_ids = type->array;
int32_t i, count = type->count;
for (i = 0; i < count; i ++) {
ecs_id_t id = type_ids[i];
char *id_str = ecs_id_str(ecs, id);
printf("%d: %s\n", i, id_str);
ecs_os_free(id_str);
}
printf("\n");
// 3. we can also inspect and print the ids in our own way. This is a
// bit more complicated as we need to handle the edge cases of what can be
// encoded in an id, but provides the most flexibility.
for (i = 0; i < count; i ++) {
ecs_id_t id = type_ids[i];
printf("%d: ", i);
if (ECS_HAS_ID_FLAG(id, PAIR)) { // See relationships
ecs_entity_t rel = ecs_pair_first(ecs, id);
ecs_entity_t tgt = ecs_pair_second(ecs, id);
printf("rel: %s, tgt: %s",
ecs_get_name(ecs, rel), ecs_get_name(ecs, tgt));
} else {
ecs_entity_t comp = id & ECS_COMPONENT_MASK;
printf("entity: %s", ecs_get_name(ecs, comp));
}
printf("\n");
}
printf("\n\n");
}
int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
// Ordinary components
ECS_COMPONENT(ecs, Position);
ECS_COMPONENT(ecs, Velocity);
// A tag
ECS_TAG(ecs, Human);
// Two tags used to create a pair
ECS_TAG(ecs, Eats);
ECS_TAG(ecs, Apples);
// Create an entity which all of the above
ecs_entity_t Bob = ecs_new_id(ecs);
ecs_set(ecs, Bob, Position, {10, 20});
ecs_set(ecs, Bob, Velocity, {1, 1});
ecs_add(ecs, Bob, Human);
ecs_add_pair(ecs, Bob, Eats, Apples);
// Iterate & components of Bob
printf("Bob's components:\n");
iterate_components(ecs, Bob);
// We can use the same function to iterate the components of a component
printf("Position's components:\n");
iterate_components(ecs, ecs_id(Position));
return ecs_fini(ecs);
}
// Output:
// Bob's components:
// ecs_type_str: Position,Velocity,Human,(Eats,Apples)
// 0: Position
// 1: Velocity
// 2: Human
// 3: (Eats,Apples)
// 0: entity: Position
// 1: entity: Velocity
// 2: entity: Human
// 3: rel: Eats, tgt: Eats
// Position's components:
// ecs_type_str: EcsComponent,(Identifier,Name),(Identifier,Symbol),(OnDelete,Panic)
// 0: Component
// 1: (Identifier,Name)
// 2: (Identifier,Symbol)
// 3: (OnDelete,Panic)
// 0: entity: Component
// 1: rel: Identifier, tgt: Identifier
// 2: rel: Identifier, tgt: Identifier
// 3: rel: OnDelete, tgt: OnDelete

View File

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

View File

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

View File

@@ -0,0 +1,47 @@
#include <explorer.h>
typedef struct {
double value;
} Mass;
int main(int argc, char *argv[]) {
// Passing in the command line arguments will allow the explorer to display
// the application name.
ecs_world_t *world = ecs_init_w_args(argc, argv);
ECS_IMPORT(world, FlecsUnits);
ECS_IMPORT(world, FlecsMonitor); // Collect statistics periodically
// Mass component
ECS_COMPONENT(world, Mass);
ecs_struct(world, {
.entity = ecs_id(Mass),
.members = {
{ .name = "value", .type = ecs_id(ecs_f64_t), .unit = EcsKiloGrams}
}
});
// Simple hierarchy
ecs_entity_t Sun = ecs_new_entity(world, "Sun");
ecs_set(world, Sun, Mass, {1.988500e31});
ecs_set_scope(world, Sun);
ecs_entity_t Earth = ecs_new_entity(world, "Earth");
ecs_set(world, Earth, Mass, {5.9722e24});
ecs_set_scope(world, Earth);
ecs_entity_t Moon = ecs_new_entity(world, "Moon");
ecs_set(world, Moon, Mass, {7.34767309e22});
// Restore default hierarchy scope to root
ecs_set_scope(world, 0);
// Run application with REST interface. When the application is running,
// navigate to https://flecs.dev/explorer to inspect it!
//
// See docs/RestApi.md#explorer for more information.
return ecs_app_run(world, &(ecs_app_desc_t){
.enable_rest = true
});
}

View File

@@ -0,0 +1,5 @@
.bake_cache
.DS_Store
.vscode
gcov
bin

View File

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

View File

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

View File

@@ -0,0 +1,64 @@
#include <hello_world.h>
#include <stdio.h>
typedef struct {
double x;
double y;
} Position, Velocity;
/* Move system implementation. System callbacks may be called multiple times,
* as entities are grouped in tables by which components they have, and each
* table has its own set of component arrays. */
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
Velocity *v = ecs_field(it, Velocity, 2);
/* Print the set of components for the iterated over entities */
char *type_str = ecs_table_str(it->world, it->table);
printf("Move entities with [%s]\n", type_str);
ecs_os_free(type_str);
/* Iterate entities for the current table */
for (int i = 0; i < it->count; i ++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
}
}
int main(int argc, char *argv[]) {
/* Create the world */
ecs_world_t *world = ecs_init_w_args(argc, argv);
/* Register components */
ECS_COMPONENT(world, Position);
ECS_COMPONENT(world, Velocity);
/* Register system */
ECS_SYSTEM(world, Move, EcsOnUpdate, Position, Velocity);
/* Register tags (components without a size) */
ECS_TAG(world, Eats);
ECS_TAG(world, Apples);
ECS_TAG(world, Pears);
/* Create an entity with name Bob, add Position and food preference */
ecs_entity_t Bob = ecs_new_entity(world, "Bob");
ecs_set(world, Bob, Position, {0, 0});
ecs_set(world, Bob, Velocity, {1, 2});
ecs_add_pair(world, Bob, Eats, Apples);
/* Run systems twice. Usually this function is called once per frame */
ecs_progress(world, 0);
ecs_progress(world, 0);
/* See if Bob has moved (he has) */
const Position *p = ecs_get(world, Bob, Position);
printf("Bob's position is {%f, %f}\n", p->x, p->y);
return ecs_fini(world);
// Output
// Move entities with [Position, Velocity, (Identifier,Name), (Eats,Apples)]
// Move entities with [Position, Velocity, (Identifier,Name), (Eats,Apples)]
// Bob's position is {2.000000, 4.000000}
}

View File

@@ -0,0 +1,27 @@
#ifndef SIMPLE_MODULE_H
#define SIMPLE_MODULE_H
/* This generated file contains includes for project dependencies */
#include "simple_module/bake_config.h"
#ifdef __cplusplus
extern "C" {
#endif
// Component types
typedef struct {
double x, y;
} Position, Velocity;
// Global variables that hold component ids
extern ECS_COMPONENT_DECLARE(Position);
extern ECS_COMPONENT_DECLARE(Velocity);
// Module import function
void SimpleModuleImport(ecs_world_t *world);
#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 SIMPLE_MODULE_BAKE_CONFIG_H
#define SIMPLE_MODULE_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

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

View File

@@ -0,0 +1,38 @@
#include <simple_module.h>
#include <stdio.h>
void PrintPosition(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
for (int i = 0; i < it->count; i ++) {
printf("p = {%f, %f} (system)\n", p[i].x, p[i].y);
}
}
int main(int argc, char *argv[]) {
ecs_world_t *world = ecs_init_w_args(argc, argv);
// Import module which calls the SimpleModuleImport function
ECS_IMPORT(world, SimpleModule);
// Create system that uses component from module. Note how the component
// identifier is prefixed with the module.
ECS_SYSTEM(world, PrintPosition, EcsOnUpdate, simple.module.Position);
// Create entity with components imported from module
ecs_entity_t e = ecs_set(world, 0, Position, {10, 20});
ecs_set(world, e, Velocity, {1, 2});
// Call progress which runs imported Move system
ecs_progress(world, 0);
// Use component from module in operation
const Position *p = ecs_get(world, e, Position);
printf("p = {%f, %f} (get)\n", p->x, p->y);
return ecs_fini(world);
// Output:
// p = {11.000000, 22.000000} (system)
// p = {11.000000, 22.000000} (get)
}

View File

@@ -0,0 +1,28 @@
#include <simple_module.h>
ECS_COMPONENT_DECLARE(Position);
ECS_COMPONENT_DECLARE(Velocity);
void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
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;
}
}
void SimpleModuleImport(ecs_world_t *world) {
// Create the module entity. The PascalCase module name is translated to a
// lower case path for the entity name, like "simple.module".
ECS_MODULE(world, SimpleModule);
// All contents of the module are created inside the module's namespace, so
// the Position component will be created as simple.module.Position
ECS_COMPONENT_DEFINE(world, Position);
ECS_COMPONENT_DEFINE(world, Velocity);
ECS_SYSTEM(world, Move, EcsOnUpdate, Position, Velocity);
}

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}
}

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);
}

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,85 @@
#include <basics.h>
#include <stdio.h>
typedef struct {
double x;
double y;
} Position, Velocity;
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 query for Position, Velocity. Queries are the fastest way to
// iterate entities as they cache results.
ecs_query_t *q = ecs_query(ecs, {
.filter.terms = {
{ .id = ecs_id(Position) },
{ .id = ecs_id(Velocity), .inout = EcsIn}
}
});
// 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});
// Iterate entities matching the query
ecs_iter_t it = ecs_query_iter(ecs, q);
// Outer loop, iterates archetypes
while (ecs_query_next(&it)) {
Position *p = ecs_field(&it, Position, 1);
const Velocity *v = ecs_field(&it, Velocity, 2);
// Inner loop, iterates entities in archetype
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(ecs, it.entities[i]),
p[i].x, p[i].y);
}
}
// Filters are uncached queries. They are a bit slower to iterate but faster
// to create & have lower overhead as they don't have to maintain a cache.
ecs_filter_t *f = ecs_filter(ecs, {
.terms = {
{ .id = ecs_id(Position) },
{ .id = ecs_id(Velocity), .inout = EcsIn}
}
});
// Filter iteration looks the same as query iteration
it = ecs_filter_iter(ecs, f);
while (ecs_filter_next(&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(ecs, it.entities[i]),
p[i].x, p[i].y);
}
}
// Cleanup filter. Filters can allocate memory if the number of terms
// exceeds their internal buffer, or when terms have names. In this case the
// filter didn't allocate, so while fini isn't strictly necessary here, it's
// still good practice to add it.
ecs_filter_fini(f);
return ecs_fini(ecs);
}

Some files were not shown because too many files have changed in this diff Show More