Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.
Expand Down
33 changes: 16 additions & 17 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;

///////////////////////////////////
Expand Down Expand Up @@ -98,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;
Expand All @@ -121,6 +126,7 @@ pub fn emit(s: []const u8) !void {
if (sz == 0) {
return;
} // cauze I c
try stdout.flush();
return;
}
}
Expand Down Expand Up @@ -211,11 +217,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),
Expand All @@ -231,7 +233,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);
Expand Down Expand Up @@ -339,7 +341,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
Expand Down Expand Up @@ -576,11 +578,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;
Expand All @@ -598,7 +600,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);
}
Expand Down Expand Up @@ -689,7 +691,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();
Expand Down Expand Up @@ -835,9 +837,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 {};

Expand Down