ncurses-gameoflife/ui.c

47 lines
747 B
C
Raw Normal View History

2020-07-09 08:26:08 +08:00
#include <stddef.h>
#include <stdlib.h>
2020-07-10 06:58:17 +08:00
#include <ncurses.h>
2020-07-08 07:30:37 +08:00
#include "ui.h"
2020-07-09 08:26:08 +08:00
typedef struct {
char* msg;
void* var;
size_t size;
ui_type type;
2020-07-09 08:26:08 +08:00
}Linei;
static unsigned int ln_count = 0;
static Linei* lines = NULL;
2020-07-10 06:58:17 +08:00
static void startUI();
2020-07-09 08:26:08 +08:00
void addLinei(char* msg, void* var, int size, ui_type type)
2020-07-09 08:26:08 +08:00
{
2020-07-10 06:58:17 +08:00
if (!lines) startUI();
else lines = realloc(lines, sizeof(Linei) * ++ln_count);
2020-07-09 08:26:08 +08:00
Linei line = { msg, var };
lines[ln_count-1] = line;
}
void drawUI()
{
2020-07-10 06:58:17 +08:00
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);
}
attroff(COLOR_PAIR(2));
2020-07-09 08:26:08 +08:00
}
2020-07-10 06:58:17 +08:00
void endUI()
{
free(lines);
}
static void startUI()
{
lines = (Linei*)malloc(sizeof(Linei) * ++ln_count);
}