Properly link flecs library

This commit is contained in:
2023-11-09 11:38:29 +01:00
parent dc585396c3
commit 8edcf9305c
1392 changed files with 390081 additions and 164 deletions

View File

@@ -0,0 +1,16 @@
#ifndef BASICS_H
#define BASICS_H
/* This generated file contains includes for project dependencies */
#include "basics/bake_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef BASICS_BAKE_CONFIG_H
#define BASICS_BAKE_CONFIG_H
/* Headers of public dependencies */
#include <flecs.h>
#endif

View File

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

View File

@@ -0,0 +1,24 @@
#include <basics.h>
#include <iostream>
struct Position {
float x;
float y;
};
int main(int, char *[]) {
flecs::world ecs;
// Register component with reflection data
ecs.component<Position>()
.member<float>("x")
.member<float>("y");
// Create entity with Position as usual
flecs::entity e = ecs.entity()
.set<Position>({10, 20});
// Convert position component to flecs expression string
const Position *ptr = e.get<Position>();
std::cout << ecs.to_expr(ptr).c_str() << "\n"; // {x: 10, y: 20}
}

View File

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

View File

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

View File

@@ -0,0 +1,33 @@
#include <basics_bitmask.h>
#include <iostream>
struct Toppings : flecs::bitmask {
static constexpr uint32_t Bacon = 0x1;
static constexpr uint32_t Lettuce = 0x2;
static constexpr uint32_t Tomato = 0x4;
};
struct Sandwich {
uint32_t toppings;
};
int main(int, char *[]) {
flecs::world ecs;
// Register components with reflection data
ecs.component<Toppings>()
.bit("Bacon", Toppings::Bacon)
.bit("Lettuce", Toppings::Lettuce)
.bit("Tomato", Toppings::Tomato);
ecs.component<Sandwich>()
.member<Toppings>("toppings");
// Create entity with Position as usual
flecs::entity e = ecs.entity()
.set<Sandwich>({Toppings::Bacon | Toppings::Lettuce});
// Convert position component to flecs expression string
const Sandwich *ptr = e.get<Sandwich>();
std::cout << ecs.to_expr(ptr).c_str() << "\n"; // {toppings: Lettuce|Bacon}
}

View File

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

View File

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

View File

@@ -0,0 +1,40 @@
#include <basics_deserialize.h>
#include <iostream>
struct Position {
float x;
float y;
};
int main(int, char *[]) {
flecs::world ecs;
// Register component with reflection data
ecs.component<Position>()
.member<float>("x")
.member<float>("y");
// Create entity, set value of position using reflection API
flecs::entity e = ecs.entity();
Position *ptr = e.get_mut<Position>();
flecs::cursor cur = ecs.cursor<Position>(ptr);
cur.push(); // {
cur.set_float(10.0); // 10
cur.next(); // ,
cur.set_float(20.0); // 20
cur.pop(); // }
std::cout << ecs.to_expr(ptr).c_str() << "\n"; // {x: 10.00, y: 20.00}
// Use member names before assigning values
cur = ecs.cursor<Position>(ptr);
cur.push(); // {
cur.member("y"); // y:
cur.set_float(10); // 10
cur.member("x"); // x:
cur.set_float(20); // 20
cur.pop(); // }
std::cout << ecs.to_expr(ptr).c_str() << "\n"; // {x: 20.00, y: 10.00}
}

View File

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

View File

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

View File

@@ -0,0 +1,33 @@
#include <basics_enum.h>
#include <iostream>
enum Color {
Red,
Green,
Blue
};
struct TypeWithEnum {
Color color;
};
int main(int, char *[]) {
flecs::world ecs;
// Register components with reflection data
ecs.component<Color>()
.constant("Red", Red)
.constant("Green", Green)
.constant("Blue", Blue);
ecs.component<TypeWithEnum>()
.member<Color>("color");
// Create entity with Position as usual
flecs::entity e = ecs.entity()
.set<TypeWithEnum>({Green});
// Convert position component to flecs expression string
const TypeWithEnum *ptr = e.get<TypeWithEnum>();
std::cout << ecs.to_expr(ptr).c_str() << "\n"; // {color: Green}
}

View File

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

View File

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

View File

@@ -0,0 +1,39 @@
#include <basics_json.h>
#include <iostream>
struct Position {
float x;
float y;
};
int main(int, char *[]) {
flecs::world ecs;
// Register component with reflection data
ecs.component<Position>()
.member<float>("x")
.member<float>("y");
// Create entity with Position as usual
flecs::entity e = ecs.entity("ent")
.set<Position>({10, 20});
// Convert position component to JSON string
const Position *ptr = e.get<Position>();
std::cout << ecs.to_json(ptr) << "\n"; // {"x":10, "y":20}
// Convert entity to JSON
flecs::entity_to_json_desc_t desc;
desc.serialize_path = true;
desc.serialize_values = true;
std::cout << e.to_json(&desc) << "\n";
// {
// "path":"ent",
// "ids":[["Position"]],
// "values":[{
// "x":10,
// "y":20
// }]
// }
}

View File

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

View File

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

View File

@@ -0,0 +1,25 @@
#include <entity_type.h>
#include <iostream>
struct TypeWithEntity {
flecs::entity_t e; /* Only naked entity ids are supported at the moment */
};
int main(int, char *[]) {
flecs::world ecs;
// Using flecs::entity_t directly would resolve to a uint64_t datatype, so
// use flecs::Entity instead.
ecs.component<TypeWithEntity>()
.member(flecs::Entity, "e");
flecs::entity foo = ecs.entity("Foo");
// Create entity with PortableType
flecs::entity e = ecs.entity()
.set<TypeWithEntity>({ foo });
// Convert PortableType component to flecs expression string
const TypeWithEntity *ptr = e.get<TypeWithEntity>();
std::cout << ecs.to_expr(ptr).c_str() << "\n"; // {e: Foo}
}

View File

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

View File

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

View File

@@ -0,0 +1,24 @@
#include <member_ranges.h>
#include <iostream>
struct CpuUtilization {
double value;
};
int main() {
flecs::world ecs;
ecs.component<CpuUtilization>()
.member<double>("value")
.range(0.0, 100.0) // Specifics values that the member can assume
.warning_range(0.0, 60.0) // Values outside this range are considerd a warning
.error_range(0.0, 80.0); // Values outside this range are considerd an error
ecs.entity("MachineA").set<CpuUtilization>({ 50.0 });
ecs.entity("MachineB").set<CpuUtilization>({ 75.0 });
ecs.entity("MachineC").set<CpuUtilization>({ 90.0 });
// Open https://www.flecs.dev/explorer?show=query&query=CpuUtilization to
// see how ranges affect visualization.
ecs.app().enable_rest().run();
}

View File

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

View File

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

View File

@@ -0,0 +1,50 @@
#include <nested_set_member.h>
#include <iostream>
struct Point {
float x;
float y;
};
struct Line {
Point start;
Point stop;
};
int main(int, char *[]) {
flecs::world ecs;
ecs.component<Point>()
.member<float>("x")
.member<float>("y");
ecs.component<Line>()
.member<Point>("start")
.member<Point>("stop");
// Create entity, set value of Line using reflection API
flecs::entity e = ecs.entity();
Line *ptr = e.get_mut<Line>();
flecs::cursor cur = ecs.cursor<Line>(ptr);
cur.push(); // {
cur.member("start"); // start:
cur.push(); // {
cur.member("x"); // x:
cur.set_float(10); // 10
cur.member("y"); // y:
cur.set_float(20); // 20
cur.pop(); // }
cur.member("stop"); // stop:
cur.push(); // {
cur.member("x"); // x:
cur.set_float(30); // 30
cur.member("y"); // y:
cur.set_float(40); // 40
cur.pop(); // }
cur.pop(); // }
// Convert component to string
std::cout << ecs.to_expr(ptr).c_str() << "\n";
// {start: {x: 10.00, y: 20.00}, stop: {x: 30.00, y: 40.00}}
}

View File

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

View File

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

View File

@@ -0,0 +1,34 @@
#include <nested_struct.h>
#include <iostream>
struct Point {
float x;
float y;
};
struct Line {
Point start;
Point stop;
};
int main(int, char *[]) {
flecs::world ecs;
// Register components with reflection data
ecs.component<Point>()
.member<float>("x")
.member<float>("y");
ecs.component<Line>()
.member<Point>("start")
.member<Point>("stop");
// Create entity with Position as usual
flecs::entity e = ecs.entity()
.set<Line>({{10, 20}, {30, 40}});
// Convert position component to flecs expression string
const Line *ptr = e.get<Line>();
std::cout << ecs.to_expr(ptr).c_str() << std::endl;
// {start: {x: 10.00, y: 20.00}, stop: {x: 30.00, y: 40.00}}
}

View File

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

View File

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

View File

@@ -0,0 +1,10 @@
{
"id": "portable_type",
"type": "application",
"value": {
"use": [
"flecs"
],
"language": "c++"
}
}

View File

@@ -0,0 +1,28 @@
#include <portable_type.h>
#include <iostream>
// The actual types of int32_t and uintptr_t vary between platforms.
struct PortableType {
int32_t i32;
uintptr_t intptr;
};
int main(int, char *[]) {
flecs::world ecs;
// Register component. Do not use std::int32_t or std::uintptr_t for the
// member type as this will resolve to a different integer type depending on
// the platform, which can cause unexpected issues when type information is
// shared between platforms.
ecs.component<PortableType>()
.member(flecs::I32, "i32") // Use platform-independent type ids
.member(flecs::Uptr, "intptr");
// Create entity with PortableType
flecs::entity e = ecs.entity()
.set<PortableType>({10, 20});
// Convert PortableType component to flecs expression string
const PortableType *ptr = e.get<PortableType>();
std::cout << ecs.to_expr(ptr).c_str() << "\n"; // {i32: 10, inptr: 20}
}

View File

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

View File

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

View File

@@ -0,0 +1,92 @@
#include <query_to_custom_json.h>
#include <iostream>
// Same example as query_to_json, but with customized serializer parameters
struct Position {
float x;
float y;
};
struct Velocity {
float x;
float y;
};
struct Mass {
float value;
};
int main(int, char *[]) {
flecs::world ecs;
// Register components with reflection data
ecs.component<Position>()
.member<float>("x")
.member<float>("y");
ecs.component<Velocity>()
.member<float>("x")
.member<float>("y");
ecs.component<Mass>()
.member<float>("value");
ecs.entity("a").set<Position>({10, 20}).set<Velocity>({1, 2});
ecs.entity("b").set<Position>({20, 30}).set<Velocity>({2, 3});
ecs.entity("c").set<Position>({30, 40}).set<Velocity>({3, 4}).set<Mass>({10});
ecs.entity("d").set<Position>({30, 40}).set<Velocity>({4, 5}).set<Mass>({20});
// Query for components
flecs::query<Position, const Velocity> q =
ecs.query<Position, const Velocity>();
// Serialize query to JSON. Customize serializer to only serialize entity
// names and component values.
flecs::iter_to_json_desc_t desc = {};
desc.serialize_entities = true;
desc.serialize_values = true;
std::cout << q.iter().to_json(&desc).c_str() << "\n";
// Iterator returns 2 sets of results, one for each table.
// {
// "results": [{
// "entities": ["a", "b"],
// "values": [
// [{
// "x": 10.00,
// "y": 20.00
// }, {
// "x": 20.00,
// "y": 30.00
// }],
// [{
// "x": 1.00,
// "y": 2.00
// }, {
// "x": 2.00,
// "y": 3.00
// }]
// ]
// }, {
// "entities": ["c", "d"],
// "values": [
// [{
// "x": 30.00,
// "y": 40.00
// }, {
// "x": 30.00,
// "y": 40.00
// }],
// [{
// "x": 3.00,
// "y": 4.00
// }, {
// "x": 4.00,
// "y": 5.00
// }]
// ]
// }]
// }
}

View File

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

View File

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

View File

@@ -0,0 +1,94 @@
#include <query_to_json.h>
#include <iostream>
struct Position {
float x;
float y;
};
struct Velocity {
float x;
float y;
};
struct Mass {
float value;
};
int main(int, char *[]) {
flecs::world ecs;
// Register components with reflection data
ecs.component<Position>()
.member<float>("x")
.member<float>("y");
ecs.component<Velocity>()
.member<float>("x")
.member<float>("y");
ecs.component<Mass>()
.member<float>("value");
ecs.entity("a").set<Position>({10, 20}).set<Velocity>({1, 2});
ecs.entity("b").set<Position>({20, 30}).set<Velocity>({2, 3});
ecs.entity("c").set<Position>({30, 40}).set<Velocity>({3, 4}).set<Mass>({10});
ecs.entity("d").set<Position>({30, 40}).set<Velocity>({4, 5}).set<Mass>({20});
// Query for components
flecs::query<Position, const Velocity> q =
ecs.query<Position, const Velocity>();
// Serialize query to JSON. Note that this works for any iterable object,
// including filters & rules.
std::cout << q.iter().to_json().c_str() << "\n";
// Iterator returns 2 sets of results, one for each table.
// {
// "ids": ["Position", "Velocity"],
// "results": [{
// "ids": ["Position", "Velocity"],
// "sources": [0, 0],
// "is_set": [true, true],
// "entities": ["a", "b"],
// "values": [
// [{
// "x": 10.00,
// "y": 20.00
// }, {
// "x": 20.00,
// "y": 30.00
// }],
// [{
// "x": 1.00,
// "y": 2.00
// }, {
// "x": 2.00,
// "y": 3.00
// }]
// ]
// }, {
// "ids": ["Position", "Velocity"],
// "sources": [0, 0],
// "is_set": [true, true],
// "entities": ["c", "d"],
// "values": [
// [{
// "x": 30.00,
// "y": 40.00
// }, {
// "x": 30.00,
// "y": 40.00
// }],
// [{
// "x": 3.00,
// "y": 4.00
// }, {
// "x": 4.00,
// "y": 5.00
// }]
// ]
// }]
// }
}

View File

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

View File

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

View File

@@ -0,0 +1,10 @@
{
"id": "runtime_component",
"type": "application",
"value": {
"use": [
"flecs"
],
"language": "c++"
}
}

View File

@@ -0,0 +1,25 @@
#include <runtime_component.h>
#include <iostream>
int main(int, char *[]) {
flecs::world ecs;
// Create component for a type that isn't known at compile time
flecs::entity position = ecs.component("Position")
.member<float>("x")
.member<float>("y");
// Create entity, set value of position using reflection API
flecs::entity e = ecs.entity();
void *ptr = e.get_mut(position);
flecs::cursor cur = ecs.cursor(position, ptr);
cur.push();
cur.set_float(10);
cur.next();
cur.set_float(20);
cur.pop();
// Convert component to string
std::cout << ecs.to_expr(position, ptr).c_str() << "\n"; // {x: 10, y: 20}
}

View File

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

View File

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

View File

@@ -0,0 +1,38 @@
#include <runtime_nested_component.h>
#include <iostream>
int main(int, char *[]) {
flecs::world ecs;
// Create components for types that aren't known at compile time
flecs::entity point = ecs.component("Point")
.member<float>("x")
.member<float>("y");
flecs::entity line = ecs.component("Line")
.member(point, "start")
.member(point, "stop");
// Create entity, set value of position using reflection API
flecs::entity e = ecs.entity();
void *ptr = e.get_mut(line);
flecs::cursor cur = ecs.cursor(line, ptr);
cur.push(); // {
cur.push(); // {
cur.set_float(10); // 10
cur.next(); // ,
cur.set_float(20); // 20
cur.pop(); // }
cur.next(); // ,
cur.push(); // {
cur.set_float(30); // 30
cur.next(); // ,
cur.set_float(40); // 40
cur.pop(); // }
cur.pop(); // }
// Convert component to string
std::cout << ecs.to_expr(line, ptr).c_str() << "\n";
// {start: {x: 10.00, y: 20.00}, stop: {x: 30.00, y: 40.00}}
}

View File

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

View File

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

View File

@@ -0,0 +1,58 @@
#include <ser_opaque_type.h>
#include <iostream>
#include <string>
// Use opaque reflection support to add a computed 'result' member to type
struct Sum {
int32_t a;
int32_t b;
};
int main(int, char *[]) {
flecs::world ecs;
// Register serialization support for opaque type
ecs.component<Sum>()
// Serialize as struct
.opaque(ecs.component()
.member<int32_t>("a")
.member<int32_t>("b")
.member<int32_t>("result"))
// Forward struct members to serializer
.serialize([](const flecs::serializer *s, const Sum *data) {
s->member("x");
s->value(data->a);
s->member("y");
s->value(data->b);
s->member("result");
s->value(data->a + data->b); // Serialize fake member
return 0;
})
// Return address for requested member
.ensure_member([](Sum *dst, const char *member) -> void* {
if (!strcmp(member, "a")) {
return &dst->a;
} else if (!strcmp(member, "b")) {
return &dst->b;
} else {
return nullptr; // We can't serialize into fake result member
}
});
// Serialize value of Sum to JSON
Sum v = {10, 20};
std::cout << ecs.to_json(&v) << std::endl;
// Deserialize new value into Sum
ecs.from_json(&v, "{\"a\": 20, \"b\": 22}");
// Serialize value again
std::cout << ecs.to_json(&v) << std::endl;
// Output
// {"a":10, "b":20, "result":30}
// {"a":22, "b":20, "result":42}
}

View File

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

View File

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

View File

@@ -0,0 +1,42 @@
#include <ser_std_string.h>
#include <iostream>
#include <string>
// This example shows how to serialize a component with an std::string
struct StringComponent {
std::string a;
std::string b;
};
int main(int, char *[]) {
flecs::world ecs;
// Register reflection for std::string
ecs.component<std::string>()
.opaque(flecs::String) // Opaque type that maps to string
.serialize([](const flecs::serializer *s, const std::string *data) {
const char *str = data->c_str();
return s->value(flecs::String, &str); // Forward to serializer
})
.assign_string([](std::string* data, const char *value) {
*data = value; // Assign new value to std::string
});
// Register component with std::string members
ecs.component<StringComponent>()
.member<std::string>("a")
.member<std::string>("b");
// Create value & serialize it
StringComponent v = {"foo", "bar"};
std::cout << ecs.to_json(&v) << std::endl;
// Deserialize new strings into value
ecs.from_json(&v, "{\"a\": \"hello\", \"b\": \"world\"}");
std::cout << "{a: " << v.a << ", b: " << v.b << "}" << std::endl;
// Output:
// {"a": "foo", "b": "bar"}
// {a: "hello", b: "world"}
}

View File

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

View File

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

View File

@@ -0,0 +1,88 @@
#include <ser_std_vector.h>
#include <iostream>
#include <string>
#include <vector>
// This example shows how to serialize a component with std::vectors
struct VectorComponent {
std::vector<int32_t> ints;
std::vector<std::string> strings;
};
// Reusable reflection support for std::vector
template <typename Elem, typename Vector = std::vector<Elem>>
flecs::opaque<Vector, Elem> std_vector_support(flecs::world& world) {
return flecs::opaque<Vector, Elem>()
.as_type(world.vector<Elem>())
// Forward elements of std::vector value to serializer
.serialize([](const flecs::serializer *s, const Vector *data) {
for (const auto& el : *data) {
s->value(el);
}
return 0;
})
// Return vector count
.count([](const Vector *data) {
return data->size();
})
// Resize contents of vector
.resize([](Vector *data, size_t size) {
data->resize(size);
})
// Ensure element exists, return pointer
.ensure_element([](Vector *data, size_t elem) {
if (data->size() <= elem) {
data->resize(elem + 1);
}
return &data->data()[elem];
});
}
int main(int, char *[]) {
flecs::world ecs;
// Register reflection for std::string
ecs.component<std::string>()
.opaque(flecs::String) // Maps to string
.serialize([](const flecs::serializer *s, const std::string *data) {
const char *str = data->c_str();
return s->value(flecs::String, &str); // Forward to serializer
})
.assign_string([](std::string* data, const char *value) {
*data = value; // Assign new value to std::string
});
// Register reflection for std::vector<int>
ecs.component<std::vector<int>>()
.opaque(std_vector_support<int>);
// Register reflection for std::vector<std::string>
ecs.component<std::vector<std::string>>()
.opaque(std_vector_support<std::string>);
// Register component with std::vector members
ecs.component<VectorComponent>()
.member<std::vector<int>>("ints")
.member<std::vector<std::string>>("strings");
// Create value & serialize it to JSON
VectorComponent v = {{1, 2, 3}, {"foo", "bar"}};
std::cout << ecs.to_json(&v) << std::endl;
// Deserialize new values from JSON into value
ecs.from_json(&v,
"{\"ints\": [4, 5], \"strings\":[\"hello\", \"flecs\", \"reflection\"]}");
// Serialize again
std::cout << ecs.to_json(&v) << std::endl;
// Output:
// {"ints":[1, 2, 3], "strings":["foo", "bar"]}
// {"ints":[4, 5], "strings":["hello", "flecs", "reflection"]}
}

View File

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

View File

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

View File

@@ -0,0 +1,50 @@
#include <units.h>
#include <iostream>
struct WeatherStation {
double temperature;
double pressure;
double precipitation;
};
void print_value(const flecs::cursor& cur) {
// Get unit entity and component
flecs::entity u = cur.get_unit();
const flecs::Unit *u_data = u.get<flecs::Unit>();
// Print value with unit symbol
std::cout << cur.get_member() << ": " << cur.get_float()
<< " " << u_data->symbol << "\n";
}
int main(int, char *[]) {
flecs::world ecs;
// Import units module.
ecs.import<flecs::units>();
// Register reflection data with units. This can improve the way information
// is visualized in tools, such as the explorer.
ecs.component<WeatherStation>()
.member<double, flecs::units::temperature::Celsius>("temperature")
.member<double, flecs::units::pressure::Bar>("pressure")
.member<double, flecs::units::length::MilliMeters>("precipitation");
flecs::entity e = ecs.entity().set<WeatherStation>({24, 1.2, 0.5});
// Use cursor API to print values with units
WeatherStation *ptr = e.get_mut<WeatherStation>();
flecs::cursor cur = ecs.cursor<WeatherStation>(ptr);
cur.push();
print_value(cur);
cur.next();
print_value(cur);
cur.next();
print_value(cur);
cur.pop();
// Output:
// temperature: 24 °C
// pressure: 1.2 bar
// precipitation: 0.5 mm
}

View File

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

View File

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

View File

@@ -0,0 +1,101 @@
#include <world_ser_deser.h>
#include <iostream>
#include <string>
struct Position {
double x;
double y;
};
struct Velocity {
double x;
double y;
};
// Register components and systems in a module. This excludes them by default
// from the serialized data, and makes it easier to import across worlds.
struct move {
move(flecs::world& world) {
world.component<Position>()
.member<double>("x")
.member<double>("y");
world.component<Velocity>()
.member<double>("x")
.member<double>("y");
world.system<Position, const Velocity>("Move")
.each([](flecs::entity e, Position& p, const Velocity& v) {
p.x += v.x;
p.y += v.y;
std::cout << e.path() << " moved to "
<< "{x: " << p.x << ", y: " << p.y << "}"
<< std::endl;
});
}
};
int main(int, char *[]) {
flecs::world world_a;
world_a.import<move>();
world_a.entity("ent_1")
.set<Position>({10, 20})
.set<Velocity>({1, -1});
world_a.entity("ent_2")
.set<Position>({30, 40})
.set<Velocity>({-1, 1});
// Serialize world to JSON
auto json = world_a.to_json();
std::cout << json << std::endl << std::endl;
// Output:
// {
// "results": [{
// "ids": [
// ["my_module.Position"],
// ["my_module.Velocity"],
// ["flecs.core.Identifier", "flecs.core.Name"]
// ],
// "entities": ["ent_1", "ent_2"],
// "values": [
// [{
// "x": 10,
// "y": 20
// }, {
// "x": 30,
// "y": 40
// }],
// [{
// "x": 1,
// "y": -1
// }, {
// "x": -1,
// "y": 1
// }], 0
// ]
// }]
// }
// Create second world, import same module
flecs::world world_b;
world_b.import<move>();
// Deserialize JSON into second world
world_b.from_json(json);
// Run system once for both worlds
world_a.progress();
std::cout << std::endl;
world_b.progress();
// Output
// ::ent_1 moved to {x: 11, y: 19}
// ::ent_2 moved to {x: 29, y: 41}
//
// ::ent_1 moved to {x: 11, y: 19}
// ::ent_2 moved to {x: 29, y: 41}
}