Skip to content

Commit

Permalink
#124 reserve first HI_COMPONENT_ID ids for components
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed May 27, 2020
1 parent 7fa5474 commit b18cb33
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 317 deletions.
75 changes: 39 additions & 36 deletions include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,11 @@ struct ecs_rows_t {

/* World info */
typedef struct ecs_world_info_t {
/* Handles */
ecs_entity_t last_handle; /* Last issued handle */
ecs_entity_t min_handle; /* First allowed handle */
ecs_entity_t max_handle; /* Last allowed handle */
/* Ids */
ecs_entity_t last_component_id; /* Last issued component entity id */
ecs_entity_t last_id; /* Last issued entity id */
ecs_entity_t min_id; /* First allowed entity id */
ecs_entity_t max_id; /* Last allowed entity id */

/* Timing stats */
float delta_time; /* Time passed to or computed by ecs_progress */
Expand Down Expand Up @@ -366,48 +367,50 @@ extern ecs_type_t
ecs_type(EcsRateFilter),
ecs_type(EcsTickSource);

/** Handles to builtin components / tags */
/** Builtin component ids */
#define FLECS__EEcsComponent (1)
#define FLECS__EEcsComponentLifecycle (2)
#define FLECS__EEcsTrigger (3)
#define FLECS__EEcsType (4)
#define EcsModule (5)
#define EcsPrefab (6)
#define FLECS__EEcsSystem (7)
#define FLECS__EEcsColSystem (8)
#define FLECS__EEcsName (9)
#define EcsHidden (10)
#define EcsDisabled (11)
#define EcsDisabledIntern (12)
#define EcsInactive (13)
#define EcsOnDemand (14)
#define EcsMonitor (15)
#define EcsPipeline (17)
#define FLECS__EEcsPipelineQuery (18)
#define FLECS__EEcsTimer (19)
#define FLECS__EEcsRateFilter (20)
#define FLECS__EEcsTickSource (21)
#define FLECS__EEcsSystem (5)
#define FLECS__EEcsColSystem (6)
#define FLECS__EEcsName (7)
#define FLECS__EEcsPipelineQuery (8)
#define FLECS__EEcsTimer (9)
#define FLECS__EEcsRateFilter (10)
#define FLECS__EEcsTickSource (11)

/* Builtin tag ids */
#define EcsModule (ECS_HI_COMPONENT_ID + 0)
#define EcsPrefab (ECS_HI_COMPONENT_ID + 1)
#define EcsHidden (ECS_HI_COMPONENT_ID + 2)
#define EcsDisabled (ECS_HI_COMPONENT_ID + 3)
#define EcsDisabledIntern (ECS_HI_COMPONENT_ID + 4)
#define EcsInactive (ECS_HI_COMPONENT_ID + 5)
#define EcsOnDemand (ECS_HI_COMPONENT_ID + 6)
#define EcsMonitor (ECS_HI_COMPONENT_ID + 7)
#define EcsPipeline (ECS_HI_COMPONENT_ID + 8)

/* Trigger tags */
#define EcsOnAdd (22)
#define EcsOnRemove (23)
#define EcsOnSet (24)
#define EcsOnAdd (ECS_HI_COMPONENT_ID + 9)
#define EcsOnRemove (ECS_HI_COMPONENT_ID + 10)
#define EcsOnSet (ECS_HI_COMPONENT_ID + 11)

/* Builtin pipeline tags */
#define EcsPreFrame (25)
#define EcsOnLoad (26)
#define EcsPostLoad (27)
#define EcsPreUpdate (28)
#define EcsOnUpdate (29)
#define EcsOnValidate (30)
#define EcsPostUpdate (31)
#define EcsPreStore (32)
#define EcsOnStore (33)
#define EcsPostFrame (34)
#define EcsPreFrame (ECS_HI_COMPONENT_ID + 12)
#define EcsOnLoad (ECS_HI_COMPONENT_ID + 13)
#define EcsPostLoad (ECS_HI_COMPONENT_ID + 14)
#define EcsPreUpdate (ECS_HI_COMPONENT_ID + 15)
#define EcsOnUpdate (ECS_HI_COMPONENT_ID + 16)
#define EcsOnValidate (ECS_HI_COMPONENT_ID + 17)
#define EcsPostUpdate (ECS_HI_COMPONENT_ID + 18)
#define EcsPreStore (ECS_HI_COMPONENT_ID + 19)
#define EcsOnStore (ECS_HI_COMPONENT_ID + 20)
#define EcsPostFrame (ECS_HI_COMPONENT_ID + 21)

/** Builtin entity ids */
#define EcsWorld (35)
#define ECS_SINGLETON (EcsSingleton)
#define EcsWorld (ECS_HI_COMPONENT_ID + 22)
#define EcsSingleton ((ecs_entity_t)(ECS_ENTITY_MASK) - 1)

/** Value used to quickly check if component is builtin */
#define EcsLastInternal (ecs_entity(EcsColSystem))
Expand Down
64 changes: 35 additions & 29 deletions include/flecs/util/api_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ extern "C" {
typedef struct ecs_column_t ecs_column_t;
typedef struct ecs_data_t ecs_data_t;

/** This reserves entity ids for components. Regular entity ids will start after
* this constant. This affects performance of table traversal, as edges with ids
* lower than this constant are looked up in an array, whereas constants higher
* than this id are looked up in a map. Increasing this value can improve
* performance at the cost of (significantly) higher memory usage. */
#define ECS_HI_COMPONENT_ID (256) /* Maximum number of components */

////////////////////////////////////////////////////////////////////////////////
//// Private datatypes
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -130,8 +137,6 @@ typedef struct ecs_writer_t {
//// Deprecated names
////////////////////////////////////////////////////////////////////////////////

#define EcsSingleton ((ecs_entity_t)(ECS_ENTITY_MASK) - 1)

struct ecs_filter_t;
typedef struct ecs_filter_t ecs_type_filter_t;

Expand Down Expand Up @@ -321,33 +326,34 @@ void _ecs_parser_error(
#define ECS_MORE_THAN_ONE_PREFAB (13)
#define ECS_ALREADY_DEFINED (14)
#define ECS_INVALID_COMPONENT_SIZE (15)
#define ECS_OUT_OF_MEMORY (16)
#define ECS_MODULE_UNDEFINED (17)
#define ECS_COLUMN_INDEX_OUT_OF_RANGE (18)
#define ECS_COLUMN_IS_NOT_SHARED (19)
#define ECS_COLUMN_IS_SHARED (20)
#define ECS_COLUMN_HAS_NO_DATA (21)
#define ECS_COLUMN_TYPE_MISMATCH (22)
#define ECS_INVALID_WHILE_MERGING (23)
#define ECS_INVALID_WHILE_ITERATING (24)
#define ECS_INVALID_FROM_WORKER (25)
#define ECS_UNRESOLVED_IDENTIFIER (26)
#define ECS_OUT_OF_RANGE (27)
#define ECS_COLUMN_IS_NOT_SET (28)
#define ECS_UNRESOLVED_REFERENCE (29)
#define ECS_THREAD_ERROR (30)
#define ECS_MISSING_OS_API (31)
#define ECS_TYPE_TOO_LARGE (32)
#define ECS_INVALID_PREFAB_CHILD_TYPE (33)
#define ECS_UNSUPPORTED (34)
#define ECS_NO_OUT_COLUMNS (35)
#define ECS_COLUMN_ACCESS_VIOLATION (36)
#define ECS_DESERIALIZE_COMPONENT_ID_CONFLICT (37)
#define ECS_DESERIALIZE_COMPONENT_SIZE_CONFLICT (38)
#define ECS_DESERIALIZE_FORMAT_ERROR (39)
#define ECS_INVALID_REACTIVE_SIGNATURE (40)
#define ECS_INCONSISTENT_COMPONENT_NAME (41)
#define ECS_TYPE_CONSTRAINT_VIOLATION (42)
#define ECS_INVALID_COMPONENT_ALIGNMENT (16)
#define ECS_OUT_OF_MEMORY (17)
#define ECS_MODULE_UNDEFINED (18)
#define ECS_COLUMN_INDEX_OUT_OF_RANGE (19)
#define ECS_COLUMN_IS_NOT_SHARED (20)
#define ECS_COLUMN_IS_SHARED (21)
#define ECS_COLUMN_HAS_NO_DATA (22)
#define ECS_COLUMN_TYPE_MISMATCH (23)
#define ECS_INVALID_WHILE_MERGING (24)
#define ECS_INVALID_WHILE_ITERATING (25)
#define ECS_INVALID_FROM_WORKER (26)
#define ECS_UNRESOLVED_IDENTIFIER (27)
#define ECS_OUT_OF_RANGE (28)
#define ECS_COLUMN_IS_NOT_SET (29)
#define ECS_UNRESOLVED_REFERENCE (30)
#define ECS_THREAD_ERROR (31)
#define ECS_MISSING_OS_API (32)
#define ECS_TYPE_TOO_LARGE (33)
#define ECS_INVALID_PREFAB_CHILD_TYPE (34)
#define ECS_UNSUPPORTED (35)
#define ECS_NO_OUT_COLUMNS (36)
#define ECS_COLUMN_ACCESS_VIOLATION (37)
#define ECS_DESERIALIZE_COMPONENT_ID_CONFLICT (38)
#define ECS_DESERIALIZE_COMPONENT_SIZE_CONFLICT (39)
#define ECS_DESERIALIZE_FORMAT_ERROR (40)
#define ECS_INVALID_REACTIVE_SIGNATURE (41)
#define ECS_INCONSISTENT_COMPONENT_NAME (42)
#define ECS_TYPE_CONSTRAINT_VIOLATION (43)

/** Declare type variable */
#define ECS_TYPE_VAR(type)\
Expand Down
3 changes: 2 additions & 1 deletion src/api_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ ecs_entity_t ecs_new_component(

ecs_entity_t result = lookup(world, name, ecs_type(EcsComponent));
if (!result) {
result = ecs_set(world, 0, EcsName, {name});
result = ++ world->stats.last_component_id;
ecs_set(world, result, EcsName, {name});
ecs_set(world, result, EcsComponent, {
.size = size,
.alignment = alignment
Expand Down
15 changes: 5 additions & 10 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ void ecs_component_set_intern(
EcsTrigger *ct,
int32_t count)
{
ecs_component_data_t *cdata_array = ecs_vector_first(
world->component_data, ecs_component_data_t);
ecs_assert(cdata_array != NULL, ECS_INTERNAL_ERROR, NULL);
EcsTrigger *el = NULL;

int i;
for (i = 0; i < count; i ++) {
ecs_entity_t e = ct[i].component;
ecs_component_data_t *cdata = &cdata_array[e];

ecs_c_info_t *cdata = ecs_get_or_create_c_info(world, e);
switch(ct[i].kind) {
case EcsOnAdd:
el = ecs_vector_add(&cdata->on_add, EcsTrigger);
Expand Down Expand Up @@ -112,17 +110,14 @@ void EcsOnSetComponentLifecycle(
ecs_rows_t *rows)
{
EcsComponentLifecycle *cl = ecs_column(rows, EcsComponentLifecycle, 1);

ecs_world_t *world = rows->world;
ecs_component_data_t *cdata_array = ecs_vector_first(
world->component_data, ecs_component_data_t);

ecs_assert(cdata_array != NULL, ECS_INTERNAL_ERROR, NULL);

int i;
for (i = 0; i < rows->count; i ++) {
ecs_entity_t e = rows->entities[i];
cdata_array[e].lifecycle = cl[i];
ecs_c_info_t *c_info = ecs_get_or_create_c_info(world, e);

c_info->lifecycle = cl[i];

ecs_trace_1("component #[green]%s#[normal] lifecycle callbacks set",
ecs_get_name(world, e));
Expand Down
4 changes: 2 additions & 2 deletions src/dbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ int ecs_dbg_col_system(

*dbg_out = (ecs_dbg_col_system_t){.system = system};
dbg_out->active_table_count = ecs_vector_count(system_data->query->tables);
dbg_out->inactive_table_count = ecs_vector_count(system_data->query->inactive_tables);
dbg_out->inactive_table_count = ecs_vector_count(system_data->query->empty_tables);
dbg_out->enabled = !ecs_has_entity(world, system, EcsDisabled);

ecs_vector_each(system_data->query->tables, ecs_matched_table_t, mt, {
Expand Down Expand Up @@ -167,7 +167,7 @@ ecs_table_t* ecs_dbg_inactive_table(

EcsColSystem *system_data = dbg->system_data;
ecs_matched_table_t *table = ecs_vector_get(
system_data->query->inactive_tables, ecs_matched_table_t, index);
system_data->query->empty_tables, ecs_matched_table_t, index);
if (!table) {
return NULL;
}
Expand Down
Loading

0 comments on commit b18cb33

Please sign in to comment.