From 886a92f3e035dabb5652ab2f1f1fb73ead47416f Mon Sep 17 00:00:00 2001 From: Wojciech Graj Date: Mon, 2 May 2022 17:13:40 +0200 Subject: [PATCH] Fix strobing. --- demodir/termgl_demo.c | 8 +++++--- lib/termgl.h | 9 ++++++++- src/termgl.c | 13 +++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/demodir/termgl_demo.c b/demodir/termgl_demo.c index a636fc0..55da5ed 100644 --- a/demodir/termgl_demo.c +++ b/demodir/termgl_demo.c @@ -129,7 +129,7 @@ void demo_mandelbrot(const unsigned res_x, const unsigned res_y, const unsigned { TGL *tgl = tgl_init(res_x, res_y, &gradient_full); assert(tgl); - assert(!tgl_enable(tgl, TGL_OUTPUT_BUFFER)); + assert(!tgl_enable(tgl, TGL_OUTPUT_BUFFER | TGL_PROGRESSIVE)); const unsigned frame_max = 90; const unsigned i_max = 255; @@ -199,7 +199,7 @@ void demo_teapot(const unsigned res_x, const unsigned res_y, const unsigned fram assert(tgl); assert(!tgl3d_init(tgl)); tgl3d_cull_face(tgl, TGL_BACK | TGL_CCW); - assert(!tgl_enable(tgl, TGL_DOUBLE_CHARS | TGL_CULL_FACE | TGL_Z_BUFFER | TGL_OUTPUT_BUFFER)); + assert(!tgl_enable(tgl, TGL_DOUBLE_CHARS | TGL_CULL_FACE | TGL_Z_BUFFER | TGL_OUTPUT_BUFFER | TGL_PROGRESSIVE)); tgl3d_camera(tgl, 1.57f, 0.1f, 5.f); // Load triangles @@ -291,7 +291,7 @@ void demo_star(const unsigned res_x, const unsigned res_y, const unsigned framet { TGL *tgl = tgl_init(res_x, res_y, &gradient_min); assert(tgl); - assert(!tgl_enable(tgl, TGL_OUTPUT_BUFFER)); + assert(!tgl_enable(tgl, TGL_OUTPUT_BUFFER | TGL_PROGRESSIVE)); const float pi2 = 6.28319f; const unsigned n = 8, d = 3; @@ -381,6 +381,7 @@ int main(int argc, char **argv) (void)argc; (void)argv; + tgl_clear_screen(); puts(HELPTEXT_HEADER); unsigned col, row; tglutil_get_console_size(&col, &row, true); @@ -389,6 +390,7 @@ int main(int argc, char **argv) unsigned n = 0; assert(scanf("%u", &n) == 1); + tgl_clear_screen(); switch (n) { case 1u: diff --git a/lib/termgl.h b/lib/termgl.h index 5657437..70527dd 100644 --- a/lib/termgl.h +++ b/lib/termgl.h @@ -49,8 +49,9 @@ enum { TGL_Z_BUFFER = 0x04, /* settings */ TGL_DOUBLE_CHARS = 0x10, + TGL_PROGRESSIVE = 0x20, #ifdef TERMGL3D - TGL_CULL_FACE = 0x20, + TGL_CULL_FACE = 0x40, #endif }; @@ -93,6 +94,11 @@ int tgl_flush(TGL *tgl); */ void tgl_clear(TGL *tgl, uint8_t buffers); +/** + * Clears the screen + */ +void tgl_clear_screen(void); + /** * Enables or disables certain settings * @param settings: bitwise combination of settings: @@ -100,6 +106,7 @@ void tgl_clear(TGL *tgl, uint8_t buffers); * TGL_DOUBLE_CHARS - square pixels by printing 2 characters per pixel * TGL_CULL_FACE - (3D ONLY) cull specified triangle faces * TGL_OUTPUT_BUFFER - output buffer allowing for just one print to flush. Mush faster on most terminals, but requires a few hundred kilobytes of memory + * TGL_PROGRESSIVE - Over-write previous frame. Eliminates strobing but requires call to tgl_clear_screen before drawing smaller image and after resizing terminal if terminal size was smaller than frame size * @return 0 on success, -1 on failure * On failure, errno is set to value specified by: https://www.man7.org/linux/man-pages/man3/malloc.3.html#ERRORS */ diff --git a/src/termgl.c b/src/termgl.c index c9acbc2..8ad13bd 100644 --- a/src/termgl.c +++ b/src/termgl.c @@ -9,7 +9,7 @@ /** * Setting MACROs */ -#define TGL_CLEAR_SCREEN do {fputs("\033[1;1H\033[2J", stdout);} while (0) +#define TGL_CLEAR_SCR do {fputs("\033[1;1H\033[2J", stdout);} while (0) #define TGL_TYPEOF __typeof__ #define TGL_MALLOC malloc #define TGL_FREE free @@ -122,6 +122,11 @@ void tgl_clear(TGL * const tgl, const uint8_t buffers) memset(tgl->output_buffer, '\0', tgl->output_buffer_size); } +void tgl_clear_screen(void) +{ + TGL_CLEAR_SCR; +} + TGL *tgl_init(const unsigned width, const unsigned height, const TGLGradient * gradient) { #ifdef TGL_OS_WINDOWS @@ -224,7 +229,11 @@ char *itgl_generate_sgr(const uint16_t color_prev, const uint16_t color_cur, cha int tgl_flush(TGL * const tgl) { - TGL_CLEAR_SCREEN; + if (tgl->settings & TGL_PROGRESSIVE) + CALL_STDOUT(fputs("\033[;H", stdout), -1); + else + TGL_CLEAR_SCR; + uint16_t color = 0x0007; unsigned row, col; Pixel *pixel = tgl->frame_buffer;