zig-gamedev icon indicating copy to clipboard operation
zig-gamedev copied to clipboard

zflecs: C or zig style? Yes

Open Pyrolistical opened this issue 2 years ago • 3 comments

In #261, in regards to the zflecs style @michal-z said

Also, it will be easier for the users to follow flecs C code and port it to zig.

I believe we can achieve both in most (if not all) cases.

For example,

// current C style 
fn move(it: *ecs.iter_t) callconv(.C) void {
    const p = ecs.field(it, Position, 1).?;
    const v = ecs.field(it, Velocity, 2).?;
    ...

// ziggified style
fn move(it: *ecs.iter_t) callconv(.C) void {
    const p = it.field(Position, 1).?;
    const v = it.field(Velocity, 2).?;
    ...

We can keep both as the ziggified style only requires duplicating fn field into the iter_t struct.

Another example,

// current C style
const world = ecs.init();
defer _ = ecs.fini(world);

ecs.COMPONENT(world, Position);

const bob = ecs.new_entity(world, "Bob");
_ = ecs.set(world, bob, Position, .{ .x = 0, .y = 0 });

// ziggified style
const World = ecs;
const world = World.init();
defer _ = world.fini();

world.COMPONENT(Position);

const bob = world.new_entity("Bob");
_ = world.set(bob, Position, .{ .x = 0, .y = 0 });

To achieve that, I believe we only need to make zflecs.zig a top-level struct of type World, but also keep non-world static functions like fn field and other constants.

World would hold the pointer to world_t.

Is this desired direction zflecs should head in?

Pyrolistical avatar Aug 17 '23 19:08 Pyrolistical

Yes, this direction seems fine to me.

michal-z avatar Aug 18 '23 10:08 michal-z

I am curious, do you need any help with this? I am willing to contribute to get this done, since I am going to use zflecs for my game engine.

olukowski avatar Mar 24 '24 16:03 olukowski

You can lead this effort. I'm not working on this at the moment

Pyrolistical avatar Mar 24 '24 17:03 Pyrolistical