diff --git a/game.c b/game.c index c86f46f..6fb7c70 100644 --- a/game.c +++ b/game.c @@ -49,8 +49,8 @@ void initGame() // stdscr is screen created by initscr() getmaxyx(stdscr, height, width); - addLinei("x:", &cursor.x, 1, UI_INT); - addLinei("y:", &cursor.y, 1, UI_INT); + addLine("x:", &cursor.x, 1, UI_INT); + addLine("y:", &cursor.y, 1, UI_INT); grid = initGrid(width, height); //randomizeGrid(grid); diff --git a/ui.c b/ui.c index e2a9fcb..e26ddcd 100644 --- a/ui.c +++ b/ui.c @@ -2,6 +2,7 @@ #include #include #include "ui.h" +#include "log.h" typedef struct { char* msg; @@ -9,19 +10,22 @@ typedef struct { size_t size; ui_type type; -}Linei; +}Line; static unsigned int ln_count = 0; -static Linei* lines = NULL; +static Line* lines = NULL; +// declaration of locally used static void startUI(); -void addLinei(char* msg, void* var, int size, ui_type type) +static void drawLine(Line*, int offset); + +void addLine(char* msg, void* var, int size, ui_type type) { if (!lines) startUI(); - else lines = realloc(lines, sizeof(Linei) * ++ln_count); - Linei line = { msg, var }; + else lines = realloc(lines, sizeof(Line) * ++ln_count); + Line line = { msg, var, size, type}; lines[ln_count-1] = line; } @@ -29,8 +33,9 @@ void drawUI() { if (!lines) return; attron(COLOR_PAIR(2)); + for (int i = 0; i < ln_count; i++) { - mvprintw(i, 0, "%s %i", lines[i].msg, *lines[i].var); + drawLine(&lines[i], i); } attroff(COLOR_PAIR(2)); } @@ -40,7 +45,26 @@ void endUI() free(lines); } +// static definition static void startUI() { - lines = (Linei*)malloc(sizeof(Linei) * ++ln_count); + lines = (Line*)malloc(sizeof(Line) * ++ln_count); +} + +static void drawLine(Line* line, int offset) +{ + switch (line->type) { + case UI_INT: + mvprintw(offset, 0, "%s %i", line->msg, *((int*)line->var) ); + break; + case UI_FLOAT: + mvprintw(offset, 0, "%s %f", line->msg, *((float*)line->var) ); + break; + case UI_CHAR: + mvprintw(offset, 0, "%s %c", line->msg, *((char*)line->var) ); + break; + case UI_STRING: + mvprintw(offset, 0, "%s %s", line->msg, ((char*)line->var) ); + break; + } } diff --git a/ui.h b/ui.h index 60f84ef..693bdf4 100644 --- a/ui.h +++ b/ui.h @@ -3,18 +3,15 @@ #define ui_type uint8_t -#define ui_int int -#define ui_float float -#define ui_char char - #define UI_NULL 0 #define UI_INT 1 #define UI_FLOAT 2 #define UI_CHAR 3 +#define UI_STRING 4 //void startUI(); -void addLinei(char* msg, void* var, int size, ui_type type); +void addLine(char* msg, void* var, int size, ui_type type); void drawUI();