Properly link flecs library
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
11
engine/libs/flecs/examples/c/entities/basics/project.json
Normal file
11
engine/libs/flecs/examples/c/entities/basics/project.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "basics",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
61
engine/libs/flecs/examples/c/entities/basics/src/main.c
Normal file
61
engine/libs/flecs/examples/c/entities/basics/src/main.c
Normal 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}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "fwd_declare_component",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
11
engine/libs/flecs/examples/c/entities/hierarchy/project.json
Normal file
11
engine/libs/flecs/examples/c/entities/hierarchy/project.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "hierarchy",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
78
engine/libs/flecs/examples/c/entities/hierarchy/src/main.c
Normal file
78
engine/libs/flecs/examples/c/entities/hierarchy/src/main.c
Normal 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);
|
||||
}
|
||||
16
engine/libs/flecs/examples/c/entities/hooks/include/hooks.h
Normal file
16
engine/libs/flecs/examples/c/entities/hooks/include/hooks.h
Normal 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
|
||||
|
||||
@@ -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
|
||||
|
||||
9
engine/libs/flecs/examples/c/entities/hooks/project.json
Normal file
9
engine/libs/flecs/examples/c/entities/hooks/project.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"id": "hooks",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
119
engine/libs/flecs/examples/c/entities/hooks/src/main.c
Normal file
119
engine/libs/flecs/examples/c/entities/hooks/src/main.c
Normal 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
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "iterate_components",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user