From c0f3adda16dcdc777bb7c1ca75e93c3f8428fead Mon Sep 17 00:00:00 2001 From: Dario48 Date: Sat, 18 Apr 2026 22:35:31 +0200 Subject: [PATCH] raylib --- .gitignore | 1 + build.zig | 37 +++++++++++++++-------------- build.zig.zon | 7 +++++- flake.nix | 64 +++++++++++++++++++++++++++++++-------------------- src/main.zig | 58 ++++++++++++++++++++++++++++++++-------------- 5 files changed, 107 insertions(+), 60 deletions(-) diff --git a/.gitignore b/.gitignore index 6708eb8..022992f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .zig-cache/ zig-out/ +zig-pkg/ result diff --git a/build.zig b/build.zig index ab09def..e112b7f 100644 --- a/build.zig +++ b/build.zig @@ -4,6 +4,16 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const raylib_dep = b.dependency("raylib_zig", .{ + .target = target, + .optimize = optimize, + .linux_display_backend = .Wayland, + }); + + const raylib = raylib_dep.module("raylib"); + const raygui = raylib_dep.module("raygui"); + const raylib_artifact = raylib_dep.artifact("raylib"); // raylib C library + const mod = b.addModule("sand", .{ .root_source_file = b.path("src/root.zig"), .target = target, @@ -17,36 +27,29 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .imports = &.{ .{ .name = "sand", .module = mod }, + .{ .name = "raylib", .module = raylib }, + .{ .name = "raygui", .module = raygui }, }, }), }); + exe.root_module.linkLibrary(raylib_artifact); b.installArtifact(exe); - const run_step = b.step("run", "Run the app"); - - const run_cmd = b.addRunArtifact(exe); - run_step.dependOn(&run_cmd.step); - - run_cmd.step.dependOn(b.getInstallStep()); - - if (b.args) |args| { - run_cmd.addArgs(args); - } - const mod_tests = b.addTest(.{ .root_module = mod, }); const run_mod_tests = b.addRunArtifact(mod_tests); - const exe_tests = b.addTest(.{ + const test_step = b.step("test", "Run tests"); + test_step.dependOn(&run_mod_tests.step); + + const exe_check = b.addExecutable(.{ + .name = "sand", .root_module = exe.root_module, }); - const run_exe_tests = b.addRunArtifact(exe_tests); - - const test_step = b.step("test", "Run tests"); - test_step.dependOn(&run_mod_tests.step); - test_step.dependOn(&run_exe_tests.step); + const check = b.step("check", "Check if foo compiles"); + check.dependOn(&exe_check.step); } diff --git a/build.zig.zon b/build.zig.zon index da441e3..7f12c5c 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,7 +3,12 @@ .version = "1.0.0", .fingerprint = 0xa07a92116fcc91c3, // Changing this has security and trust implications. .minimum_zig_version = "0.16.0", - .dependencies = .{}, + .dependencies = .{ + .raylib_zig = .{ + .url = "git+https://github.com/raylib-zig/raylib-zig?ref=devel#4b6a05cd102d51f00523dd5c5e18e628df359d20", + .hash = "raylib_zig-5.6.0-dev-KE8REKNmBQDek2Sz27ULioxYpY9IYR0K0CeIC-iJRLCI", + }, + }, .paths = .{ "build.zig", "build.zig.zon", diff --git a/flake.nix b/flake.nix index 1451025..a042331 100644 --- a/flake.nix +++ b/flake.nix @@ -14,31 +14,45 @@ let forAllSystems = f: - builtins.mapAttrs (system: pkgs: f pkgs zig.packages.${system}.zig_0_16_0) nixpkgs.legacyPackages; + builtins.foldl' nixpkgs.lib.recursiveUpdate { } ( + builtins.map (system: f system nixpkgs.legacyPackages.${system} zig.packages.${system}.zig_0_16_0) ( + builtins.attrNames nixpkgs.legacyPackages + ) + ); in - { - devShells = forAllSystems ( - pkgs: zig: { - default = pkgs.mkShellNoCC { - packages = [ - pkgs.bash - zig - zig.zls - ]; - }; - } - ); + forAllSystems ( + system: pkgs: zig: + let + nativeBuildInputs = with pkgs; [ + wayland-scanner + pkg-config + ]; + buildInputs = with pkgs; [ + libGL + wayland + libxkbcommon + ]; + in + { + devShells.${system}.default = pkgs.mkShellNoCC { + packages = [ + pkgs.bash + zig + zig.zls + ]; + inherit buildInputs; + inherit nativeBuildInputs; + }; - packages = forAllSystems ( - pkgs: zig: { - default = zig.makePackage { - pname = "sand"; - version = "1.0.0"; - src = ./.; - zigReleaseMode = "fast"; - depsHash = "sha256-pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo="; - }; - } - ); - }; + packages.${system}.default = zig.makePackage { + pname = "sand"; + version = "1.0.0"; + src = ./.; + zigReleaseMode = "fast"; + depsHash = "sha256-pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo="; + inherit buildInputs; + inherit nativeBuildInputs; + }; + } + ); } diff --git a/src/main.zig b/src/main.zig index 576d2ad..a4e9575 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,28 +2,52 @@ const std = @import("std"); const sand = @import("sand"); +const raylib = @import("raylib"); +const raygui = @import("raygui"); + pub fn main(init: std.process.Init) !void { - var grid: sand.Grid = try .init(4, 4, init.gpa); - var gridCopy: sand.Grid = try .init(4, 4, init.gpa); + const screenWidth = 800; + const screenHeight = 450; + + var grid: sand.Grid = try .init(screenWidth, screenHeight, init.gpa); + // var gridCopy: sand.Grid = try .init(4, 4, init.gpa); defer grid.deinit(init.gpa); - defer gridCopy.deinit(init.gpa); + // defer gridCopy.deinit(init.gpa); + // + // grid.arr[2][0].alive = true; + // grid.arr[2][1].alive = true; + // grid.arr[2][2].alive = true; + // grid.arr[2][3].alive = true; + // + // grid.arr[0][1].alive = true; + // grid.arr[1][1].alive = true; + // grid.arr[1][2].alive = true; + // + // std.debug.print("{f}\n", .{grid}); + // try grid.copy(&gridCopy); + // grid.updateAll(); + // + // while (!grid.eql(gridCopy)) { + // std.debug.print("{f}\n", .{grid}); + // try grid.copy(&gridCopy); + // grid.updateAll(); + // } - grid.arr[2][0].alive = true; - grid.arr[2][1].alive = true; - grid.arr[2][2].alive = true; - grid.arr[2][3].alive = true; + raylib.initWindow(screenWidth, screenHeight, "sand"); + defer raylib.closeWindow(); - grid.arr[0][1].alive = true; - grid.arr[1][1].alive = true; - grid.arr[1][2].alive = true; + raylib.setTargetFPS(60); - std.debug.print("{f}\n", .{grid}); - try grid.copy(&gridCopy); - grid.updateAll(); - - while (!grid.eql(gridCopy)) { - std.debug.print("{f}\n", .{grid}); - try grid.copy(&gridCopy); + while (!raylib.windowShouldClose()) { grid.updateAll(); + + raylib.beginDrawing(); + defer raylib.endDrawing(); + + for (grid.arr, 0..) |line, y| { + for (line, 0..) |cell, x| { + raylib.drawPixel(@intCast(x), @intCast(y), if (cell.alive) .white else .black); + } + } } }