Modified animations.c to work with an arbitrary number of animations, as long as ANIMATION_NUM is increased and the animation init, run, and free arrays are populated

This commit is contained in:
Slade Getz 2023-05-25 20:05:52 -06:00
parent 48d48a1af3
commit 0eb0050c69
1 changed files with 49 additions and 36 deletions

View File

@ -23,10 +23,37 @@
#include <linux/kd.h> #include <linux/kd.h>
#endif #endif
#define DOOM_STEPS 13 /****************************/
/* Define Animation presets */
/* for easy extendability */
/****************************/
#define FUNC_PTR(X) (&X)
#define ANIMATION_NUM 2
// function prototypes are necessary
static void doom_free(struct term_buf* buf); static void doom_free(struct term_buf* buf);
static void doom(struct term_buf* term_buf);
static void doom_init(struct term_buf* buf);
static void matrix_free(struct term_buf* buf); static void matrix_free(struct term_buf* buf);
static void matrix(struct term_buf* term_buf);
static void matrix_init(struct term_buf* buf);
// create arrays for each type of function
static const void* ANIM_INITS[] = {
FUNC_PTR(doom_init),
FUNC_PTR(matrix_init)
};
static const void* ANIM_RUNS[] = {
FUNC_PTR(doom),
FUNC_PTR(matrix)
};
static const void* ANIM_FREES[] = {
FUNC_PTR(doom_free),
FUNC_PTR(matrix_free)
};
#define DOOM_STEPS 13
@ -347,15 +374,13 @@ void animate_free(struct term_buf* buf)
{ {
if (config.animate) if (config.animate)
{ {
switch (config.animation) int i;
{ if ((i = config.animation) < ANIMATION_NUM)
case 0: {
doom_free(buf); void (*fun_ptr)(struct term_buf*);
break; fun_ptr = ANIM_FREES[i];
case 1: (*fun_ptr)(buf);
matrix_free(buf); }
break;
}
} }
} }
@ -366,19 +391,13 @@ void animate(struct term_buf* buf)
if (config.animate) if (config.animate)
{ {
switch(config.animation) int i;
{ if ((i = config.animation) < ANIMATION_NUM)
case 0: {
{ void (*fun_ptr)(struct term_buf*);
doom(buf); fun_ptr = ANIM_RUNS[i];
break; (*fun_ptr)(buf);
} }
case 1:
{
matrix(buf);
break;
}
}
} }
} }
@ -386,19 +405,13 @@ void animate_init(struct term_buf* buf)
{ {
if (config.animate) if (config.animate)
{ {
switch(config.animation) int i;
{ if ((i = config.animation) < ANIMATION_NUM)
case 0: {
{ void (*fun_ptr)(struct term_buf*);
doom_init(buf); fun_ptr = ANIM_INITS[i];
break; (*fun_ptr)(buf);
} }
case 1:
{
matrix_init(buf);
break;
}
}
} }
} }