From bad90f12c5cdaee81b5d6e17311f9dbe4f74b5d1 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Sat, 22 Jan 2022 14:42:40 +0100 Subject: [PATCH 1/3] use raylib in zig --- build.zig | 6 ++++ build_raylib.zig | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.zig | 16 ++++++++++ 3 files changed, 105 insertions(+) create mode 100644 build_raylib.zig diff --git a/build.zig b/build.zig index 72f9d8e..d6bc6ab 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const raylib = @import("build_raylib.zig"); pub fn build(b: *std.build.Builder) void { // Standard target options allows the person running `zig build` to choose @@ -13,6 +14,11 @@ pub fn build(b: *std.build.Builder) void { const exe = b.addExecutable("doggo", "src/main.zig"); exe.setTarget(target); + + const ray = raylib.Pkg("./3rdparty/raylib/src").addRaylib(b, exe.target); + exe.linkLibrary(ray); + exe.addIncludeDir("./3rdparty/raylib/src"); + exe.setBuildMode(mode); exe.install(); diff --git a/build_raylib.zig b/build_raylib.zig new file mode 100644 index 0000000..bced3d0 --- /dev/null +++ b/build_raylib.zig @@ -0,0 +1,83 @@ +const std = @import("std"); + +pub fn Pkg(srcdir: []const u8) type { + return struct { + pub fn addRaylib(b: *std.build.Builder, target: std.zig.CrossTarget) *std.build.LibExeObjStep { + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const raylib_flags = &[_][]const u8{ + "-std=gnu99", + "-DPLATFORM_DESKTOP", + "-DGL_SILENCE_DEPRECATION=199309L", + "-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/1891 + }; + + const raylib = b.addStaticLibrary("raylib", srcdir ++ "/raylib.h"); + raylib.setTarget(target); + raylib.setBuildMode(mode); + raylib.linkLibC(); + + raylib.addIncludeDir(srcdir ++ "/external/glfw/include"); + + raylib.addCSourceFiles(&.{ + srcdir ++ "/raudio.c", + srcdir ++ "/rcore.c", + srcdir ++ "/rmodels.c", + srcdir ++ "/rshapes.c", + srcdir ++ "/rtext.c", + srcdir ++ "/rtextures.c", + srcdir ++ "/utils.c", + }, raylib_flags); + + switch (raylib.target.toTarget().os.tag) { + .windows => { + raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags); + raylib.linkSystemLibrary("winmm"); + raylib.linkSystemLibrary("gdi32"); + raylib.linkSystemLibrary("opengl32"); + raylib.addIncludeDir("external/glfw/deps/mingw"); + }, + .linux => { + raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags); + raylib.linkSystemLibrary("GL"); + raylib.linkSystemLibrary("rt"); + raylib.linkSystemLibrary("dl"); + raylib.linkSystemLibrary("m"); + raylib.linkSystemLibrary("X11"); + }, + .macos => { + // On macos rglfw.c include Objective-C files. + const raylib_flags_extra_macos = &[_][]const u8{ + "-ObjC", + }; + raylib.addCSourceFiles( + &.{srcdir ++ "/rglfw.c"}, + raylib_flags ++ raylib_flags_extra_macos, + ); + raylib.linkFramework("Foundation"); + }, + else => { + @panic("Unsupported OS"); + }, + } + + raylib.setOutputDir("./"); + raylib.install(); + return raylib; + } + }; +} + +const lib = Pkg("./3rdparty/raylib/src"); + +pub fn build(b: *std.build.Builder) 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(.{}); + + _ = lib.addRaylib(b, target); +} diff --git a/src/main.zig b/src/main.zig index a7a7c95..47404c4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,7 +1,23 @@ const std = @import("std"); +const ray = @cImport({ + @cInclude("raylib.h"); +}); pub fn main() anyerror!void { std.log.info("All your codebase are belong to us.", .{}); + + ray.InitWindow(800, 450, "ray [core] example - basic window"); + defer ray.CloseWindow(); + + while (!ray.WindowShouldClose()) + { + ray.BeginDrawing(); + defer ray.EndDrawing(); + + ray.ClearBackground(ray.RAYWHITE); + ray.DrawText("Congrats! You created your first window!", 190, 200, 20, ray.LIGHTGRAY); + } + } test "basic test" { From dfa42ab7db1a37b6709ef5853f54d6b11f4cbd24 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Sat, 22 Jan 2022 14:42:40 +0100 Subject: [PATCH 2/3] use raylib in zig --- .gitignore | 1 + build.zig | 6 ++++++ src/main.zig | 16 ++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03d24c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +libraylib.a diff --git a/build.zig b/build.zig index 72f9d8e..82edbfb 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const raylib = @import("./3rdparty/raylib/src/build.zig"); pub fn build(b: *std.build.Builder) void { // Standard target options allows the person running `zig build` to choose @@ -13,6 +14,11 @@ pub fn build(b: *std.build.Builder) void { const exe = b.addExecutable("doggo", "src/main.zig"); exe.setTarget(target); + + const ray = raylib.Pkg("./3rdparty/raylib/src").addRaylib(b, exe.target); + exe.linkLibrary(ray); + exe.addIncludeDir("./3rdparty/raylib/src"); + exe.setBuildMode(mode); exe.install(); diff --git a/src/main.zig b/src/main.zig index a7a7c95..47404c4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,7 +1,23 @@ const std = @import("std"); +const ray = @cImport({ + @cInclude("raylib.h"); +}); pub fn main() anyerror!void { std.log.info("All your codebase are belong to us.", .{}); + + ray.InitWindow(800, 450, "ray [core] example - basic window"); + defer ray.CloseWindow(); + + while (!ray.WindowShouldClose()) + { + ray.BeginDrawing(); + defer ray.EndDrawing(); + + ray.ClearBackground(ray.RAYWHITE); + ray.DrawText("Congrats! You created your first window!", 190, 200, 20, ray.LIGHTGRAY); + } + } test "basic test" { From f327df5c978930ceec1107b7197e0d1e57faf912 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Sat, 22 Jan 2022 15:22:06 +0100 Subject: [PATCH 3/3] add justfile --- justfile | 2 ++ shell.nix | 1 + 2 files changed, 3 insertions(+) create mode 100644 justfile diff --git a/justfile b/justfile new file mode 100644 index 0000000..acbc9ed --- /dev/null +++ b/justfile @@ -0,0 +1,2 @@ +run: + nixGL zig build run diff --git a/shell.nix b/shell.nix index ec522b7..9f0dece 100644 --- a/shell.nix +++ b/shell.nix @@ -2,6 +2,7 @@ pkgs.mkShell { buildInputs = with pkgs; [ + just zig xorg.libX11