From f4b239c74be05113533f91c45566a07858b0bb57 Mon Sep 17 00:00:00 2001 From: wr7 Date: Thu, 21 Dec 2023 17:57:50 -0600 Subject: [PATCH] Add support for animations after terminal resize If animations are enabled and the terminal is resized, restart the animation instead of stopping it --- src/draw.c | 64 +++++++++++++++++++++++++++++++++++++++++------------- src/draw.h | 6 +++-- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/src/draw.c b/src/draw.c index 0679244..142142b 100644 --- a/src/draw.c +++ b/src/draw.c @@ -619,11 +619,16 @@ void position_input( password->visible_len = len; } -static void doom_init(struct term_buf* buf) +static void doom_reset(struct term_buf* buf) { - buf->init_width = buf->width; - buf->init_height = buf->height; + if(buf->astate.doom) + { + doom_free(buf); + } + buf->astate.doom = malloc(sizeof(struct doom_state)); + buf->astate.doom->buffer_width = buf->width; + buf->astate.doom->buffer_height = buf->height; if (buf->astate.doom == NULL) { @@ -649,14 +654,27 @@ static void doom_free(struct term_buf* buf) free(buf->astate.doom); } -// Adapted from cmatrix -static void matrix_init(struct term_buf* buf) +static void doom_init(struct term_buf* buf) { - buf->init_width = buf->width; - buf->init_height = buf->height; + buf->astate.doom = NULL; + doom_reset(buf); +} + +static void matrix_free(struct term_buf* buf); +// Adapted from cmatrix +static void matrix_reset(struct term_buf* buf) +{ + if(buf->astate.matrix) + { + matrix_free(buf); + } + buf->astate.matrix = malloc(sizeof(struct matrix_state)); struct matrix_state* s = buf->astate.matrix; + s->grid_width = buf->width; + s->grid_height = buf->height; + if (s == NULL) { dgn_throw(DGN_ALLOC); @@ -709,6 +727,11 @@ static void matrix_init(struct term_buf* buf) dgn_throw(DGN_ALLOC); } + if(buf->height <= 3) + { + return; + } + // Initialize grid for (int i = 0; i <= buf->height; ++i) { @@ -727,6 +750,12 @@ static void matrix_init(struct term_buf* buf) } } +static void matrix_init(struct term_buf* buf) +{ + buf->astate.matrix = NULL; + matrix_reset(buf); +} + static void matrix_free(struct term_buf* buf) { free(buf->astate.matrix->grid[0]); @@ -776,23 +805,23 @@ static void doom(struct term_buf* term_buf) {0x2588, 8, 4}, // white }; + if ((term_buf->width != term_buf->astate.doom->buffer_width) || (term_buf->height != term_buf->astate.doom->buffer_height)) + { + doom_reset(term_buf); + } + uint16_t src; uint16_t random; uint16_t dst; - uint16_t w = term_buf->init_width; + uint16_t w = term_buf->astate.doom->buffer_width; uint8_t* tmp = term_buf->astate.doom->buf; - if ((term_buf->width != term_buf->init_width) || (term_buf->height != term_buf->init_height)) - { - return; - } - struct tb_cell* buf = tb_cell_buffer(); for (uint16_t x = 0; x < w; ++x) { - for (uint16_t y = 1; y < term_buf->init_height; ++y) + for (uint16_t y = 1; y < term_buf->astate.doom->buffer_height; ++y) { src = y * w + x; random = ((rand() % 7) & 3); @@ -835,11 +864,16 @@ static void matrix(struct term_buf* buf) // Chars change mid-scroll const bool changes = true; - if ((buf->width != buf->init_width) || (buf->height != buf->init_height)) + if ((buf->height <= 3)) { return; } + if ((buf->width != s->grid_width) || (buf->height != s->grid_height)) + { + matrix_reset(buf); + } + count += 1; if (count > frame_delay) { diff --git a/src/draw.h b/src/draw.h index b8a64c4..d68e893 100644 --- a/src/draw.h +++ b/src/draw.h @@ -28,6 +28,8 @@ struct matrix_dot struct matrix_state { struct matrix_dot** grid; + uint16_t grid_width; + uint16_t grid_height; int* length; int* spaces; int* updates; @@ -36,6 +38,8 @@ struct matrix_state struct doom_state { uint8_t* buf; + uint16_t buffer_width; + uint16_t buffer_height; }; union anim_state @@ -48,8 +52,6 @@ struct term_buf { uint16_t width; uint16_t height; - uint16_t init_width; - uint16_t init_height; struct box box_chars; char* info_line;