Add minor improvements and cleanup

This commit is contained in:
Justine Tunney 2020-10-27 03:39:46 -07:00
parent 9e3e985ae5
commit feed0d2b0e
163 changed files with 2286 additions and 2245 deletions

View file

@ -248,7 +248,6 @@ int main(int argc, char *argv[]) {
void *rgb;
size_t size;
unsigned yn, xn;
cancolor();
GetTermSize(&yn, &xn);
yn *= 2;
size = yn * xn * 4;

View file

@ -17,6 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/log/color.h"
#include "libc/log/log.h"
#include "libc/nexgen32e/cpuid4.h"
#include "libc/nexgen32e/nexgen32e.h"
@ -30,9 +31,6 @@
#include "tool/decode/lib/idname.h"
#include "tool/decode/lib/x86idnames.h"
#define RED (cancolor() ? "\x1b[91m" : "")
#define GREEN (cancolor() ? "\x1b[32m" : "")
#define RESET (cancolor() ? "\x1b[0m" : "")
#define CANIUSE(FEATURE) caniuse(#FEATURE, X86_HAVE(FEATURE))
#define SHOW(CONSTANT) show(#CONSTANT, CONSTANT)

View file

@ -1,7 +1,9 @@
#include "dsp/tty/tty.h"
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/termios.h"
#include "libc/log/check.h"
#include "libc/log/color.h"
#include "libc/log/log.h"
#include "libc/macros.h"
#include "libc/math.h"
@ -21,10 +23,10 @@
#define WRITE(s) write(STDOUT_FILENO, s, strlen(s))
struct Sphere {
double cx, cy, cz, r;
long double cx, cy, cz, r;
};
const char *kShades[] = {
static const char *kShades[] = {
"\e[48;5;232m ", "\e[48;5;233m ", "\e[48;5;234m ", "\e[48;5;235m ",
"\e[48;5;236m ", "\e[48;5;237m ", "\e[48;5;238m ", "\e[48;5;239m ",
"\e[48;5;240m ", "\e[48;5;241m ", "\e[48;5;242m ", "\e[48;5;243m ",
@ -33,52 +35,53 @@ const char *kShades[] = {
"\e[48;5;252m ", "\e[48;5;253m ", "\e[48;5;254m ", "\e[48;5;255m ",
};
jmp_buf jb_;
double light_[3] = {-50, 0, 50};
struct Sphere pos_ = {11, 11, 11, 11};
struct Sphere neg_ = {1, 1, -4, 11};
static jmp_buf jb_;
static long double light_[3] = {-50, 0, 50};
static struct Sphere pos_ = {11, 11, 11, 11};
static struct Sphere neg_ = {1, 1, -4, 11};
static void OnCtrlC(int sig) {
longjmp(jb_, 1);
}
static void Normalize(double v[3]) {
double len;
len = 1 / sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
static void Normalize(long double v[3]) {
long double len;
len = 1 / sqrtl(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
v[0] *= len;
v[1] *= len;
v[2] *= len;
}
static double Dot(const double x[3], const double y[3]) {
return fabs(x[0] * y[0] + x[1] * y[1] + x[2] * y[2]);
static long double Dot(const long double x[3], const long double y[3]) {
return fabsl(x[0] * y[0] + x[1] * y[1] + x[2] * y[2]);
}
/* check if a ray (x,y, -inf)->(x, y, inf) hits a sphere; if so, return
the intersecting z values. z1 is closer to the eye */
static int HitSphere(struct Sphere *s, double x, double y, double z[2]) {
double zsq;
static int HitSphere(struct Sphere *s, long double x, long double y,
long double z[2]) {
long double zsq;
x -= s->cx;
y -= s->cy;
zsq = s->r * s->r - (x * x + y * y);
if (zsq < 0) {
return 0;
} else {
zsq = sqrt(zsq);
zsq = sqrtl(zsq);
z[0] = s->cz - zsq;
z[1] = s->cz + zsq;
return 1;
}
}
static void DrawSphere(double k, double ambient) {
static void DrawSphere(long double k, long double ambient) {
int i, j, hit_result;
double x, y, vec[3], zb[2], zs[2];
for (i = floor(pos_.cy - pos_.r); i <= ceil(pos_.cy + pos_.r); i++) {
y = i + .5;
for (j = floor(pos_.cx - 2 * pos_.r); j <= ceil(pos_.cx + 2 * pos_.r);
long double x, y, vec[3], zb[2], zs[2];
for (i = floorl(pos_.cy - pos_.r); i <= ceill(pos_.cy + pos_.r); i++) {
y = i + .5L;
for (j = floorl(pos_.cx - 2 * pos_.r); j <= ceill(pos_.cx + 2 * pos_.r);
j++) {
x = .5 * (j - pos_.cx) + .5 + pos_.cx;
x = .5L * (j - pos_.cx) + .5L + pos_.cx;
if (!HitSphere(&pos_, x, y, zb)) {
/* ray lands in blank space, draw bg */
hit_result = 0;
@ -114,10 +117,10 @@ static void DrawSphere(double k, double ambient) {
break;
}
Normalize(vec);
WRITE(
kShades[MIN(ARRAYLEN(kShades) - 1,
MAX(0, lround((1 - (pow(Dot(light_, vec), k) + ambient)) *
(ARRAYLEN(kShades) - 1))))]);
WRITE(kShades[min(
ARRAYLEN(kShades) - 1,
max(0, lroundl((1 - (powl(Dot(light_, vec), k) + ambient)) *
(ARRAYLEN(kShades) - 1))))]);
}
WRITE("\e[0m\n");
}
@ -125,7 +128,7 @@ static void DrawSphere(double k, double ambient) {
}
int main() {
double ang;
long double ang;
struct termios old;
if (cancolor()) {
WRITE("\e[?25l");
@ -134,13 +137,12 @@ int main() {
ang = 0;
for (;;) {
WRITE("\e[H");
light_[1] = cos(ang * 2);
light_[2] = cos(ang);
light_[0] = sin(ang);
light_[1] = cosl(ang * 2);
sincosl(ang, &light_[0], &light_[2]);
Normalize(light_);
ang += .05;
DrawSphere(1.5, .01);
usleep(1. / FRAMERATE * 1e6);
ang += .05L;
DrawSphere(1.5L, .01L);
usleep(1.L / FRAMERATE * 1e6);
}
}
WRITE("\e[0m\e[H\e[J\e[?25h");

View file

@ -590,7 +590,6 @@ int main(int argc, char *argv[]) {
unsigned yd, xd;
__fast_math();
showcrashreports();
cancolor();
GetOpts(argc, argv);
// if sizes are given, 2 cases:
// - positive values: use that as the target size

View file

@ -668,7 +668,6 @@ int main(int argc, char *argv[]) {
break;
}
}
cancolor();
showcrashreports();
for (i = optind; i < argc; ++i) {
WithImageFile(argv[i], scaler);

View file

@ -399,7 +399,6 @@ void WithImageFile(const char *path,
int main(int argc, char *argv[]) {
int i;
cancolor();
GetOpts(&argc, argv);
stbi_set_unpremultiply_on_load(true);
for (i = optind; i < argc; ++i) {

View file

@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
r = (rgb & 0xff0000) >> 020;
tty = rgb2tty(r, g, b);
printf("\e[48;5;%dm \e[0m %d \\e[48;5;%dm %s #%02x%02x%02x\n", tty.xt,
tty.xt, tty.xt, indexdoublenulstring(kXtermName, tty.xt), r, g, b);
tty.xt, tty.xt, IndexDoubleNulString(kXtermName, tty.xt), r, g, b);
}
return 0;
}

View file

@ -1603,7 +1603,6 @@ int main(int argc, char *argv[]) {
sigaddset(&wut, SIGPIPE);
sigprocmask(SIG_SETMASK, &wut, NULL);
if (!NoDebug()) showcrashreports();
cancolor();
fullclear_ = true;
GetOpts(argc, argv);
if (!tuned_) PickDefaults();
@ -1613,7 +1612,7 @@ int main(int argc, char *argv[]) {
ffplay_ = commandvenv("FFPLAY", "ffplay");
infd_ = STDIN_FILENO;
outfd_ = STDOUT_FILENO;
nullfd_ = open("/dev/null", O_APPEND | O_RDWR | O_NONBLOCK);
nullfd_ = open("/dev/null", O_APPEND | O_RDWR);
if (!setjmp(jb_)) {
xsigaction(SIGINT, OnCtrlC, 0, 0, NULL);
xsigaction(SIGHUP, OnCtrlC, 0, 0, NULL);
@ -1625,11 +1624,11 @@ int main(int argc, char *argv[]) {
__cxa_atexit(OnExit, NULL, NULL);
g_logfile = fopen(logpath_, "a");
if (ischardev(infd_) && ischardev(outfd_)) {
CHECK_NE(-1, fcntl(infd_, F_SETFL, O_NONBLOCK));
/* CHECK_NE(-1, fcntl(infd_, F_SETFL, O_NONBLOCK)); */
} else if (infd_ != outfd_) {
infd_ = -1;
}
CHECK_NE(-1, fcntl(outfd_, F_SETFL, O_NONBLOCK));
/* CHECK_NE(-1, fcntl(outfd_, F_SETFL, O_NONBLOCK)); */
if (CanPlayAudio()) MakeLatencyLittleLessBad();
TryToOpenFrameBuffer();
RenounceSpecialPrivileges();

View file

@ -700,7 +700,7 @@ int main(int argc, char *argv[]) {
for (i = 0; i < 256; ++i) {
printf("\e[48;5;%dm \e[0m \e[38;5;%dm██\e[0m \e[1;48;5;%dm "
"\e[0m \e[1;38;5;%dm██\e[0m %-6hhu%-18s#%02hhx%02hhx%02hhx\n",
i, i, i, i, i, indexdoublenulstring(kXtermName, i), kXtermRgb[i].r,
i, i, i, i, i, IndexDoubleNulString(kXtermName, i), kXtermRgb[i].r,
kXtermRgb[i].g, kXtermRgb[i].b);
}
return 0;