Properly link flecs library
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
#ifndef AUTO_DEFINE_ENUM_H
|
||||
#define AUTO_DEFINE_ENUM_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "auto_define_enum/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 AUTO_DEFINE_ENUM_BAKE_CONFIG_H
|
||||
#define AUTO_DEFINE_ENUM_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "auto_define_enum",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <auto_define_enum.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Color is used by Car, so it must also be captured
|
||||
ECS_ENUM(Color, {
|
||||
Red, Black, White, StainlessSteel
|
||||
});
|
||||
|
||||
ECS_STRUCT(Car, {
|
||||
const char *brand;
|
||||
Color color;
|
||||
float speed;
|
||||
});
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register both components. Note that Color is registered first, so that
|
||||
// when Car is registered, the type information of Color can be looked up
|
||||
ECS_META_COMPONENT(ecs, Color);
|
||||
ECS_META_COMPONENT(ecs, Car);
|
||||
|
||||
// Serialize as usual
|
||||
Car value = {"Delorean", StainlessSteel, 1.21f};
|
||||
char *json = ecs_ptr_to_json(ecs, ecs_id(Car), &value);
|
||||
printf("%s\n", json); // {"brand": "Delorean", "color": "StainlessSteel", "speed": 1.21}
|
||||
ecs_os_free(json);
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef AUTO_DEFINE_NESTED_STRUCT_H
|
||||
#define AUTO_DEFINE_NESTED_STRUCT_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "auto_define_nested_struct/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 AUTO_DEFINE_NESTED_STRUCT_BAKE_CONFIG_H
|
||||
#define AUTO_DEFINE_NESTED_STRUCT_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "auto_define_nested_struct",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <auto_define_nested_struct.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Point is used by Line, so it must also be captured
|
||||
ECS_STRUCT(Point, {
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
});
|
||||
|
||||
ECS_STRUCT(Line, {
|
||||
Point start;
|
||||
Point stop;
|
||||
});
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register both components. Note that Point is registered first, so that
|
||||
// when Line is registered, the type information of Point can be looked up
|
||||
ECS_META_COMPONENT(ecs, Point);
|
||||
ECS_META_COMPONENT(ecs, Line);
|
||||
|
||||
// Serialize as usual
|
||||
Line value = {{1, 2}, {3, 4}};
|
||||
char *json = ecs_ptr_to_json(ecs, ecs_id(Line), &value);
|
||||
printf("%s\n", json); // {"start": {"x": 1, "y": 2}, "stop": {"x": 3, "y": 4}}
|
||||
ecs_os_free(json);
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef AUTO_DEFINE_STRUCT_H
|
||||
#define AUTO_DEFINE_STRUCT_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "auto_define_struct/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 AUTO_DEFINE_STRUCT_BAKE_CONFIG_H
|
||||
#define AUTO_DEFINE_STRUCT_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "auto_define_struct",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <auto_define_struct.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// This type captured is used later to inject reflection data for Position
|
||||
ECS_STRUCT(Position, {
|
||||
float x;
|
||||
float y;
|
||||
});
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// This registers the component with reflection data
|
||||
ECS_META_COMPONENT(ecs, Position);
|
||||
|
||||
// Serialize as usual
|
||||
Position value = {10, 20};
|
||||
char *json = ecs_ptr_to_json(ecs, ecs_id(Position), &value);
|
||||
printf("%s\n", json); // {"x": 10.0, "y": 20.0}
|
||||
ecs_os_free(json);
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
12
engine/libs/flecs/examples/c/reflection/basics/project.json
Normal file
12
engine/libs/flecs/examples/c/reflection/basics/project.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "basics",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
34
engine/libs/flecs/examples/c/reflection/basics/src/main.c
Normal file
34
engine/libs/flecs/examples/c/reflection/basics/src/main.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <basics.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
float x, y;
|
||||
} Position;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register component as usual
|
||||
ECS_COMPONENT(ecs, Position);
|
||||
|
||||
// Add reflection data to component
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Position), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) }, // builtin float type
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
// Create entity with Position as usual
|
||||
ecs_entity_t ent = ecs_new_id(ecs);
|
||||
ecs_set(ecs, ent, Position, {10, 20});
|
||||
|
||||
// Convert position component to flecs expression string
|
||||
const Position *ptr = ecs_get(ecs, ent, Position);
|
||||
char *str = ecs_ptr_to_expr(ecs, ecs_id(Position), ptr);
|
||||
printf("%s\n", str); // {x: 10, y: 20}
|
||||
ecs_os_free(str);
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "basics_bitmask",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <basics_bitmask.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef uint32_t Toppings;
|
||||
static const uint32_t Bacon = 0x1;
|
||||
static const uint32_t Lettuce = 0x2;
|
||||
static const uint32_t Tomato = 0x4;
|
||||
|
||||
typedef struct {
|
||||
Toppings toppings;
|
||||
} Sandwich;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register component as usual
|
||||
ECS_COMPONENT(ecs, Toppings);
|
||||
ECS_COMPONENT(ecs, Sandwich);
|
||||
|
||||
// Add reflection data to components
|
||||
ecs_bitmask(ecs,{
|
||||
.entity = ecs_id(Toppings), // Make sure to use existing id
|
||||
.constants = {
|
||||
{ .name = "Bacon", .value = Bacon },
|
||||
{ .name = "Lettuce", .value = Lettuce },
|
||||
{ .name = "Tomato", .value = Tomato }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Sandwich), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "toppings", .type = ecs_id(Toppings) }
|
||||
}
|
||||
});
|
||||
|
||||
// Create entity with Position as usual
|
||||
ecs_entity_t ent = ecs_new_id(ecs);
|
||||
ecs_set(ecs, ent, Sandwich, {Bacon | Lettuce});
|
||||
|
||||
// Convert position component to flecs expression string
|
||||
const Sandwich *ptr = ecs_get(ecs, ent, Sandwich);
|
||||
char *str = ecs_ptr_to_expr(ecs, ecs_id(Sandwich), ptr);
|
||||
printf("%s\n", str); // {toppings: Bacon|Lettuce}
|
||||
ecs_os_free(str);
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "basics_deserialize",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <basics_deserialize.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
float x, y;
|
||||
} Position;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register component as usual
|
||||
ECS_COMPONENT(ecs, Position);
|
||||
|
||||
// Add reflection data to component
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Position), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) }, // builtin float type
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
// Create entity, set value of position using reflection API
|
||||
ecs_entity_t ent = ecs_new_entity(ecs, "ent");
|
||||
Position *ptr = ecs_get_mut(ecs, ent, Position);
|
||||
|
||||
ecs_meta_cursor_t cur = ecs_meta_cursor(ecs, ecs_id(Position), ptr);
|
||||
ecs_meta_push(&cur); // {
|
||||
ecs_meta_set_float(&cur, 10); // 10
|
||||
ecs_meta_next(&cur); // ,
|
||||
ecs_meta_set_float(&cur, 20); // 20
|
||||
ecs_meta_pop(&cur); // }
|
||||
|
||||
char *str = ecs_ptr_to_expr(ecs, ecs_id(Position), ptr);
|
||||
printf("%s\n", str); // {x: 10.00, y: 20.00}
|
||||
ecs_os_free(str);
|
||||
|
||||
// Use member names before assigning values
|
||||
cur = ecs_meta_cursor(ecs, ecs_id(Position), ptr);
|
||||
ecs_meta_push(&cur); // {
|
||||
ecs_meta_member(&cur, "y"); // y:
|
||||
ecs_meta_set_float(&cur, 10); // 10
|
||||
ecs_meta_member(&cur, "x"); // x:
|
||||
ecs_meta_set_float(&cur, 20); // 20
|
||||
ecs_meta_pop(&cur); // }
|
||||
|
||||
str = ecs_ptr_to_expr(ecs, ecs_id(Position), ptr);
|
||||
printf("%s\n", str); // {x: 20.00, y: 10.00}
|
||||
ecs_os_free(str);
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "basics_enum",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <basics_enum.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef enum {
|
||||
Red,
|
||||
Green,
|
||||
Blue
|
||||
} Color;
|
||||
|
||||
typedef struct {
|
||||
Color color;
|
||||
} TypeWithEnum;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register component as usual
|
||||
ECS_COMPONENT(ecs, Color);
|
||||
ECS_COMPONENT(ecs, TypeWithEnum);
|
||||
|
||||
// Add reflection data to components
|
||||
ecs_enum(ecs, {
|
||||
.entity = ecs_id(Color), // Make sure to use existing id
|
||||
.constants = {
|
||||
{ .name = "Red", .value = Red },
|
||||
{ .name = "Green", .value = Green },
|
||||
{ .name = "Blue", .value = Blue }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(TypeWithEnum), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "color", .type = ecs_id(Color) }
|
||||
}
|
||||
});
|
||||
|
||||
// Create entity with Position as usual
|
||||
ecs_entity_t ent = ecs_new_id(ecs);
|
||||
ecs_set(ecs, ent, TypeWithEnum, {Green});
|
||||
|
||||
// Convert position component to flecs expression string
|
||||
const TypeWithEnum *ptr = ecs_get(ecs, ent, TypeWithEnum);
|
||||
char *str = ecs_ptr_to_expr(ecs, ecs_id(TypeWithEnum), ptr);
|
||||
printf("%s\n", str); // {color: Green}
|
||||
ecs_os_free(str);
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "basics_json",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <basics_json.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
float x, y;
|
||||
} Position;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register component as usual
|
||||
ECS_COMPONENT(ecs, Position);
|
||||
|
||||
// Add reflection data to component
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Position), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) }, // builtin float type
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
// Create entity with Position as usual
|
||||
ecs_entity_t ent = ecs_new_entity(ecs, "ent");
|
||||
ecs_set(ecs, ent, Position, {10, 20});
|
||||
|
||||
// Convert position component to JSON
|
||||
const Position *ptr = ecs_get(ecs, ent, Position);
|
||||
char *str = ecs_ptr_to_json(ecs, ecs_id(Position), ptr);
|
||||
printf("Position = %s\n", str);
|
||||
ecs_os_free(str);
|
||||
|
||||
// Convert entity & all its components to json
|
||||
str = ecs_entity_to_json(ecs, ent, &(ecs_entity_to_json_desc_t) {
|
||||
.serialize_path = true,
|
||||
.serialize_values = true
|
||||
});
|
||||
printf("ent = %s\n", str);
|
||||
ecs_os_free(str);
|
||||
|
||||
// {
|
||||
// "path":"ent",
|
||||
// "ids":[["Position"]],
|
||||
// "values":[{
|
||||
// "x":10,
|
||||
// "y":20
|
||||
// }]
|
||||
// }
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"id": "entity_type",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <entity_type.h>
|
||||
#include <stdio.h>
|
||||
|
||||
ECS_STRUCT(TypeWithEntity, {
|
||||
ecs_entity_t e;
|
||||
});
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
ECS_META_COMPONENT(ecs, TypeWithEntity);
|
||||
|
||||
ecs_entity_t foo = ecs_set_name(ecs, 0, "Foo");
|
||||
ecs_entity_t e = ecs_set(ecs, 0, TypeWithEntity, {foo});
|
||||
|
||||
// Convert component to flecs expression string
|
||||
const TypeWithEntity *ptr = ecs_get(ecs, e, TypeWithEntity);
|
||||
char *str = ecs_ptr_to_expr(ecs, ecs_id(TypeWithEntity), ptr);
|
||||
printf("%s\n", str); // {e: Foo}
|
||||
ecs_os_free(str);
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "member_ranges",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <member_ranges.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
double value;
|
||||
} CpuUtilization;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
ECS_COMPONENT(ecs, CpuUtilization);
|
||||
|
||||
// Register reflection data warning and error ranges. This adds metadata to
|
||||
// a type which can be used by tools (like the explorer) to visualize data.
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(CpuUtilization),
|
||||
.members = {{
|
||||
.name = "value",
|
||||
.type = ecs_id(ecs_f64_t),
|
||||
.range = {0, 100}, // Specifics values that the member can assume
|
||||
.warning_range = {0, 60}, // Values outside this range are considerd a warning
|
||||
.error_range = {0, 80} // Values outside this range are considerd an error
|
||||
}}
|
||||
});
|
||||
|
||||
ecs_entity_t m1 = ecs_new_entity(ecs, "MachineA");
|
||||
ecs_set(ecs, m1, CpuUtilization, {50});
|
||||
|
||||
ecs_entity_t m2 = ecs_new_entity(ecs, "MachineB");
|
||||
ecs_set(ecs, m2, CpuUtilization, {75});
|
||||
|
||||
ecs_entity_t m3 = ecs_new_entity(ecs, "MachineC");
|
||||
ecs_set(ecs, m3, CpuUtilization, {90});
|
||||
|
||||
// Open https://www.flecs.dev/explorer?show=query&query=CpuUtilization to
|
||||
// see how ranges affect visualization.
|
||||
return ecs_app_run(ecs, &(ecs_app_desc_t){ .enable_rest = true });
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"id": "nested_set_member",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <nested_set_member.h>
|
||||
#include <stdio.h>
|
||||
|
||||
ECS_STRUCT(Point, {
|
||||
float x;
|
||||
float y;
|
||||
});
|
||||
|
||||
ECS_STRUCT(Line, {
|
||||
Point start;
|
||||
Point stop;
|
||||
});
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
ECS_META_COMPONENT(ecs, Point);
|
||||
ECS_META_COMPONENT(ecs, Line);
|
||||
|
||||
ecs_entity_t ent = ecs_new_id(ecs);
|
||||
Line *ptr = ecs_get_mut(ecs, ent, Line);
|
||||
|
||||
ecs_meta_cursor_t cur = ecs_meta_cursor(ecs, ecs_id(Line), ptr);
|
||||
ecs_meta_push(&cur); // {
|
||||
ecs_meta_member(&cur, "start"); // start:
|
||||
ecs_meta_push(&cur); // {
|
||||
ecs_meta_member(&cur, "x"); // x:
|
||||
ecs_meta_set_float(&cur, 10); // 10
|
||||
ecs_meta_member(&cur, "y"); // y:
|
||||
ecs_meta_set_float(&cur, 20); // 20
|
||||
ecs_meta_pop(&cur); // }
|
||||
ecs_meta_member(&cur, "stop"); // stop:
|
||||
ecs_meta_push(&cur); // {
|
||||
ecs_meta_member(&cur, "x"); // x:
|
||||
ecs_meta_set_float(&cur, 30); // 30
|
||||
ecs_meta_member(&cur, "y"); // y:
|
||||
ecs_meta_set_float(&cur, 40); // 40
|
||||
ecs_meta_pop(&cur); // }
|
||||
ecs_meta_pop(&cur); // }
|
||||
|
||||
char *str = ecs_ptr_to_expr(ecs, ecs_id(Line), ptr);
|
||||
printf("%s\n", str);
|
||||
ecs_os_free(str);
|
||||
|
||||
// {start: {x: 10.00, y: 20.00}, stop: {x: 30.00, y: 40.00}}
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "nested_struct",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <nested_struct.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
float x, y;
|
||||
} Point;
|
||||
|
||||
typedef struct {
|
||||
Point start, stop;
|
||||
} Line;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
ECS_COMPONENT(ecs, Point);
|
||||
ECS_COMPONENT(ecs, Line);
|
||||
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Point),
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) },
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Line),
|
||||
.members = {
|
||||
{ .name = "start", .type = ecs_id(Point) },
|
||||
{ .name = "stop", .type = ecs_id(Point) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_entity_t ent = ecs_new_id(ecs);
|
||||
ecs_set(ecs, ent, Line, {{10, 20}, {30, 40}});
|
||||
|
||||
const Line *ptr = ecs_get(ecs, ent, Line);
|
||||
char *str = ecs_ptr_to_expr(ecs, ecs_id(Line), ptr);
|
||||
printf("%s\n", str); // {start: {x: 10, y: 20}, stop: {x: 30, y: 40}}
|
||||
ecs_os_free(str);
|
||||
|
||||
return ecs_fini(ecs);
|
||||
}
|
||||
5
engine/libs/flecs/examples/c/reflection/query_to_custom_json/.gitignore
vendored
Normal file
5
engine/libs/flecs/examples/c/reflection/query_to_custom_json/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.bake_cache
|
||||
.DS_Store
|
||||
.vscode
|
||||
gcov
|
||||
bin
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"id": "query_to_custom_json",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
#include <query_to_custom_json.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Same example as query_to_json, but with customized serializer parameters
|
||||
|
||||
typedef struct {
|
||||
float x, y;
|
||||
} Position, Velocity;
|
||||
|
||||
typedef struct {
|
||||
float value;
|
||||
} Mass;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register component as usual
|
||||
ECS_COMPONENT(ecs, Position);
|
||||
ECS_COMPONENT(ecs, Velocity);
|
||||
ECS_COMPONENT(ecs, Mass);
|
||||
|
||||
// Add reflection data to components
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Position), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) },
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Velocity), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) },
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Mass), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "value", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_entity_t a = ecs_set_name(ecs, 0, "a");
|
||||
ecs_entity_t b = ecs_set_name(ecs, 0, "b");
|
||||
ecs_entity_t c = ecs_set_name(ecs, 0, "c");
|
||||
ecs_entity_t d = ecs_set_name(ecs, 0, "d");
|
||||
|
||||
ecs_set(ecs, a, Position, {10, 20});
|
||||
ecs_set(ecs, b, Position, {20, 30});
|
||||
ecs_set(ecs, c, Position, {30, 40});
|
||||
ecs_set(ecs, d, Position, {40, 50});
|
||||
|
||||
ecs_set(ecs, a, Velocity, {1, 2});
|
||||
ecs_set(ecs, b, Velocity, {2, 3});
|
||||
ecs_set(ecs, c, Velocity, {3, 4});
|
||||
ecs_set(ecs, d, Velocity, {4, 5});
|
||||
|
||||
ecs_set(ecs, c, Mass, {10});
|
||||
ecs_set(ecs, d, Mass, {20});
|
||||
|
||||
// Query for components
|
||||
ecs_query_t *q = ecs_query(ecs, {
|
||||
.filter.terms = {
|
||||
{ .id = ecs_id(Position) }, { .id = ecs_id(Velocity) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_iter_t it = ecs_query_iter(ecs, q);
|
||||
|
||||
// Serialize query to JSON. Customize serializer to only serialize entity
|
||||
// names and component values.
|
||||
ecs_iter_to_json_desc_t desc = {
|
||||
.serialize_entities = true,
|
||||
.serialize_values = true
|
||||
};
|
||||
char *json = ecs_iter_to_json(ecs, &it, &desc);
|
||||
printf("%s\n", json);
|
||||
ecs_os_free(json);
|
||||
|
||||
// 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": 40.00,
|
||||
// "y": 50.00
|
||||
// }],
|
||||
// [{
|
||||
// "x": 3.00,
|
||||
// "y": 4.00
|
||||
// }, {
|
||||
// "x": 4.00,
|
||||
// "y": 5.00
|
||||
// }]
|
||||
// ]
|
||||
// }]
|
||||
// }
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "query_to_json",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
126
engine/libs/flecs/examples/c/reflection/query_to_json/src/main.c
Normal file
126
engine/libs/flecs/examples/c/reflection/query_to_json/src/main.c
Normal file
@@ -0,0 +1,126 @@
|
||||
#include <query_to_json.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
float x, y;
|
||||
} Position, Velocity;
|
||||
|
||||
typedef struct {
|
||||
float value;
|
||||
} Mass;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register component as usual
|
||||
ECS_COMPONENT(ecs, Position);
|
||||
ECS_COMPONENT(ecs, Velocity);
|
||||
ECS_COMPONENT(ecs, Mass);
|
||||
|
||||
// Add reflection data to components
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Position), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) },
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Velocity), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) },
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(Mass), // Make sure to use existing id
|
||||
.members = {
|
||||
{ .name = "value", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_entity_t a = ecs_set_name(ecs, 0, "a");
|
||||
ecs_entity_t b = ecs_set_name(ecs, 0, "b");
|
||||
ecs_entity_t c = ecs_set_name(ecs, 0, "c");
|
||||
ecs_entity_t d = ecs_set_name(ecs, 0, "d");
|
||||
|
||||
ecs_set(ecs, a, Position, {10, 20});
|
||||
ecs_set(ecs, b, Position, {20, 30});
|
||||
ecs_set(ecs, c, Position, {30, 40});
|
||||
ecs_set(ecs, d, Position, {40, 50});
|
||||
|
||||
ecs_set(ecs, a, Velocity, {1, 2});
|
||||
ecs_set(ecs, b, Velocity, {2, 3});
|
||||
ecs_set(ecs, c, Velocity, {3, 4});
|
||||
ecs_set(ecs, d, Velocity, {4, 5});
|
||||
|
||||
ecs_set(ecs, c, Mass, {10});
|
||||
ecs_set(ecs, d, Mass, {20});
|
||||
|
||||
// Query for components
|
||||
ecs_query_t *q = ecs_query(ecs, {
|
||||
.filter.terms = {
|
||||
{ .id = ecs_id(Position) }, { .id = ecs_id(Velocity) }
|
||||
}
|
||||
});
|
||||
|
||||
// Serialize query to JSON. Note that this works for iterators from any
|
||||
// source, including filters & rules.
|
||||
ecs_iter_t it = ecs_query_iter(ecs, q);
|
||||
char *json = ecs_iter_to_json(ecs, &it, NULL);
|
||||
printf("%s\n", json);
|
||||
ecs_os_free(json);
|
||||
|
||||
// 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": 40.00,
|
||||
// "y": 50.00
|
||||
// }],
|
||||
// [{
|
||||
// "x": 3.00,
|
||||
// "y": 4.00
|
||||
// }, {
|
||||
// "x": 4.00,
|
||||
// "y": 5.00
|
||||
// }]
|
||||
// ]
|
||||
// }]
|
||||
// }
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "runtime_component",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <runtime_component.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Create component for a type that isn't known at compile time
|
||||
ecs_entity_t Position = ecs_struct(ecs, {
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) }, // builtin float type
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
// Create entity, set value of position using reflection API
|
||||
ecs_entity_t ent = ecs_new_entity(ecs, "ent");
|
||||
void *ptr = ecs_get_mut_id(ecs, ent, Position);
|
||||
|
||||
ecs_meta_cursor_t cur = ecs_meta_cursor(ecs, Position, ptr);
|
||||
ecs_meta_push(&cur); // {
|
||||
ecs_meta_set_float(&cur, 10); // 10
|
||||
ecs_meta_next(&cur); // ,
|
||||
ecs_meta_set_float(&cur, 20); // 20
|
||||
ecs_meta_pop(&cur); // }
|
||||
|
||||
// Convert component to string
|
||||
char *str = ecs_ptr_to_expr(ecs, Position, ptr);
|
||||
printf("%s\n", str); // {x: 10, y: 20}
|
||||
ecs_os_free(str);
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "runtime_nested_component",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <runtime_nested_component.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Create components for types that aren't known at compile time
|
||||
ecs_entity_t Point = ecs_struct(ecs, {
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f32_t) }, // builtin float type
|
||||
{ .name = "y", .type = ecs_id(ecs_f32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
ecs_entity_t Line = ecs_struct(ecs, {
|
||||
.members = {
|
||||
{ .name = "start", .type = Point },
|
||||
{ .name = "stop", .type = Point }
|
||||
}
|
||||
});
|
||||
|
||||
// Create entity, set value of position using reflection API
|
||||
ecs_entity_t ent = ecs_new_entity(ecs, "ent");
|
||||
void *ptr = ecs_get_mut_id(ecs, ent, Line);
|
||||
|
||||
ecs_meta_cursor_t cur = ecs_meta_cursor(ecs, Line, ptr);
|
||||
ecs_meta_push(&cur); // {
|
||||
ecs_meta_push(&cur); // {
|
||||
ecs_meta_set_float(&cur, 10); // 10
|
||||
ecs_meta_next(&cur); // ,
|
||||
ecs_meta_set_float(&cur, 20); // 20
|
||||
ecs_meta_pop(&cur); // }
|
||||
ecs_meta_next(&cur); // ,
|
||||
ecs_meta_push(&cur); // {
|
||||
ecs_meta_set_float(&cur, 30); // 30
|
||||
ecs_meta_next(&cur); // ,
|
||||
ecs_meta_set_float(&cur, 40); // 40
|
||||
ecs_meta_pop(&cur); // }
|
||||
ecs_meta_pop(&cur); // }
|
||||
|
||||
// Convert component to string
|
||||
char *str = ecs_ptr_to_expr(ecs, Line, ptr);
|
||||
printf("%s\n", str); // {start: {x: 10.00, y: 20.00}, stop: {x: 30.00, y: 40.00}}
|
||||
ecs_os_free(str);
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef SER_OPAQUE_STRING_H
|
||||
#define SER_OPAQUE_STRING_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "ser_opaque_string/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 SER_OPAQUE_STRING_BAKE_CONFIG_H
|
||||
#define SER_OPAQUE_STRING_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "ser_opaque_string",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <ser_opaque_string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Simple string type
|
||||
typedef struct {
|
||||
char *value;
|
||||
} String;
|
||||
|
||||
// Reflection support for String
|
||||
int String_serialize(const ecs_serializer_t *ser, const void *ptr) {
|
||||
const String *data = ptr;
|
||||
return ser->value(ser, ecs_id(ecs_string_t), &data->value);
|
||||
}
|
||||
|
||||
static void String_assign_string(void *ptr, const char *value) {
|
||||
String *data = ptr;
|
||||
|
||||
if (data->value) {
|
||||
ecs_os_free(data->value);
|
||||
}
|
||||
|
||||
data->value = ecs_os_strdup(value);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register type as component so the size is known
|
||||
ECS_COMPONENT(ecs, String);
|
||||
|
||||
// Register String as opaque type. An opaque type is a type of which the
|
||||
// reflection framework doesn't know the layout, but which it can serialize.
|
||||
ecs_opaque(ecs, {
|
||||
// Link reflection with component
|
||||
.entity = ecs_id(String),
|
||||
// Let reflection framework know the type represents a string
|
||||
.type.as_type = ecs_id(ecs_string_t),
|
||||
// Register functions for de/serializing vector contents
|
||||
.type.serialize = String_serialize,
|
||||
.type.assign_string = String_assign_string
|
||||
});
|
||||
|
||||
String v = {0};
|
||||
|
||||
// Deserialize new value into string
|
||||
ecs_ptr_from_json(ecs, ecs_id(String), &v, "\"Hello World\"", NULL);
|
||||
|
||||
// Print string contents
|
||||
printf("%s\n", v.value);
|
||||
|
||||
// Serialize string contents to JSON
|
||||
char *json = ecs_ptr_to_json(ecs, ecs_id(String), &v);
|
||||
printf("%s\n", json);
|
||||
ecs_os_free(json);
|
||||
|
||||
// Free string. If this type were used as a component, we could
|
||||
// register a dtor to do this for us (see the entity/hooks example).
|
||||
ecs_os_free(v.value);
|
||||
|
||||
ecs_fini(ecs);
|
||||
|
||||
// Output
|
||||
// Hello World
|
||||
// "Hello World"
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "ser_opaque_type",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
#include <ser_opaque_type.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Use opaque reflection support to add a computed 'result' member to type
|
||||
typedef struct {
|
||||
int32_t a;
|
||||
int32_t b;
|
||||
} Sum;
|
||||
|
||||
// Reflection support for Sum
|
||||
int Sum_serialize(const ecs_serializer_t *ser, const void *ptr) {
|
||||
const Sum *data = ptr;
|
||||
ser->member(ser, "a");
|
||||
ser->value(ser, ecs_id(ecs_i32_t), &data->a);
|
||||
ser->member(ser, "b");
|
||||
ser->value(ser, ecs_id(ecs_i32_t), &data->b);
|
||||
|
||||
int32_t result = data->a + data->b; // Serialize fake member
|
||||
ser->member(ser, "result");
|
||||
ser->value(ser, ecs_id(ecs_i32_t), &result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Return address for requested member
|
||||
void* Sum_ensure_member(void *ptr, const char *member) {
|
||||
Sum *data = ptr;
|
||||
if (!strcmp(member, "a")) {
|
||||
return &data->a;
|
||||
} else if (!strcmp(member, "b")) {
|
||||
return &data->b;
|
||||
} else {
|
||||
return NULL; // We can't serialize into fake result member
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register type as component so the size is known
|
||||
ECS_COMPONENT(ecs, Sum);
|
||||
|
||||
// Create struct type that describes the structure of Sum
|
||||
ecs_entity_t sum_type_descriptor = ecs_struct(ecs, {
|
||||
.members = {
|
||||
{ .name = "a", .type = ecs_id(ecs_i32_t) },
|
||||
{ .name = "b", .type = ecs_id(ecs_i32_t) },
|
||||
{ .name = "result", .type = ecs_id(ecs_i32_t) }
|
||||
}
|
||||
});
|
||||
|
||||
// Register Sum as opaque type. An opaque type is a type of which the
|
||||
// reflection framework doesn't know the layout, but which it can serialize.
|
||||
ecs_opaque(ecs, {
|
||||
// Link reflection with component
|
||||
.entity = ecs_id(Sum),
|
||||
// Let reflection framework know the type represents a struct
|
||||
.type.as_type = sum_type_descriptor,
|
||||
// Register function for serializing sum contents
|
||||
.type.serialize = Sum_serialize,
|
||||
// Register function for getting access to members
|
||||
.type.ensure_member = Sum_ensure_member
|
||||
});
|
||||
|
||||
Sum v = { .a = 10, .b = 20 };
|
||||
|
||||
// Serialize value of Sum to JSON
|
||||
char *json = ecs_ptr_to_json(ecs, ecs_id(Sum), &v);
|
||||
printf("%s\n", json);
|
||||
ecs_os_free(json);
|
||||
|
||||
// Deserialize new value into Sum
|
||||
ecs_ptr_from_json(ecs, ecs_id(Sum), &v, "{\"a\": 20, \"b\": 22}", NULL);
|
||||
|
||||
// Serialize value again
|
||||
json = ecs_ptr_to_json(ecs, ecs_id(Sum), &v);
|
||||
printf("%s\n", json);
|
||||
ecs_os_free(json);
|
||||
|
||||
// Output
|
||||
// {"a":10, "b":20, "result":30}
|
||||
// {"a":22, "b":20, "result":42}
|
||||
|
||||
ecs_fini(ecs);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef SER_OPAQUE_VECTOR_H
|
||||
#define SER_OPAQUE_VECTOR_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "ser_opaque_vector/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 SER_OPAQUE_VECTOR_BAKE_CONFIG_H
|
||||
#define SER_OPAQUE_VECTOR_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"id": "ser_opaque_vector",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
#include <ser_opaque_vector.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Simple integer vector type
|
||||
typedef struct {
|
||||
size_t count;
|
||||
int32_t *array;
|
||||
} IntVector;
|
||||
|
||||
// Reflection support for IntVector
|
||||
int IntVector_serialize(const ecs_serializer_t *ser, const void *ptr) {
|
||||
const IntVector *data = ptr;
|
||||
for (size_t i = 0; i < data->count; i ++) {
|
||||
ser->value(ser, ecs_id(ecs_i32_t), &data->array[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t IntVector_count(const void *ptr) {
|
||||
const IntVector *data = ptr;
|
||||
return data->count;
|
||||
}
|
||||
|
||||
static void IntVector_resize(void *ptr, size_t size) {
|
||||
IntVector *data = ptr;
|
||||
if (data->count != size) {
|
||||
data->count = size;
|
||||
if (!data->count) {
|
||||
ecs_os_free(data->array);
|
||||
data->array = NULL;
|
||||
} else {
|
||||
data->array = ecs_os_realloc_n(data->array, int32_t, (ecs_size_t)size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void* IntVector_ensure_element(void *ptr, size_t index) {
|
||||
IntVector *data = ptr;
|
||||
if (data->count <= index) {
|
||||
IntVector_resize(ptr, index + 1);
|
||||
}
|
||||
return &data->array[index];
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Register type as component so the size is known
|
||||
ECS_COMPONENT(ecs, IntVector);
|
||||
|
||||
// Register IntVector as opaque type. An opaque type is a type of which the
|
||||
// reflection framework doesn't know the layout, but which it can serialize.
|
||||
ecs_opaque(ecs, {
|
||||
// Link reflection with component
|
||||
.entity = ecs_id(IntVector),
|
||||
.type = {
|
||||
// Let reflection framework know the type represents a vector<i32>
|
||||
.as_type = ecs_vector(ecs, { .type = ecs_id(ecs_i32_t) }),
|
||||
// Register functions for de/serializing vector contents
|
||||
.serialize = IntVector_serialize,
|
||||
.count = IntVector_count,
|
||||
.resize = IntVector_resize,
|
||||
.ensure_element = IntVector_ensure_element
|
||||
}
|
||||
});
|
||||
|
||||
IntVector v = {0};
|
||||
|
||||
// Deserialize new value into vector
|
||||
ecs_ptr_from_json(ecs, ecs_id(IntVector), &v, "[10, 20, 30]", NULL);
|
||||
|
||||
// Print vector contents
|
||||
printf("[");
|
||||
for (size_t i = 0; i < v.count; i ++) {
|
||||
if (i) printf(", ");
|
||||
printf("%d", v.array[i]);
|
||||
}
|
||||
printf("]\n");
|
||||
|
||||
// Serialize vector contents to JSON
|
||||
char *json = ecs_ptr_to_json(ecs, ecs_id(IntVector), &v);
|
||||
printf("%s\n", json);
|
||||
ecs_os_free(json);
|
||||
|
||||
// Free array of vector. If this type were used as a component, we could
|
||||
// register a dtor to do this for us (see the entity/hooks example).
|
||||
ecs_os_free(v.array);
|
||||
|
||||
ecs_fini(ecs);
|
||||
|
||||
// Output
|
||||
// [10, 20, 30]
|
||||
// [10, 20, 30]
|
||||
}
|
||||
5
engine/libs/flecs/examples/c/reflection/units/.gitignore
vendored
Normal file
5
engine/libs/flecs/examples/c/reflection/units/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.bake_cache
|
||||
.DS_Store
|
||||
.vscode
|
||||
gcov
|
||||
bin
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"id": "units",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
]
|
||||
}
|
||||
}
|
||||
71
engine/libs/flecs/examples/c/reflection/units/src/main.c
Normal file
71
engine/libs/flecs/examples/c/reflection/units/src/main.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <units.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
double temperature;
|
||||
double pressure;
|
||||
double precipitation;
|
||||
} WeatherStation;
|
||||
|
||||
void print_value(ecs_world_t *world, const ecs_meta_cursor_t *cur) {
|
||||
// Get unit entity and component
|
||||
ecs_entity_t u = ecs_meta_get_unit(cur);
|
||||
const EcsUnit *u_data = ecs_get(world, u, EcsUnit);
|
||||
|
||||
// Print value with unit symbol
|
||||
printf("%s: %.1f %s\n", ecs_meta_get_member(cur), ecs_meta_get_float(cur),
|
||||
u_data->symbol);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *ecs = ecs_init_w_args(argc, argv);
|
||||
|
||||
// Import units module.
|
||||
ECS_IMPORT(ecs, FlecsUnits);
|
||||
|
||||
ECS_COMPONENT(ecs, WeatherStation);
|
||||
|
||||
// Register reflection data with units. This can improve the way information
|
||||
// is visualized in tools, such as the explorer.
|
||||
ecs_struct(ecs, {
|
||||
.entity = ecs_id(WeatherStation),
|
||||
.members = {
|
||||
{
|
||||
.name = "temperature",
|
||||
.type = ecs_id(ecs_f64_t),
|
||||
.unit = EcsCelsius
|
||||
},
|
||||
{
|
||||
.name = "pressure",
|
||||
.type = ecs_id(ecs_f64_t),
|
||||
.unit = EcsBar
|
||||
},
|
||||
{
|
||||
.name = "precipitation",
|
||||
.type = ecs_id(ecs_f64_t),
|
||||
.unit = EcsMilliMeters
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
ecs_entity_t e = ecs_set(ecs, 0, WeatherStation, {24, 1.2, 0.5});
|
||||
|
||||
// Use cursor API to print values with units
|
||||
WeatherStation *ptr = ecs_get_mut(ecs, e, WeatherStation);
|
||||
ecs_meta_cursor_t cur = ecs_meta_cursor(ecs, ecs_id(WeatherStation), ptr);
|
||||
|
||||
ecs_meta_push(&cur);
|
||||
print_value(ecs, &cur);
|
||||
ecs_meta_next(&cur);
|
||||
print_value(ecs, &cur);
|
||||
ecs_meta_next(&cur);
|
||||
print_value(ecs, &cur);
|
||||
ecs_meta_pop(&cur);
|
||||
|
||||
return ecs_fini(ecs);
|
||||
|
||||
// Output:
|
||||
// temperature: 24.0 °C
|
||||
// pressure: 1.2 bar
|
||||
// precipitation: 0.5 mm
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "world_ser_deser",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
#include <world_ser_deser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
double x, y;
|
||||
} Position, Velocity;
|
||||
|
||||
ECS_COMPONENT_DECLARE(Position);
|
||||
ECS_COMPONENT_DECLARE(Velocity);
|
||||
|
||||
void Move(ecs_iter_t *it) {
|
||||
Position *p = ecs_field(it, Position, 1);
|
||||
const Velocity *v = ecs_field(it, Velocity, 2);
|
||||
|
||||
for (int i = 0; i < it->count; i ++) {
|
||||
p[i].x += v[i].x;
|
||||
p[i].y += v[i].y;
|
||||
printf("%s: {%f, %f}\n", ecs_get_name(it->world, it->entities[i]),
|
||||
p[i].x, p[i].y);
|
||||
}
|
||||
}
|
||||
|
||||
// Register components and systems in a module. This excludes them by default
|
||||
// from the serialized data, and makes it easier to import across worlds.
|
||||
void MoveModuleImport(ecs_world_t *world) {
|
||||
ECS_MODULE(world, MoveModule);
|
||||
|
||||
ECS_COMPONENT_DEFINE(world, Position);
|
||||
ECS_COMPONENT_DEFINE(world, Velocity);
|
||||
|
||||
ecs_struct(world, {
|
||||
.entity = ecs_id(Position),
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f64_t) },
|
||||
{ .name = "y", .type = ecs_id(ecs_f64_t) },
|
||||
}
|
||||
});
|
||||
|
||||
ecs_struct(world, {
|
||||
.entity = ecs_id(Velocity),
|
||||
.members = {
|
||||
{ .name = "x", .type = ecs_id(ecs_f64_t) },
|
||||
{ .name = "y", .type = ecs_id(ecs_f64_t) },
|
||||
}
|
||||
});
|
||||
|
||||
ECS_SYSTEM(world, Move, EcsOnUpdate, Position, [in] Velocity);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ecs_world_t *world_a = ecs_init_w_args(argc, argv); {
|
||||
ECS_IMPORT(world_a, MoveModule); // put in a scope so variable doens't overlap
|
||||
}
|
||||
|
||||
ecs_entity_t ent_1 = ecs_new_entity(world_a, "ent_1");
|
||||
ecs_entity_t ent_2 = ecs_new_entity(world_a, "ent_2");
|
||||
ecs_set(world_a, ent_1, Position, {10, 20});
|
||||
ecs_set(world_a, ent_2, Position, {30, 40});
|
||||
ecs_set(world_a, ent_1, Velocity, {1, -1});
|
||||
ecs_set(world_a, ent_2, Velocity, {-1, 1});
|
||||
|
||||
// Serialize world to JSON
|
||||
char *json = ecs_world_to_json(world_a, NULL);
|
||||
printf("%s\n\n", json);
|
||||
|
||||
// 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
|
||||
ecs_world_t *world_b = ecs_init(); {
|
||||
ECS_IMPORT(world_b, MoveModule);
|
||||
}
|
||||
|
||||
// Deserialize JSON into second world
|
||||
ecs_world_from_json(world_b, json, NULL);
|
||||
|
||||
// Run system once for both worlds
|
||||
ecs_progress(world_a, 0);
|
||||
printf("\n");
|
||||
ecs_progress(world_b, 0);
|
||||
|
||||
// Output
|
||||
// ent_1: {11.000000, 19.000000}
|
||||
// ent_2: {29.000000, 41.000000}
|
||||
//
|
||||
// ent_1: {11.000000, 19.000000}
|
||||
// ent_2: {29.000000, 41.000000}
|
||||
|
||||
ecs_fini(world_a);
|
||||
ecs_fini(world_b);
|
||||
|
||||
ecs_os_free(json);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user