mirror of
https://github.com/RGBCube/DOOM-fire-zig
synced 2025-07-28 09:37:44 +00:00
Resolve the conflicts
This commit is contained in:
parent
2699c566f3
commit
c1fe1abfca
2 changed files with 31 additions and 61 deletions
|
@ -13,7 +13,6 @@ pub fn build(b: *std.build.Builder) void {
|
|||
|
||||
const exe = b.addExecutable("DOOM-fire", "src/main.zig");
|
||||
|
||||
exe.addIncludeDir("src");
|
||||
exe.linkLibC();
|
||||
exe.setTarget(target);
|
||||
exe.setBuildMode(mode);
|
||||
|
|
91
src/main.zig
91
src/main.zig
|
@ -4,8 +4,6 @@
|
|||
// Copy/paste as it helps!
|
||||
//
|
||||
const std = @import("std");
|
||||
|
||||
// Gets the correct TIOCGWINSZ value from libc.
|
||||
const c = @cImport({
|
||||
@cInclude("sys/ioctl.h");
|
||||
});
|
||||
|
@ -16,7 +14,7 @@ const stdout = std.io.getStdOut().writer();
|
|||
const stdin = std.io.getStdIn().reader();
|
||||
|
||||
///////////////////////////////////
|
||||
// Tested on M1 osx12.1 + Linux
|
||||
// Tested on M1 osx12.1 + Artix Linux.
|
||||
// fast - vs code terminal
|
||||
// slow - Terminal.app
|
||||
///////////////////////////////////
|
||||
|
@ -40,7 +38,7 @@ const stdin = std.io.getStdIn().reader();
|
|||
///////////////////////////////////
|
||||
// do or do not - there is no try: catch unreachable instead of try on memory / file io
|
||||
// - put all try to initXXX()
|
||||
// for (i=0; i<MAX; i++) { ... } => var i=0; while (i<MAX) { defer i+=1; ... }
|
||||
// for (i=0; i<MAX; i++) { ... } => var i=0; while (i<MAX) : (i+=1) { ... }
|
||||
|
||||
///////////////////////////////////
|
||||
// zig helpers
|
||||
|
@ -72,7 +70,7 @@ pub fn emit(s: []const u8) void {
|
|||
}
|
||||
|
||||
// format a string then print
|
||||
pub fn emit_fmt(comptime s: []const u8, args: anytype) void {
|
||||
pub fn emitFmt(comptime s: []const u8, args: anytype) void {
|
||||
const t = std.fmt.allocPrint(allocator, s, args) catch unreachable;
|
||||
defer allocator.free(t);
|
||||
emit(t);
|
||||
|
@ -84,8 +82,8 @@ pub fn emit_fmt(comptime s: []const u8, args: anytype) void {
|
|||
|
||||
//// Settings
|
||||
|
||||
// Get this from libc instead of some random value.
|
||||
const TIOCGWINSZ = c.TIOCGWINSZ;
|
||||
// Get this value from libc.
|
||||
const TIOCGWINSZ = c.TIOCGWINSZ; // ioctl flag
|
||||
|
||||
//term size
|
||||
const TermSz = struct { height: usize, width: usize };
|
||||
|
@ -137,10 +135,9 @@ var bg: [MAX_COLOR][]u8 = undefined;
|
|||
// cache fg/bg ansi codes
|
||||
pub fn initColor() void {
|
||||
var color_idx: u16 = 0;
|
||||
while (color_idx < MAX_COLOR) {
|
||||
while (color_idx < MAX_COLOR) : (color_idx += 1) {
|
||||
fg[color_idx] = std.fmt.allocPrint(allocator, "{s}38;5;{d}m", .{ csi, color_idx }) catch unreachable;
|
||||
bg[color_idx] = std.fmt.allocPrint(allocator, "{s}48;5;{d}m", .{ csi, color_idx }) catch unreachable;
|
||||
color_idx += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -149,9 +146,7 @@ pub fn initColor() void {
|
|||
|
||||
//get terminal size given a tty
|
||||
pub fn getTermSz(tty: std.os.fd_t) !TermSz {
|
||||
// Get this struct from libc instead of Zig's std. I shall add this struct regardless so you can call std.os.system.winsize.
|
||||
// This is hacky and bad.
|
||||
var winsz = c.winsize{ .ws_col = 0, .ws_row = 0, .ws_xpixel = 0, .ws_ypixel = 0 };
|
||||
var winsz = std.os.system.winsize{ .ws_col = 0, .ws_row = 0, .ws_xpixel = 0, .ws_ypixel = 0 };
|
||||
const rv = std.os.system.ioctl(tty, TIOCGWINSZ, @ptrToInt(&winsz));
|
||||
const err = std.os.errno(rv);
|
||||
if (rv == 0) {
|
||||
|
@ -229,11 +224,11 @@ pub fn checkTermSz() void {
|
|||
|
||||
//check conditions
|
||||
if (w_ok and !h_ok) {
|
||||
emit_fmt("Screen may be too short - height is {d} and need {d}.", .{ term_sz.height, min_h });
|
||||
emitFmt("Screen may be too short - height is {d} and need {d}.", .{ term_sz.height, min_h });
|
||||
} else if (!w_ok and h_ok) {
|
||||
emit_fmt("Screen may be too narrow - width is {d} and need {d}.", .{ term_sz.width, min_w });
|
||||
emitFmt("Screen may be too narrow - width is {d} and need {d}.", .{ term_sz.width, min_w });
|
||||
} else {
|
||||
emit_fmt("Screen is too small - have {d} x {d} and need {d} x {d}", .{ term_sz.width, term_sz.height, min_w, min_h });
|
||||
emitFmt("Screen is too small - have {d} x {d} and need {d} x {d}", .{ term_sz.width, term_sz.height, min_w, min_h });
|
||||
}
|
||||
|
||||
emit(nl);
|
||||
|
@ -264,11 +259,11 @@ pub fn checkTermSz() void {
|
|||
///
|
||||
pub fn showTermSz() void {
|
||||
//todo - show os, os ver, zig ver
|
||||
emit_fmt("Screen size: {d}w x {d}h\n\n", .{ term_sz.width, term_sz.height });
|
||||
emitFmt("Screen size: {d}w x {d}h\n\n", .{ term_sz.width, term_sz.height });
|
||||
}
|
||||
|
||||
pub fn showLabel(label: []const u8) void {
|
||||
emit_fmt("{s}{s}:\n", .{ color_def, label });
|
||||
emitFmt("{s}{s}:\n", .{ color_def, label });
|
||||
}
|
||||
|
||||
pub fn showStdColors() void {
|
||||
|
@ -277,25 +272,23 @@ pub fn showStdColors() void {
|
|||
//first 8 colors (standard)
|
||||
emit(fg[15]);
|
||||
var color_idx: u8 = 0;
|
||||
while (color_idx < 8) {
|
||||
defer color_idx += 1;
|
||||
while (color_idx < 8) : (color_idx += 1) {
|
||||
emit(bg[color_idx]);
|
||||
if (color_idx == 7) {
|
||||
emit(fg[0]);
|
||||
}
|
||||
emit_fmt("{u} {d:2} ", .{ sep, color_idx });
|
||||
emitFmt("{u} {d:2} ", .{ sep, color_idx });
|
||||
}
|
||||
emit(nl);
|
||||
|
||||
//next 8 colors ("hilight")
|
||||
emit(fg[15]);
|
||||
while (color_idx < 16) {
|
||||
defer color_idx += 1;
|
||||
while (color_idx < 16) : (color_idx += 1) {
|
||||
emit(bg[color_idx]);
|
||||
if (color_idx == 15) {
|
||||
emit(fg[0]);
|
||||
}
|
||||
emit_fmt("{u} {d:2} ", .{ sep, color_idx });
|
||||
emitFmt("{u} {d:2} ", .{ sep, color_idx });
|
||||
}
|
||||
|
||||
emit(nl);
|
||||
|
@ -313,15 +306,12 @@ pub fn show216Colors() void {
|
|||
|
||||
// 6 rows of color
|
||||
var color_shift: u8 = 0;
|
||||
while (color_shift < 6) {
|
||||
defer color_shift += 1;
|
||||
|
||||
while (color_shift < 6) : (color_shift += 1) {
|
||||
color_addendum = color_shift * 36 + 16;
|
||||
|
||||
// colors are pre-organized into blocks
|
||||
var color_idx: u8 = 0;
|
||||
while (color_idx < 36) {
|
||||
defer color_idx += 1;
|
||||
while (color_idx < 36) : (color_idx += 1) {
|
||||
bg_idx = color_idx + color_addendum;
|
||||
|
||||
// invert color id for readability
|
||||
|
@ -334,7 +324,7 @@ pub fn show216Colors() void {
|
|||
// display color
|
||||
emit(bg[bg_idx]);
|
||||
emit(fg[fg_idx]);
|
||||
emit_fmt("{d:3}", .{bg_idx});
|
||||
emitFmt("{d:3}", .{bg_idx});
|
||||
}
|
||||
emit(nl);
|
||||
}
|
||||
|
@ -348,16 +338,14 @@ pub fn showGrayscale() void {
|
|||
emit(fg[fg_idx]);
|
||||
|
||||
var bg_idx: u16 = 232;
|
||||
while (bg_idx < 256) {
|
||||
defer bg_idx += 1;
|
||||
|
||||
while (bg_idx < 256) : (bg_idx += 1) {
|
||||
if (bg_idx > 243) {
|
||||
fg_idx = 0;
|
||||
emit(fg[fg_idx]);
|
||||
}
|
||||
|
||||
emit(bg[bg_idx]);
|
||||
emit_fmt("{u}{d} ", .{ sep, bg_idx });
|
||||
emitFmt("{u}{d} ", .{ sep, bg_idx });
|
||||
}
|
||||
emit(nl);
|
||||
|
||||
|
@ -388,14 +376,10 @@ pub fn scrollMarquee() void {
|
|||
var fade_idx: u8 = 0;
|
||||
var txt_idx: u8 = 0;
|
||||
|
||||
while (txt_idx < txt_len) {
|
||||
defer txt_idx += 1;
|
||||
|
||||
while (txt_idx < txt_len) : (txt_idx += 1) {
|
||||
//fade in
|
||||
fade_idx = 0;
|
||||
while (fade_idx < fade_len) {
|
||||
defer fade_idx += 1;
|
||||
|
||||
while (fade_idx < fade_len) : (fade_idx += 1) {
|
||||
//reset to 1,1 of marquee
|
||||
emit(cursor_load);
|
||||
emit(bg[bg_idx]);
|
||||
|
@ -418,9 +402,7 @@ pub fn scrollMarquee() void {
|
|||
|
||||
//fade out
|
||||
fade_idx = fade_len - 1;
|
||||
while (fade_idx > 0) {
|
||||
defer fade_idx -= 1;
|
||||
|
||||
while (fade_idx > 0) : (fade_idx -= 1) {
|
||||
//reset to 1,1 of marquee
|
||||
emit(cursor_load);
|
||||
emit(bg[bg_idx]);
|
||||
|
@ -524,7 +506,7 @@ pub fn paintBuf() void {
|
|||
fps = @intToFloat(f64, bs_frame_tic) / t_dur;
|
||||
|
||||
emit(fg[0]);
|
||||
emit_fmt("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 });
|
||||
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 });
|
||||
}
|
||||
|
||||
// initBuf(); defer freeBuf();
|
||||
|
@ -551,16 +533,13 @@ pub fn showDoomFire() void {
|
|||
|
||||
//init buffer
|
||||
var buf_idx: u16 = 0;
|
||||
while (buf_idx < FIRE_SZ) {
|
||||
defer buf_idx += 1;
|
||||
|
||||
while (buf_idx < FIRE_SZ) : (buf_idx += 1) {
|
||||
screen_buf[buf_idx] = fire_black;
|
||||
}
|
||||
|
||||
//last row is white...white is "fire source"
|
||||
buf_idx = 0;
|
||||
while (buf_idx < FIRE_W) {
|
||||
defer buf_idx += 1;
|
||||
while (buf_idx < FIRE_W) : (buf_idx += 1) {
|
||||
screen_buf[FIRE_LAST_ROW + buf_idx] = fire_white;
|
||||
}
|
||||
|
||||
|
@ -603,13 +582,9 @@ pub fn showDoomFire() void {
|
|||
|
||||
//update fire buf
|
||||
doFire_x = 0;
|
||||
while (doFire_x < FIRE_W) {
|
||||
defer doFire_x += 1;
|
||||
|
||||
while (doFire_x < FIRE_W) : (doFire_x += 1) {
|
||||
doFire_y = 0;
|
||||
while (doFire_y < FIRE_H) {
|
||||
defer doFire_y += 1;
|
||||
|
||||
while (doFire_y < FIRE_H) : (doFire_y += 1) {
|
||||
doFire_idx = doFire_y * FIRE_W + doFire_x;
|
||||
|
||||
//spread fire
|
||||
|
@ -636,14 +611,10 @@ pub fn showDoomFire() void {
|
|||
|
||||
// for each row
|
||||
frame_y = 0;
|
||||
while (frame_y < FIRE_H) {
|
||||
defer frame_y += 2; // 'paint' two rows at a time because of half height char
|
||||
|
||||
while (frame_y < FIRE_H) : (frame_y += 2) { // 'paint' two rows at a time because of half height char
|
||||
// for each col
|
||||
frame_x = 0;
|
||||
while (frame_x < FIRE_W) {
|
||||
defer frame_x += 1;
|
||||
|
||||
while (frame_x < FIRE_W) : (frame_x += 1) {
|
||||
//each character rendered is actually to rows of 'pixels'
|
||||
// - "hi" (current px row => fg char)
|
||||
// - "low" (next row => bg color)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue