summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c102
1 files changed, 69 insertions, 33 deletions
diff --git a/main.c b/main.c
index be9fea8..a765669 100644
--- a/main.c
+++ b/main.c
@@ -65,9 +65,9 @@ typedef enum {
A_REVERSE = 1 << 13,
} VuiAttr;
-#define ATTR_FG(a) (a & 0xf)
-#define ATTR_BG(a) ((a>>4) & 0xf)
-#define ATTR_A(a) (a & ~0xff)
+#define ATTR_FG(a) ((a) & 0xf)
+#define ATTR_BG(a) (((a)>>4) & 0xf)
+#define ATTR_A(a) ((a) & ~0xff)
#define ATTR_DEFAULT (FG_WHITE | BG_BLACK)
typedef struct {
@@ -215,36 +215,50 @@ static inline void curs_move(int src_x, int src_y, int dst_x, int dst_y) {
}
}
-static void attr_chg(VuiAttr from, VuiAttr to) {
+static void attr_chg(VuiAttr *ptr, VuiAttr to) {
+ VuiAttr from = *ptr;
if (from == to) return;
- printf("\x1b[");
+ int chg_attr = (ATTR_A(from) != ATTR_A(to));
+ chg_attr = 0;
- if (ATTR_A(from) != ATTR_A(to)) {
- printf("0");
- from = ATTR_DEFAULT;
+ if (chg_attr) {
+ printf("\x1b[0");
if (to & A_BOLD) printf(";1");
if (to & A_DIM) printf(";2");
if (to & A_ITALIC) printf(";3");
if (to & A_UNDERSCORE) printf(";4");
if (to & A_BLINK) printf(";5");
if (to & A_REVERSE) printf(";7");
+ from = ATTR_DEFAULT;
+ assert(ATTR_FG(from) == FG_WHITE);
+ assert(ATTR_BG(from) == BG_BLACK);
}
int f_fg = ATTR_FG(from);
int f_bg = ATTR_BG(from);
int t_fg = ATTR_FG(to);
int t_bg = ATTR_BG(to);
- int chg_fg = (f_fg != t_fg);
- int chg_bg = (f_bg != t_bg);
- if (chg_fg) printf(";%d", t_fg > 7 ? 90 + (t_fg&7) : 30 + t_fg);
- if (chg_bg) printf(";%d", t_bg > 7 ? 100 + (t_bg&7) : 40 + t_bg);
+ int chg_fg = (t_fg != f_fg);
+ int chg_bg = (t_bg != f_bg);
+ if (chg_fg || chg_bg) {
+ if (chg_attr) putchar(';');
+ else printf("\x1b[");
+ if (chg_fg) printf("%d", t_fg > 7 ? 90 + (t_fg&7) : 30 + t_fg);
+ if (chg_bg) {
+ if (chg_fg) putchar(';');
+ printf("%d", t_bg > 7 ? 100 + (t_bg&7) : 40 + t_bg);
+ }
+ }
+
+ if (chg_fg || chg_bg || chg_attr) putchar('m');
- putchar('m');
+ *ptr = to;
}
unsigned vui_changes = 0;
+unsigned vui_max_change = 0;
void vui_blit(void) {
VuiBuffer *front = win.front;
VuiBuffer *back = win.back;
@@ -256,10 +270,8 @@ void vui_blit(void) {
vui_changes = win.width * win.height;
for (unsigned y = 0; y < win.height; y++) {
for (unsigned x = 0; x < win.width; x++) {
- VuiAttr a = BATTR(front, x, y);
- attr_chg(attr_last, a);
- putchar(BCHR(front, x, y));
- attr_last = a;
+ attr_chg(&attr_last, ATTR(x, y));
+ putchar(CHR(x, y));
}
}
win.redraw_all = 0;
@@ -267,6 +279,7 @@ void vui_blit(void) {
}
vui_changes = 0;
+ vui_max_change = 0;
unsigned cur_x = 0, cur_y = 0;
for (unsigned y = 0; y < win.height; y++) {
unsigned x = 0;
@@ -274,16 +287,16 @@ void vui_blit(void) {
while (x < win.width && bchr_equiv(back, front, x, y)) x++;
if (x >= win.width) break;
unsigned x0 = x;
- VuiAttr a = BATTR(front, x0, y);
- while (x < win.width && !bchr_equiv(back, front, x, y) && BATTR(front, x, y) == a) x++;
+ VuiAttr a = ATTR(x0, y);
+ while (x < win.width && !bchr_equiv(back, front, x, y) && ATTR(x, y) == a) x++;
if (x0 != x) {
vui_changes++;
curs_move(cur_x, cur_y, x0, y);
- attr_chg(attr_last, a);
- printf("%.*s", x - x0, &BCHR(front, x0, y));
+ attr_chg(&attr_last, a);
+ printf("%.*s", x - x0, &CHR(x0, y));
cur_x = x;
cur_y = y;
- attr_last = a;
+ if (x - x0 > vui_max_change) vui_max_change = x - x0;
}
}
}
@@ -304,7 +317,7 @@ void vui_chra(int x, int y, char c, VuiAttr a) {
}
void vui_chr(int x, int y, char c) {
- vui_chra(x, y, c, 0);
+ vui_chra(x, y, c, ATTR_DEFAULT);
}
int vui_avprintf(int x, int y, VuiAttr a, const char *fmt, va_list ap) {
@@ -362,21 +375,28 @@ int main(int argc, const char **argv) {
signal(SIGWINCH, on_sigwinch);
int x = 0, y = 0;
int dx = 0, dy = 0;
+ int camx = 0, camy = 0;
int C = '*';
unsigned frame = 0;
for (;;) {
- //vui_clear();
frame++;
+ camx += dx;
x += dx;
- if (frame & 1) y += dy;
+ if (frame & 1) {
+ camy += dy;
+ y += dy;
+ }
if (x < 0) x += win.width;
if (x > win.width) x -= win.width;
if (y < 0) y += win.height;
if (y > win.height) y -= win.height;
- static int spawning = 1;
- if (spawning) {
+ static int paused = 0;
+#if 1
+ /* weird cellular automata */
+ if (!paused) {
+ // int dx = 0, dy = 0; /* shadow on purpose */
for (int y = 0; y < win.height; y++) {
for (int x = 0; x < win.width; x++) {
int tx = x - dx, ty = y - dy;
@@ -394,11 +414,14 @@ int main(int argc, const char **argv) {
tyc += (random() % 3) - 1;
}
if (txa >= 0 && txa < win.width && tya >= 0 && tya < win.height) {
- ATTR(txa, tya) = BATTR(win.back, x, y);
+ //ATTR(txa, tya) = BATTR(win.back, x, y);
+ ATTR(txa, tya) = (BATTR(win.back,x,y) & ~0xf) | (ATTR(txa,tya) & 0xf);
if (!(random() & 127)) ATTR(x,y) = ATTR_DEFAULT;
+ //ATTR(txc, tyc) = (ATTR(txc, tyc) & ~0xf) | (ATTR(x, y) & 0xf);
}
if (txc >= 0 && txc < win.width && tyc >= 0 && tyc < win.height) {
CHR(txc, tyc) = BCHR(win.back, x, y);
+ ATTR(txc, tyc) = (ATTR(txc,tyc) & ~0xf) | (BATTR(win.back,x,y) & 0xf);
if (!(random() & 127)) CHR(x,y) = ' ';
}
}
@@ -412,7 +435,20 @@ int main(int argc, const char **argv) {
}
}
- vui_chra(x, y, C, ((x + y) & 0xf) | BG_BLUE | A_UNDERSCORE);
+#else
+ vui_clear();
+ for (unsigned y = 0; y < win.height; y++) {
+ for (unsigned x = 0; x < win.width; x++) {
+ int tx = x + camx, ty = y + camy;
+ char ch = " ',."[(tx^ty)&3];
+ ch = ' ';
+ VuiAttr a = (((tx>>2)^(ty>>2)) % 3 == 0) ? BG_WHITE : BG_RED;
+ vui_chra(x, y, ch, a);
+ }
+ }
+#endif
+
+ vui_chra(x, y, C, ((x + y) & 0xf) | BG_BBLUE);
vui_chra(x-1, y, C, ((x + y) & 0xf) | BG_BLUE | A_UNDERSCORE);
vui_chra(x+1, y, C, ((x + y) & 0xf) | BG_BLUE | A_UNDERSCORE);
vui_chra(x, y-1, C, ((x + y) & 0xf) | BG_BLUE | A_UNDERSCORE);
@@ -420,16 +456,16 @@ int main(int argc, const char **argv) {
vui_printf(-1, -1, "(%u, %u)", win.width, win.height);
vui_printf(0, -1, "front buffer = %p", (void*)win.front);
- vui_printf(-1, 0, "changes = %04u, chars = %04u", vui_changes, win.width * win.height);
+ vui_printf(-1, 0, "longest change = %02u, changes = %04u, chars = %04u", vui_max_change, vui_changes, win.width * win.height);
// win.redraw_all = 1;
vui_blit();
- if (!wait_for_input(STDIN_FILENO, 33)) continue;
+ if (!wait_for_input(STDIN_FILENO, 20)) continue;
int c = getchar();
if (c == 'q') break;
- if (c > 0x1f && c < 0x7f) C = c;
+ if (c > 0x20 && c < 0x7f) C = c;
switch (c) {
case 'k': dx=0; dy=-1; break;
case 'j': dx=0; dy=1; break;
@@ -439,7 +475,7 @@ int main(int argc, const char **argv) {
case 'u': dx=1; dy=-1; break;
case 'b': dx=-1; dy=1; break;
case 'n': dx=1; dy=1; break;
- case ' ': spawning = !spawning; break;
+ case ' ': paused = !paused; break;
default: dx=1; dy=0; break;
}
}