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,5 @@
.bake_cache
.DS_Store
.vscode
gcov
bin

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"
],
"language": "c++"
}
}

View File

@@ -0,0 +1,53 @@
#include <basics.h>
#include <iostream>
struct Position {
double x, y;
};
struct Walking { };
int main(int, char *[]) {
flecs::world ecs;
// Create an entity with name Bob
flecs::entity bob = ecs.entity("Bob")
// The set operation finds or creates a component, and sets it.
// Components are automatically registered with the world.
.set<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.
.add<Walking>();
// Get the value for the Position component
const Position* ptr = bob.get<Position>();
std::cout << "{" << ptr->x << ", " << ptr->y << "}" << "\n";
// Overwrite the value of the Position component
bob.set<Position>({20, 30});
// Create another named entity
flecs::entity alice = ecs.entity("Alice")
.set<Position>({10, 20});
// Add a tag after entity is created
alice.add<Walking>();
// Print all of the components the entity has. This will output:
// Position, Walking, (Identifier,Name)
std::cout << "[" << alice.type().str() << "]" << "\n";
// Remove tag
alice.remove<Walking>();
// Iterate all entities with Position
ecs.each([](flecs::entity e, Position& p) {
std::cout << e.name() << ": {" << p.x << ", " << p.y << "}" << "\n";
});
// Output
// {10, 20}
// [Position, Walking, (Identifier,Name)]
// Alice: {10, 20}
// Bob: {20, 30}
}

View File

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

View File

@@ -0,0 +1,11 @@
{
"id": "emplace",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false,
"language": "c++"
}
}

View File

@@ -0,0 +1,28 @@
#include <emplace.h>
#include <iostream>
// The emplace method constructs a component directly in the storage vs. a
// regular set, which moves the value into an already constructed value in the
// storage. This reduces the overhead of creating a new component.
//
// Components that do not have a default constructor need to be created with
// emplace, as they can't be constructed automatically by flecs.
struct NoDefaultCtor {
NoDefaultCtor(double x_, double y_) : x(x_), y(y_) { }
double x, y;
};
int main(int, char *[]) {
flecs::world ecs;
flecs::entity e = ecs.entity()
.emplace<NoDefaultCtor>(10.0, 20.0);
const NoDefaultCtor *ptr = e.get<NoDefaultCtor>();
std::cout << "{" << ptr->x << ", " << ptr->y << "}" << "\n";
// Output
// {10, 20}
}

View File

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

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

View File

@@ -0,0 +1,65 @@
#include <hierarchy.h>
#include <iostream>
struct Position {
double x, y;
};
struct Star { };
struct Planet { };
struct Moon { };
void iterate_tree(flecs::entity e, Position p_parent = {0, 0}) {
// Print hierarchical name of entity & the entity type
std::cout << e.path() << " [" << e.type().str() << "]\n";
// Get entity position
const Position *p = e.get<Position>();
// Calculate actual position
Position p_actual = {p->x + p_parent.x, p->y + p_parent.y};
std::cout << "{" << p_actual.x << ", " << p_actual.y << "}\n\n";
// Iterate children recursively
e.children([&](flecs::entity child) {
iterate_tree(child, p_actual);
});
}
int main(int, char *[]) {
flecs::world ecs;
// Create a simple hierarchy.
// Hierarchies use ECS relationships and the builtin flecs::ChildOf relationship to
// create entities as children of other entities.
flecs::entity sun = ecs.entity("Sun")
.add<Star>()
.set<Position>({1, 1});
ecs.entity("Mercury")
.child_of(sun) // Shortcut for add(flecs::ChildOf, sun)
.add<Planet>()
.set<Position>({1, 1});
ecs.entity("Venus")
.child_of(sun)
.add<Planet>()
.set<Position>({2, 2});
flecs::entity earth = ecs.entity("Earth")
.child_of(sun)
.add<Planet>()
.set<Position>({3, 3});
flecs::entity moon = ecs.entity("Moon")
.child_of(earth)
.add<Moon>()
.set<Position>({0.1, 0.1});
// Is the Moon a child of Earth?
std::cout << "Child of Earth? " << moon.has(flecs::ChildOf, earth) << "\n\n";
// Do a depth-first walk of the tree
iterate_tree(sun);
}

View File

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

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,10 @@
{
"id": "hooks",
"type": "application",
"value": {
"use": [
"flecs"
],
"language": "c++"
}
}

View File

@@ -0,0 +1,108 @@
#include <hooks.h>
#include <iostream>
// Component hooks are callbacks that can be registered for a type that are
// invoked during different parts of the component lifecycle.
//
// In the C++ API the ctor, dtor, copy and move methods of a type are
// automatically registered as component hooks.
struct String {
String(const char *v = nullptr) {
flecs::log::trace("Ctor(const char*)");
value = ecs_os_strdup(v);
}
~String() {
flecs::log::trace("Dtor");
ecs_os_free(value);
}
String(const String& obj) {
flecs::log::trace("Copy");
value = ecs_os_strdup(obj.value);
}
String(String&& obj) {
flecs::log::trace("Move");
value = obj.value;
obj.value = nullptr;
}
String& operator=(const String& obj) {
flecs::log::trace("Copy assign");
ecs_os_free(value);
value = ecs_os_strdup(obj.value);
return *this;
}
String& operator=(String&& obj) {
flecs::log::trace("Move assign");
ecs_os_free(value);
value = obj.value;
obj.value = nullptr;
return *this;
}
char *value;
};
struct Tag { };
int main(int, char *[]) {
flecs::world ecs;
// Register additional add, remove and set hooks for type
ecs.component<String>()
.on_add([](flecs::entity e, String&) {
flecs::log::trace("OnAdd: %s", e.name().c_str());
})
.on_remove([](flecs::entity e, String&) {
flecs::log::trace("OnRemove: %s", e.name().c_str());
})
.on_set([](flecs::entity e, String&) {
flecs::log::trace("OnSet: %s", e.name().c_str());
});
// Register in advance to keep output trace clean
ecs.component<Tag>();
flecs::log::set_level(0);
flecs::entity e = ecs.entity("Entity");
flecs::log::push("e.add<String>()");
e.add<String>();
flecs::log::pop();
flecs::log::push("e.set<String>({\"Hello World\"})");
e.set<String>({"Hello World"});
flecs::log::pop();
// This operation changes the entity's archetype, which invokes a move
flecs::log::push("e.add<Tag>()");
e.add<Tag>();
flecs::log::pop();
flecs::log::push("e.destruct()");
e.destruct();
flecs::log::pop();
flecs::log::set_level(-1);
// Output:
// info: e.add<String>()
// info: | Ctor
// info: | OnAdd: Entity
// info: e.set<String>({"Hello World"})
// info: | Ctor
// info: | Move assign
// info: | OnSet: Entity
// info: | Dtor
// info: e.add<Tag>()
// info: | Move
// info: | Dtor
// info: e.destruct()
// 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,13 @@
{
"id": "iterate_components",
"type": "application",
"value": {
"author": "Jane Doe",
"description": "A simple hello world flecs application",
"use": [
"flecs"
],
"language": "c++",
"public": false
}
}

View File

@@ -0,0 +1,99 @@
#include <iterate_components.h>
#include <iostream>
// Ordinary components
struct Position {
double x, y;
};
struct Velocity {
double x, y;
};
// Tag
struct Human { };
// Two tags used to create a pair
struct Eats { };
struct Apples { };
void iterate_components(flecs::entity e) {
// 1. The easiest way to print the components is to use type::str
std::cout << e.type().str() << "\n\n";
// 2. To get individual component ids, use entity::each
int32_t i = 0;
e.each([&](flecs::id id) {
std::cout << i++ << ": " << id.str() << "\n";
});
std::cout << "\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.
i = 0;
e.each([&](flecs::id id) {
std::cout << i++ << ": ";
if (id.is_pair()) {
// If id is a pair, extract & print both parts of the pair
flecs::entity rel = id.first();
flecs::entity tgt = id.second();
std::cout << "rel: " << rel.name() << ", " << "tgt: " << tgt.name();
} else {
// Id contains a regular entity. Strip role before printing.
flecs::entity comp = id.entity();
std::cout << "entity: " << comp.name();
}
std::cout << "\n";
});
std::cout << "\n\n";
}
int main(int, char *[]) {
flecs::world ecs;
flecs::entity bob = ecs.entity()
.set<Position>({10, 20})
.set<Velocity>({1, 1})
.add<Human>()
.add<Eats, Apples>();
std::cout << "Bob's components:\n";
iterate_components(bob);
// We can use the same function to iterate the components of a component
std::cout << "Position's components:\n";
iterate_components(ecs.component<Position>());
}
// Output:
// Bob's components:
// 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: Apples
//
//
// Position's components:
// Component, (Identifier,Name), (Identifier,Symbol), (OnDelete,Panic)
//
// 0: Component
// 1: (Identifier,Name)
// 2: (Identifier,Symbol)
// 3: (OnDelete,Panic)
//
// 0: entity: Component
// 1: rel: Identifier, tgt: Name
// 2: rel: Identifier, tgt: Symbol
// 3: rel: OnDelete, tgt: Panic

View File

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

View File

@@ -0,0 +1,11 @@
{
"id": "multi_set_get",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false,
"language": "c++"
}
}

View File

@@ -0,0 +1,39 @@
#include <multi_set_get.h>
#include <iostream>
/* This code shows how to get & set multiple components in a single command */
struct Position {
double x, y;
};
struct Mass {
double value;
};
int main(int, char *[]) {
flecs::world ecs;
// Create new entity, set Position and Mass component
flecs::entity e = ecs.entity()
.set([](Position& p, Mass& m) {
p.x = 10;
p.y = 20;
m.value = 100;
});
// Print values of Position and Mass component
bool found = e.get([](const Position& p, const Mass& m) {
std::cout << "Position: {" << p.x << ", " << p.y << "}\n"
<< "Mass: {" << m.value << "}\n";
});
std::cout << "Components found: " << (found ? "true" : "false") << "\n";
// Output:
// Position: {10, 20}
// Mass: {100}
// Components found: true
return 0;
}

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,70 @@
#include <prefab.h>
#include <iostream>
// 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.
struct Attack { double value; };
struct Defense { double value; };
struct FreightCapacity { double value; };
struct ImpulseSpeed { double value; };
struct HasFTL { };
struct Position { double x = 0; double y = 0; };
int main(int, char *[]) {
flecs::world ecs;
// Create a prefab hierarchy.
flecs::entity spaceship = ecs.prefab("Spaceship")
// Add components to prefab entity as usual
.set<ImpulseSpeed>({50})
.set<Defense>({50})
// By default components in an inheritance hierarchy are shared between
// entities. The override function ensures that instances have a private
// copy of the component.
.override<Position>();
flecs::entity freighter = ecs.prefab("Freighter")
// Short for .add(flecs::IsA, spaceship). This ensures the entity
// inherits all components from spaceship.
.is_a(spaceship)
.add<HasFTL>()
.set<FreightCapacity>({100})
.set<Defense>({100});
flecs::entity mammoth_freighter = ecs.prefab("MammothFreighter")
.is_a(freighter)
.set<FreightCapacity>({500})
.set<Defense>({300});
ecs.prefab("Frigate")
.is_a(spaceship)
.add<HasFTL>()
.set<Attack>({100})
.set<Defense>({75})
.set<ImpulseSpeed>({125});
// Create a regular entity from a prefab.
// The instance will have a private copy of the Position component, because
// of the override in the spaceship entity. All other components are shared.
flecs::entity inst = ecs.entity("my_mammoth_freighter")
.is_a(mammoth_freighter);
// Inspect the type of the entity. This outputs:
// Position,(Identifier,Name),(IsA,MammothFreighter)
std::cout << "Instance type: [" << inst.type().str() << "]\n";
// Even though the instance doesn't have a private copy of ImpulseSpeed, we
// can still get it using the regular API (outputs 50)
const ImpulseSpeed *ptr = inst.get<ImpulseSpeed>();
std::cout << "Impulse speed: " << ptr->value << "\n";
// Prefab components can be iterated just like regular components:
ecs.each([](flecs::entity e, ImpulseSpeed& is, Position& p) {
p.x += is.value;
std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
});
}