Skip to content

Latest commit

 

History

History
203 lines (132 loc) · 19.1 KB

README.md

File metadata and controls

203 lines (132 loc) · 19.1 KB

Raylib Zig Examples

These are some of raylib (raylib on github) examples ported to Zig.

See the screenshot gallery!

Please note these are raylib 4.5 examples, they have been updated to compile with either raylib 4.5 or raylib 5.0, but the content of example programs has not been updated to match raylib 5.0 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

Building the examples

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:

  1. 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 works too so far, 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.367+a57479afc. 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 .bashrc file in your home folder (replace /path/to/zig with the actual path, of course):

    export PATH="$PATH:/path/to/zig"

    Alternatively, you can install Zig from your distribution's repositories, if they contain Zig 0.11.

  2. Install raylib. Versions 4.5 and 5.0 do work. Earlier or later version may work too. Use one of the following methods:

    1. Install it from your distribution's repositories. For example on Arch you can do it with pacman -S raylib command.

      You then should be able to build examples by running zig build-exe main.zig -lc -lraylib in each example's folder. To build using build_example.sh, (optionally) edit this file, setting RAYLIB_PATH, RAYLIB_INCLUDE_PATH, RAYLIB_EXTERNAL_INCLUDE_PATH and RAYLIB_LIB_PATH variables to '' (empty string). To build using build.sh found in each folder, (optionally) edit build.sh, setting tmp_raylib_path, tmp_raylib_include_path, tmp_raylib_external_include_path and tmp_raylib_lib_path variables to '' (empty string) at lines 12 - 15.

      Alternatively, you can

    2. 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:

      1. If you're using raylib 5.0, open src\build.zig, find lines containing lib.installHeader (they should be in pub fn build), and add the following line after them:

        lib.installHeader("src/rcamera.h", "rcamera.h");

        Otherwise example 13 won't compile.

      2. If you're using raylib 4.5, do one of the following:

        a. If build.zig in 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.zig in raylib's root folder does not contain lib.install(); (see this commit), then in src/build.zig, in function pub fn build(b: *std.Build) void, after lib.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=ReleaseSmall or zig build -Doptimize=ReleaseFast. You could also use -Doptimize=ReleaseSafe, -Doptimize=Debug or simply run zig build.

      This should create zig-out folder, with two folders inside: include and lib, these contain raylib header files and static library, respectively.

      In raylib-zig-examples, in build_example.shset RAYLIB_PATH variable to the correct raylib path and make sure the values of RAYLIB_INCLUDE_PATH, RAYLIB_EXTERNAL_INCLUDE_PATH and RAYLIB_LIB_PATH make sense.

  3. Build the examples. You can use build_example.sh to 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.sh contained in each example's folder. raylib paths and Zig build mode set within each build.sh are used in this case.

    clean_all.sh and clean.sh in examples' folders can be used to delete binaries generated by the compiler.

On Windows:

  1. 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 works too so far, 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.367+a57479afc. 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.

  2. 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:

    1. If you're using raylib 5.0, open src\build.zig, find lines containing lib.installHeader (they should be in pub fn build), and add the following line after them:

      lib.installHeader("src/rcamera.h", "rcamera.h");

      Otherwise example 13 won't compile.

    2. If you're using raylib 4.5, do one of the following:

      a. If build.zig in 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.zig in raylib's root folder does not contain lib.install(); (see this commit), then in src/build.zig, in function pub fn build(b: *std.Build) void, after lib.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=ReleaseSmall or zig build -Doptimize=ReleaseFast.

    Warning: leaving out -Doptimize parameter, using -Doptimize=Debug or -Doptimize=ReleaseSafe currently 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_guard
    

    Running zig build... should create zig-out folder, with two folders inside: include and lib, these contain raylib header files and static library, respectively.

    In raylib-zig-examples, in build_example.batset RAYLIB_PATH variable to the correct raylib path and make sure the values of RAYLIB_INCLUDE_PATH, RAYLIB_EXTERNAL_INCLUDE_PATH and RAYLIB_LIB_PATH make sense.

  3. Build the examples. You can use build_example.bat to 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.bat contained in each example's folder. raylib paths and Zig build mode set within each build.bat are used in this case.

    clean_all.bat and clean.bat in examples' folders can be used to delete binaries generated by the compiler.