Skip to content

Commit acbe7c6

Browse files
committed
Adapt code for Zig 0.15
1 parent 76efb53 commit acbe7c6

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ As a comparison, this is the younger sibling of a node variant ( https://github.
1111
```
1212
$ git clone https://github.com/const-void/DOOM-fire-zig/
1313
$ cd DOOM-fire-zig
14-
$ zig build run
14+
$ zig build run -Doptimize=ReleaseFast
1515
...
1616
```
1717
Build Requirements:

build.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ pub fn build(b: *std.Build) void {
1515
// set a preferred release mode, allowing the user to decide how to optimize.
1616
const optimize = b.standardOptimizeOption(.{});
1717

18-
const exe = b.addExecutable(.{
19-
.name = "DOOM-fire",
18+
const exe = b.addExecutable(.{ .name = "DOOM-fire", .root_module = b.createModule(.{
2019
.root_source_file = b.path("src/main.zig"),
2120
.target = target,
2221
.optimize = optimize,
23-
});
22+
}) });
2423

2524
//libc linking
2625
exe.linkLibC();

src/main.zig

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ const std = @import("std");
88

99
const allocator = std.heap.page_allocator;
1010

11-
var stdout: std.fs.File.Writer = undefined;
12-
var stdin: std.fs.File.Reader = undefined;
11+
var stdout_buffer: [1024]u8 = undefined;
12+
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
13+
const stdout = &stdout_writer.interface;
14+
var stdin_buffer: [1024]u8 = undefined;
15+
var stdin_reader = std.fs.File.stdin().reader(&stdin_buffer);
16+
const stdin = &stdin_reader.interface;
1317
var g_tty_win: win32.HANDLE = undefined;
1418

1519
///////////////////////////////////
@@ -121,6 +125,7 @@ pub fn emit(s: []const u8) !void {
121125
if (sz == 0) {
122126
return;
123127
} // cauze I c
128+
try stdout.flush();
124129
return;
125130
}
126131
}
@@ -211,11 +216,7 @@ pub fn initColor() !void {
211216

212217
pub fn getTermSzWin() !TermSz {
213218
//Microsoft Windows Case
214-
var info: win32.CONSOLE_SCREEN_BUFFER_INFO = .{ .dwSize = .{ .X = 0, .Y = 0 },
215-
.dwCursorPosition = .{.X= 0, .Y= 0},
216-
.wAttributes= 0,
217-
.srWindow = .{ .Left = 0, .Top = 0, .Right = 0, .Bottom = 0},
218-
.dwMaximumWindowSize = .{.X = 0, .Y = 0} };
219+
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 } };
219220

220221
if (0 == win32.GetConsoleScreenBufferInfo(g_tty_win, &info)) switch (std.os.windows.kernel32.GetLastError()) {
221222
else => |e| return std.os.windows.unexpectedError(e),
@@ -231,7 +232,7 @@ pub fn getTermSzLinux() !TermSz {
231232
//Linux-MacOS Case
232233

233234
//base case - invoked from cmd line
234-
const tty_nix = stdout.context.handle;
235+
const tty_nix = std.fs.File.stdout().handle;
235236
var winsz = std.c.winsize{ .col = 0, .row = 0, .xpixel = 0, .ypixel = 0 };
236237
const rv = std.c.ioctl(tty_nix, TIOCGWINSZ, @intFromPtr(&winsz));
237238
const err = std.posix.errno(rv);
@@ -339,7 +340,7 @@ pub fn pause() !void {
339340
try emit(color_reset);
340341
try emit("Press return to continue...");
341342
var b: u8 = undefined;
342-
b = stdin.readByte() catch undefined;
343+
b = try stdin.takeByte();
343344

344345
if (b == 'q') {
345346
//exit cleanly
@@ -576,11 +577,11 @@ pub fn scrollMarquee() !void {
576577
try emit(line_clear_to_eol);
577578
try emit(nl);
578579

579-
std.time.sleep(10 * std.time.ns_per_ms);
580+
std.Thread.sleep(10 * std.time.ns_per_ms);
580581
}
581582

582583
//let quote chill for a second
583-
std.time.sleep(1000 * std.time.ns_per_ms);
584+
std.Thread.sleep(1000 * std.time.ns_per_ms);
584585

585586
//fade out
586587
fade_idx = fade_len - 1;
@@ -598,7 +599,7 @@ pub fn scrollMarquee() !void {
598599
try emit(txt[txt_idx * 2 + 1]);
599600
try emit(line_clear_to_eol);
600601
try emit(nl);
601-
std.time.sleep(10 * std.time.ns_per_ms);
602+
std.Thread.sleep(10 * std.time.ns_per_ms);
602603
}
603604
try emit(nl);
604605
}
@@ -689,7 +690,7 @@ pub fn paintBuf() !void {
689690
fps = @as(f64, @floatFromInt(bs_frame_tic)) / t_dur;
690691

691692
try emit(fg[0]);
692-
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 });
693+
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 });
693694
}
694695

695696
// initBuf(); defer freeBuf();
@@ -835,9 +836,6 @@ pub fn showDoomFire() !void {
835836
///////////////////////////////////
836837

837838
pub fn main() anyerror!void {
838-
stdout = std.io.getStdOut().writer();
839-
stdin = std.io.getStdIn().reader();
840-
841839
try initTerm();
842840
defer complete() catch {};
843841

0 commit comments

Comments
 (0)