zig-gamedev / zflecs

Zig build package and bindings for https://github.com/SanderMertens/flecs
MIT License
3 stars 2 forks source link

C or zig style? Yes #1

Open Pyrolistical opened 1 year ago

Pyrolistical commented 1 year ago

In zig-gamedev/zig-gamedev#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?

michal-z commented 1 year ago

Yes, this direction seems fine to me.

Olle-Lukowski commented 8 months ago

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.

Pyrolistical commented 8 months ago

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