Merge pull request #31 from dacto/main

`examples/minimal-zig`: update for Zig 0.13.0
This commit is contained in:
Serge Zaitsev
2024-09-15 22:08:09 +02:00
committed by GitHub
3 changed files with 59 additions and 24 deletions

View File

@ -1,6 +1,9 @@
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
// 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
@ -9,36 +12,68 @@ pub fn build(b: *std.build.Builder) void {
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable("minimal-zig", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.addIncludePath("src");
exe.addCSourceFile("src/fenster.c", &[_][]const u8{});
if (exe.target.isDarwin()) {
exe.linkFramework("Cocoa");
} else if (exe.target.isWindows()) {
exe.linkSystemLibrary("gdi32");
} else {
exe.linkSystemLibrary("X11");
const exe = b.addExecutable(.{
.name = "minimal-zig",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.addIncludePath(b.path("src"));
exe.addCSourceFile(.{ .file = b.path("src/fenster.c"), .flags = &[_][]const u8{} });
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());
// 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| {
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");
run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
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");
test_step.dependOn(&exe_tests.step);
test_step.dependOn(&run_unit_tests.step);
}

View File

@ -7,7 +7,7 @@ pub const Fenster = struct {
};
pub fn new(comptime w: i32, comptime h: i32, title: [:0]const u8) type {
var buf: [w*h]u32 = undefined;
var buf: [w * h]u32 = undefined;
const f = .{
.width = w,
.height = h,

View File

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