Implemented type flexibility for UI.

This commit is contained in:
Sheldon Lee 2020-07-12 02:39:17 +01:00
parent 305c090e31
commit bd9e09e53f
3 changed files with 35 additions and 14 deletions

4
game.c
View File

@ -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);

38
ui.c
View File

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <ncurses.h>
#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;
}
}

7
ui.h
View File

@ -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();