These are some of raylib (raylib on github) examples ported to Zig.
Please note these are raylib 4.5 examples, they have been updated to compile with either raylib 4.5, raylib 5.0 or raylib 5.5, but the content of example programs has not been updated to match raylib 5.0 or 5.5 examples.
The examples don't use any bindings or some other intermediate layer between Zig code and raylib. Instead, Zig's built-in translate-C feature takes care of everything (well, almost, see below).
For whatever reason, example 27 (custom frame control) does not work properly on Windows, and runs with certain jerkiness on Linux. My knowledge of raylib is not enough to figure out why.
I have done some minor modifications to the code, like changing camelCase variable names to snake_case, to fit Zig naming conventions.
Some of the examples are presented in multiple versions (14a and 14b; 54a and 54b; 87a and 87b), see the comments in the Zig code.
To make things easier, some of the examples come with resource files, necessary to run them. Their authors are credited below:
| resource | examples | author | licence | notes |
|---|---|---|---|---|
| raylib_logo.png | 46, 50, 53 | @raysan5 (?) | ? | |
| fudesumi.raw | 54a, 54b | Eiden Marsal | CC-BY-NC | |
| road.png | 68 | ? | ? | |
| fonts/alagard.png | 69 | Hewett Tsoi | Freeware | Atlas created by @raysan5 |
| fonts/alpha_beta.png | 69 | Brian Kent (AEnigma) | Freeware | Atlas created by @raysan5 |
| fonts/jupiter_crash.png | 69 | Brian Kent (AEnigma) | Freeware | Atlas created by @raysan5 |
| fonts/mecha.png | 69 | Captain Falcon | Freeware | Atlas created by @raysan5 |
| fonts/pixantiqua.ttf | 69 | Gerhard Großmann | Freeware | Atlas created by @raysan5 |
| fonts/pixelplay.png | 69 | Aleksander Shevchuk | Freeware | Atlas created by @raysan5 |
| fonts/romulus.png | 69 | Hewett Tsoi | Freeware | Atlas created by @raysan5 |
| fonts/setback.png | 69 | Brian Kent (AEnigma) | Freeware | Atlas created by @raysan5 |
| custom_alagard.png | 70 | Brian Kent (AEnigma) | Freeware | Atlas created by @raysan5 |
| custom_jupiter_crash.png | 70 | Brian Kent (AEnigma) | Freeware | Atlas created by @raysan5 |
| custom_mecha.png | 70 | Brian Kent (AEnigma) | Freeware | Atlas created by @raysan5 |
| KAISG.ttf | 71 | Dieter Steffmann | Freeware | Kaiserzeit Gotisch font |
| pixantiqua.fnt, pixantiqua.png | 72 | Gerhard Großmann | Freeware | Atlas made with BMFont by @raysan5 |
| pixantiqua.ttf | 72 | Gerhard Großmann | Freeware | |
| cubicmap.png | 84, 85 | @raysan5 | CC0 | |
| cubicmap_atlas.png | 84, 85 | @emegeme | CC0 |
Note: some examples require additional header files. I recommend downloading them to the corresponding example's folder, as described in the comments in Zig code.
Examples 39, 40, 41 need reasings.h from https://github.com/raysan5/raylib/blob/master/examples/others/reasings.h; examples 42, 43, 44 need raygui.h from https://github.com/raysan5/raygui/blob/master/src/raygui.h.
In other words, the following additional header files are necessary for their respective examples to build:
zig-raylib-39-shapes-easings-ball_anim/reasings.h
zig-raylib-40-shapes-easings-box_anim/reasings.h
zig-raylib-41-shapes-easings-rectangle_array/reasings.h
zig-raylib-42-shapes-draw-ring/raygui.h
zig-raylib-43-shapes-draw-circle_sector/raygui.h
zig-raylib-44-shapes-draw-rectangle_rounded/raygui.h
On Linux:
-
Install Zig - download from https://ziglang.org/download/.
Versions that work are 0.12.0, 0.12.1, 0.13.0 (current stable release), and 0.14.0 (nightly development build). 0.11.0 probably still works too (come on, really? 0.11?), but this may change in the future.
Latest version of Zig 0.14.0 I have tested the project with was 0.14.0-dev.3237+ddff1fa4c. Later versions may or may not work, you're welcome to try them and raise an issue on github if they don't.
Unpack your version of Zig and add its folder to environment variable PATH. In many Linux distributions this is done by adding the following line to the end of
.bashrcfile in your home folder (replace /path/to/zig with theactualpath, of course):export PATH="$PATH:/path/to/zig"Alternatively, you can install Zig from your distribution's repositories, if they contain Zig 0.12 and up.
-
Install raylib. Versions 4.5 and 5.0 do work. Earlier or later version may work too. Use one of the following methods:
-
Install it from your distribution's repositories. For example on Arch you can do it with
pacman -S raylibcommand.You then should be able to build examples by running
zig build-exe main.zig -lc -lraylibin each example's folder. To build usingbuild_example.sh, (optionally) edit this file, settingRAYLIB_PATH,RAYLIB_INCLUDE_PATH,RAYLIB_EXTERNAL_INCLUDE_PATHandRAYLIB_LIB_PATHvariables to '' (empty string). To build usingbuild.shfound in each folder, (optionally) edit build.sh, settingtmp_raylib_path,tmp_raylib_include_path,tmp_raylib_external_include_pathandtmp_raylib_lib_pathvariables to '' (empty string) at lines 12 - 15.Alternatively, you can
-
Build raylib from source code. Download raylib from github. Click "tar.gz" under the release you want to download, or click "Downloads", then scroll down and click "Source code (tar.gz)". Unpack the downloaded archive.
Now, in order to make raylib and/or raylib-zig-examples compile without errors, do one of the following, depending on your version of raylib:
-
If you're using raylib 5.0, open
src\build.zig, find lines containinglib.installHeader(they should be inpub fn build), and add the following line after them:lib.installHeader("src/rcamera.h", "rcamera.h");
Otherwise example 13 won't compile.
-
If you're using raylib 4.5, do one of the following:
a. If
build.zigin raylib root folder contains the following lines:const lib = raylib.addRaylib(b, target, optimize); lib.installHeader("src/raylib.h", "raylib.h"); lib.install();
then edit this file - remove or comment out this line:
lib.install();Add these lines below it, before the closing}:lib.installHeader("src/rlgl.h", "rlgl.h"); lib.installHeader("src/raymath.h", "raymath.h"); lib.installHeader("src/rcamera.h", "rcamera.h"); b.installArtifact(lib);
b. If, on the other hand,
build.zigin raylib's root folder does not containlib.install();(see this commit), then insrc/build.zig, in functionpub fn build(b: *std.Build) void, afterlib.installHeader("src/raylib.h", "raylib.h");, add these lines:lib.installHeader("src/rlgl.h", "rlgl.h"); lib.installHeader("src/raymath.h", "raymath.h"); lib.installHeader("src/rcamera.h", "rcamera.h");
In raylib root folder, run
zig build -Doptimize=ReleaseSmallorzig build -Doptimize=ReleaseFast. You could also use-Doptimize=ReleaseSafe,-Doptimize=Debugor simply runzig build.This should create
zig-outfolder, with two folders inside:includeandlib, these contain raylib header files and static library, respectively.In
raylib-zig-examples, inbuild_example.shsetRAYLIB_PATHvariable to the correct raylib path and make sure the values ofRAYLIB_INCLUDE_PATH,RAYLIB_EXTERNAL_INCLUDE_PATHandRAYLIB_LIB_PATHmake sense. -
-
-
Build the examples. You can use
build_example.shto either build individual examples by providing the example number, e.g../build_example.sh 03, or build them all:./build_example.sh all.You can also run
build.shcontained in each example's folder. raylib paths and Zig build mode set within each build.sh are used in this case.clean_all.shandclean.shin examples' folders can be used to delete binaries generated by the compiler.
On Windows:
-
Install Zig - download from https://ziglang.org/download/.
Versions that work are 0.12.0, 0.12.1, 0.13.0 (current stable release), and 0.14.0 (nightly development build). 0.11.0 probably still works too (come on, really? 0.11?), but this may change in the future.
Latest version of Zig 0.14.0 I have tested the project with was 0.14.0-dev.3237+ddff1fa4c. Later versions may or may not work, you're welcome to try them and raise an issue on github if they don't.
Unpack your version of Zig and add its folder to environment variable PATH.
-
Install raylib. These examples were built using raylib 4.5.0, but an earlier or later version may work too.
Build raylib from source code. Download raylib from github. Click "zip" under the release you want to download, or click "Downloads", then scroll down and click "Source code (zip)".
Unpack the downloaded archive. Now, in order to make raylib and/or raylib-zig-examples compile without errors, do one of the following, depending on your version of raylib:
-
If you're using raylib 5.0, open
src\build.zig, find lines containinglib.installHeader(they should be inpub fn build), and add the following line after them:lib.installHeader("src/rcamera.h", "rcamera.h");
Otherwise example 13 won't compile.
-
If you're using raylib 4.5, do one of the following:
a. If
build.zigin raylib root folder contains the following lines:const lib = raylib.addRaylib(b, target, optimize); lib.installHeader("src/raylib.h", "raylib.h"); lib.install();
then edit this file - remove or comment out this line:
lib.install();Add these lines below it, before the closing}:lib.installHeader("src/rlgl.h", "rlgl.h"); lib.installHeader("src/raymath.h", "raymath.h"); lib.installHeader("src/rcamera.h", "rcamera.h"); b.installArtifact(lib);
b. If, on the other hand,
build.zigin raylib's root folder does not containlib.install();(see this commit), then insrc/build.zig, in functionpub fn build(b: *std.Build) void, afterlib.installHeader("src/raylib.h", "raylib.h");, add these lines:lib.installHeader("src/rlgl.h", "rlgl.h"); lib.installHeader("src/raymath.h", "raymath.h"); lib.installHeader("src/rcamera.h", "rcamera.h");
In raylib root folder, run
zig build -Doptimize=ReleaseSmallorzig build -Doptimize=ReleaseFast.Warning: leaving out
-Doptimizeparameter, using-Doptimize=Debugor-Doptimize=ReleaseSafecurrently causes compilation of raylib-zig-examples to fail in ReleaseSmall and ReleaseFast modes down the road. You will see errors similar to these:error: lld-link: undefined symbol: __stack_chk_fail error: lld-link: undefined symbol: __stack_chk_guardRunning zig build... should create
zig-outfolder, with two folders inside:includeandlib, these contain raylib header files and static library, respectively.In
raylib-zig-examples, inbuild_example.batsetRAYLIB_PATHvariable to the correct raylib path and make sure the values ofRAYLIB_INCLUDE_PATH,RAYLIB_EXTERNAL_INCLUDE_PATHandRAYLIB_LIB_PATHmake sense. -
-
Build the examples. You can use
build_example.batto either build individual examples by providing the example number, e.g.build_example.bat 03, or build them all:build_example.bat all.You can also run
build.batcontained in each example's folder. raylib paths and Zig build mode set within each build.bat are used in this case.clean_all.batandclean.batin examples' folders can be used to delete binaries generated by the compiler.