From bfb13b9c40359a509162894da6a36996dfcb84f9 Mon Sep 17 00:00:00 2001 From: stale Date: Sun, 2 May 2021 19:24:06 -0300 Subject: [PATCH] Add animation states --- src/draw.c | 41 ++++++++++++++++++++++++++++++----------- src/draw.h | 13 ++++++++++++- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/draw.c b/src/draw.c index 5b6ca62..1b40b5a 100644 --- a/src/draw.c +++ b/src/draw.c @@ -68,11 +68,18 @@ void draw_init(struct term_buf* buf) #endif } +static void doom_free(struct term_buf* buf); + void draw_free(struct term_buf* buf) { if (config.animate) { - free(buf->tmp_buf); + switch (config.animation) + { + case 0: + doom_free(buf); + break; + } } } @@ -480,18 +487,30 @@ static void doom_init(struct term_buf* buf) { buf->init_width = buf->width; buf->init_height = buf->height; + buf->astate.doom = malloc(sizeof(struct doom_state)); - u16 tmp_len = buf->width * buf->height; - buf->tmp_buf = malloc(tmp_len); - tmp_len -= buf->width; - - if (buf->tmp_buf == NULL) + if (buf->astate.doom == NULL) { dgn_throw(DGN_ALLOC); } - memset(buf->tmp_buf, 0, tmp_len); - memset(buf->tmp_buf + tmp_len, DOOM_STEPS - 1, buf->width); + u16 tmp_len = buf->width * buf->height; + buf->astate.doom->buf = malloc(tmp_len); + tmp_len -= buf->width; + + if (buf->astate.doom->buf == NULL) + { + dgn_throw(DGN_ALLOC); + } + + memset(buf->astate.doom->buf, 0, tmp_len); + memset(buf->astate.doom->buf + tmp_len, DOOM_STEPS - 1, buf->width); +} + +static void doom_free(struct term_buf* buf) +{ + free(buf->astate.doom->buf); + free(buf->astate.doom); } void animate_init(struct term_buf* buf) @@ -500,7 +519,7 @@ void animate_init(struct term_buf* buf) { switch(config.animation) { - default: + case 0: { doom_init(buf); break; @@ -533,7 +552,7 @@ static void doom(struct term_buf* term_buf) u16 dst; u16 w = term_buf->init_width; - u8* tmp = term_buf->tmp_buf; + u8* tmp = term_buf->astate.doom->buf; if ((term_buf->width != term_buf->init_width) || (term_buf->height != term_buf->init_height)) { @@ -581,7 +600,7 @@ void animate(struct term_buf* buf) { switch(config.animation) { - default: + case 0: { doom(buf); break; diff --git a/src/draw.h b/src/draw.h index 059c60b..1258596 100644 --- a/src/draw.h +++ b/src/draw.h @@ -18,6 +18,17 @@ struct box u32 right; }; +struct doom_state +{ + u8* buf; +}; + +union anim_state +{ + struct doom_state* doom; + struct matrix_state* matrix; +}; + struct term_buf { u16 width; @@ -33,7 +44,7 @@ struct term_buf u16 box_width; u16 box_height; - u8* tmp_buf; + union anim_state astate; }; void draw_init(struct term_buf* buf);