Skip to content

Commit

Permalink
Fix strobing.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed May 2, 2022
1 parent ddda1d0 commit 886a92f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 5 additions & 3 deletions demodir/termgl_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion lib/termgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -93,13 +94,19 @@ 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:
* TGL_Z_BUFFER - depth buffer
* 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
*/
Expand Down
13 changes: 11 additions & 2 deletions src/termgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 886a92f

Please sign in to comment.