├── src ├── raylib.zig └── main.zig └── README.md /src/raylib.zig: -------------------------------------------------------------------------------- 1 | pub const raylib = @cImport({ 2 | @cInclude("raylib.h"); 3 | @cInclude("raymath.h"); 4 | @cInclude("rlgl.h"); 5 | }); 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zigraylib 2 | A fairly minimal [raylib](https://www.raylib.com/) [zig](https://ziglang.org/download/) example codebase using the zig package manager. 3 | 4 | ## install 5 | ```bash 6 | git clone https://github.com/SimonLSchlee/zigraylib.git 7 | cd zigraylib 8 | zig build run 9 | ``` 10 | 11 | tested with zig version: `0.14.0` 12 | using raylib commit: https://github.com/raysan5/raylib/tree/bbeade636cd8c90e18b7e9e841c20b5f8bd15d94 13 | 14 | ## resources 15 | [raylib cheatsheet](https://www.raylib.com/cheatsheet/cheatsheet.html) 16 | [Learn ⚡ Zig Programming Language](https://ziglang.org/learn/) 17 | [Zig Community](https://github.com/ziglang/zig/wiki/Community) 18 | 19 | ## raygui 20 | 21 | If you want to use raygui checkout the [raygui](https://github.com/SimonLSchlee/zigraylib/tree/raygui) branch. 22 | 23 | ## code completion 24 | [zls installation](https://github.com/zigtools/zls/wiki/Installation) 25 | 26 | ## ideas or improvements? 27 | Let me know if you have ideas about things that could make this example better. 28 | I want to keep it fairly minimal, but maybe we can add some more links to useful resources, or point out common problems. 29 | Ziggit topic for discussion: https://ziggit.dev/t/raylib-example-using-the-package-manager/1787 30 | 31 | ## license 32 | public domain or MIT 33 | -------------------------------------------------------------------------------- /src/main.zig: -------------------------------------------------------------------------------- 1 | const std = @import("std"); 2 | const builtin = @import("builtin"); 3 | const ray = @import("raylib.zig").raylib; 4 | 5 | pub fn main() !void { 6 | try ray_main(); 7 | try old_main(); // remove this if you don't need it 8 | try hints(); 9 | } 10 | 11 | var debug_allocator: std.heap.DebugAllocator(.{}) = .init; 12 | 13 | fn ray_main() !void { 14 | var gpa, const is_debug = gpa: { 15 | if (builtin.os.tag == .wasi) break :gpa .{ std.heap.wasm_allocator, false }; 16 | break :gpa switch (builtin.mode) { 17 | .Debug, .ReleaseSafe => .{ debug_allocator, true }, 18 | .ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false }, 19 | }; 20 | }; 21 | defer _ = if (is_debug) debug_allocator.deinit(); 22 | var allocator = gpa.allocator(); 23 | 24 | // const monitor = ray.GetCurrentMonitor(); 25 | // const width = ray.GetMonitorWidth(monitor); 26 | // const height = ray.GetMonitorHeight(monitor); 27 | const width = 800; 28 | const height = 450; 29 | 30 | ray.SetConfigFlags(ray.FLAG_MSAA_4X_HINT | ray.FLAG_VSYNC_HINT); 31 | ray.InitWindow(width, height, "zig raylib example"); 32 | defer ray.CloseWindow(); 33 | 34 | const colors = [_]ray.Color{ ray.GRAY, ray.RED, ray.GOLD, ray.LIME, ray.BLUE, ray.VIOLET, ray.BROWN }; 35 | const colors_len: i32 = @intCast(colors.len); 36 | var current_color: i32 = 2; 37 | var hint = true; 38 | 39 | while (!ray.WindowShouldClose()) { 40 | // input 41 | var delta: i2 = 0; 42 | if (ray.IsKeyPressed(ray.KEY_UP)) delta += 1; 43 | if (ray.IsKeyPressed(ray.KEY_DOWN)) delta -= 1; 44 | if (delta != 0) { 45 | current_color = @mod(current_color + delta, colors_len); 46 | hint = false; 47 | } 48 | 49 | // draw 50 | { 51 | ray.BeginDrawing(); 52 | defer ray.EndDrawing(); 53 | 54 | ray.ClearBackground(colors[@intCast(current_color)]); 55 | if (hint) ray.DrawText("press up or down arrow to change background color", 120, 140, 20, ray.BLUE); 56 | ray.DrawText("Congrats! You created your first window!", 190, 200, 20, ray.BLACK); 57 | 58 | // now lets use an allocator to create some dynamic text 59 | // pay attention to the Z in `allocPrintZ` that is a convention 60 | // for functions that return zero terminated strings 61 | const seconds: u32 = @intFromFloat(ray.GetTime()); 62 | const dynamic = try std.fmt.allocPrintZ(allocator, "running since {d} seconds", .{seconds}); 63 | defer allocator.free(dynamic); 64 | ray.DrawText(dynamic, 300, 250, 20, ray.WHITE); 65 | 66 | ray.DrawFPS(width - 100, 10); 67 | } 68 | } 69 | } 70 | 71 | // remove this function if you don't need it 72 | fn old_main() !void { 73 | // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) 74 | std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); 75 | 76 | // stdout is for the actual output of your application, for example if you 77 | // are implementing gzip, then only the compressed bytes should be sent to 78 | // stdout, not any debugging messages. 79 | const stdout_file = std.io.getStdOut().writer(); 80 | var bw = std.io.bufferedWriter(stdout_file); 81 | const stdout = bw.writer(); 82 | 83 | try stdout.print("Run `zig build test` to run the tests.\n", .{}); 84 | 85 | try bw.flush(); // don't forget to flush! 86 | } 87 | 88 | fn hints() !void { 89 | const stdout_file = std.io.getStdOut().writer(); 90 | var bw = std.io.bufferedWriter(stdout_file); 91 | const stdout = bw.writer(); 92 | 93 | try stdout.print("\n⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯\n", .{}); 94 | try stdout.print("Here are some hints:\n", .{}); 95 | try stdout.print("Run `zig build --help` to see all the options\n", .{}); 96 | try stdout.print("Run `zig build -Doptimize=ReleaseSmall` for a small release build\n", .{}); 97 | try stdout.print("Run `zig build -Doptimize=ReleaseSmall -Dstrip=true` for a smaller release build, that strips symbols\n", .{}); 98 | try stdout.print("Run `zig build -Draylib-optimize=ReleaseFast` for a debug build of your application, that uses a fast release of raylib (if you are only debugging your code)\n", .{}); 99 | try stdout.print("\nDon't forget to update your `build.zig.zon`!\n", .{}); 100 | 101 | try bw.flush(); // don't forget to flush! 102 | } 103 | 104 | test "simple test" { 105 | var list = std.ArrayList(i32).init(std.testing.allocator); 106 | defer list.deinit(); // try commenting this out and see if zig detects the memory leak! 107 | try list.append(42); 108 | try std.testing.expectEqual(@as(i32, 42), list.pop()); 109 | } 110 | --------------------------------------------------------------------------------