Skip to content

Commit 886a92f

Browse files
committed
Fix strobing.
1 parent ddda1d0 commit 886a92f

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

demodir/termgl_demo.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void demo_mandelbrot(const unsigned res_x, const unsigned res_y, const unsigned
129129
{
130130
TGL *tgl = tgl_init(res_x, res_y, &gradient_full);
131131
assert(tgl);
132-
assert(!tgl_enable(tgl, TGL_OUTPUT_BUFFER));
132+
assert(!tgl_enable(tgl, TGL_OUTPUT_BUFFER | TGL_PROGRESSIVE));
133133

134134
const unsigned frame_max = 90;
135135
const unsigned i_max = 255;
@@ -199,7 +199,7 @@ void demo_teapot(const unsigned res_x, const unsigned res_y, const unsigned fram
199199
assert(tgl);
200200
assert(!tgl3d_init(tgl));
201201
tgl3d_cull_face(tgl, TGL_BACK | TGL_CCW);
202-
assert(!tgl_enable(tgl, TGL_DOUBLE_CHARS | TGL_CULL_FACE | TGL_Z_BUFFER | TGL_OUTPUT_BUFFER));
202+
assert(!tgl_enable(tgl, TGL_DOUBLE_CHARS | TGL_CULL_FACE | TGL_Z_BUFFER | TGL_OUTPUT_BUFFER | TGL_PROGRESSIVE));
203203
tgl3d_camera(tgl, 1.57f, 0.1f, 5.f);
204204

205205
// Load triangles
@@ -291,7 +291,7 @@ void demo_star(const unsigned res_x, const unsigned res_y, const unsigned framet
291291
{
292292
TGL *tgl = tgl_init(res_x, res_y, &gradient_min);
293293
assert(tgl);
294-
assert(!tgl_enable(tgl, TGL_OUTPUT_BUFFER));
294+
assert(!tgl_enable(tgl, TGL_OUTPUT_BUFFER | TGL_PROGRESSIVE));
295295

296296
const float pi2 = 6.28319f;
297297
const unsigned n = 8, d = 3;
@@ -381,6 +381,7 @@ int main(int argc, char **argv)
381381
(void)argc;
382382
(void)argv;
383383

384+
tgl_clear_screen();
384385
puts(HELPTEXT_HEADER);
385386
unsigned col, row;
386387
tglutil_get_console_size(&col, &row, true);
@@ -389,6 +390,7 @@ int main(int argc, char **argv)
389390

390391
unsigned n = 0;
391392
assert(scanf("%u", &n) == 1);
393+
tgl_clear_screen();
392394

393395
switch (n) {
394396
case 1u:

lib/termgl.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ enum {
4949
TGL_Z_BUFFER = 0x04,
5050
/* settings */
5151
TGL_DOUBLE_CHARS = 0x10,
52+
TGL_PROGRESSIVE = 0x20,
5253
#ifdef TERMGL3D
53-
TGL_CULL_FACE = 0x20,
54+
TGL_CULL_FACE = 0x40,
5455
#endif
5556
};
5657

@@ -93,13 +94,19 @@ int tgl_flush(TGL *tgl);
9394
*/
9495
void tgl_clear(TGL *tgl, uint8_t buffers);
9596

97+
/**
98+
* Clears the screen
99+
*/
100+
void tgl_clear_screen(void);
101+
96102
/**
97103
* Enables or disables certain settings
98104
* @param settings: bitwise combination of settings:
99105
* TGL_Z_BUFFER - depth buffer
100106
* TGL_DOUBLE_CHARS - square pixels by printing 2 characters per pixel
101107
* TGL_CULL_FACE - (3D ONLY) cull specified triangle faces
102108
* 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
109+
* 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
103110
* @return 0 on success, -1 on failure
104111
* On failure, errno is set to value specified by: https://www.man7.org/linux/man-pages/man3/malloc.3.html#ERRORS
105112
*/

src/termgl.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Setting MACROs
1111
*/
12-
#define TGL_CLEAR_SCREEN do {fputs("\033[1;1H\033[2J", stdout);} while (0)
12+
#define TGL_CLEAR_SCR do {fputs("\033[1;1H\033[2J", stdout);} while (0)
1313
#define TGL_TYPEOF __typeof__
1414
#define TGL_MALLOC malloc
1515
#define TGL_FREE free
@@ -122,6 +122,11 @@ void tgl_clear(TGL * const tgl, const uint8_t buffers)
122122
memset(tgl->output_buffer, '\0', tgl->output_buffer_size);
123123
}
124124

125+
void tgl_clear_screen(void)
126+
{
127+
TGL_CLEAR_SCR;
128+
}
129+
125130
TGL *tgl_init(const unsigned width, const unsigned height, const TGLGradient * gradient)
126131
{
127132
#ifdef TGL_OS_WINDOWS
@@ -224,7 +229,11 @@ char *itgl_generate_sgr(const uint16_t color_prev, const uint16_t color_cur, cha
224229

225230
int tgl_flush(TGL * const tgl)
226231
{
227-
TGL_CLEAR_SCREEN;
232+
if (tgl->settings & TGL_PROGRESSIVE)
233+
CALL_STDOUT(fputs("\033[;H", stdout), -1);
234+
else
235+
TGL_CLEAR_SCR;
236+
228237
uint16_t color = 0x0007;
229238
unsigned row, col;
230239
Pixel *pixel = tgl->frame_buffer;

0 commit comments

Comments
 (0)