$npx -y skills add rudedogg/zig-skills --skill zig-raylibZig bindings for raylib 5.5 game development library. Use when writing raylib games/applications in Zig, working with 2D/3D graphics, handling input, loading textures/sounds/models, or implementing game loops. Covers idiomatic Zig patterns for raylib including error handling with
| 1 | # Zig raylib 5.5 Bindings Reference |
| 2 | |
| 3 | Idiomatic Zig bindings for raylib 5.5, wrapping the C API with Zig patterns: error unions, optionals, slices, and defer-based resource management. |
| 4 | |
| 5 | **Version:** raylib 5.5+ (raylib-zig bindings) |
| 6 | **Minimum Zig:** 0.15.1 |
| 7 | |
| 8 | ## Critical: Build Configuration |
| 9 | |
| 10 | ### build.zig.zon Dependency |
| 11 | |
| 12 | ```zig |
| 13 | .dependencies = .{ |
| 14 | .raylib_zig = .{ |
| 15 | .url = "git+https://github.com/raylib-zig/raylib-zig#main", |
| 16 | .hash = "...", // Get from build error on first run |
| 17 | }, |
| 18 | }, |
| 19 | ``` |
| 20 | |
| 21 | ### build.zig Setup |
| 22 | |
| 23 | ```zig |
| 24 | const std = @import("std"); |
| 25 | |
| 26 | pub fn build(b: *std.Build) void { |
| 27 | const target = b.standardTargetOptions(.{}); |
| 28 | const optimize = b.standardOptimizeOption(.{}); |
| 29 | |
| 30 | // Get raylib-zig dependency |
| 31 | const raylib_dep = b.dependency("raylib_zig", .{ |
| 32 | .target = target, |
| 33 | .optimize = optimize, |
| 34 | }); |
| 35 | |
| 36 | const exe = b.addExecutable(.{ |
| 37 | .name = "my-game", |
| 38 | .root_module = b.createModule(.{ |
| 39 | .root_source_file = b.path("src/main.zig"), |
| 40 | .target = target, |
| 41 | .optimize = optimize, |
| 42 | }), |
| 43 | }); |
| 44 | |
| 45 | // Add raylib module and link library |
| 46 | exe.root_module.addImport("raylib", raylib_dep.module("raylib")); |
| 47 | exe.root_module.linkLibrary(raylib_dep.artifact("raylib")); |
| 48 | |
| 49 | // Optional: add raygui for GUI widgets |
| 50 | exe.root_module.addImport("raygui", raylib_dep.module("raygui")); |
| 51 | |
| 52 | b.installArtifact(exe); |
| 53 | |
| 54 | const run_cmd = b.addRunArtifact(exe); |
| 55 | run_cmd.step.dependOn(b.getInstallStep()); |
| 56 | |
| 57 | const run_step = b.step("run", "Run the game"); |
| 58 | run_step.dependOn(&run_cmd.step); |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ### Import in Code |
| 63 | |
| 64 | ```zig |
| 65 | const rl = @import("raylib"); |
| 66 | ``` |
| 67 | |
| 68 | ## Critical: Basic Game Loop |
| 69 | |
| 70 | ```zig |
| 71 | const rl = @import("raylib"); |
| 72 | |
| 73 | pub fn main() !void { |
| 74 | // Initialize window |
| 75 | rl.initWindow(800, 450, "My Game"); |
| 76 | defer rl.closeWindow(); |
| 77 | |
| 78 | rl.setTargetFPS(60); |
| 79 | |
| 80 | // Main game loop |
| 81 | while (!rl.windowShouldClose()) { |
| 82 | // Update game state here |
| 83 | |
| 84 | // Draw |
| 85 | rl.beginDrawing(); |
| 86 | defer rl.endDrawing(); |
| 87 | |
| 88 | rl.clearBackground(.ray_white); |
| 89 | rl.drawText("Hello, raylib!", 190, 200, 20, .dark_gray); |
| 90 | } |
| 91 | } |
| 92 | ``` |
| 93 | |
| 94 | ## Critical: Error Handling Pattern |
| 95 | |
| 96 | All loading functions return `RaylibError!T`: |
| 97 | |
| 98 | ```zig |
| 99 | pub const RaylibError = error{ |
| 100 | LoadFileData, LoadImage, LoadTexture, LoadRenderTexture, |
| 101 | LoadFont, LoadFontData, LoadShader, LoadModel, |
| 102 | LoadModelAnimations, LoadMaterial, LoadMaterials, |
| 103 | LoadWave, LoadSound, LoadMusic, LoadAudioStream, |
| 104 | // ... and more |
| 105 | }; |
| 106 | ``` |
| 107 | |
| 108 | Use `try` for loading resources: |
| 109 | |
| 110 | ```zig |
| 111 | const texture = try rl.loadTexture("assets/sprite.png"); |
| 112 | defer rl.unloadTexture(texture); |
| 113 | |
| 114 | const model = try rl.loadModel("assets/character.glb"); |
| 115 | defer rl.unloadModel(model); |
| 116 | |
| 117 | const shader = try rl.loadShader(null, "shaders/effect.fs"); |
| 118 | defer rl.unloadShader(shader); |
| 119 | ``` |
| 120 | |
| 121 | ## Critical: Common Mistakes (WRONG vs CORRECT) |
| 122 | |
| 123 | ```zig |
| 124 | // WRONG: Importing raymath as a separate module |
| 125 | const raymath = @import("raymath"); |
| 126 | // CORRECT: Access raymath through the raylib module |
| 127 | const rl = @import("raylib"); |
| 128 | // then use: rl.math.clamp(), rl.math.lerp(), rl.math.matrixMultiply(), etc. |
| 129 | |
| 130 | // WRONG: Importing rlgl as a separate module |
| 131 | const rlgl = @import("rlgl"); |
| 132 | // CORRECT: Access rlgl through the raylib module |
| 133 | const rlgl = rl.gl; // or use rl.gl.* directly |
| 134 | |
| 135 | // WRONG: Calling play as a method on sound |
| 136 | sound.play(); |
| 137 | // CORRECT: Use free function for sound playback |
| 138 | rl.playSound(sound); |
| 139 | |
| 140 | // WRONG: Calling crossProduct as a method on Vector2 |
| 141 | const cross = v1.crossProduct(v2); |
| 142 | // CORRECT: Vector2 crossProduct is a FREE FUNCTION only |
| 143 | const cross = rl.math.vector2CrossProduct(v1, v2); |
| 144 | // NOTE: Vector3 DOES have crossProduct as a method: v1.crossProduct(v2) |
| 145 | |
| 146 | // WRONG: Using try on functions that don't return error unions |
| 147 | const s = try rl.loadSoundFromWave(wave); |
| 148 | const a = try rl.loadSoundAlias(sound); |
| 149 | const img = try rl.getClipboardImage(); |
| 150 | // CORRECT: These return their types directly (no error union) |
| 151 | const s = rl.loadSoundFromWave(wave); |
| 152 | const a = rl.loadSoundAlias(sound); |
| 153 | const img = rl.getClipboardImage(); |
| 154 | |
| 155 | // WRONG: Expecting raygui button() to return i32 |
| 156 | const result: i32 = rg.button(bounds, "Click"); |
| 157 | // CORRECT: raygui button() returns bool |
| 158 | if (rg.button(bounds, "Click")) { ... } |
| 159 | |
| 160 | // WRONG: Passing const slice to raygui textBox |
| 161 | rg.textBox(bounds, "text", 64, editMode); |
| 162 | // CORRECT: textBox requires a mutable [:0]u8 slice |
| 163 | var buf: [64:0]u8 = .{0} ** 64; |
| 164 | _ = rg.textBox(bounds, &buf, 64, editMode); |
| 165 | ``` |
| 166 | |
| 167 | ## Critical: Resource Man |