mirror of
https://github.com/RGBCube/DOOM-fire-zig
synced 2025-07-29 01:57:44 +00:00
format
This commit is contained in:
parent
add37add17
commit
7710413d80
1 changed files with 274 additions and 269 deletions
87
src/main.zig
87
src/main.zig
|
@ -60,7 +60,9 @@ pub fn initRNG() !void {
|
||||||
// print
|
// print
|
||||||
pub fn emit(s: []const u8) void {
|
pub fn emit(s: []const u8) void {
|
||||||
const sz = stdout.write(s) catch unreachable;
|
const sz = stdout.write(s) catch unreachable;
|
||||||
if (sz==0) {return;} // cauze I c
|
if (sz == 0) {
|
||||||
|
return;
|
||||||
|
} // cauze I c
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,14 +144,14 @@ pub fn initColor() void {
|
||||||
|
|
||||||
//get terminal size given a tty
|
//get terminal size given a tty
|
||||||
pub fn getTermSz(tty: std.os.fd_t) !TermSz {
|
pub fn getTermSz(tty: std.os.fd_t) !TermSz {
|
||||||
var winsz = std.os.system.winsize {
|
var winsz = std.os.system.winsize{ .ws_col = 0, .ws_row = 0, .ws_xpixel = 0, .ws_ypixel = 0 };
|
||||||
.ws_col=0, .ws_row=0,
|
|
||||||
.ws_xpixel=0, .ws_ypixel=0
|
|
||||||
};
|
|
||||||
const rv = std.os.system.ioctl(tty, TIOCGWINSZ, @ptrToInt(&winsz));
|
const rv = std.os.system.ioctl(tty, TIOCGWINSZ, @ptrToInt(&winsz));
|
||||||
const err = std.os.errno(rv);
|
const err = std.os.errno(rv);
|
||||||
if (rv==0) { return TermSz{ .height = winsz.ws_row, .width = winsz.ws_col }; }
|
if (rv == 0) {
|
||||||
else {return std.os.unexpectedErrno(err);}
|
return TermSz{ .height = winsz.ws_row, .width = winsz.ws_col };
|
||||||
|
} else {
|
||||||
|
return std.os.unexpectedErrno(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn initTermSize() !void {
|
pub fn initTermSize() !void {
|
||||||
|
@ -189,7 +191,6 @@ pub fn pause() void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Part I - Terminal Size Check
|
/// Part I - Terminal Size Check
|
||||||
/// showTermCap() needs about 120x22; if screen is too small, give user a chance to abort and try again.
|
/// showTermCap() needs about 120x22; if screen is too small, give user a chance to abort and try again.
|
||||||
///
|
///
|
||||||
|
@ -204,22 +205,32 @@ pub fn checkTermSz() void {
|
||||||
var h_ok = true;
|
var h_ok = true;
|
||||||
|
|
||||||
// chk cur < min
|
// chk cur < min
|
||||||
if (term_sz.width<min_w) { w_ok=false; }
|
if (term_sz.width < min_w) {
|
||||||
if (term_sz.height<min_h) { h_ok=false; }
|
w_ok = false;
|
||||||
|
}
|
||||||
|
if (term_sz.height < min_h) {
|
||||||
|
h_ok = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (w_ok and h_ok) { return; }
|
if (w_ok and h_ok) {
|
||||||
else {
|
return;
|
||||||
|
} else {
|
||||||
//screen is too small
|
//screen is too small
|
||||||
|
|
||||||
//red text
|
//red text
|
||||||
emit(fg[9]);
|
emit(fg[9]);
|
||||||
|
|
||||||
//check conditions
|
//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}); }
|
if (w_ok and !h_ok) {
|
||||||
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}); }
|
emit_fmt("Screen may be too short - height is {d} and need {d}.", .{ term_sz.height, min_h });
|
||||||
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}); }
|
} 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 });
|
||||||
|
}
|
||||||
|
|
||||||
emit(nl);emit(nl);
|
emit(nl);
|
||||||
|
emit(nl);
|
||||||
|
|
||||||
//warn user w/white on red
|
//warn user w/white on red
|
||||||
emit(bg[1]);
|
emit(bg[1]);
|
||||||
|
@ -239,12 +250,12 @@ pub fn checkTermSz() void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Part II - Show terminal capabilities
|
/// Part II - Show terminal capabilities
|
||||||
///
|
///
|
||||||
/// Since user terminals vary in capabilities, handy to have a screen that renders ACTUAL colors
|
/// Since user terminals vary in capabilities, handy to have a screen that renders ACTUAL colors
|
||||||
/// and exercises various terminal commands prior to DOOM fire.
|
/// and exercises various terminal commands prior to DOOM fire.
|
||||||
///
|
///
|
||||||
|
|
||||||
pub fn showTermSz() void {
|
pub fn showTermSz() void {
|
||||||
//todo - show os, os ver, zig ver
|
//todo - show os, os ver, zig ver
|
||||||
emit_fmt("Screen size: {d}w x {d}h\n\n", .{ term_sz.width, term_sz.height });
|
emit_fmt("Screen size: {d}w x {d}h\n\n", .{ term_sz.width, term_sz.height });
|
||||||
|
@ -263,7 +274,9 @@ pub fn showStdColors() void {
|
||||||
while (color_idx < 8) {
|
while (color_idx < 8) {
|
||||||
defer color_idx += 1;
|
defer color_idx += 1;
|
||||||
emit(bg[color_idx]);
|
emit(bg[color_idx]);
|
||||||
if (color_idx==7) { emit(fg[0]); }
|
if (color_idx == 7) {
|
||||||
|
emit(fg[0]);
|
||||||
|
}
|
||||||
emit_fmt("{u} {d:2} ", .{ sep, color_idx });
|
emit_fmt("{u} {d:2} ", .{ sep, color_idx });
|
||||||
}
|
}
|
||||||
emit(nl);
|
emit(nl);
|
||||||
|
@ -273,9 +286,10 @@ pub fn showStdColors() void {
|
||||||
while (color_idx < 16) {
|
while (color_idx < 16) {
|
||||||
defer color_idx += 1;
|
defer color_idx += 1;
|
||||||
emit(bg[color_idx]);
|
emit(bg[color_idx]);
|
||||||
if (color_idx==15) { emit(fg[0]); }
|
if (color_idx == 15) {
|
||||||
|
emit(fg[0]);
|
||||||
|
}
|
||||||
emit_fmt("{u} {d:2} ", .{ sep, color_idx });
|
emit_fmt("{u} {d:2} ", .{ sep, color_idx });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(nl);
|
emit(nl);
|
||||||
|
@ -307,8 +321,7 @@ pub fn show216Colors() void {
|
||||||
// invert color id for readability
|
// invert color id for readability
|
||||||
if (color_idx > 17) {
|
if (color_idx > 17) {
|
||||||
fg_idx = 0;
|
fg_idx = 0;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
fg_idx = 15;
|
fg_idx = 15;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -359,15 +372,7 @@ pub fn scrollMarquee() void {
|
||||||
emit(marquee_bg);
|
emit(marquee_bg);
|
||||||
|
|
||||||
//quotes - will confirm animations are working on current terminal
|
//quotes - will confirm animations are working on current terminal
|
||||||
const txt =[_][]const u8{
|
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 };
|
||||||
" 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
|
const txt_len: u8 = txt.len / 2; // print two rows at a time
|
||||||
|
|
||||||
//fade txt in and out
|
//fade txt in and out
|
||||||
|
@ -405,7 +410,6 @@ pub fn scrollMarquee() void {
|
||||||
//let quote chill for a second
|
//let quote chill for a second
|
||||||
std.time.sleep(1000 * std.time.ns_per_ms);
|
std.time.sleep(1000 * std.time.ns_per_ms);
|
||||||
|
|
||||||
|
|
||||||
//fade out
|
//fade out
|
||||||
fade_idx = fade_len - 1;
|
fade_idx = fade_len - 1;
|
||||||
while (fade_idx > 0) {
|
while (fade_idx > 0) {
|
||||||
|
@ -461,7 +465,6 @@ var t_now:i64=0;
|
||||||
var t_dur: f64 = 0.0;
|
var t_dur: f64 = 0.0;
|
||||||
var fps: f64 = 0.0;
|
var fps: f64 = 0.0;
|
||||||
|
|
||||||
|
|
||||||
pub fn initBuf() void {
|
pub fn initBuf() void {
|
||||||
//some lazy guesswork to make sure we have enough of a buffer to render DOOM fire.
|
//some lazy guesswork to make sure we have enough of a buffer to render DOOM fire.
|
||||||
const px_char_sz = px.len;
|
const px_char_sz = px.len;
|
||||||
|
@ -501,10 +504,13 @@ pub fn paintBuf() void {
|
||||||
bs_sz_min = bs_len;
|
bs_sz_min = bs_len;
|
||||||
bs_sz_max = bs_len;
|
bs_sz_max = bs_len;
|
||||||
bs_sz_avg = 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;
|
bs_sz_avg = bs_sz_avg * (bs_frame_tic - 1) / bs_frame_tic + bs_len / bs_frame_tic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -516,8 +522,9 @@ pub fn paintBuf() void {
|
||||||
}
|
}
|
||||||
|
|
||||||
// initBuf(); defer freeBuf();
|
// initBuf(); defer freeBuf();
|
||||||
pub fn freeBuf() void { allocator.free(bs); }
|
pub fn freeBuf() void {
|
||||||
|
allocator.free(bs);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn showDoomFire() void {
|
pub fn showDoomFire() void {
|
||||||
//term size => fire size
|
//term size => fire size
|
||||||
|
@ -605,8 +612,7 @@ pub fn showDoomFire() void {
|
||||||
//bounds checking
|
//bounds checking
|
||||||
if ((spread_px == 0) and (doFire_idx >= FIRE_W)) {
|
if ((spread_px == 0) and (doFire_idx >= FIRE_W)) {
|
||||||
screen_buf[doFire_idx - FIRE_W] = 0;
|
screen_buf[doFire_idx - FIRE_W] = 0;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
spread_rnd_idx = rand.intRangeAtMost(u8, 0, 3);
|
spread_rnd_idx = rand.intRangeAtMost(u8, 0, 3);
|
||||||
if (doFire_idx >= (spread_rnd_idx + 1)) {
|
if (doFire_idx >= (spread_rnd_idx + 1)) {
|
||||||
spread_dst = doFire_idx - spread_rnd_idx + 1;
|
spread_dst = doFire_idx - spread_rnd_idx + 1;
|
||||||
|
@ -669,5 +675,4 @@ pub fn main() anyerror!void {
|
||||||
checkTermSz();
|
checkTermSz();
|
||||||
showTermCap();
|
showTermCap();
|
||||||
showDoomFire();
|
showDoomFire();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue