minimal-zig: update for Zig 0.13.0

This commit is contained in:
Dacto
2024-09-14 18:38:01 -07:00
parent 92aa0ecd30
commit c0168974c8
3 changed files with 59 additions and 24 deletions

View File

@ -1,6 +1,9 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.build.Builder) void { // Although this function looks imperative, note that its job is to
// 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 // Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which // 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 // means any target is allowed, and the default is native. Other options
@ -9,36 +12,68 @@ pub fn build(b: *std.build.Builder) void {
// Standard release options allow the person running `zig build` to select // Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable("minimal-zig", "src/main.zig"); const exe = b.addExecutable(.{
exe.setTarget(target); .name = "minimal-zig",
exe.setBuildMode(mode); .root_source_file = b.path("src/main.zig"),
exe.addIncludePath("src"); .target = target,
exe.addCSourceFile("src/fenster.c", &[_][]const u8{}); .optimize = optimize,
if (exe.target.isDarwin()) { });
exe.linkFramework("Cocoa");
} else if (exe.target.isWindows()) { exe.addIncludePath(b.path("src"));
exe.linkSystemLibrary("gdi32"); exe.addCSourceFile(.{ .file = b.path("src/fenster.c"), .flags = &[_][]const u8{} });
} else {
exe.linkSystemLibrary("X11"); switch (target.result.os.tag) {
.macos => exe.linkFramework("Cocoa"),
.windows => exe.linkSystemLibrary("gdi32"),
.linux => exe.linkSystemLibrary("X11"),
else => {},
} }
exe.linkSystemLibrary("c");
exe.install();
const run_cmd = exe.run(); exe.linkLibC();
// 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`).
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| { if (b.args) |args| {
run_cmd.addArgs(args); run_cmd.addArgs(args);
} }
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main.zig"); // Creates a step for unit testing. This only builds the test executable
exe_tests.setTarget(target); // but does not run it.
exe_tests.setBuildMode(mode); const unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests"); const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step); test_step.dependOn(&run_unit_tests.step);
} }

View File

@ -21,12 +21,12 @@ pub fn main() void {
break; break;
} }
// Render x^y^t pattern // Render x^y^t pattern
for (buf) |_, i| { for (buf, 0..) |_, i| {
buf[i] = @intCast(u32, i % 320) ^ @intCast(u32, i / 240) ^ t; buf[i] = @as(u32, @intCast(i % 320)) ^ @as(u32, @intCast(i / 240)) ^ t;
} }
t +%= 1; t +%= 1;
// Keep ~60 FPS // Keep ~60 FPS
var diff: i64 = 1000 / 60 - (c.fenster_time() - now); const diff: i64 = 1000 / 60 - (c.fenster_time() - now);
if (diff > 0) { if (diff > 0) {
c.fenster_sleep(diff); c.fenster_sleep(diff);
} }