ncurses-gameoflife/ui.c

71 lines
1.2 KiB
C
Raw Permalink 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-12 09:39:17 +08:00
#include "log.h"
2020-07-09 08:26:08 +08:00
typedef struct {
char* msg;
void* var;
size_t size;
ui_type type;
2020-07-12 09:39:17 +08:00
}Line;
2020-07-09 08:26:08 +08:00
static unsigned int ln_count = 0;
2020-07-12 09:39:17 +08:00
static Line* lines = NULL;
2020-07-09 08:26:08 +08:00
2020-07-12 09:39:17 +08:00
// declaration of locally used
2020-07-10 06:58:17 +08:00
static void startUI();
2020-07-09 08:26:08 +08:00
2020-07-12 09:39:17 +08:00
static void drawLine(Line*, int offset);
void addLine(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();
2020-07-12 09:39:17 +08:00
else lines = realloc(lines, sizeof(Line) * ++ln_count);
Line line = { msg, var, size, type};
2020-07-09 08:26:08 +08:00
lines[ln_count-1] = line;
}
void drawUI()
{
2020-07-10 06:58:17 +08:00
if (!lines) return;
attron(COLOR_PAIR(2));
2020-07-12 09:39:17 +08:00
2020-07-10 06:58:17 +08:00
for (int i = 0; i < ln_count; i++) {
2020-07-12 09:39:17 +08:00
drawLine(&lines[i], i);
2020-07-10 06:58:17 +08:00
}
attroff(COLOR_PAIR(2));
2020-07-09 08:26:08 +08:00
}
2020-07-10 06:58:17 +08:00
void endUI()
{
free(lines);
}
2020-07-12 09:39:17 +08:00
// static definition
2020-07-10 06:58:17 +08:00
static void startUI()
{
2020-07-12 09:39:17 +08:00
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;
}
2020-07-10 06:58:17 +08:00
}