flecs-hub/flecs-json

Nested structures do not access data correctly

Opened this issue · 0 comments

pikly commented

When serializing nested structs, the pointer offsets are not calculated correctly for the nested fields.

Code to reproduce:

#include <flecs.h>
#include <flecs_components_meta.h>
#include <flecs_json.h>

ECS_STRUCT(Position, {
    int32_t x;
    int32_t y;
});

ECS_STRUCT(Color, {
        float r;
        float g;
        float b;
        Position test;
});

void SysPos(ecs_rows_t *rows)
{
    ECS_COLUMN(rows, Position, p, 1);
    ECS_COLUMN(rows, Color, c, 2);

    for (int i = 0; i < rows->count; i++)
    {
        printf("p.x = %d, p.y = %d\n", p[i].x, p[i].y);
        printf("c.r = %f, c.g = %f, c.b = %f, test.x = %d, test.y = %d\n", c[i].r, c[i].g, c[i].b, c[i].test.x, c[i].test.y);
    }
}

int main(void)
{
    ecs_world_t *world = ecs_init();

    ECS_IMPORT(world, FlecsComponentsMeta, 0);

    ECS_META(world, Position);
    ECS_META(world, Color);

    ECS_SYSTEM(world, SysPos, EcsOnUpdate, Position, Color);

    for (int i = 0; i < 2; i++)
    {
        ecs_entity_t e = ecs_new(world, 0);
        ecs_set(world, e, Position, { i, i * 2 });
        Color c;
        c.r = (float)i;
        c.g = c.r*c.r;
        c.b = c.r*c.g;
        c.test.x = i + 100;
        c.test.y = i + 200;
        ecs_set_ptr(world, e, Color, &c);
    }

    ecs_filter_t filter = { . include = ecs_type(Position) };

    char *json = ecs_filter_to_json(world, &filter);
	printf("%s\n", json);

    // to print out values
    ecs_progress(world, 0);

    return ecs_fini(world);
}