Git Product home page Git Product logo

raylib-zig-wayland's Introduction

raylib-zig-wayland

Setup C interop with raylib on Wayland for dummies (aka me).

screenshot of GUI program

Environment

  • Zig 0.13.0
  • raylib commit efce4d6 July 25, 2024
  • Debian 12
  • SwayWM

Install System Libraries

You need to install dev libraries such as for X11/Wayland via your distro's package manager. We're on Debian so we'll follow the Ubuntu section of the raylib wiki.

sudo apt install libasound2-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev libglu1-mesa-dev libxcursor-dev libxinerama-dev libwayland-dev libxkbcommon-dev

This is because in raylib's own build file src/build.zig, you'll see it link system libraries.

fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile {
    // ...
    switch (target.result.os.tag) {
        // ...
        .linux => {
            // ...
                if (options.linux_display_backend == .Wayland or options.linux_display_backend == .Both) {
                    raylib.defineCMacro("_GLFW_WAYLAND", null);
                    raylib.linkSystemLibrary("wayland-client");
                    raylib.linkSystemLibrary("wayland-cursor");
                    raylib.linkSystemLibrary("wayland-egl");
                    raylib.linkSystemLibrary("xkbcommon");
                    raylib.addIncludePath(b.path("src"));
                    waylandGenerate(b, raylib, "wayland.xml", "wayland-client-protocol");
                    waylandGenerate(b, raylib, "xdg-shell.xml", "xdg-shell-client-protocol");
                    waylandGenerate(b, raylib, "xdg-decoration-unstable-v1.xml", "xdg-decoration-unstable-v1-client-protocol");
                    waylandGenerate(b, raylib, "viewporter.xml", "viewporter-client-protocol");
                    waylandGenerate(b, raylib, "relative-pointer-unstable-v1.xml", "relative-pointer-unstable-v1-client-protocol");
                    waylandGenerate(b, raylib, "pointer-constraints-unstable-v1.xml", "pointer-constraints-unstable-v1-client-protocol");
                    waylandGenerate(b, raylib, "fractional-scale-v1.xml", "fractional-scale-v1-client-protocol");
                    waylandGenerate(b, raylib, "xdg-activation-v1.xml", "xdg-activation-v1-client-protocol");
                    waylandGenerate(b, raylib, "idle-inhibit-unstable-v1.xml", "idle-inhibit-unstable-v1-client-protocol");
                }
                raylib.defineCMacro("PLATFORM_DESKTOP", null);
            // ...
}

Linking raylib

There's two ways to do this. Either add the raylib repo as a git submodule or zig fetch --save a raylib tarball. Let's use zig fetch.

Zig Fetch

Source Code Archive URL

Since the release of raylib 5.0 back in November 2023, there's been breaking changes in Zig's build system and updates to raylib's wayland compatibility. The latest raylib commit addresses these changes so let's use it.

zig fetch --save=raylib zig fetch --save=raylib https://github.com/raysan5/raylib/archive/efce4d69ce913bca42289184b0bffe4339c0193f.tar.gz 

This is a source code archive URL. It's a tarball of a specific commit. You won't find this URL via the GitHub user interface. It is something you'll have to type in. Refer to the GitHub docs.

The alternative here is to fetch Source Code (tar.gz) of a specific release.

Zig fetch then automatically sets up build.zig.zon for you.

build.zig and main.zig

As for setting up build.zig and importing raylib in yourfile.zig, I don't know too lol. I just copied this minimal zig raylib repo. Don't let nescience stop you from progressing.

Git Submodule

Cloning

We could also keep a local copy of the raylib repo and manually link the library. In the project root directory:

mkdir lib
cd lib
git submodule add https://github.com/raysan5/raylib.git

Refer to this youtube video from Codotaku

build.zig.zon

// ...
.dependencies = .{
    .raylib = .{
        .path = "lib/raylib"
    },
},
// ...

raylib-zig-wayland's People

Contributors

amesaine avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.