Skip to content

Commit

Permalink
Allow ecs_field functions to work for index 0
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroErrors committed Jul 8, 2023
1 parent d073a1a commit 819d2a1
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 72 deletions.
51 changes: 35 additions & 16 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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;
Expand All @@ -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];
}
}

Expand Down
38 changes: 19 additions & 19 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
19 changes: 0 additions & 19 deletions include/flecs/addons/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 35 additions & 16 deletions src/iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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;
Expand All @@ -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];
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions test/cpp_api/src/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Position>({10, 20});
auto e2 = ecs.entity().set<Position>({10, 20});
auto e3 = ecs.entity().set<Position>({10, 20});

ecs.query<Position>()
.iter([&](flecs::iter& it) {
test_int(it.count(), 3);

auto entities = it.field<flecs::entity_t>(0);
test_assert(entities[0] == e1);
test_assert(entities[1] == e2);
test_assert(entities[2] == e3);
});
}
Loading

0 comments on commit 819d2a1

Please sign in to comment.