code: update flecs & rework vehicle enter/leave states

isolation_bkp/dynres
Dominik Madarász 2021-08-11 10:45:23 +02:00
parent 275cdc65ab
commit 9b829074d4
11 changed files with 23307 additions and 13716 deletions

View File

@ -19,8 +19,7 @@ uint64_t player_spawn(char *name) {
name = zpl_bprintf("player_%d", e);
}
ecs_set(world_ecs(), e, EcsName, {.alloc_value = name });
ecs_add(world_ecs(), e, EcsClient);
ecs_set_name(world_ecs(), e, name);
ecs_set(world_ecs(), e, ClientInfo, {0});
ecs_set(world_ecs(), e, Input, {0});
ecs_set(world_ecs(), e, Health, {.hp = PLAYER_MAX_HP, .max_hp = PLAYER_MAX_HP});

View File

@ -10,14 +10,13 @@ ECS_COMPONENT_DECLARE(ClientInfo);
ECS_COMPONENT_DECLARE(Health);
ECS_COMPONENT_DECLARE(Classify);
ECS_COMPONENT_DECLARE(Vehicle);
ECS_COMPONENT_DECLARE(IsInVehicle);
ECS_TAG_DECLARE(EcsActor);
ECS_TAG_DECLARE(EcsDemoNPC);
ECS_TAG_DECLARE(EcsInVehicle);
ECS_TYPE_DECLARE(Player);
ECS_TYPE_DECLARE(Movement);
ECS_TYPE_DECLARE(Walking);
ECS_TYPE_DECLARE(Flying);
ECS_TYPE_DECLARE(EcsClient);
void ComponentsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Components);
@ -34,13 +33,13 @@ void ComponentsImport(ecs_world_t *ecs) {
ECS_META_DEFINE(ecs, Classify);
ECS_META_DEFINE(ecs, Vehicle);
ECS_COMPONENT_DEFINE(ecs, IsInVehicle);
ECS_TAG_DEFINE(ecs, Walking);
ECS_TAG_DEFINE(ecs, Flying);
ECS_TAG_DEFINE(ecs, EcsClient);
ECS_TAG_DEFINE(ecs, EcsActor);
ECS_TAG_DEFINE(ecs, EcsDemoNPC);
ECS_TAG_DEFINE(ecs, EcsInVehicle);
ECS_PREFAB(ecs, Base, Position, Velocity, Input, EcsActor);
ECS_TYPE_DEFINE(ecs, Movement, Walking, Flying);
@ -55,11 +54,10 @@ void ComponentsImport(ecs_world_t *ecs) {
ECS_SET_COMPONENT(Health);
ECS_SET_COMPONENT(Classify);
ECS_SET_COMPONENT(Vehicle);
ECS_SET_ENTITY(EcsClient);
ECS_SET_COMPONENT(IsInVehicle);
ECS_SET_ENTITY(Walking);
ECS_SET_ENTITY(Flying);
ECS_SET_ENTITY(EcsActor);
ECS_SET_ENTITY(EcsDemoNPC);
ECS_SET_ENTITY(EcsInVehicle);
ECS_SET_TYPE(Movement);
}

View File

@ -41,8 +41,6 @@ ECS_STRUCT(Input, {
uint8_t use;
uint8_t sprint;
uint8_t is_blocked;
ecs_entity_t parent;
});
ECS_STRUCT(ClientInfo, {
@ -72,6 +70,10 @@ ECS_STRUCT(Vehicle, {
float wheel_base;
});
typedef struct {
ecs_entity_t veh;
} IsInVehicle;
ECS_COMPONENT_EXTERN(Chunk);
ECS_COMPONENT_EXTERN(Position);
ECS_COMPONENT_EXTERN(Vector2D);
@ -82,9 +84,9 @@ ECS_COMPONENT_EXTERN(ClientInfo);
ECS_COMPONENT_EXTERN(Health);
ECS_COMPONENT_EXTERN(Classify);
ECS_COMPONENT_EXTERN(Vehicle);
ECS_COMPONENT_EXTERN(IsInVehicle);
ECS_TAG_EXTERN(EcsActor);
ECS_TAG_EXTERN(EcsDemoNPC);
ECS_TAG_EXTERN(EcsInVehicle);
ECS_TYPE_EXTERN(Player);
ECS_TYPE_EXTERN(Movement);
ECS_TYPE_EXTERN(Walking);
@ -102,15 +104,14 @@ typedef struct {
ECS_DECLARE_COMPONENT(Health);
ECS_DECLARE_COMPONENT(Classify);
ECS_DECLARE_COMPONENT(Vehicle);
ECS_DECLARE_COMPONENT(IsInVehicle);
ECS_DECLARE_ENTITY(EcsActor);
ECS_DECLARE_ENTITY(EcsDemoNPC);
ECS_DECLARE_ENTITY(EcsInVehicle);
ECS_DECLARE_TYPE(Player);
ECS_DECLARE_TYPE(Builder);
ECS_DECLARE_TYPE(Movement);
ECS_DECLARE_ENTITY(Walking);
ECS_DECLARE_ENTITY(Flying);
ECS_DECLARE_ENTITY(EcsClient);
} Components;
#define ComponentsImportHandles(handles)\
@ -124,6 +125,7 @@ ECS_IMPORT_COMPONENT(handles, ClientInfo);\
ECS_IMPORT_COMPONENT(handles, Health);\
ECS_IMPORT_COMPONENT(handles, Classify);\
ECS_IMPORT_COMPONENT(handles, Vehicle);\
ECS_IMPORT_COMPONENT(handles, IsInVehicle);\
ECS_IMPORT_TYPE(handles, Player);\
ECS_IMPORT_TYPE(handles, Builder);\
ECS_IMPORT_TYPE(handles, Movement);\
@ -131,7 +133,5 @@ ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_ENTITY(handles, Walking);\
ECS_IMPORT_ENTITY(handles, Flying);\
ECS_IMPORT_ENTITY(handles, EcsClient);\
ECS_IMPORT_ENTITY(handles, EcsInVehicle);\
void ComponentsImport(ecs_world_t *ecs);

View File

@ -102,10 +102,11 @@ void RegenerateHP(ecs_iter_t *it) {
void SystemsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Systems);
ECS_SYSTEM(ecs, MovementImpulse, EcsOnLoad, components.Input, components.Velocity);
ECS_SYSTEM(ecs, MovementImpulse, EcsOnLoad, components.Input, components.Velocity, !components.IsInVehicle);
//ECS_SYSTEM(ecs, DemoPlaceIceBlock, EcsOnLoad, components.Input, components.Position);
ECS_SYSTEM(ecs, DemoNPCMoveAround, EcsOnLoad, components.Velocity, components.EcsDemoNPC);
ECS_SYSTEM(ecs, EnterOrLeaveVehicle, EcsOnLoad, components.Input, components.Position);
ECS_SYSTEM(ecs, EnterVehicle, EcsOnLoad, components.Input, components.Position, !components.IsInVehicle);
ECS_SYSTEM(ecs, LeaveVehicle, EcsOnLoad, components.Input, components.IsInVehicle);
ECS_SYSTEM(ecs, MoveWalk, EcsOnUpdate, components.Position, components.Velocity);
ECS_SYSTEM(ecs, HurtOnHazardBlock, EcsOnUpdate, components.Position, components.Health);

View File

@ -19,7 +19,6 @@ void MovementImpulse(ecs_iter_t *it) {
Velocity *v = ecs_column(it, Velocity, 2);
for (int i = 0; i < it->count; i++) {
if (world_entity_valid(in[i].parent)) continue;
double speed = PLR_MOVE_SPEED * (in[i].sprint ? PLR_MOVE_SPEED_MULT : 1.0);
if (zpl_abs(v[i].x) < speed && in[i].x)
v[i].x = in[i].x*speed;

View File

@ -1,7 +1,31 @@
#define VEH_ENTER_RADIUS 45.0f
void EnterOrLeaveVehicle(ecs_iter_t *it) {
void LeaveVehicle(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1);
IsInVehicle *vehp = ecs_column(it, IsInVehicle, 2);
for (int i = 0; i < it->count; i++) {
if (!in[i].use) continue;
in[i].use = false;
Vehicle *veh = 0;
if ((veh = ecs_get_mut_if(it->world, vehp->veh, Vehicle))) {
for (int k = 0; k < 4; k++) {
if (veh->seats[k] == it->entities[i]) {
veh->seats[k] = 0;
break;
}
}
ecs_remove(it->world, it->entities[i], IsInVehicle);
} else {
ZPL_PANIC("unreachable code");
}
}
}
void EnterVehicle(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1);
Position *p = ecs_column(it, Position, 2);
@ -9,44 +33,30 @@ void EnterOrLeaveVehicle(ecs_iter_t *it) {
if (!in[i].use) continue;
in[i].use = false;
if (!world_entity_valid(in[i].parent)) {
size_t ents_count;
int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 2);
for (size_t j = 0; j < ents_count; j++) {
Vehicle *veh = 0;
if ((veh = ecs_get_mut_if(world_ecs(), ents[j], Vehicle))) {
Position const* p2 = ecs_get(world_ecs(), ents[j], Position);
float dx = p2->x - p[i].x;
float dy = p2->y - p[i].y;
float range = zpl_sqrt(dx*dx + dy*dy);
if (range <= VEH_ENTER_RADIUS) {
for (int k = 0; k < 4; k++) {
if (veh->seats[k] != 0) continue;
// NOTE(zaklaus): We can enter the vehicle, yay!
veh->seats[k] = it->entities[i];
in[i].parent = ents[j];
p[i] = *p2;
break;
}
}
}
}
} else {
size_t ents_count;
int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 2);
for (size_t j = 0; j < ents_count; j++) {
Vehicle *veh = 0;
if ((veh = ecs_get_mut_if(world_ecs(), in[i].parent, Vehicle))) {
for (int k = 0; k < 4; k++) {
if (veh->seats[k] == it->entities[i]) {
veh->seats[k] = 0;
if ((veh = ecs_get_mut_if(it->world, ents[j], Vehicle))) {
Position const* p2 = ecs_get(it->world, ents[j], Position);
float dx = p2->x - p[i].x;
float dy = p2->y - p[i].y;
float range = zpl_sqrt(dx*dx + dy*dy);
if (range <= VEH_ENTER_RADIUS) {
for (int k = 0; k < 4; k++) {
if (veh->seats[k] != 0) continue;
// NOTE(zaklaus): We can enter the vehicle, yay!
veh->seats[k] = it->entities[i];
ecs_set(it->world, it->entities[i], IsInVehicle, {
.veh = ents[j]
});
p[i] = *p2;
break;
}
}
in[i].parent = 0;
} else {
ZPL_PANIC("unreachable code");
}
}
}
@ -81,15 +91,15 @@ void VehicleHandling(ecs_iter_t *it) {
// NOTE(zaklaus): Update passenger position
{
Position *p2 = ecs_get_mut(world_ecs(), pe, Position, NULL);
Velocity *v2 = ecs_get_mut(world_ecs(), pe, Velocity, NULL);
Position *p2 = ecs_get_mut(it->world, pe, Position, NULL);
Velocity *v2 = ecs_get_mut(it->world, pe, Velocity, NULL);
*p2 = p[i];
*v2 = v[i];
}
// NOTE(zaklaus): Handle driver input
if (j == 0) {
Input const* in = ecs_get(world_ecs(), pe, Input);
Input const* in = ecs_get(it->world, pe, Input);
car->force += zpl_lerp(0.0f, in->y * VEHICLE_FORCE, VEHICLE_ACCEL);
car->steer += in->x * -VEHICLE_STEER;

View File

@ -2,16 +2,9 @@ use_cxx11()
file(GLOB SRCS *.h
flecs.c
flecs_json.c
flecs_meta.c
flecs_rest.c
flecs_player.c
flecs_rest.c
flecs_systems_civetweb.c
flecs_dash.c
flecs_monitor.c
flecs-os_api-stdcpp.cpp
flecs_components_http.c
)
add_library(flecs-bundle STATIC ${SRCS})

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -60,6 +60,35 @@ void ecs_meta_parse_params(
ecs_meta_params_t *token_out,
ecs_meta_parse_ctx_t *ctx);
#endif
#ifndef FLECS_META_SERIALIZER_H
#define FLECS_META_SERIALIZER_H
void EcsAddStruct(
ecs_iter_t *it);
void EcsSetPrimitive(
ecs_iter_t *it);
void EcsSetEnum(
ecs_iter_t *it);
void EcsSetBitmask(
ecs_iter_t *it);
void EcsSetStruct(
ecs_iter_t *it);
void EcsSetArray(
ecs_iter_t *it);
void EcsSetVector(
ecs_iter_t *it);
void EcsSetMap(
ecs_iter_t *it);
#endif
static
@ -67,8 +96,7 @@ ecs_vector_t* serialize_type(
ecs_world_t *world,
ecs_entity_t entity,
ecs_vector_t *ops,
int32_t offset,
FlecsMeta *module);
int32_t offset);
static
ecs_size_t ecs_get_primitive_size(
@ -129,12 +157,10 @@ ecs_vector_t* serialize_primitive(
ecs_world_t *world,
ecs_entity_t entity,
const EcsPrimitive *type,
ecs_vector_t *ops,
FlecsMeta *module)
ecs_vector_t *ops)
{
(void)world;
(void)entity;
(void)module;
ecs_type_op_t *op;
if (!ops) {
@ -164,13 +190,10 @@ ecs_vector_t* serialize_enum(
ecs_world_t *world,
ecs_entity_t entity,
const EcsEnum *type,
ecs_vector_t *ops,
FlecsMeta *module)
ecs_vector_t *ops)
{
(void)type;
FlecsMetaImportHandles(*module);
ecs_type_op_t *op;
if (!ops) {
op = ecs_vector_add(&ops, ecs_type_op_t);
@ -202,13 +225,10 @@ ecs_vector_t* serialize_bitmask(
ecs_world_t *world,
ecs_entity_t entity,
const EcsBitmask *type,
ecs_vector_t *ops,
FlecsMeta *module)
ecs_vector_t *ops)
{
(void)type;
FlecsMetaImportHandles(*module);
ecs_type_op_t *op;
if (!ops) {
op = ecs_vector_add(&ops, ecs_type_op_t);
@ -241,11 +261,8 @@ ecs_vector_t* serialize_struct(
ecs_entity_t entity,
const EcsStruct *type,
ecs_vector_t *ops,
int32_t offset,
FlecsMeta *module)
int32_t offset)
{
FlecsMetaImportHandles(*module);
ecs_type_op_t *op_header = NULL;
if (!ops) {
op_header = ecs_vector_add(&ops, ecs_type_op_t);
@ -267,7 +284,7 @@ ecs_vector_t* serialize_struct(
for (i = 0; i < count; i ++) {
/* Add type operations of member to struct ops */
int32_t prev_count = ecs_vector_count(ops);
ops = serialize_type(world, members[i].type, ops, offset + size, module);
ops = serialize_type(world, members[i].type, ops, offset + size);
#ifndef NDEBUG
int32_t op_count = ecs_vector_count(ops);
@ -281,7 +298,7 @@ ecs_vector_t* serialize_struct(
op->name = members[i].name;
const EcsMetaType *meta_type = ecs_get(world, members[i].type, EcsMetaType);
ecs_size_t member_size = meta_type->size * op->count;
ecs_size_t member_size = meta_type->size * (meta_type->kind == EcsStructType ? 1 : op->count);
int16_t member_alignment = meta_type->alignment;
ecs_assert(member_size != 0, ECS_INTERNAL_ERROR, op->name);
@ -367,7 +384,7 @@ ecs_vector_t* serialize_struct(
ecs_assert(op_push->kind == EcsOpPush, ECS_INTERNAL_ERROR, NULL);
op_push->size = size;
op_push->alignment = alignment;
op_push->count = 1;
op_push->count = count;
return ops;
}
@ -377,13 +394,10 @@ ecs_vector_t* serialize_array(
ecs_world_t *world,
ecs_entity_t entity,
const EcsArray *type,
ecs_vector_t *ops,
FlecsMeta *handles)
ecs_vector_t *ops)
{
(void)entity;
FlecsMetaImportHandles(*handles);
ecs_type_op_t *op_header = NULL;
if (!ops) {
op_header = ecs_vector_add(&ops, ecs_type_op_t);
@ -428,13 +442,10 @@ ecs_vector_t* serialize_vector(
ecs_world_t *world,
ecs_entity_t entity,
const EcsVector *type,
ecs_vector_t *ops,
FlecsMeta *handles)
ecs_vector_t *ops)
{
(void)entity;
FlecsMetaImportHandles(*handles);
ecs_type_op_t *op = NULL;
if (!ops) {
op = ecs_vector_add(&ops, ecs_type_op_t);
@ -468,11 +479,8 @@ ecs_vector_t* serialize_map(
ecs_world_t *world,
ecs_entity_t entity,
const EcsMap *type,
ecs_vector_t *ops,
FlecsMeta *handles)
ecs_vector_t *ops)
{
FlecsMetaImportHandles(*handles);
ecs_type_op_t *op = NULL;
if (!ops) {
op = ecs_vector_add(&ops, ecs_type_op_t);
@ -545,11 +553,8 @@ ecs_vector_t* serialize_type(
ecs_world_t *world,
ecs_entity_t entity,
ecs_vector_t *ops,
int32_t offset,
FlecsMeta *module)
int32_t offset)
{
FlecsMetaImportHandles(*module);
const EcsMetaType *type = ecs_get(world, entity, EcsMetaType);
ecs_assert(type != NULL, ECS_INVALID_PARAMETER, NULL);
@ -557,43 +562,43 @@ ecs_vector_t* serialize_type(
case EcsPrimitiveType: {
const EcsPrimitive *t = ecs_get(world, entity, EcsPrimitive);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_primitive(world, entity, t, ops, module);
return serialize_primitive(world, entity, t, ops);
}
case EcsEnumType: {
const EcsEnum *t = ecs_get(world, entity, EcsEnum);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_enum(world, entity, t, ops, module);
return serialize_enum(world, entity, t, ops);
}
case EcsBitmaskType: {
const EcsBitmask *t = ecs_get(world, entity, EcsBitmask);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_bitmask(world, entity, t, ops, module);
return serialize_bitmask(world, entity, t, ops);
}
case EcsStructType: {
const EcsStruct *t = ecs_get(world, entity, EcsStruct);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_struct(world, entity, t, ops, offset, module);
return serialize_struct(world, entity, t, ops, offset);
}
case EcsArrayType: {
const EcsArray *t = ecs_get(world, entity, EcsArray);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_array(world, entity, t, ops, module);
return serialize_array(world, entity, t, ops);
}
case EcsVectorType: {
const EcsVector *t = ecs_get(world, entity, EcsVector);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_vector(world, entity, t, ops, module);
return serialize_vector(world, entity, t, ops);
}
case EcsMapType: {
const EcsMap *t = ecs_get(world, entity, EcsMap);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_map(world, entity, t, ops, module);
return serialize_map(world, entity, t, ops);
}
default:
@ -605,7 +610,6 @@ ecs_vector_t* serialize_type(
void EcsSetPrimitive(ecs_iter_t *it) {
EcsPrimitive *type = ecs_column(it, EcsPrimitive, 1);
ECS_IMPORT_COLUMN(it, FlecsMeta, 2);
ecs_world_t *world = it->world;
@ -625,14 +629,13 @@ void EcsSetPrimitive(ecs_iter_t *it) {
ecs_set(world, e, EcsMetaTypeSerializer, {
serialize_primitive(
world, e, &type[i], NULL, &ecs_module(FlecsMeta))
world, e, &type[i], NULL)
});
}
}
void EcsSetEnum(ecs_iter_t *it) {
EcsEnum *type = ecs_column(it, EcsEnum, 1);
ECS_IMPORT_COLUMN(it, FlecsMeta, 2);
ecs_world_t *world = it->world;
@ -641,14 +644,13 @@ void EcsSetEnum(ecs_iter_t *it) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_enum(world, e, &type[i], NULL, &ecs_module(FlecsMeta))
serialize_enum(world, e, &type[i], NULL)
});
}
}
void EcsSetBitmask(ecs_iter_t *it) {
EcsBitmask *type = ecs_column(it, EcsBitmask, 1);
ECS_IMPORT_COLUMN(it, FlecsMeta, 2);
ecs_world_t *world = it->world;
@ -656,14 +658,13 @@ void EcsSetBitmask(ecs_iter_t *it) {
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_bitmask(world, e, &type[i], NULL, &ecs_module(FlecsMeta))
serialize_bitmask(world, e, &type[i], NULL)
});
}
}
void EcsSetStruct(ecs_iter_t *it) {
EcsStruct *type = ecs_column(it, EcsStruct, 1);
ECS_IMPORT_COLUMN(it, FlecsMeta, 2);
ecs_world_t *world = it->world;
@ -671,14 +672,13 @@ void EcsSetStruct(ecs_iter_t *it) {
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_struct(world, e, &type[i], NULL, 0, &ecs_module(FlecsMeta))
serialize_struct(world, e, &type[i], NULL, 0)
});
}
}
void EcsSetArray(ecs_iter_t *it) {
EcsArray *type = ecs_column(it, EcsArray, 1);
ECS_IMPORT_COLUMN(it, FlecsMeta, 2);
ecs_world_t *world = it->world;
@ -686,14 +686,13 @@ void EcsSetArray(ecs_iter_t *it) {
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_array(world, e, &type[i], NULL, &ecs_module(FlecsMeta))
serialize_array(world, e, &type[i], NULL)
});
}
}
void EcsSetVector(ecs_iter_t *it) {
EcsVector *type = ecs_column(it, EcsVector, 1);
ECS_IMPORT_COLUMN(it, FlecsMeta, 2);
ecs_world_t *world = it->world;
@ -701,14 +700,13 @@ void EcsSetVector(ecs_iter_t *it) {
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_vector(world, e, &type[i], NULL, &ecs_module(FlecsMeta))
serialize_vector(world, e, &type[i], NULL)
});
}
}
void EcsSetMap(ecs_iter_t *it) {
EcsMap *type = ecs_column(it, EcsMap, 1);
ECS_IMPORT_COLUMN(it, FlecsMeta, 2);
ecs_world_t *world = it->world;
@ -716,7 +714,7 @@ void EcsSetMap(ecs_iter_t *it) {
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_map(world, e, &type[i], NULL, &ecs_module(FlecsMeta))
serialize_map(world, e, &type[i], NULL)
});
}
}
@ -935,7 +933,7 @@ void str_ser_primitive(
ecs_strbuf_append(str, "%lld", *(int64_t*)base);
break;
case EcsF32:
ecs_strbuf_append(str, "%f", *(float*)base);
ecs_strbuf_append(str, "%f", (double)*(float*)base);
break;
case EcsF64:
ecs_strbuf_append(str, "%f", *(double*)base);
@ -969,12 +967,13 @@ void str_ser_primitive(
}
case EcsEntity: {
ecs_entity_t e = *(ecs_entity_t*)base;
const char *name = ecs_get_name(world, e);
if (name) {
const char *name;
if (e && (name = ecs_get_name(world, e))) {
ecs_strbuf_appendstr(str, name);
} else {
ecs_strbuf_append(str, "%u", e);
}
break;
}
}
@ -1263,7 +1262,6 @@ char* ecs_ptr_to_str(
ecs_entity_t type,
void* ptr)
{
ecs_entity_t ecs_entity(EcsMetaTypeSerializer) = ecs_lookup_fullpath(world, "flecs.meta.MetaTypeSerializer");
const EcsMetaTypeSerializer *ser = ecs_get(world, type, EcsMetaTypeSerializer);
ecs_assert(ser != NULL, ECS_INVALID_PARAMETER, NULL);
@ -1283,7 +1281,6 @@ char* ecs_entity_to_str(
ecs_entity_t *ids = (ecs_entity_t*)ecs_vector_first(type, ecs_entity_t);
int32_t count = ecs_vector_count(type);
ecs_entity_t ecs_entity(EcsMetaTypeSerializer) = ecs_lookup_fullpath(world, "flecs.meta.MetaTypeSerializer");
ecs_strbuf_t str = ECS_STRBUF_INIT;
const char *name = ecs_get_name(world, entity);
@ -1315,35 +1312,6 @@ error:
ecs_strbuf_reset(&str);
return NULL;
}
#ifndef FLECS_META_SERIALIZER_H
#define FLECS_META_SERIALIZER_H
void EcsAddStruct(
ecs_iter_t *it);
void EcsSetPrimitive(
ecs_iter_t *it);
void EcsSetEnum(
ecs_iter_t *it);
void EcsSetBitmask(
ecs_iter_t *it);
void EcsSetStruct(
ecs_iter_t *it);
void EcsSetArray(
ecs_iter_t *it);
void EcsSetVector(
ecs_iter_t *it);
void EcsSetMap(
ecs_iter_t *it);
#endif
#ifndef FLECS_META_TYPE_H
#define FLECS_META_TYPE_H
@ -1366,6 +1334,12 @@ ecs_entity_t ecs_meta_lookup_map(
const char *params_decl,
ecs_meta_parse_ctx_t *ctx);
ecs_entity_t ecs_meta_lookup_bitmask(
ecs_world_t *world,
ecs_entity_t e,
const char *params_decl,
ecs_meta_parse_ctx_t *ctx);
ecs_entity_t ecs_meta_lookup(
ecs_world_t *world,
ecs_meta_type_t *token,
@ -1375,18 +1349,31 @@ ecs_entity_t ecs_meta_lookup(
#endif
ECS_CTOR(EcsMetaType, ptr, {
ECS_COMPONENT_DECLARE(EcsPrimitive);
ECS_COMPONENT_DECLARE(EcsEnum);
ECS_COMPONENT_DECLARE(EcsBitmask);
ECS_COMPONENT_DECLARE(EcsMember);
ECS_COMPONENT_DECLARE(EcsStruct);
ECS_COMPONENT_DECLARE(EcsArray);
ECS_COMPONENT_DECLARE(EcsVector);
ECS_COMPONENT_DECLARE(EcsMap);
ECS_COMPONENT_DECLARE(EcsMetaType);
ECS_COMPONENT_DECLARE(ecs_type_op_kind_t);
ECS_COMPONENT_DECLARE(ecs_type_op_t);
ECS_COMPONENT_DECLARE(EcsMetaTypeSerializer);
static ECS_CTOR(EcsMetaType, ptr, {
ptr->descriptor = NULL;
ptr->alias = NULL;
})
ECS_DTOR(EcsMetaType, ptr, {
static ECS_DTOR(EcsMetaType, ptr, {
ecs_os_free((char*)ptr->descriptor);
ptr->descriptor = NULL;
ptr->alias = NULL;
})
ECS_COPY(EcsMetaType, dst, src, {
static ECS_COPY(EcsMetaType, dst, src, {
if (dst->descriptor) {
ecs_os_free((char*)dst->descriptor);
dst->descriptor = NULL;
@ -1404,55 +1391,45 @@ ECS_COPY(EcsMetaType, dst, src, {
dst->alias = src->alias;
})
ECS_MOVE(EcsMetaType, dst, src, {
dst->kind = src->kind;
dst->size = src->size;
dst->alignment = src->alignment;
dst->alias = src->alias;
src->descriptor = NULL;
src->alias = NULL;
})
ECS_CTOR(EcsStruct, ptr, {
static ECS_CTOR(EcsStruct, ptr, {
ptr->members = NULL;
ptr->is_partial = false;
})
ECS_DTOR(EcsStruct, ptr, {
static ECS_DTOR(EcsStruct, ptr, {
ecs_vector_each(ptr->members, EcsMember, m, {
ecs_os_free(m->name);
});
ecs_vector_free(ptr->members);
})
ECS_CTOR(EcsEnum, ptr, {
static ECS_CTOR(EcsEnum, ptr, {
ptr->constants = NULL;
})
ECS_DTOR(EcsEnum, ptr, {
static ECS_DTOR(EcsEnum, ptr, {
ecs_map_each(ptr->constants, char*, key, c_ptr, {
ecs_os_free(*c_ptr);
})
ecs_map_free(ptr->constants);
})
ECS_CTOR(EcsBitmask, ptr, {
static ECS_CTOR(EcsBitmask, ptr, {
ptr->constants = NULL;
})
ECS_DTOR(EcsBitmask, ptr, {
static ECS_DTOR(EcsBitmask, ptr, {
ecs_map_each(ptr->constants, char*, key, c_ptr, {
ecs_os_free(*c_ptr);
})
ecs_map_free(ptr->constants);
})
ECS_CTOR(EcsMetaTypeSerializer, ptr, {
static ECS_CTOR(EcsMetaTypeSerializer, ptr, {
ptr->ops = NULL;
})
ECS_DTOR(EcsMetaTypeSerializer, ptr, {
static ECS_DTOR(EcsMetaTypeSerializer, ptr, {
ecs_vector_free(ptr->ops);
})
@ -1466,9 +1443,6 @@ void ecs_set_primitive(
ecs_assert(e != 0, ECS_INTERNAL_ERROR, NULL);
ecs_assert(type != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_entity_t ecs_entity(EcsPrimitive) = ecs_lookup_fullpath(world, "flecs.meta.Primitive");
ecs_assert(ecs_entity(EcsPrimitive) != 0, ECS_INTERNAL_ERROR, NULL);
const char *descr = type->descriptor;
if (!strcmp(descr, "bool")) {
@ -1513,7 +1487,7 @@ void ecs_set_primitive(
if (!strcmp(descr, "uptr")) {
ecs_set(world, e, EcsPrimitive, {EcsUPtr});
} else
if (!strcmp(descr, "string")) {
if (!strcmp(descr, "string_t")) {
ecs_set(world, e, EcsPrimitive, {EcsString});
} else
if (!strcmp(descr, "entity")) {
@ -1570,9 +1544,7 @@ void ecs_set_bitmask(
ecs_entity_t e,
EcsMetaType *type)
{
ecs_entity_t comp = ecs_lookup_fullpath(world, "flecs.meta.Bitmask");
ecs_assert(comp != 0, ECS_INTERNAL_ERROR, NULL);
ecs_set_constants(world, e, comp, true, type);
ecs_set_constants(world, e, ecs_id(EcsBitmask), true, type);
}
static
@ -1581,9 +1553,7 @@ void ecs_set_enum(
ecs_entity_t e,
EcsMetaType *type)
{
ecs_entity_t comp = ecs_lookup_fullpath(world, "flecs.meta.Enum");
ecs_assert(comp != 0, ECS_INTERNAL_ERROR, NULL);
ecs_set_constants(world, e, comp, false, type);
ecs_set_constants(world, e, ecs_id(EcsEnum), false, type);
}
static
@ -1615,10 +1585,12 @@ void ecs_set_struct(
ecs_assert(type != 0, ECS_INTERNAL_ERROR, NULL);
}
if (!ecs_vector_count(members)) {
ecs_parser_error(name, type->descriptor, 0, "empty struct declaration");
}
is_partial = token.is_partial;
ecs_entity_t ecs_entity(EcsStruct) = ecs_lookup_fullpath(world, "flecs.meta.Struct");
ecs_assert(ecs_entity(EcsStruct) != 0, ECS_INTERNAL_ERROR, NULL);
ecs_set(world, e, EcsStruct, {members, is_partial});
}
@ -1687,7 +1659,7 @@ void ecs_set_map(
static
void EcsSetType(ecs_iter_t *it) {
ECS_COLUMN(it, EcsMetaType, type, 1);
EcsMetaType *type = ecs_column(it, EcsMetaType, 1);
ecs_world_t *world = it->world;
@ -1754,9 +1726,7 @@ void ecs_new_meta(
ecs_entity_t component,
EcsMetaType *meta_type)
{
ecs_entity_t ecs_entity(EcsMetaType) =
ecs_lookup_fullpath(world, "flecs.meta.MetaType");
ecs_assert(ecs_entity(EcsMetaType) != 0, ECS_MODULE_UNDEFINED, "flecs.meta");
ecs_assert(ecs_id(EcsMetaType) != 0, ECS_MODULE_UNDEFINED, "flecs.meta");
if (meta_type->alias) {
EcsMetaType *alias = meta_type->alias;
@ -1769,13 +1739,13 @@ void ecs_new_meta(
/* Utility macro to insert meta data for type with meta descriptor */
#define ECS_COMPONENT_TYPE(world, type)\
ecs_set_ptr(world, ecs_entity(type), EcsMetaType, &__##type##__)
ecs_set_ptr(world, ecs_id(type), EcsMetaType, &__##type##__)
/* Utility macro to insert metadata for primitive type */
#define ECS_COMPONENT_PRIMITIVE(world, type, kind)\
ECS_COMPONENT(world, type);\
ecs_set(world, ecs_entity(type), EcsMetaType, {EcsPrimitiveType, 0, 0, NULL, NULL});\
ecs_set(world, ecs_entity(type), EcsPrimitive, {kind})
ecs_set(world, ecs_id(type), EcsMetaType, {EcsPrimitiveType, 0, 0, NULL, NULL});\
ecs_set(world, ecs_id(type), EcsPrimitive, {kind})
void FlecsMetaImport(
ecs_world_t *world)
@ -1784,26 +1754,25 @@ void FlecsMetaImport(
ecs_set_name_prefix(world, "Ecs");
ECS_COMPONENT(world, EcsPrimitive);
ECS_COMPONENT(world, EcsEnum);
ECS_COMPONENT(world, EcsBitmask);
ECS_COMPONENT(world, EcsMember);
ECS_COMPONENT(world, EcsStruct);
ECS_COMPONENT(world, EcsArray);
ECS_COMPONENT(world, EcsVector);
ECS_COMPONENT(world, EcsMap);
ECS_COMPONENT(world, EcsMetaType);
ECS_COMPONENT(world, ecs_type_op_kind_t);
ECS_COMPONENT(world, ecs_type_op_t);
ECS_COMPONENT(world, EcsMetaTypeSerializer);
ECS_COMPONENT_DEFINE(world, EcsPrimitive);
ECS_COMPONENT_DEFINE(world, EcsEnum);
ECS_COMPONENT_DEFINE(world, EcsBitmask);
ECS_COMPONENT_DEFINE(world, EcsMember);
ECS_COMPONENT_DEFINE(world, EcsStruct);
ECS_COMPONENT_DEFINE(world, EcsArray);
ECS_COMPONENT_DEFINE(world, EcsVector);
ECS_COMPONENT_DEFINE(world, EcsMap);
ECS_COMPONENT_DEFINE(world, EcsMetaType);
ECS_COMPONENT_DEFINE(world, ecs_type_op_kind_t);
ECS_COMPONENT_DEFINE(world, ecs_type_op_t);
ECS_COMPONENT_DEFINE(world, EcsMetaTypeSerializer);
ECS_SYSTEM(world, EcsSetType, EcsOnSet, EcsMetaType);
ecs_set_component_actions(world, EcsMetaType, {
.ctor = ecs_ctor(EcsMetaType),
.dtor = ecs_dtor(EcsMetaType),
.copy = ecs_copy(EcsMetaType),
.move = ecs_move(EcsMetaType)
.copy = ecs_copy(EcsMetaType)
});
ecs_set_component_actions(world, EcsStruct, {
@ -1826,13 +1795,13 @@ void FlecsMetaImport(
.dtor = ecs_dtor(EcsMetaTypeSerializer)
});
ECS_SYSTEM(world, EcsSetPrimitive, EcsOnSet, Primitive, flecs.meta:flecs.meta);
ECS_SYSTEM(world, EcsSetEnum, EcsOnSet, Enum, flecs.meta:flecs.meta);
ECS_SYSTEM(world, EcsSetBitmask, EcsOnSet, Bitmask, flecs.meta:flecs.meta);
ECS_SYSTEM(world, EcsSetStruct, EcsOnSet, Struct, flecs.meta:flecs.meta);
ECS_SYSTEM(world, EcsSetArray, EcsOnSet, Array, flecs.meta:flecs.meta);
ECS_SYSTEM(world, EcsSetVector, EcsOnSet, Vector, flecs.meta:flecs.meta);
ECS_SYSTEM(world, EcsSetMap, EcsOnSet, Map, flecs.meta:flecs.meta);
ECS_SYSTEM(world, EcsSetPrimitive, EcsOnSet, Primitive);
ECS_SYSTEM(world, EcsSetEnum, EcsOnSet, Enum);
ECS_SYSTEM(world, EcsSetBitmask, EcsOnSet, Bitmask);
ECS_SYSTEM(world, EcsSetStruct, EcsOnSet, Struct);
ECS_SYSTEM(world, EcsSetArray, EcsOnSet, Array);
ECS_SYSTEM(world, EcsSetVector, EcsOnSet, Vector);
ECS_SYSTEM(world, EcsSetMap, EcsOnSet, Map);
ECS_EXPORT_COMPONENT(EcsPrimitive);
ECS_EXPORT_COMPONENT(EcsEnum);
@ -1870,8 +1839,12 @@ void FlecsMetaImport(
* registered with the name _Bool. To make sure meta also knows the type by
* its regular name, check and register if necessary */
if (!ecs_lookup(world, "bool")) {
ecs_entity_t type = ecs_new_component(
world, 0, "bool", sizeof(bool), ECS_ALIGNOF(bool));
ecs_entity_t type = ecs_component_init(world, &(ecs_component_desc_t) {
.entity.name = "bool",
.size = sizeof(bool),
.alignment = ECS_ALIGNOF(bool)
});
ecs_set(world, type, EcsMetaType, {
EcsPrimitiveType, 0, 0, NULL, NULL});
ecs_set(world, type, EcsPrimitive, {EcsBool});
@ -1880,12 +1853,11 @@ void FlecsMetaImport(
ecs_set_scope(world, old_scope);
/* -- Initialize builtin meta components -- */
ecs_set_ptr(world, ecs_set(world, 0,
EcsName, {"ecs_primitive_kind_t", NULL, NULL}),
ecs_set_ptr(world, ecs_set_name(world, 0, "ecs_primitive_kind_t"),
EcsMetaType, &__ecs_primitive_kind_t__);
ecs_set(world, ecs_set(world, 0,
EcsName, {"ecs_type_kind_t", NULL, NULL}),
ecs_set(world, ecs_set_name(world, 0, "ecs_type_kind_t"),
EcsMetaType, {
EcsEnumType,
sizeof(ecs_type_kind_t),
@ -1909,29 +1881,21 @@ void FlecsMetaImport(
ECS_COMPONENT_TYPE(world, EcsMetaTypeSerializer);
/* -- Initialize metadata for public Flecs core components -- */
ecs_set(world, ecs_entity(EcsName), EcsMetaType, {
ecs_set(world, ecs_id(EcsIdentifier), EcsMetaType, {
.kind = EcsStructType,
.size = sizeof(EcsName),
.alignment = ECS_ALIGNOF(EcsName),
.descriptor = "{ecs_string_t value; ECS_PRIVATE; }",
.size = sizeof(EcsIdentifier),
.alignment = ECS_ALIGNOF(EcsIdentifier),
.descriptor = "{ char *value; ECS_PRIVATE }",
.alias = NULL
});
ecs_set(world, ecs_entity(EcsComponent), EcsMetaType, {
ecs_set(world, ecs_id(EcsComponent), EcsMetaType, {
.kind = EcsStructType,
.size = sizeof(EcsComponent),
.alignment = ECS_ALIGNOF(EcsComponent),
.descriptor = "{int32_t size; int32_t alignment;}",
.alias = NULL
});
ecs_set(world, ecs_entity(EcsSignatureExpr), EcsMetaType, {
.kind = EcsStructType,
.size = sizeof(EcsSignatureExpr),
.alignment = ECS_ALIGNOF(EcsSignatureExpr),
.descriptor = "{const char *expr;}",
.alias = NULL
});
}
ecs_entity_t ecs_meta_lookup_array(
@ -1939,7 +1903,7 @@ ecs_entity_t ecs_meta_lookup_array(
ecs_entity_t e,
const char *params_decl,
ecs_meta_parse_ctx_t *ctx)
{
{
ecs_meta_parse_ctx_t param_ctx = {
.name = ctx->name,
.decl = params_decl
@ -1955,31 +1919,25 @@ ecs_entity_t ecs_meta_lookup_array(
ecs_meta_error(ctx, params_decl, "invalid array size");
}
ecs_entity_t element_type = ecs_lookup_symbol(world, params.type.type);
ecs_entity_t element_type = ecs_lookup_symbol(world, params.type.type, true);
if (!element_type) {
ecs_meta_error(ctx, params_decl, "unknown element type '%s'",
ecs_meta_error(ctx, params_decl, "unknown element type '%s'",
params.type.type);
}
if (!e) {
ecs_entity_t ecs_entity(EcsMetaType) = ecs_lookup_fullpath(world, "flecs.meta.MetaType");
ecs_assert(ecs_entity(EcsMetaType) != 0, ECS_INTERNAL_ERROR, NULL);
const EcsMetaType *elem_type = ecs_get(world, element_type, EcsMetaType);
ecs_assert(elem_type != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(elem_type->size * params.count <= INT32_MAX,
ecs_assert(elem_type->size * params.count <= INT32_MAX,
ECS_INVALID_PARAMETER, NULL);
e = ecs_set(world, 0, EcsMetaType, {
EcsArrayType, (int32_t)(elem_type->size * params.count),
EcsArrayType, (int32_t)(elem_type->size * params.count),
elem_type->alignment, NULL, NULL
});
}
ecs_entity_t ecs_entity(EcsArray) = ecs_lookup_fullpath(world, "flecs.meta.Array");
ecs_assert(ecs_entity(EcsArray) != 0, ECS_INTERNAL_ERROR, NULL);
ecs_assert(params.count <= INT32_MAX, ECS_INVALID_PARAMETER, NULL);
return ecs_set(world, e, EcsArray, { element_type, (int32_t)params.count });
@ -1990,7 +1948,7 @@ ecs_entity_t ecs_meta_lookup_vector(
ecs_entity_t e,
const char *params_decl,
ecs_meta_parse_ctx_t *ctx)
{
{
ecs_meta_parse_ctx_t param_ctx = {
.name = ctx->name,
.decl = params_decl
@ -1999,7 +1957,7 @@ ecs_entity_t ecs_meta_lookup_vector(
ecs_meta_params_t params;
ecs_meta_parse_params(params_decl, &params, &param_ctx);
if (params.is_key_value) {
ecs_meta_error(ctx, params_decl,
ecs_meta_error(ctx, params_decl,
"unexpected key value parameters for vector");
}
@ -2007,24 +1965,18 @@ ecs_entity_t ecs_meta_lookup_vector(
world, &params.type, params_decl, 1, &param_ctx);
if (!e) {
ecs_entity_t ecs_entity(EcsMetaType) = ecs_lookup_fullpath(world, "flecs.meta.MetaType");
ecs_assert(ecs_entity(EcsMetaType) != 0, ECS_INTERNAL_ERROR, NULL);
e = ecs_set(world, 0, EcsMetaType, {EcsVectorType, 0, 0, NULL, NULL});
}
ecs_entity_t ecs_entity(EcsVector) = ecs_lookup_fullpath(world, "flecs.meta.Vector");
ecs_assert(ecs_entity(EcsVector) != 0, ECS_INTERNAL_ERROR, NULL);
return ecs_set(world, e, EcsVector, { element_type });
}
ecs_entity_t ecs_meta_lookup_map(
ecs_world_t *world,
ecs_entity_t e,
ecs_entity_t e,
const char *params_decl,
ecs_meta_parse_ctx_t *ctx)
{
{
ecs_meta_parse_ctx_t param_ctx = {
.name = ctx->name,
.decl = params_decl
@ -2033,7 +1985,7 @@ ecs_entity_t ecs_meta_lookup_map(
ecs_meta_params_t params;
ecs_meta_parse_params(params_decl, &params, &param_ctx);
if (!params.is_key_value) {
ecs_meta_error(ctx, params_decl,
ecs_meta_error(ctx, params_decl,
"missing key type for map");
}
@ -2042,17 +1994,11 @@ ecs_entity_t ecs_meta_lookup_map(
ecs_entity_t element_type = ecs_meta_lookup(
world, &params.type, params_decl, 1, &param_ctx);
if (!e) {
ecs_entity_t ecs_entity(EcsMetaType) = ecs_lookup_fullpath(world, "flecs.meta.MetaType");
ecs_assert(ecs_entity(EcsMetaType) != 0, ECS_INTERNAL_ERROR, NULL);
e = ecs_set(world, 0, EcsMetaType, {EcsMapType, 0, 0, NULL, NULL});
}
ecs_entity_t ecs_entity(EcsMap) = ecs_lookup_fullpath(world, "flecs.meta.Map");
ecs_assert(ecs_entity(EcsMap) != 0, ECS_INTERNAL_ERROR, NULL);
return ecs_set(world, e, EcsMap, { key_type, element_type });
}
@ -2072,12 +2018,12 @@ ecs_entity_t ecs_meta_lookup_bitmask(
ecs_meta_params_t params;
ecs_meta_parse_params(params_decl, &params, &param_ctx);
if (params.is_key_value) {
ecs_meta_error(ctx, params_decl,
ecs_meta_error(ctx, params_decl,
"unexpected key value parameters for bitmask");
}
if (params.is_fixed_size) {
ecs_meta_error(ctx, params_decl,
ecs_meta_error(ctx, params_decl,
"unexpected size for bitmask");
}
@ -2087,9 +2033,6 @@ ecs_entity_t ecs_meta_lookup_bitmask(
#ifndef NDEBUG
/* Make sure this is a bitmask type */
ecs_entity_t ecs_entity(EcsMetaType) = ecs_lookup_fullpath(world, "flecs.meta.MetaType");
ecs_assert(ecs_entity(EcsMetaType) != 0, ECS_INTERNAL_ERROR, NULL);
const EcsMetaType *type_ptr = ecs_get(world, bitmask_type, EcsMetaType);
ecs_assert(type_ptr != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(type_ptr->kind == EcsBitmaskType, ECS_INVALID_PARAMETER, NULL);
@ -2135,12 +2078,12 @@ ecs_entity_t ecs_meta_lookup(
} else
if (token->is_ptr) {
typename = "uintptr_t";
} else
} else
if (!strcmp(typename, "char*") || !strcmp(typename, "flecs::string")) {
typename = "ecs_string_t";
}
type = ecs_lookup_symbol(world, typename);
type = ecs_lookup_symbol(world, typename, true);
if (!type) {
ecs_meta_error(ctx, ptr, "unknown type '%s'", typename);
return 0;
@ -2151,13 +2094,11 @@ ecs_entity_t ecs_meta_lookup(
/* If count is not 1, insert array type. First lookup EcsMetaType of the
* element type to get the size and alignment. Then create a new
* entity for the array type, and assign it to the member type. */
ecs_entity_t ecs_entity(EcsMetaType) = ecs_lookup_fullpath(world, "flecs.meta.MetaType");
ecs_entity_t ecs_entity(EcsArray) = ecs_lookup_fullpath(world, "flecs.meta.Array");
const EcsMetaType *type_ptr = ecs_get(world, type, EcsMetaType);
ecs_assert(count <= INT32_MAX, ECS_INVALID_PARAMETER, NULL);
type = ecs_set(world, ecs_set(world, 0,
type = ecs_set(world, ecs_set(world, 0,
EcsMetaType, {EcsArrayType, type_ptr->size, type_ptr->alignment, NULL, NULL}),
EcsArray, {type, (int32_t)count});
}
@ -2197,7 +2138,7 @@ ecs_type_op_t* get_ptr(
}
ecs_meta_cursor_t ecs_meta_cursor(
ecs_world_t *world,
const ecs_world_t *world,
ecs_entity_t type,
void *base)
{
@ -2206,9 +2147,6 @@ ecs_meta_cursor_t ecs_meta_cursor(
ecs_assert(base != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_meta_cursor_t result;
ecs_entity_t ecs_entity(EcsMetaTypeSerializer) =
ecs_lookup_fullpath(world, "flecs.meta.MetaTypeSerializer");
ecs_assert(ecs_entity(EcsMetaTypeSerializer) != 0, ECS_INVALID_PARAMETER, NULL);
const EcsMetaTypeSerializer *ser = ecs_get(world, type, EcsMetaTypeSerializer);
ecs_assert(ser != NULL, ECS_INVALID_PARAMETER, NULL);

View File

@ -51,22 +51,22 @@
#define ECS_ENUM_BOOTSTRAP(name, ...)\
typedef enum name __VA_ARGS__ name;\
ECS_UNUSED \
static const char * __##name##__ = #__VA_ARGS__
static const char * __##name##__ = #__VA_ARGS__;
#define ECS_STRUCT_IMPL(name, descriptor, ...)\
typedef struct name __VA_ARGS__ name;\
ECS_UNUSED \
static EcsMetaType __##name##__ = {EcsStructType, sizeof(name), ECS_ALIGNOF(name), descriptor, NULL}\
static EcsMetaType __##name##__ = {EcsStructType, sizeof(name), ECS_ALIGNOF(name), descriptor, NULL};
#define ECS_ENUM_IMPL(name, descriptor, ...)\
typedef enum name __VA_ARGS__ name;\
ECS_UNUSED \
static EcsMetaType __##name##__ = {EcsEnumType, sizeof(name), ECS_ALIGNOF(name), descriptor, NULL}
static EcsMetaType __##name##__ = {EcsEnumType, sizeof(name), ECS_ALIGNOF(name), descriptor, NULL};
#define ECS_BITMASK_IMPL(name, descriptor, ...)\
typedef enum name __VA_ARGS__ name;\
ECS_UNUSED \
static EcsMetaType __##name##__ = {EcsBitmaskType, sizeof(name), ECS_ALIGNOF(name), descriptor, NULL}
static EcsMetaType __##name##__ = {EcsBitmaskType, sizeof(name), ECS_ALIGNOF(name), descriptor, NULL};
#define ECS_STRUCT_C(T, ...) ECS_STRUCT_IMPL(T, #__VA_ARGS__, __VA_ARGS__)
#define ECS_ENUM_C(T, ...) ECS_ENUM_IMPL(T, #__VA_ARGS__, __VA_ARGS__)
@ -92,11 +92,11 @@ static EcsMetaType __##name##__ = {EcsMapType, sizeof(ecs_map_t*), ECS_ALIGNOF(e
// Unspecialized class (see below)
namespace flecs {
template <typename T>
class __meta__ { };
class __meta__ { };
}
// Specialized C++ class that stores name and descriptor of type
#define ECS_META_CPP(T, kind, descr);\
#define ECS_META_CPP(T, kind, descr)\
namespace flecs {\
template<>\
class __meta__ <T> {\
@ -120,21 +120,21 @@ public:\
#ifdef __cplusplus
// C++
// C++
// Define a struct
#define ECS_STRUCT(T, ...)\
ECS_STRUCT_IMPL(T, #__VA_ARGS__, __VA_ARGS__);\
ECS_STRUCT_IMPL(T, #__VA_ARGS__, __VA_ARGS__)\
ECS_META_CPP(T, EcsStructType, #__VA_ARGS__)
// Define an enumeration
#define ECS_ENUM(T, ...)\
ECS_ENUM_IMPL(T, #__VA_ARGS__, __VA_ARGS__);\
ECS_META_CPP(T, EcsEnumType, #__VA_ARGS__);
ECS_ENUM_IMPL(T, #__VA_ARGS__, __VA_ARGS__)\
ECS_META_CPP(T, EcsEnumType, #__VA_ARGS__)
// Define a bitmask
#define ECS_BITMASK(T, ...)\
ECS_BITMASK_IMPL(T, #__VA_ARGS__, __VA_ARGS__);\
ECS_BITMASK_IMPL(T, #__VA_ARGS__, __VA_ARGS__)\
ECS_META_CPP(T, EcsBitmaskType, #__VA_ARGS__)
#else
@ -189,12 +189,12 @@ typedef uint8_t ecs_byte_t;
#ifdef __cplusplus
namespace flecs {
using string = ecs_string_t;
using byte = ecs_byte_t;
using string_t = ecs_string_t;
using byte_t = ecs_byte_t;
// Define a bitmask
// In C++ trying to assign multiple flags to a variable of an enum type will
// result in a compiler error. Use this template so that the serializer knows
// result in a compiler error. Use this template so that the serializer knows
// this value is a bitmask, while also keeping the compiler happy.
template<typename T>
using bitmask = int32_t;
@ -210,7 +210,7 @@ ECS_ENUM_BOOTSTRAP( ecs_type_kind_t, {
EcsArrayType,
EcsVectorType,
EcsMapType
});
})
ECS_STRUCT( EcsMetaType, {
ecs_type_kind_t kind;
@ -218,7 +218,7 @@ ECS_STRUCT( EcsMetaType, {
int16_t alignment;
const char *descriptor;
void *alias;
});
})
ECS_ENUM( ecs_primitive_kind_t, {
EcsBool,
@ -238,40 +238,40 @@ ECS_ENUM( ecs_primitive_kind_t, {
EcsIPtr,
EcsString,
EcsEntity
});
})
ECS_STRUCT( EcsPrimitive, {
ecs_primitive_kind_t kind;
});
})
// Define EcsBitmask for both C and C++. Both representations are equivalent in
// memory, but allow for a nicer type-safe API in C++
#if defined(__cplusplus) && !defined(FLECS_NO_CPP)
ECS_STRUCT( EcsBitmask, {
flecs::map<int32_t, flecs::string> constants;
});
flecs::map<int32_t, flecs::string_t> constants;
})
#else
ECS_STRUCT( EcsBitmask, {
ecs_map(int32_t, ecs_string_t) constants;
});
})
#endif
// Define EcsEnum for both C and C++. Both representations are equivalent in
// memory, but allow for a nicer type-safe API in C++
#if defined(__cplusplus) && !defined(FLECS_NO_CPP)
ECS_STRUCT( EcsEnum, {
flecs::map<int32_t, flecs::string> constants;
});
flecs::map<int32_t, flecs::string_t> constants;
})
#else
ECS_STRUCT( EcsEnum, {
ecs_map(int32_t, ecs_string_t) constants;
});
})
#endif
ECS_STRUCT( EcsMember, {
char *name;
ecs_entity_t type;
});
})
// Define EcsStruct for both C and C++. Both representations are equivalent in
// memory, but allow for a nicer type-safe API in C++
@ -279,27 +279,27 @@ ECS_STRUCT( EcsMember, {
ECS_STRUCT( EcsStruct, {
flecs::vector<EcsMember> members;
bool is_partial;
});
})
#else
ECS_STRUCT( EcsStruct, {
ecs_vector(EcsMember) members;
bool is_partial;
});
})
#endif
ECS_STRUCT( EcsArray, {
ecs_entity_t element_type;
int32_t count;
});
})
ECS_STRUCT( EcsVector, {
ecs_entity_t element_type;
});
})
ECS_STRUCT( EcsMap, {
ecs_entity_t key_type;
ecs_entity_t element_type;
});
})
////////////////////////////////////////////////////////////////////////////////
@ -316,7 +316,7 @@ ECS_ENUM_C( ecs_type_op_kind_t, {
EcsOpArray,
EcsOpVector,
EcsOpMap
});
})
typedef ecs_vector_t ecs_type_op_vector_t;
typedef ecs_vector_t ecs_constant_vector_t;
@ -326,7 +326,7 @@ ECS_STRUCT_C( ecs_type_op_t, {
ecs_type_op_kind_t kind;
ecs_size_t size; /* Size of value or element type if array or vector */
int16_t alignment; /* Alignment of value */
int32_t count; /* Number of elements (only used for arrays) */
int32_t count; /* Number of array elements or struct members */
int32_t offset; /* Offset of value */
const char *name; /* Name of value (only used for struct members) */
@ -343,11 +343,11 @@ ECS_PRIVATE
ecs_ref_t element;
} map;
} is;
});
})
ECS_STRUCT_C( EcsMetaTypeSerializer, {
ecs_vector(ecs_type_op_t) ops;
});
})
#endif
@ -363,14 +363,14 @@ extern "C" {
/** Convert value to a string. */
FLECS_META_API
char* ecs_ptr_to_str(
ecs_world_t *world,
ecs_entity_t type,
ecs_world_t *world,
ecs_entity_t type,
void* ptr);
/** Convert value to a string. */
FLECS_META_API
char* ecs_entity_to_str(
ecs_world_t *world,
ecs_world_t *world,
ecs_entity_t entity);
@ -415,22 +415,22 @@ char* ecs_entity_to_str(
/** Escape a character */
FLECS_META_API
char* ecs_chresc(
char *out,
char in,
char *out,
char in,
char delimiter);
/** Parse an escaped character */
FLECS_META_API
const char* ecs_chrparse(
const char *in,
const char *in,
char *out);
/** Escape a string */
FLECS_META_API
ecs_size_t ecs_stresc(
char *out,
ecs_size_t n,
char delimiter,
char *out,
ecs_size_t n,
char delimiter,
const char *in);
#ifdef __cplusplus
@ -457,15 +457,15 @@ typedef struct ecs_meta_scope_t {
} ecs_meta_scope_t;
typedef struct ecs_meta_cursor_t {
ecs_world_t *world;
const ecs_world_t *world;
ecs_meta_scope_t scope[ECS_META_MAX_SCOPE_DEPTH];
int32_t depth;
} ecs_meta_cursor_t;
FLECS_META_API
ecs_meta_cursor_t ecs_meta_cursor(
ecs_world_t *world,
ecs_entity_t type,
const ecs_world_t *world,
ecs_entity_t type,
void *base);
FLECS_META_API
@ -554,20 +554,24 @@ typedef struct FlecsMeta {
extern "C" {
#endif
FLECS_META_API ECS_COMPONENT_EXTERN(EcsPrimitive);
FLECS_META_API ECS_COMPONENT_EXTERN(EcsEnum);
FLECS_META_API ECS_COMPONENT_EXTERN(EcsBitmask);
FLECS_META_API ECS_COMPONENT_EXTERN(EcsMember);
FLECS_META_API ECS_COMPONENT_EXTERN(EcsStruct);
FLECS_META_API ECS_COMPONENT_EXTERN(EcsArray);
FLECS_META_API ECS_COMPONENT_EXTERN(EcsVector);
FLECS_META_API ECS_COMPONENT_EXTERN(EcsMap);
FLECS_META_API ECS_COMPONENT_EXTERN(EcsMetaType);
FLECS_META_API ECS_COMPONENT_EXTERN(ecs_type_op_kind_t);
FLECS_META_API ECS_COMPONENT_EXTERN(ecs_type_op_t);
FLECS_META_API ECS_COMPONENT_EXTERN(EcsMetaTypeSerializer);
FLECS_META_API
void FlecsMetaImport(
ecs_world_t *world);
#define FlecsMetaImportHandles(handles)\
ECS_IMPORT_COMPONENT(handles, EcsPrimitive);\
ECS_IMPORT_COMPONENT(handles, EcsEnum);\
ECS_IMPORT_COMPONENT(handles, EcsBitmask);\
ECS_IMPORT_COMPONENT(handles, EcsStruct);\
ECS_IMPORT_COMPONENT(handles, EcsArray);\
ECS_IMPORT_COMPONENT(handles, EcsVector);\
ECS_IMPORT_COMPONENT(handles, EcsMap);\
ECS_IMPORT_COMPONENT(handles, EcsMetaType);\
ECS_IMPORT_COMPONENT(handles, EcsMetaTypeSerializer);
#define FlecsMetaImportHandles(handles)
////////////////////////////////////////////////////////////////////////////////
//// Macro for inserting metadata in C application
@ -583,6 +587,14 @@ void ecs_new_meta(
ECS_COMPONENT(world, T);\
ecs_new_meta(world, ecs_entity(T), &__##T##__);
/** Define a meta component, store in variable outside of the current scope.
* Use this macro in a header when defining a component identifier globally.
* Must be used together with ECS_COMPONENT_DECLARE.
*/
#define ECS_META_DEFINE(world, T)\
ECS_COMPONENT_DEFINE(world, T);\
ecs_new_meta(world, ecs_entity(T), &__##T##__);
#ifdef __cplusplus
}
#endif
@ -616,22 +628,22 @@ public:
StructType = EcsStructType,
ArrayType = EcsArrayType,
VectorType = EcsVectorType,
MapType = EcsMapType
MapType = EcsMapType
};
meta(flecs::world& world) {
FlecsMetaImport(world.c_ptr());
FlecsMetaImport(world);
flecs::module<flecs::components::meta>(world, "flecs::components::meta");
world.module<flecs::components::meta>("flecs::components::meta");
flecs::component<Primitive>(world, "EcsPrimitive");
flecs::component<Enum>(world, "EcsEnum");
flecs::component<Bitmask>(world, "EcsBitmask");
flecs::component<Struct>(world, "EcsStruct");
flecs::component<Array>(world, "EcsArray");
flecs::component<Vector>(world, "EcsVector");
flecs::component<Type>(world, "EcsMetaType");
flecs::component<TypeSerializer>(world, "EcsMetaTypeSerializer");
world.component<Primitive>("EcsPrimitive");
world.component<Enum>("EcsEnum");
world.component<Bitmask>("EcsBitmask");
world.component<Struct>("EcsStruct");
world.component<Array>("EcsArray");
world.component<Vector>("EcsVector");
world.component<Type>("EcsMetaType");
world.component<TypeSerializer>("EcsMetaTypeSerializer");
}
};
}
@ -654,29 +666,27 @@ flecs::entity meta(flecs::world& world) {
////////////////////////////////////////////////////////////////////////////////
template <typename T>
std::string pretty_print(flecs::world& world, flecs::entity_t type, T& data) {
flecs::string pretty_print(flecs::world& world, flecs::entity_t type, T& data) {
char *str = ecs_ptr_to_str(world.c_ptr(), type, &data);
std::string result = std::string(str);
free(str);
flecs::string result = flecs::string(str);
return result;
}
template <typename T>
std::string pretty_print(flecs::world& world, flecs::entity type, T& data) {
flecs::string pretty_print(flecs::world& world, flecs::entity type, T& data) {
return pretty_print(world, type.id(), data);
}
template <typename T>
std::string pretty_print(flecs::world& world, T& data) {
entity_t type = _::component_info<T>::id();
flecs::string pretty_print(flecs::world& world, T& data) {
entity_t type = _::cpp_type<T>::id();
return flecs::pretty_print(world, type, data);
}
template <>
inline std::string pretty_print<flecs::entity>(flecs::world& world, flecs::entity& entity) {
inline flecs::string pretty_print<flecs::entity>(flecs::world& world, flecs::entity& entity) {
char *str = ecs_entity_to_str(world.c_ptr(), entity.id());
std::string result = std::string(str);
free(str);
flecs::string result = flecs::string(str);
return result;
}