diff options
| -rw-r--r-- | main.c | 85 |
1 files changed, 55 insertions, 30 deletions
@@ -218,8 +218,8 @@ static void free_buf(VuiBuffer *buf) { buf->height = 0; } -static inline int bchr_equiv(VuiBuffer *back, VuiBuffer *front, int x, int y) { - return BCHR(back,x,y) == BCHR(front,x,y) && (BATTR(back,x,y) == BATTR(front,x,y)); +static inline int bchr_equiv(VuiBuffer *a, VuiBuffer *b, unsigned i) { + return a->chr[i] == b->chr[i] && a->attr[i] == b->attr[i]; } static char *vui_out = NULL; @@ -284,7 +284,7 @@ static inline void vui_out_flush(void) { fflush(stdout); #if 0 static unsigned out_frame = 0; - FILE *f = fopen("out_log.txt", "w"); + FILE *f = fopen("out_log.txt", "a"); assert(f); fprintf(f, "\n\n:: OUTPUT FRAME %u ::\n", ++out_frame); fwrite(vui_out, 1, vui_outn, f); @@ -293,12 +293,16 @@ static inline void vui_out_flush(void) { vui_outn = 0; } -static inline void curs_move(int src_x, int src_y, int dst_x, int dst_y) { +static inline void curs_move(unsigned *restrict ptr_x, unsigned *restrict ptr_y, unsigned dst_x, unsigned dst_y) { + unsigned src_x = *ptr_x; + unsigned src_y = *ptr_y; if (src_x != dst_x && src_y != dst_y) { - if (dst_x > 0) { - vui_outf(CSI "%d;%dH", dst_y + 1, dst_x + 1); + if (dst_x == src_x - 1) { + vui_outf(CSI "\b%ud", dst_y + 1); + } else if (dst_x > 0) { + vui_outf(CSI "%u;%uH", dst_y + 1, dst_x + 1); } else if (dst_y > 0) { - vui_outf(CSI "%dH", dst_y + 1); + vui_outf(CSI "%uH", dst_y + 1); } else { vui_outs(CSI "H"); } @@ -308,19 +312,23 @@ static inline void curs_move(int src_x, int src_y, int dst_x, int dst_y) { } else if (dst_x == src_x - 1) { vui_outc('\b'); } else { - vui_outf(CSI "%dG", dst_x + 1); + vui_outf(CSI "%uG", dst_x + 1); } } else if (src_y != dst_y) { - if (dst_y == src_y - 1) { + if (dst_y == src_y - 1 && dst_x == src_x + 1) { vui_outs("\x1b" "M"); - } else if (dst_y == src_y + 1) { + abort(); + } else if (dst_y == src_y + 1 && dst_x == src_x + 1) { vui_outs("\x1b" "D"); - } if (dst_y > 0) { - vui_outf(CSI "%dd", dst_y + 1); + abort(); + } else if (dst_y > 0) { + vui_outf(CSI "%ud", dst_y + 1); } else { vui_outs(CSI "d"); } } + *ptr_x = dst_x; + *ptr_y = dst_y; } #define M(s) do {\ @@ -420,21 +428,42 @@ void vui_blit(void) { VuiBuffer *front = vui_win.front; VuiBuffer *back = vui_win.back; - vui_outf(CSI "H" CSI "0m"); - //vui_outs(CSI "2J"); + // vui_outf(CSI "H" CSI "0m"); + // vui_outs(CSI "2J"); + // vui_outs(CSI "3J"); static VuiAttr attr_last = ATTR_DEFAULT; + static unsigned cur_x = 0; + static unsigned cur_y = 0; + + vui_outs(CSI "H" CSI "0m"); + attr_last = ATTR_DEFAULT; + cur_x = 0; + cur_y = 0; scrolled_x = vui_win.scroll_x; scrolled_y = vui_win.scroll_y; + //vui_win.redraw_all = 1; if (vui_win.redraw_all) { + vui_outs(CSI "H" CSI "0m"); + attr_last = ATTR_DEFAULT; vui_changes = COLS * LINES; + unsigned max = COLS * LINES; + for (unsigned i = 0; i < max; i++) { + attr_chg(&attr_last, front->attr[i]); + vui_outc(front->chr[i]); + } + vui_outs(CSI "H"); + cur_x = 0; + cur_y = 0; + /* for (unsigned y = 0; y < LINES; y++) { for (unsigned x = 0; x < COLS; x++) { attr_chg(&attr_last, ATTR(x, y)); vui_outc(CHR(x, y)); } } + */ vui_win.redraw_all = 0; vui_win.scroll_x = 0; vui_win.scroll_y = 0; @@ -443,7 +472,6 @@ void vui_blit(void) { vui_changes = 0; vui_max_change = 0; - unsigned cur_x = 0, cur_y = 0; /* TODO: * @@ -464,15 +492,13 @@ void vui_blit(void) { if (vui_win.scroll_x < 0) { for (unsigned y = 0; y < LINES; y++) { - curs_move(cur_x, cur_y, 0, y); + curs_move(&cur_x, &cur_y, 0, y); vui_outf(CSI "%dP", -vui_win.scroll_x); - cur_y = y; } } else if (vui_win.scroll_x > 0) { for (unsigned y = 0; y < LINES; y++) { - curs_move(cur_x, cur_y, 0, y); + curs_move(&cur_x, &cur_y, 0, y); vui_outf(CSI "%d@", vui_win.scroll_x); - cur_y = y; } } @@ -490,24 +516,20 @@ scrolled: vui_win.scroll_y = 0; } - vui_win.scroll_x = 0; - vui_win.scroll_y = 0; - for (unsigned y = 0; y < LINES; y++) { unsigned x = 0; while (x < COLS) { - while (x < COLS && bchr_equiv(back, front, x, y)) x++; + while (x < COLS && bchr_equiv(back, front, (y * COLS) + x)) x++; if (x >= COLS) break; unsigned x0 = x; VuiAttr a = ATTR(x0, y); - while (x < COLS && !bchr_equiv(back, front, x, y) && ATTR(x, y) == a) x++; + while (x < COLS && !bchr_equiv(back, front, (y * COLS) + x) && BATTR(front, x, y) == a) x++; if (x0 != x) { vui_changes++; - curs_move(cur_x, cur_y, x0, y); + curs_move(&cur_x, &cur_y, x0, y); attr_chg(&attr_last, a); vui_outsn(&CHR(x0, y), x - x0); cur_x = x; - cur_y = y; if (x - x0 > vui_max_change) vui_max_change = x - x0; } } @@ -659,9 +681,6 @@ int main(int argc, const char **argv) { if (y < top) { camy += y - top; y = top; } if (y > bottom) { camy += y - bottom; y = bottom; } - int dcx = camx - lcamx, dcy = camy - lcamy; - vui_scroll(-dcx, -dcy); - /* if (x < 0) x += win.width; if (x > win.width) x -= win.width; @@ -716,12 +735,18 @@ int main(int argc, const char **argv) { for (unsigned x = 0; x < COLS; x++) { int tx = x + camx, ty = y + camy; char ch = " ',."[(tx^ty)&3]; - VuiAttr a = (((tx>>2)^(ty>>2)) % 3 == 0) ? BG_WHITE : BG_RED; + int abp = (((tx>>4)^(ty>>4)) % 3 == 0); + VuiAttr aa = abp ? BG_CYAN : BG_WHITE; + VuiAttr ab = abp ? BG_BLUE : BG_RED; + VuiAttr a = (((tx>>2)^(ty>>2)) % 3 == 0) ? aa : ab; vui_chra(x, y, ch, a); } } #endif + + int dcx = camx - lcamx, dcy = camy - lcamy; draw(NULL); + vui_scroll(-dcx, -dcy); clock_nanosleep(CLOCK_MONOTONIC, 0, &(struct timespec) { .tv_nsec = 15 * 1000000 }, NULL); if (!wait_for_input(STDIN_FILENO, 0)) continue; |
