diff --git a/flecs.c b/flecs.c index f82d9cf3c6..bb0b1f8a07 100644 --- a/flecs.c +++ b/flecs.c @@ -59455,32 +59455,36 @@ bool flecs_iter_next_instanced( void* ecs_field_w_size( const ecs_iter_t *it, size_t size, - int32_t term) + int32_t index) { ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL); ecs_check(it->ptrs != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_check(!size || ecs_field_size(it, term) == size || - (!ecs_field_size(it, term) && (!it->ptrs[term - 1])), + ecs_check(!size || ecs_field_size(it, index) == size || + (!ecs_field_size(it, index) && (!it->ptrs[index - 1])), ECS_INVALID_PARAMETER, NULL); (void)size; - if (!term) { + if (!index) { return it->entities; } - return it->ptrs[term - 1]; + return it->ptrs[index - 1]; error: return NULL; } bool ecs_field_is_readonly( const ecs_iter_t *it, - int32_t term_index) + int32_t index) { ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL); - ecs_check(term_index > 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); + + if (index == 0) { + return true; + } - ecs_term_t *term = &it->terms[term_index - 1]; + ecs_term_t *term = &it->terms[index - 1]; ecs_check(term != NULL, ECS_INVALID_PARAMETER, NULL); if (term->inout == EcsIn) { @@ -59505,12 +59509,12 @@ bool ecs_field_is_readonly( bool ecs_field_is_writeonly( const ecs_iter_t *it, - int32_t term_index) + int32_t index) { ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL); - ecs_check(term_index > 0, ECS_INVALID_PARAMETER, NULL); + ecs_check(index >= 1, ECS_INVALID_PARAMETER, NULL); - ecs_term_t *term = &it->terms[term_index - 1]; + ecs_term_t *term = &it->terms[index - 1]; ecs_check(term != NULL, ECS_INVALID_PARAMETER, NULL); if (term->inout == EcsOut) { @@ -59526,6 +59530,11 @@ bool ecs_field_is_set( int32_t index) { ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL); + ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); + + if (index == 0) { + return true; + } int32_t column = it->columns[index - 1]; if (!column) { @@ -59551,7 +59560,8 @@ bool ecs_field_is_self( { ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); - return it->sources == NULL || it->sources[index - 1] == 0; + + return index == 0 || it->sources == NULL || it->sources[index - 1] == 0; } ecs_id_t ecs_field_id( @@ -59560,7 +59570,12 @@ ecs_id_t ecs_field_id( { ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); - return it->ids[index - 1]; + + if (index == 0) { + return ecs_id(ecs_entity_t); + } else { + return it->ids[index - 1]; + } } int32_t ecs_field_column_index( @@ -59570,6 +59585,10 @@ int32_t ecs_field_column_index( ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); + if (index == 0) { + return -1; + } + int32_t result = it->columns[index - 1]; if (result <= 0) { return -1; @@ -59584,10 +59603,10 @@ ecs_entity_t ecs_field_src( { ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); - if (it->sources) { - return it->sources[index - 1]; - } else { + if (index == 0 || !it->sources) { return 0; + } else { + return it->sources[index - 1]; } } diff --git a/flecs.h b/flecs.h index e28ea826f7..a9418caaf2 100644 --- a/flecs.h +++ b/flecs.h @@ -4104,6 +4104,25 @@ FLECS_API extern const ecs_entity_t EcsOnStore; FLECS_API extern const ecs_entity_t EcsPostFrame; FLECS_API extern const ecs_entity_t EcsPhase; +/* Primitive type component ids */ +FLECS_API extern const ecs_entity_t ecs_id(ecs_bool_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_char_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_byte_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_u8_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_u16_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_u32_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_u64_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_uptr_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_i8_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_i16_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_i32_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_i64_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_iptr_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_f32_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_f64_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_string_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_entity_t); + /** Value used to quickly check if component is builtin. This is used to quickly * filter out tables with builtin components (for example for ecs_delete) */ #define EcsLastInternalComponentId (ecs_id(EcsPoly)) @@ -12923,25 +12942,6 @@ FLECS_API extern const ecs_entity_t ecs_id(EcsUnitPrefix); FLECS_API extern const ecs_entity_t EcsConstant; FLECS_API extern const ecs_entity_t EcsQuantity; -/* Primitive type component ids */ -FLECS_API extern const ecs_entity_t ecs_id(ecs_bool_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_char_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_byte_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_u8_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_u16_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_u32_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_u64_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_uptr_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_i8_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_i16_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_i32_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_i64_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_iptr_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_f32_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_f64_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_string_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_entity_t); - /** Type kinds supported by meta addon */ typedef enum ecs_type_kind_t { EcsPrimitiveType, diff --git a/include/flecs.h b/include/flecs.h index 521588877e..af45ec275a 100644 --- a/include/flecs.h +++ b/include/flecs.h @@ -1397,6 +1397,25 @@ FLECS_API extern const ecs_entity_t EcsOnStore; FLECS_API extern const ecs_entity_t EcsPostFrame; FLECS_API extern const ecs_entity_t EcsPhase; +/* Primitive type component ids */ +FLECS_API extern const ecs_entity_t ecs_id(ecs_bool_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_char_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_byte_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_u8_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_u16_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_u32_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_u64_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_uptr_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_i8_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_i16_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_i32_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_i64_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_iptr_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_f32_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_f64_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_string_t); +FLECS_API extern const ecs_entity_t ecs_id(ecs_entity_t); + /** Value used to quickly check if component is builtin. This is used to quickly * filter out tables with builtin components (for example for ecs_delete) */ #define EcsLastInternalComponentId (ecs_id(EcsPoly)) diff --git a/include/flecs/addons/meta.h b/include/flecs/addons/meta.h index 24e1d7cfcf..6bf4f9864f 100644 --- a/include/flecs/addons/meta.h +++ b/include/flecs/addons/meta.h @@ -118,25 +118,6 @@ FLECS_API extern const ecs_entity_t ecs_id(EcsUnitPrefix); FLECS_API extern const ecs_entity_t EcsConstant; FLECS_API extern const ecs_entity_t EcsQuantity; -/* Primitive type component ids */ -FLECS_API extern const ecs_entity_t ecs_id(ecs_bool_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_char_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_byte_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_u8_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_u16_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_u32_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_u64_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_uptr_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_i8_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_i16_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_i32_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_i64_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_iptr_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_f32_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_f64_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_string_t); -FLECS_API extern const ecs_entity_t ecs_id(ecs_entity_t); - /** Type kinds supported by meta addon */ typedef enum ecs_type_kind_t { EcsPrimitiveType, diff --git a/src/iter.c b/src/iter.c index 6b27401e2f..aa49aa4db8 100644 --- a/src/iter.c +++ b/src/iter.c @@ -348,32 +348,36 @@ bool flecs_iter_next_instanced( void* ecs_field_w_size( const ecs_iter_t *it, size_t size, - int32_t term) + int32_t index) { ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL); ecs_check(it->ptrs != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_check(!size || ecs_field_size(it, term) == size || - (!ecs_field_size(it, term) && (!it->ptrs[term - 1])), + ecs_check(!size || ecs_field_size(it, index) == size || + (!ecs_field_size(it, index) && (!it->ptrs[index - 1])), ECS_INVALID_PARAMETER, NULL); (void)size; - if (!term) { + if (!index) { return it->entities; } - return it->ptrs[term - 1]; + return it->ptrs[index - 1]; error: return NULL; } bool ecs_field_is_readonly( const ecs_iter_t *it, - int32_t term_index) + int32_t index) { ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL); - ecs_check(term_index > 0, ECS_INVALID_PARAMETER, NULL); + ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); + + if (index == 0) { + return true; + } - ecs_term_t *term = &it->terms[term_index - 1]; + ecs_term_t *term = &it->terms[index - 1]; ecs_check(term != NULL, ECS_INVALID_PARAMETER, NULL); if (term->inout == EcsIn) { @@ -398,12 +402,12 @@ bool ecs_field_is_readonly( bool ecs_field_is_writeonly( const ecs_iter_t *it, - int32_t term_index) + int32_t index) { ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL); - ecs_check(term_index > 0, ECS_INVALID_PARAMETER, NULL); + ecs_check(index >= 1, ECS_INVALID_PARAMETER, NULL); - ecs_term_t *term = &it->terms[term_index - 1]; + ecs_term_t *term = &it->terms[index - 1]; ecs_check(term != NULL, ECS_INVALID_PARAMETER, NULL); if (term->inout == EcsOut) { @@ -419,6 +423,11 @@ bool ecs_field_is_set( int32_t index) { ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL); + ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); + + if (index == 0) { + return true; + } int32_t column = it->columns[index - 1]; if (!column) { @@ -444,7 +453,8 @@ bool ecs_field_is_self( { ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); - return it->sources == NULL || it->sources[index - 1] == 0; + + return index == 0 || it->sources == NULL || it->sources[index - 1] == 0; } ecs_id_t ecs_field_id( @@ -453,7 +463,12 @@ ecs_id_t ecs_field_id( { ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); - return it->ids[index - 1]; + + if (index == 0) { + return ecs_id(ecs_entity_t); + } else { + return it->ids[index - 1]; + } } int32_t ecs_field_column_index( @@ -463,6 +478,10 @@ int32_t ecs_field_column_index( ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); + if (index == 0) { + return -1; + } + int32_t result = it->columns[index - 1]; if (result <= 0) { return -1; @@ -477,10 +496,10 @@ ecs_entity_t ecs_field_src( { ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL); ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL); - if (it->sources) { - return it->sources[index - 1]; - } else { + if (index == 0 || !it->sources) { return 0; + } else { + return it->sources[index - 1]; } } diff --git a/src/world.c b/src/world.c index a27fd85240..9b4fcbbd66 100644 --- a/src/world.c +++ b/src/world.c @@ -119,7 +119,7 @@ const ecs_entity_t EcsOnStore = FLECS_HI_COMPONENT_ID + 73; const ecs_entity_t EcsPostFrame = FLECS_HI_COMPONENT_ID + 74; const ecs_entity_t EcsPhase = FLECS_HI_COMPONENT_ID + 75; -/* Meta primitive components (don't use low ids to save id space) */ +/* Primitive type components (don't use low ids to save id space) */ const ecs_entity_t ecs_id(ecs_bool_t) = FLECS_HI_COMPONENT_ID + 80; const ecs_entity_t ecs_id(ecs_char_t) = FLECS_HI_COMPONENT_ID + 81; const ecs_entity_t ecs_id(ecs_byte_t) = FLECS_HI_COMPONENT_ID + 82; diff --git a/test/cpp_api/src/Query.cpp b/test/cpp_api/src/Query.cpp index b4a56b096f..1ada15e58f 100644 --- a/test/cpp_api/src/Query.cpp +++ b/test/cpp_api/src/Query.cpp @@ -2229,3 +2229,21 @@ void Query_worker_iter_captured_query() { test_int(count, 1); }(); } + +void Query_test_entities_field_iter() { + flecs::world ecs; + + auto e1 = ecs.entity().set({10, 20}); + auto e2 = ecs.entity().set({10, 20}); + auto e3 = ecs.entity().set({10, 20}); + + ecs.query() + .iter([&](flecs::iter& it) { + test_int(it.count(), 3); + + auto entities = it.field(0); + test_assert(entities[0] == e1); + test_assert(entities[1] == e2); + test_assert(entities[2] == e3); + }); +} diff --git a/test/cpp_api/src/main.cpp b/test/cpp_api/src/main.cpp index 900e209046..e4343b79b1 100644 --- a/test/cpp_api/src/main.cpp +++ b/test/cpp_api/src/main.cpp @@ -561,6 +561,7 @@ void Query_instanced_nested_query_w_world(void); void Query_captured_query(void); void Query_page_iter_captured_query(void); void Query_worker_iter_captured_query(void); +void Query_test_entities_field_iter(void); // Testsuite 'QueryBuilder' void QueryBuilder_builder_assign_same_type(void); @@ -3407,6 +3408,10 @@ bake_test_case Query_testcases[] = { { "worker_iter_captured_query", Query_worker_iter_captured_query + }, + { + "test_entities_field_iter", + Query_test_entities_field_iter } }; @@ -6112,7 +6117,7 @@ static bake_test_suite suites[] = { "Query", NULL, NULL, - 78, + 79, Query_testcases }, {