# Using raygui with raylib

**URL:** <https://ziggit.dev/t/using-raygui-with-raylib/3399>\
**Category:** Help\
**Tags:** ecosystem, raylib\
**Created:** [February 29, 2024, 1:49am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399 "2024-02-29T01:49:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiangbo](https://ziggit.dev/user_avatar/ziggit.dev/jiangbo/32/801_2.png) [@jiangbo](https://ziggit.dev/u/jiangbo)\
**Post date:** [February 29, 2024, 1:49am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/1 "2024-02-29T01:49:00Z")

</div>

Do you konw how to use raygui? [raylib gui option](https://github.com/raysan5/raylib/blob/master/src/build.zig). I’ve turned on the options for the raygui:

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

```

, but it seems that the source code of raygui has not been found. error info:

```zig
run
└─ run demo
   └─ zig build-exe demo Debug native
      └─ install ../raygui/src/raygui.h to raygui.h failure
error: unable to update file from '/root/.cache/zig/p/raygui/src/raygui.h' to '/root/workspace/demo/zig-cache/i/a92a39660ff6ff24bf9055882ff0d65f/include/raygui.h': FileNotFound

```

The zon file:

```zig
.{
    .name = "demo",
    .version = "0.0.0",
    .dependencies = .{
        .raylib = .{
            .url = "https://github.com/raysan5/raylib/archive/e9291fa4c77c85e1fe6808289632e5ce4a93eed6.tar.gz",
            .hash = "1220ee786fc07876bb53e359c0a204e13dfb1a89735c1221363888021322f0b9f131",
        },
    },
    .paths = .{""},
}

```

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 29, 2024, 2:05am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/2 "2024-02-29T02:05:41Z")

</div>

Welcome to the forum @jiangbo!

I have moved this to a new topic, because it seemed only vaguely related to the original topic (both using raylib) and you maybe have a different problem.

Are you using raylib directly, or do you use the the binding `Not-Nik/raylib-zig`?

If you are using it directly you could try this:

> [@Raylib example using the package manager](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/11):
>
> I managed to get raygui to work alongside raylib by downloading the single raygui header file. I added it as a local dependency in the build.zig.zon: .raygui = .{ .path = "vendor/raygui", // contains the raygui.h }, build.zig: const raygui\_dep = b.dependency("raygui", .{ .target = target, .optimize = raylib\_optimize, }); exe.addIncludePath(raygui\_dep.path("")); raylib.zig: pub usingnamespace @cImport({ @cInclude("raylib.h"); @cInclude("r…

The only thing I would try to change about this, is to use a second dependency on the raygui repository in the `build.zig.zon` instead of manually downloading the raygui header file.

This doesn’t use the raylib gui option, but just adds it directly in the cImport using the define, the problem with the raygui option (as far as I remember) is that it assumes a specific path where to find the raygui header and that doesn’t work well for all cases.

I guess in the future raylib could instead define raygui as an optional dependency and then it should work properly (without these shenanigans), but I think optional dependencies aren’t implemented yet.

* * *

If somebody tries / tests this, I would be happy to add this to the example repo as a raygui branch, or alternatively you could keep it in your own fork.

If you do, let me know and I will link to it in the readme, because it seems a bunch of people want this every few months.

---

<div class="post-metadata">

**Author:** ![jiangbo](https://ziggit.dev/user_avatar/ziggit.dev/jiangbo/32/801_2.png) [@jiangbo](https://ziggit.dev/u/jiangbo)\
**Post date:** [February 29, 2024, 3:06am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/3 "2024-02-29T03:06:42Z")

</div>

I used raylib directly.

---

<div class="post-metadata">

**Author:** ![jiangbo](https://ziggit.dev/user_avatar/ziggit.dev/jiangbo/32/801_2.png) [@jiangbo](https://ziggit.dev/u/jiangbo)\
**Post date:** [February 29, 2024, 3:15am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/4 "2024-02-29T03:15:19Z")

</div>

I downloaded the header file of raygui manually, but it didn’t work. I get a error:

```zig
root@jiangbo:~/workspace/demo# zig build run
run
└─ run demo
   └─ zig build-exe demo Debug native 1 errors
/root/workspace/demo/zig-cache/o/5204200a6170fd0d9018a4aabd4b699b/cimport.zig:7980:36: error: expected type 'usize', found '[*c]u8'
                    fileDataPtr += @as([*c]u8, @ptrFromInt(@sizeOf(c_int)));
                                   ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    GuiLoadStyle: /root/workspace/demo/zig-cache/o/5204200a6170fd0d9018a4aabd4b699b/cimport.zig:1554:17
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

```

build.zig.zon file:

```zig
.{
    .name = "demo",
    .version = "0.0.0",
    .dependencies = .{
        .raylib = .{
            .url = "https://github.com/raysan5/raylib/archive/ed9a6d862b9e31acaa59c6ffffd1f8aa398d54d8.tar.gz",
            .hash = "12200b5a963e542a0d0d3a4b588e2410951c407c68ea44d98ae0572f3a48cb2fa516",
        },
        .raygui = .{
            .path = "vendor/raygui", // contains the raygui.h
        },
    },
    .paths = .{""},
}

```

build.zig:

```zig
const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "demo",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const raylib_dep = b.dependency("raylib", .{
        .target = target,
        .optimize = optimize,
        // .raygui = true,
    });
    exe.linkLibrary(raylib_dep.artifact("raylib"));

    const raygui_dep = b.dependency("raygui", .{
        .target = target,
        .optimize = optimize,
    });
    exe.addIncludePath(raygui_dep.path(""));

    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}

```

raylib.zig:

```zig
pub usingnamespace @cImport({
    @cInclude("raylib.h");
    @cDefine("RAYGUI_IMPLEMENTATION", {});
    @cInclude("raygui.h");
});

```

main.zig:

```zig
const std = @import("std");
const ray = @import("raylib.zig");

pub fn main() !void {
    const screenWidth: c_int = 800;
    const screenHeight: c_int = 450;

    ray.InitWindow(screenWidth, screenHeight, "raylib [shapes] example");
    defer ray.CloseWindow();
    ray.SetTargetFPS(60);

    while (!ray.WindowShouldClose()) {

        // Update

        // Draw
        ray.BeginDrawing();
        defer ray.EndDrawing();
        ray.ClearBackground(ray.RAYWHITE);

        const rec = ray.Rectangle{ .x = 600, .y = 40, .width = 120, .height = 20 };
        _ = ray.GuiSliderBar(rec, "StartAngle", null, 0, -450, 450);

        ray.DrawFPS(10, 10);
    }
}

```

zig version: 0.12.0-dev.3076+6e078883e

github repo: [GitHub - jiangbo/demo](https://github.com/jiangbo/demo)

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 29, 2024, 4:13am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/5 "2024-02-29T04:13:22Z")

</div>

your raygui version is too old (at least I suspect that if you downloaded it from the raygui 4 release):

> raygui v4.1-dev

try a recent commit like [raygui/src/raygui.h at bc67f42209cc32965c7d1c00028e3ed0c4158659 · raysan5/raygui · GitHub](https://github.com/raysan5/raygui/blob/bc67f42209cc32965c7d1c00028e3ed0c4158659/src/raygui.h)  
(the comment is still old but I think the source is updated)

---

<div class="post-metadata">

**Author:** ![jiangbo](https://ziggit.dev/user_avatar/ziggit.dev/jiangbo/32/801_2.png) [@jiangbo](https://ziggit.dev/u/jiangbo)\
**Post date:** [February 29, 2024, 5:50am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/6 "2024-02-29T05:50:18Z")

</div>

I downloaded it from the URL you gave and still get the same error. I’m not quite sure if it’s a problem with raygui or zig source conversion.

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 29, 2024, 10:14am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/7 "2024-02-29T10:14:01Z")

</div>

> [@jiangbo](#):
>
> ```zig
> fileDataPtr += @as([*c]u8, @ptrFromInt(@sizeOf(c_int)));
> ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> ```

I also got that error.  
I modified the source to ~~`fileDataPtr += @as(usize, @sizeOf(c_int));`~~ to make it more similar like other lines and with that I got a few similar errors.  
(I currently don’t know why these don’t get generated correctly, automatically)

After that it compiled and segfaulted, because of a null pointer being dereferenced.  
I had to change the code to:

```zig
    ...
    const rec = ray.Rectangle{ .x = 600, .y = 40, .width = 120, .height = 20 };
    var value: f32 = 0;

    while (!ray.WindowShouldClose()) {

        // Update

        // Draw
        ray.BeginDrawing();
        defer ray.EndDrawing();
        ray.ClearBackground(ray.RAYWHITE);

        _ = ray.GuiSliderBar(rec, "StartAngle", null, &value, -450, 450);

        ray.DrawFPS(10, 10);
    }

```

The `&value` parameter expects a pointer to a float variable that can be set to the value of the slider.

With that it worked.

It seems the cImport generates code that doesn’t work, I wonder whether there are more cases in other functions (that just aren’t used yet).  
I don’t know whether there is a general solution to this, or if maybe raygui uses something that isn’t well supported by cImport.  
~~It seems the `sscanf` calls in `GuiLoadStyle` don’t always get converted correctly.~~

* * *

> <https://github.com/raysan5/raygui/blob/bc67f42209cc32965c7d1c00028e3ed0c4158659/src/raygui.h#L4429>

Somehow becomes:

```zig
fileDataPtr += @as([*c]u8, @ptrFromInt(@sizeOf(c_int)));

```

I think it should just be:

```zig
fileDataPtr += @as(usize, @intCast(recsDataCompressedSize));
// and the next ones:
// fileDataPtr += @as([*c]u8, @ptrFromInt(@sizeOf(Rectangle)));
fileDataPtr += @as(usize, @sizeOf(Rectangle));
// fileDataPtr += @as([*c]u8, @ptrFromInt(@sizeOf(c_int)));
fileDataPtr += @as(usize, @sizeOf(c_int));

```

It seems cImport gets confused by the pointer arithmetic and instead wants to add a pointer to the pointer instead of adding an integer value to the pointer.

* * *

Edit:  
I think the whole issue can be avoided by only applying translate-c to the cImported header file of raygui, but using a c source file for the implementation part. Raylib does this in its `build.zig`: [raylib/src/build.zig at 30f9ca7eb60d527e623066bbdc7257474da2baef · raysan5/raylib · GitHub](https://github.com/raysan5/raylib/blob/30f9ca7eb60d527e623066bbdc7257474da2baef/src/build.zig#L239)

This uses raygui directly via the `addRaylib` function provided by raylib: [Adding raygui · Issue #5 · SimonLSchlee/zigraylib · GitHub](https://github.com/SimonLSchlee/zigraylib/issues/5) and commit [added raygui #5 · SimonLSchlee/zigraylib@60b2ed0 · GitHub](https://github.com/SimonLSchlee/zigraylib/commit/60b2ed0af9634421a936308152315c0017194c3b)

---

<div class="post-metadata">

**Author:** ![Durobot](https://ziggit.dev/user_avatar/ziggit.dev/durobot/32/447_2.png) [@Durobot](https://ziggit.dev/u/Durobot)\
**Post date:** [February 29, 2024, 10:17am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/8 "2024-02-29T10:17:23Z")

</div>

Hi, jiangbo!

You can try exploring what I do in my [Zig Raylib examples](https://github.com/Durobot/raylib-zig-examples) collection, namely in examples [42](https://github.com/Durobot/raylib-zig-examples/tree/main/zig-raylib-42-shapes-draw-ring), [43](https://github.com/Durobot/raylib-zig-examples/tree/main/zig-raylib-43-shapes-draw-circle_sector), and [44](https://github.com/Durobot/raylib-zig-examples/tree/main/zig-raylib-44-shapes-draw-rectangle_rounded) - I just download [raygui/src/raygui.h at master · raysan5/raygui · GitHub](https://github.com/raysan5/raygui/blob/master/src/raygui.h) to their folders.  
There’s a small C “adapter”, [raygui\_impl.c](https://github.com/Durobot/raylib-zig-examples/blob/main/zig-raylib-42-shapes-draw-ring/raygui_impl.c) in these examples. I use it because raygui.h is a single-header C library.

The command that is actually used to build these three examples is

```zig
zig build-exe main.zig raygui_impl.c -O Debug -idirafter ./ -lc -lraylib

```

You can try playing with my project to see if it compiles and work from that point in the direction you want.

---

<div class="post-metadata">

**Author:** ![jiangbo](https://ziggit.dev/user_avatar/ziggit.dev/jiangbo/32/801_2.png) [@jiangbo](https://ziggit.dev/u/jiangbo)\
**Post date:** [March 1, 2024, 1:23am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/9 "2024-03-01T01:23:14Z")

</div>

Wow, thank you, raygui works.

---

<div class="post-metadata">

**Author:** ![jiangbo](https://ziggit.dev/user_avatar/ziggit.dev/jiangbo/32/801_2.png) [@jiangbo](https://ziggit.dev/u/jiangbo)\
**Post date:** [March 1, 2024, 1:27am UTC](https://ziggit.dev/t/using-raygui-with-raylib/3399/10 "2024-03-01T01:27:00Z")

</div>

This is a great learning material for Raylib and Zig, and I believe I can learn a lot from it.
