Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Briand committed May 24, 2024
0 parents commit 5f16210
Show file tree
Hide file tree
Showing 13 changed files with 3,925 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
zig-cache
zig-out
release.tar.gz
16 changes: 16 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Copyright (c) 2024 Lionel Briand (@L-Briand)

This software is provided "as-is", without any express or implied warranty. In no event
will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial
applications, and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you
wrote the original software. If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be misrepresented
as being the original software.

3. This notice may not be removed or altered from any source distribution.
150 changes: 150 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Raylib Zig Bindings

One to one translation of [raylib](https://github.com/raysan5/raylib)
and [raygui](https://github.com/raysan5/raygui) `.h` files in zig **and** build tools to compile raylib from source.

- Raylib version `5.0`
- Raygui version `4.0`
- Zig version`0.12.0`

At first, I wanted to learn how the zig build system worked and make a game.
But I took too much time, and now it's a tool to compile raylib directly from source with zig package manager and build
system.

# TLDR

[Sample project here]() using what's described below

# Setting up a project

You'll need raylib and raygui sources with the correct version:

```bash
git clone --depth 1 --branch 5.0 https://github.com/raysan5/raylib.git
git clone --depth 1 --branch 4.0 https://github.com/raysan5/raygui.git
```

Create a `build.zig.zon` file and use this project as dependency.

```zon
.{
.name = "game",
.version = "0.0.0",
.dependencies = .{
.@"raylib-zig-bindings" = .{
.url = "TODO",
.hash = "TODO",
}
},
.paths = .{
"",
},
}
```

Create a `build.zig` file looking like this:

```zig
const std = @import("std");
// Import the project from zon file.
const rlzb = @import("raylib-zig-bindings");
pub fn build(b: *std.Build) !void {
// Default zig setup
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "game",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// Adding rlzb binding files for us to use in the main.zig file.
const bindings = b.dependency("raylib-zig-bindings", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("rlzb", bindings.module("raylib-zig-bindings"));
// Compiling raylib with main.zig
// You can select which raylib C file to add in the third parameter
var setup = try rlzb.Setup.init(b, .{ .cwd_relative = "raylib/src" }, .{});
defer setup.deinit();
// This line copy the raygui.h file into raylib/src to build with it.
try setup.addRayguiToRaylibSrc(b, .{ .cwd_relative = "raygui/src/raygui.h" });
// If you have some raylib's C #define requirements that need to be at build time. You can set them here.
setup.setRayguiOptions(b, exe, .{});
setup.setRCameraOptions(b, exe, .{});
setup.setRlglOptions(b, exe, .{});
// Build specific for platform.
switch (target.result.os.tag) {
.windows => try setup.linkWindows(exe),
.macos => try setup.linkMacos(b, exe),
.linux => try setup.linkLinux(b, exe, .{ .platform = .DESKTOP, .backend = .X11 }),
else => @panic("Unsupported os"),
}
// Add everything to the exe.
setup.finalize(b, exe);
// Default zig build run command setup
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);
}
```

And create a `src/main.zig` file. This is the translation
of [raygui code sample](https://github.com/raysan5/raygui/tree/master?tab=readme-ov-file#code-sample).

```zig
const std = @import("std");
const rlzb = @import("rlzb");
const rl = rlzb.raylib;
const rg = rlzb.raygui;
pub fn main() !void {
rl.InitWindow(400, 200, "raygui - controls test suite");
defer rl.CloseWindow();
rl.SetTargetFPS(60);
var showMessageBox = false;
while (!rl.WindowShouldClose()) {
rl.BeginDrawing();
const style = rg.GuiGetStyle(
rg.GuiControl.DEFAULT.toValue(),
rg.GuiDefaultProperty.BACKGROUND_COLOR.toValue(),
);
rl.ClearBackground(rl.GetColor(@bitCast(style)));
if (rg.GuiButton(rl.Rectangle.init(24, 24, 120, 30), "#191#Show Message") > 0)
showMessageBox = true;
if (showMessageBox) {
const bounds = rl.Rectangle.init(85, 70, 250, 100);
const result = rg.GuiMessageBox(bounds, "#191#Message Box", "Hi! This is a message!", "Nice;Cool");
if (result >= 0) showMessageBox = false;
}
rl.EndDrawing();
}
return;
}
```

Then run `zig build run`. You should see a window popping up.

![Raygui window](./raygui_screenshot.png)
65 changes: 65 additions & 0 deletions bindings/bindings.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
pub const raylib = @import("raylib.zig");
pub const rcamera = @import("rcamera.zig");
pub const rlgl = @import("rlgl.zig");
pub const raygui = @import("raygui.zig");

pub const rayall = struct {
usingnamespace raylib;
usingnamespace raylib.ConfigFlags;
usingnamespace raylib.TraceLogLevel;
usingnamespace raylib.KeyboardKey;
usingnamespace raylib.MouseButton;
usingnamespace raylib.MouseCursor;
usingnamespace raylib.GamepadButton;
usingnamespace raylib.GamepadAxis;
usingnamespace raylib.MaterialMapIndex;
usingnamespace raylib.ShaderLocationIndex;
usingnamespace raylib.ShaderUniformDataType;
usingnamespace raylib.ShaderAttributeDataType;
usingnamespace raylib.PixelFormat;
usingnamespace raylib.TextureFilter;
usingnamespace raylib.TextureWrap;
usingnamespace raylib.CubemapLayout;
usingnamespace raylib.FontType;
usingnamespace raylib.BlendMode;
usingnamespace raylib.Gesture;
usingnamespace raylib.CameraMode;
usingnamespace raylib.CameraProjection;
usingnamespace raylib.NPatchLayout;

usingnamespace rcamera;

usingnamespace rlgl;
usingnamespace rlgl.rlGlVersion;
usingnamespace rlgl.rlTraceLogLevel;
usingnamespace rlgl.rlPixelFormat;
usingnamespace rlgl.rlTextureFilter;
usingnamespace rlgl.rlBlendMode;
usingnamespace rlgl.rlShaderLocationIndex;
usingnamespace rlgl.rlShaderUniformDataType;
usingnamespace rlgl.rlShaderAttributeDataType;
usingnamespace rlgl.rlFramebufferAttachType;
usingnamespace rlgl.rlFramebufferAttachTextureType;
usingnamespace rlgl.rlCullMode;

usingnamespace raygui;
usingnamespace raygui.GuiState;
usingnamespace raygui.GuiTextAlignment;
usingnamespace raygui.GuiTextAlignmentVertical;
usingnamespace raygui.GuiTextWrapMode;
usingnamespace raygui.GuiControl;
usingnamespace raygui.GuiControlProperty;
usingnamespace raygui.GuiDefaultProperty;
usingnamespace raygui.GuiToggleProperty;
usingnamespace raygui.GuiSliderProperty;
usingnamespace raygui.GuiProgressBarProperty;
usingnamespace raygui.GuiScrollBarProperty;
usingnamespace raygui.GuiCheckBoxProperty;
usingnamespace raygui.GuiComboBoxProperty;
usingnamespace raygui.GuiDropdownBoxProperty;
usingnamespace raygui.GuiTextBoxProperty;
usingnamespace raygui.GuiSpinnerProperty;
usingnamespace raygui.GuiListViewProperty;
usingnamespace raygui.GuiColorPickerProperty;
usingnamespace raygui.GuiIconName;
};
Loading

0 comments on commit 5f16210

Please sign in to comment.