1
Fork 0
mirror of https://github.com/RGBCube/DOOM-fire-zig synced 2025-07-28 17:47:45 +00:00

Merge pull request #1 from bradms/cleanup

Be more "Zig-like"
This commit is contained in:
const void* 2022-01-16 15:52:19 -05:00 committed by GitHub
commit be6440f205
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -35,7 +35,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
@ -60,12 +60,14 @@ pub fn initRNG() !void {
// print
pub fn emit(s: []const u8) void {
const sz = stdout.write(s) catch unreachable;
if (sz==0) {return;} // cauze I c
if (sz == 0) {
return;
} // cauze I c
return;
}
// 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);
@ -130,10 +132,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;
}
}
@ -142,14 +143,14 @@ pub fn initColor() void {
//get terminal size given a tty
pub fn getTermSz(tty: std.os.fd_t) !TermSz {
var winsz = std.os.system.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) { return TermSz{ .height = winsz.ws_row, .width = winsz.ws_col }; }
else {return std.os.unexpectedErrno(err);}
if (rv == 0) {
return TermSz{ .height = winsz.ws_row, .width = winsz.ws_col };
} else {
return std.os.unexpectedErrno(err);
}
}
pub fn initTermSize() !void {
@ -189,7 +190,6 @@ pub fn pause() void {
}
}
/// Part I - Terminal Size Check
/// showTermCap() needs about 120x22; if screen is too small, give user a chance to abort and try again.
///
@ -204,22 +204,32 @@ pub fn checkTermSz() void {
var h_ok = true;
// chk cur < min
if (term_sz.width<min_w) { w_ok=false; }
if (term_sz.height<min_h) { h_ok=false; }
if (term_sz.width < min_w) {
w_ok = false;
}
if (term_sz.height < min_h) {
h_ok = false;
}
if (w_ok and h_ok) { return; }
else {
if (w_ok and h_ok) {
return;
} else {
//screen is too small
//red text
emit(fg[9]);
//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}); }
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}); }
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}); }
if (w_ok and !h_ok) {
emitFmt("Screen may be too short - height is {d} and need {d}.", .{ term_sz.height, min_h });
} else if (!w_ok and h_ok) {
emitFmt("Screen may be too narrow - width is {d} and need {d}.", .{ term_sz.width, min_w });
} else {
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);emit(nl);
emit(nl);
emit(nl);
//warn user w/white on red
emit(bg[1]);
@ -244,14 +254,13 @@ pub fn checkTermSz() void {
/// Since user terminals vary in capabilities, handy to have a screen that renders ACTUAL colors
/// and exercises various terminal commands prior to DOOM fire.
///
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 {
@ -260,22 +269,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});
if (color_idx == 7) {
emit(fg[0]);
}
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});
if (color_idx == 15) {
emit(fg[0]);
}
emitFmt("{u} {d:2} ", .{ sep, color_idx });
}
emit(nl);
@ -293,29 +303,25 @@ 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
if (color_idx > 17) {
fg_idx = 0;
}
else {
} else {
fg_idx = 15;
}
// display color
emit(bg[bg_idx]);
emit(fg[fg_idx]);
emit_fmt("{d:3}",.{bg_idx});
emitFmt("{d:3}", .{bg_idx});
}
emit(nl);
}
@ -329,16 +335,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);
@ -359,15 +363,7 @@ pub fn scrollMarquee() void {
emit(marquee_bg);
//quotes - will confirm animations are working on current terminal
const txt =[_][]const u8{
" Things move along so rapidly nowadays that people saying "++color_italic++"It can't be done"++color_not_italic++" are always being interrupted",
" by somebody doing it. "++color_italic++"-- Puck, 1902"++color_not_italic,
" Test your might!",
" "++color_italic++"-- Mortal Kombat"++color_not_italic,
" How much is the fish?",
" "++color_italic++"-- Scooter"++color_not_italic};
const txt = [_][]const u8{ " Things move along so rapidly nowadays that people saying " ++ color_italic ++ "It can't be done" ++ color_not_italic ++ " are always being interrupted", " by somebody doing it. " ++ color_italic ++ "-- Puck, 1902" ++ color_not_italic, " Test your might!", " " ++ color_italic ++ "-- Mortal Kombat" ++ color_not_italic, " How much is the fish?", " " ++ color_italic ++ "-- Scooter" ++ color_not_italic };
const txt_len: u8 = txt.len / 2; // print two rows at a time
//fade txt in and out
@ -377,14 +373,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]);
@ -405,12 +397,9 @@ pub fn scrollMarquee() void {
//let quote chill for a second
std.time.sleep(1000 * std.time.ns_per_ms);
//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]);
@ -461,7 +450,6 @@ var t_now:i64=0;
var t_dur: f64 = 0.0;
var fps: f64 = 0.0;
pub fn initBuf() void {
//some lazy guesswork to make sure we have enough of a buffer to render DOOM fire.
const px_char_sz = px.len;
@ -501,10 +489,13 @@ pub fn paintBuf() void {
bs_sz_min = bs_len;
bs_sz_max = bs_len;
bs_sz_avg = bs_len;
} else {
if (bs_len < bs_sz_min) {
bs_sz_min = bs_len;
}
if (bs_len > bs_sz_max) {
bs_sz_max = bs_len;
}
else {
if( bs_len < bs_sz_min) { bs_sz_min=bs_len; }
if( bs_len > bs_sz_max) { bs_sz_max=bs_len; }
bs_sz_avg = bs_sz_avg * (bs_frame_tic - 1) / bs_frame_tic + bs_len / bs_frame_tic;
}
@ -512,12 +503,13 @@ 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();
pub fn freeBuf() void { allocator.free(bs); }
pub fn freeBuf() void {
allocator.free(bs);
}
pub fn showDoomFire() void {
//term size => fire size
@ -538,16 +530,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;
}
@ -590,13 +579,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
@ -605,8 +590,7 @@ pub fn showDoomFire() void {
//bounds checking
if ((spread_px == 0) and (doFire_idx >= FIRE_W)) {
screen_buf[doFire_idx - FIRE_W] = 0;
}
else {
} else {
spread_rnd_idx = rand.intRangeAtMost(u8, 0, 3);
if (doFire_idx >= (spread_rnd_idx + 1)) {
spread_dst = doFire_idx - spread_rnd_idx + 1;
@ -624,14 +608,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)
@ -669,5 +649,4 @@ pub fn main() anyerror!void {
checkTermSz();
showTermCap();
showDoomFire();
}