From 755ab6404f812bd6530c2bde4a41f53c38c27c84 Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Sun, 2 Apr 2023 20:05:35 -0400 Subject: [PATCH] add `tb_invalidate` --- termbox2.h | 26 ++++++++++++++++++++------ tests/test_invalidate/expected.ansi | 24 ++++++++++++++++++++++++ tests/test_invalidate/test.php | 18 ++++++++++++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 tests/test_invalidate/expected.ansi create mode 100755 tests/test_invalidate/test.php diff --git a/termbox2.h b/termbox2.h index 5db3f9e..0d61812 100644 --- a/termbox2.h +++ b/termbox2.h @@ -405,6 +405,11 @@ int tb_set_clear_attrs(uintattr_t fg, uintattr_t bg); /* Synchronizes the internal back buffer with the terminal by writing to tty. */ int tb_present(void); +/* Clears the internal front buffer effectively forcing a complete re-render of + * the back buffer to the tty. It is not necessary to call this under normal + * circumstances. */ +int tb_invalidate(void); + /* Sets the position of the cursor. Upper-left character is (0, 0). */ int tb_set_cursor(int cx, int cy); int tb_hide_cursor(void); @@ -516,12 +521,14 @@ int tb_set_input_mode(int mode); * TB_DEFAULT. For convenience, the value 0 is interpreted as TB_DEFAULT in * all modes. * - * Note, attributes persist after changing the output mode. This means if you, - * for example, start in TB_OUTPUT_NORMAL with some cells colored TB_CYAN (7) - * and then switch to TB_OUTPUT_GRAYSCALE, those cells will now render as a - * shade of gray. As such, it is recommended to avoid switching output modes - * at runtime unless your program can sensibly re-assign all attributes of all - * cells in the new output mode. + * Note, cell attributes persist after switching output modes. Any translation + * between, for example, TB_OUTPUT_NORMAL's TB_RED and TB_OUTPUT_TRUECOLOR's + * 0xff0000 must be performed by the caller. Also note that cells previously + * rendered in one mode may persist unchanged until the front buffer is cleared + * (such as after a resize event) at which point it will be re-interpreted and + * flushed according to the current mode. Callers may invoke tb_invalidate if + * it is desirable to immediately re-interpret and flush the entire screen + * according to the current mode. * * Note, not all terminals support all output modes, especially beyond * TB_OUTPUT_NORMAL. There is also no very reliable way to determine color @@ -1565,6 +1572,13 @@ int tb_present(void) { return TB_OK; } +int tb_invalidate(void) { + int rv; + if_not_init_return(); + if_err_return(rv, resize_cellbufs()); + return TB_OK; +} + int tb_set_cursor(int cx, int cy) { if_not_init_return(); int rv; diff --git a/tests/test_invalidate/expected.ansi b/tests/test_invalidate/expected.ansi new file mode 100644 index 0000000..04194e8 --- /dev/null +++ b/tests/test_invalidate/expected.ansi @@ -0,0 +1,24 @@ +#5[0;38:5:238mc[0;38:5:238my[0;38:5:238ma[0;38:5:238mn[0;38:5:238m [0;38:5:238m([0;38:5:238mt[0;38:5:238mh[0;38:5:238me[0;38:5:238mn[0;38:5:238m [0;38:5:238mg[0;38:5:238mr[0;38:5:238ma[0;38:5:238my[0;38:5:238m [0;38:5:238ma[0;38:5:238mf[0;38:5:238mt[0;38:5:238me[0;38:5:238mr[0;38:5:238m [0;38:5:238mm[0;38:5:238mo[0;38:5:238md[0;38:5:238me[0;38:5:238m [0;38:5:238ms[0;38:5:238mw[0;38:5:238mi[0;38:5:238mt[0;38:5:238mc[0;38:5:238mh[0;38:5:238m [0;38:5:238ma[0;38:5:238mn[0;38:5:238md[0;38:5:238m [0;38:5:238mi[0;38:5:238mn[0;38:5:238mv[0;38:5:238ma[0;38:5:238ml[0;38:5:238mi[0;38:5:238md[0;38:5:238ma[0;38:5:238mt[0;38:5:238me[0;38:5:238m) +#5[0;38:5:238mg[0;38:5:238mr[0;38:5:238ma[0;38:5:238my + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/test_invalidate/test.php b/tests/test_invalidate/test.php new file mode 100755 index 0000000..7b68c94 --- /dev/null +++ b/tests/test_invalidate/test.php @@ -0,0 +1,18 @@ +ffi->tb_init(); + +$y = 0; +$fg = 7; + +$test->ffi->tb_printf(0, $y++, $fg, 0, "cyan (then gray after mode switch and invalidate)"); +$test->ffi->tb_present(); + +$test->ffi->tb_set_output_mode($test->defines['TB_OUTPUT_GRAYSCALE']); +$test->ffi->tb_invalidate(); + +$test->ffi->tb_printf(0, $y++, $fg, 0, "gray"); +$test->ffi->tb_present(); + +$test->screencap(); -- 2.39.5