Raylib-zig is missing a function I need. Can I fix this in my zig.build somehow?

Hi!
I’m using raylib-zig, which includes raygui in my app. Raygui provides a function called GuiLoadStyleFromMemory which raylib-zig does not wrap.

I’ve implemented the function like this:

extern "c" fn GuiLoadStyleFromMemory(data: [*c]const u8, dataSize: usize) void;

pub fn LoadStyleFromMemory(data: []const u8) void {
    GuiLoadStyleFromMemory(@as([*c]const u8, @ptrCast(data)), data.len);
}

I’m new to Zig, and my C days are at least a decade behind mine, so there could be errors in that code.

I had several ideas for how to move forward:

Fork raylib-zig and add the wrapping function upstream.

The problem is that it uses a hand-crafted code generator, which I would have to modify and understand; I’ve abandoned that idea for now.

Try to add the wrapper function to the raygui module in my build.zig.

Is there a way to add this code to the existing module?

build.zig:

const raylib_dep = b.dependency("raylib-zig", .{
    .target = target,
    .optimize = optimize,
});

const raylib = raylib_dep.module("raylib"); // main raylib module
const raygui = raylib_dep.module("raygui"); // raygui module
// can I add code to the raygui module here somehow?

Trying to include the raygui.h that ships with zig-raylib in my project

Maybe it is possible to make the definitions in raygui.h somehow available inside my project.

I think adding:

extern "c" fn GuiLoadStyleFromMemory(data: [*c]const u8, dataSize: usize) void;

pub fn LoadStyleFromMemory(data: []const u8) void {
    GuiLoadStyleFromMemory(data.ptr, data.len);
}

to your application code should work, because normally everything just gets linked together statically anyway.

You can’t really add it to an existing module, if you wanted that you would change it upstream. You could create your own module but for such a small addition it seems silly.

That probably would be possible, but I think it is more complicated than forking the wrapper and adding the function.

If I understand the code correctly the function is missing here:

and the wrapper here:

I think I would either try to add it myself and then add a pull request, or create an issue in the github repo to get help, it doesn’t make sense to me that raygui is included, loading styles too, just not loading them from memory, so I think it is most likely just missing because nobody asked for it.

1 Like

GuiLoadStyleFromMemory is not exported from raygui.

Some options for going forward are:

1 Like

Here is my preferred flow chart for this use case:

  1. Fork the open source project that is missing the desired feature.
  2. Add the feature.
  3. Submit the patch to the open source project.
  4. Switch your project to use your fork while you wait for it to be merged upstream.
  5. If it is merged upstream, great! Next time they make a release you can switch back from your fork.
  6. If your enhancement is rejected, then switch back from your fork, resorting to a workaround instead, such as the one suggested in this thread.
  7. If many of your patches are rejected by upstream, consider making an actual fork that you maintain and take in a different direction, providing healthy competition for the ecosystem.
8 Likes

Thanks everyone for looking into this. These answers are very helpful!