From e0657afa535a6fd5d90e80363d2e9b6fdc5331c6 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Fri, 29 Sep 2023 15:39:51 +0200 Subject: [PATCH 1/3] add zig packaging --- zig/build.zig | 8 ++++++++ zig/build.zig.zon | 10 ++++++++++ 2 files changed, 18 insertions(+) create mode 100644 zig/build.zig.zon diff --git a/zig/build.zig b/zig/build.zig index 93ff5e0..2a7e984 100644 --- a/zig/build.zig +++ b/zig/build.zig @@ -15,6 +15,12 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); + const raylib_dep = b.dependency("raylib", .{ + // These are the arguments to the dependency. It expects a target and optimization level. + .target = target, + .optimize = optimize, + }); + const exe = b.addExecutable(.{ .name = "zig", // In this case the main source file is merely a path, however, in more @@ -24,6 +30,8 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + exe.addModule(raylib_dep.module("some_mod")); + // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default // step when running `zig build`). diff --git a/zig/build.zig.zon b/zig/build.zig.zon new file mode 100644 index 0000000..7fb9d8c --- /dev/null +++ b/zig/build.zig.zon @@ -0,0 +1,10 @@ +.{ + .name = "fabapp", + .version = "0.1.0", + .dependencies = .{ + .raylib = .{ + .url = "https://github.com/raysan5/raylib/archive/refs/tags/4.5.0.tar.gz", + .hash = "1220ff8eeb8c51ed28ef7d1224ba2968e00e689774dd6316cb25347a44844dbf210f", + }, + }, +} From f9fae58f94504fb1499bb7ce84ec23de74b5cff8 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Tue, 3 Oct 2023 13:39:30 +0200 Subject: [PATCH 2/3] add raylib submodule --- .gitmodules | 3 +++ zig/3rd-party/raylib | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 zig/3rd-party/raylib diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..32be516 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "zig/3rd-party/raylib"] + path = zig/3rd-party/raylib + url = https://github.com/raysan5/raylib/ diff --git a/zig/3rd-party/raylib b/zig/3rd-party/raylib new file mode 160000 index 0000000..bc15c19 --- /dev/null +++ b/zig/3rd-party/raylib @@ -0,0 +1 @@ +Subproject commit bc15c19518968878b68bbfe8eac3fe4297f11770 From 911d13fc1c432078345ad6a99141318753cff453 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Fri, 29 Sep 2023 15:39:51 +0200 Subject: [PATCH 3/3] call raylib from zig --- zig/build.zig | 18 ++++++++---------- zig/justfile | 16 ++++++++++++++++ zig/src/main.zig | 26 ++++++++++++++------------ 3 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 zig/justfile diff --git a/zig/build.zig b/zig/build.zig index 93ff5e0..047fe53 100644 --- a/zig/build.zig +++ b/zig/build.zig @@ -4,26 +4,24 @@ const std = @import("std"); // declaratively construct a build graph that will be executed by an external // runner. pub fn build(b: *std.Build) void { - // Standard target options allows the person running `zig build` to choose - // what target to build for. Here we do not override the defaults, which - // means any target is allowed, and the default is native. Other options - // for restricting supported target set are available. const target = b.standardTargetOptions(.{}); - - // Standard optimization options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not - // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); const exe = b.addExecutable(.{ .name = "zig", - // In this case the main source file is merely a path, however, in more - // complicated build scripts, this could be a generated file. .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize, }); + // We cannot directly use modules since v4.5 is not compatible with zig + // v0.11 + const raylib_dep = b.anonymousDependency("3rd-party/raylib/", @import("3rd-party/raylib/build.zig"), .{ + .target = target, + .optimize = optimize, + }); + exe.linkLibrary(raylib_dep.artifact("raylib")); + // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default // step when running `zig build`). diff --git a/zig/justfile b/zig/justfile new file mode 100644 index 0000000..3abe747 --- /dev/null +++ b/zig/justfile @@ -0,0 +1,16 @@ +alias b := build +alias r := run +alias t := test +alias fmt := format + +build: + zig build + +run: + nixGL zig build run + +test: + zig build test + +format: + fd -e zig -X zig fmt {} diff --git a/zig/src/main.zig b/zig/src/main.zig index c8a3f67..d9cff41 100644 --- a/zig/src/main.zig +++ b/zig/src/main.zig @@ -1,19 +1,21 @@ const std = @import("std"); +const raylib = @cImport({ + @cInclude("raylib.h"); +}); pub fn main() !void { - // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + const screen_width = 800; + const screen_height = 450; + raylib.InitWindow(screen_width, screen_height, "raylib [core] example - basic window"); + raylib.SetTargetFPS(60); + while (!raylib.WindowShouldClose()) { + raylib.BeginDrawing(); + defer raylib.EndDrawing(); - // stdout is for the actual output of your application, for example if you - // are implementing gzip, then only the compressed bytes should be sent to - // stdout, not any debugging messages. - const stdout_file = std.io.getStdOut().writer(); - var bw = std.io.bufferedWriter(stdout_file); - const stdout = bw.writer(); - - try stdout.print("Run `zig build test` to run the tests.\n", .{}); - - try bw.flush(); // don't forget to flush! + raylib.ClearBackground(raylib.RAYWHITE); + raylib.DrawText("Congrats! You created your first window!", 190, 200, 20, raylib.LIGHTGRAY); + } + raylib.CloseWindow(); } test "simple test" {