From 139bc9409d5888fb4da0453d5c3f67f98a43275c Mon Sep 17 00:00:00 2001 From: Alexandre Veyrenc Date: Thu, 2 Oct 2025 19:50:47 +0200 Subject: [PATCH 1/2] Adapt code for Zig 0.15 --- README.md | 2 +- build.zig | 5 ++--- build.zig.zon | 4 ++-- src/main.zig | 30 ++++++++++++++---------------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8d59836..91702e3 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ As a comparison, this is the younger sibling of a node variant ( https://github. ``` $ git clone https://github.com/const-void/DOOM-fire-zig/ $ cd DOOM-fire-zig -$ zig build run +$ zig build run -Doptimize=ReleaseFast ... ``` Build Requirements: diff --git a/build.zig b/build.zig index 34a4771..94c80e4 100644 --- a/build.zig +++ b/build.zig @@ -15,12 +15,11 @@ 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 exe = b.addExecutable(.{ - .name = "DOOM-fire", + const exe = b.addExecutable(.{ .name = "DOOM-fire", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, - }); + }) }); //libc linking exe.linkLibC(); diff --git a/build.zig.zon b/build.zig.zon index 1c7ad06..ebc0d12 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -10,7 +10,7 @@ // This is a [Semantic Version](https://semver.org/). // In a future version of Zig it will be used for package deduplication. - .version = "0.14.0", + .version = "0.15.0", // Together with name, this represents a globally unique package // identifier. This field is generated by the Zig toolchain when the @@ -28,7 +28,7 @@ // Tracks the earliest Zig version that the package considers to be a // supported use case. - .minimum_zig_version = "0.14.0", + .minimum_zig_version = "0.15.0", // This field is optional. // Each dependency must either provide a `url` and `hash`, or a `path`. diff --git a/src/main.zig b/src/main.zig index cceca94..f172e26 100644 --- a/src/main.zig +++ b/src/main.zig @@ -8,8 +8,12 @@ const std = @import("std"); const allocator = std.heap.page_allocator; -var stdout: std.fs.File.Writer = undefined; -var stdin: std.fs.File.Reader = undefined; +var stdout_buffer: [1024]u8 = undefined; +var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); +const stdout = &stdout_writer.interface; +var stdin_buffer: [1024]u8 = undefined; +var stdin_reader = std.fs.File.stdin().reader(&stdin_buffer); +const stdin = &stdin_reader.interface; var g_tty_win: win32.HANDLE = undefined; /////////////////////////////////// @@ -121,6 +125,7 @@ pub fn emit(s: []const u8) !void { if (sz == 0) { return; } // cauze I c + try stdout.flush(); return; } } @@ -211,11 +216,7 @@ pub fn initColor() !void { pub fn getTermSzWin() !TermSz { //Microsoft Windows Case - var info: win32.CONSOLE_SCREEN_BUFFER_INFO = .{ .dwSize = .{ .X = 0, .Y = 0 }, - .dwCursorPosition = .{.X= 0, .Y= 0}, - .wAttributes= 0, - .srWindow = .{ .Left = 0, .Top = 0, .Right = 0, .Bottom = 0}, - .dwMaximumWindowSize = .{.X = 0, .Y = 0} }; + var info: win32.CONSOLE_SCREEN_BUFFER_INFO = .{ .dwSize = .{ .X = 0, .Y = 0 }, .dwCursorPosition = .{ .X = 0, .Y = 0 }, .wAttributes = 0, .srWindow = .{ .Left = 0, .Top = 0, .Right = 0, .Bottom = 0 }, .dwMaximumWindowSize = .{ .X = 0, .Y = 0 } }; if (0 == win32.GetConsoleScreenBufferInfo(g_tty_win, &info)) switch (std.os.windows.kernel32.GetLastError()) { else => |e| return std.os.windows.unexpectedError(e), @@ -231,7 +232,7 @@ pub fn getTermSzLinux() !TermSz { //Linux-MacOS Case //base case - invoked from cmd line - const tty_nix = stdout.context.handle; + const tty_nix = std.fs.File.stdout().handle; var winsz = std.c.winsize{ .col = 0, .row = 0, .xpixel = 0, .ypixel = 0 }; const rv = std.c.ioctl(tty_nix, TIOCGWINSZ, @intFromPtr(&winsz)); const err = std.posix.errno(rv); @@ -339,7 +340,7 @@ pub fn pause() !void { try emit(color_reset); try emit("Press return to continue..."); var b: u8 = undefined; - b = stdin.readByte() catch undefined; + b = try stdin.takeByte(); if (b == 'q') { //exit cleanly @@ -576,11 +577,11 @@ pub fn scrollMarquee() !void { try emit(line_clear_to_eol); try emit(nl); - std.time.sleep(10 * std.time.ns_per_ms); + std.Thread.sleep(10 * std.time.ns_per_ms); } //let quote chill for a second - std.time.sleep(1000 * std.time.ns_per_ms); + std.Thread.sleep(1000 * std.time.ns_per_ms); //fade out fade_idx = fade_len - 1; @@ -598,7 +599,7 @@ pub fn scrollMarquee() !void { try emit(txt[txt_idx * 2 + 1]); try emit(line_clear_to_eol); try emit(nl); - std.time.sleep(10 * std.time.ns_per_ms); + std.Thread.sleep(10 * std.time.ns_per_ms); } try emit(nl); } @@ -689,7 +690,7 @@ pub fn paintBuf() !void { fps = @as(f64, @floatFromInt(bs_frame_tic)) / t_dur; try emit(fg[0]); - try emitFmt("mem: {s:.2} min / {s:.2} avg / {s:.2} max [ {d:.2} fps ]", .{ std.fmt.fmtIntSizeBin(bs_sz_min), std.fmt.fmtIntSizeBin(bs_sz_avg), std.fmt.fmtIntSizeBin(bs_sz_max), fps }); + try emitFmt("mem: {Bi:.2} min / {Bi:.2} avg / {Bi:.2} max [ {d:.2} fps ]", .{ bs_sz_min, bs_sz_avg, bs_sz_max, fps }); } // initBuf(); defer freeBuf(); @@ -835,9 +836,6 @@ pub fn showDoomFire() !void { /////////////////////////////////// pub fn main() anyerror!void { - stdout = std.io.getStdOut().writer(); - stdin = std.io.getStdIn().reader(); - try initTerm(); defer complete() catch {}; From 9521a8b9b7afc53d58f0bc451f01a2ce45ea6d47 Mon Sep 17 00:00:00 2001 From: Alexandre Veyrenc Date: Mon, 3 Nov 2025 11:50:39 +0100 Subject: [PATCH 2/2] Avoid random number generator corruption --- src/main.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index f172e26..a967cbb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -102,7 +102,8 @@ var rand: std.Random = undefined; // seed & prep for rng pub fn initRNG() !void { //rnd setup -- https://ziglearn.org/chapter-2/#random-numbers - var prng = std.Random.DefaultPrng.init(blk: { + var prng = try allocator.create(std.Random.DefaultPrng); + prng.* = std.Random.DefaultPrng.init(blk: { var seed: u64 = undefined; try std.posix.getrandom(std.mem.asBytes(&seed)); break :blk seed;