From 3df203c588c5e28ab78257a072a54f7cf99b9abf Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Thu, 9 Jul 2020 23:58:17 +0100 Subject: [PATCH] Can now add lines to ui and draw. --- game.c | 7 ++++--- main.c | 7 +++++-- ui.c | 25 +++++++++++++++++++------ ui.h | 4 +++- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/game.c b/game.c index 902f472..993b283 100644 --- a/game.c +++ b/game.c @@ -4,6 +4,7 @@ #include "grid.h" #include "vect.h" #include "log.h" +#include "ui.h" static Grid* grid = 0; @@ -47,6 +48,9 @@ void initGame() int height = 0; // stdscr is screen created by initscr() getmaxyx(stdscr, height, width); + + addLinei("x:", &cursor.x); + addLinei("y:", &cursor.y); grid = initGrid(width, height); //randomizeGrid(grid); @@ -143,9 +147,6 @@ void drawLastPressed(char ch) void drawCurPos() { - attron(COLOR_PAIR(2)); - mvprintw(1, 0, "curpos: %i, %i", cursor.x, cursor.y); - attroff(COLOR_PAIR(2)); attron(COLOR_PAIR(3)); mvaddch(cursor.y, cursor.x, ' '); attroff(COLOR_PAIR(3)); diff --git a/main.c b/main.c index 7d2682d..818c061 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,7 @@ // game #include "game.h" #include "log.h" +#include "ui.h" int main() @@ -15,8 +16,8 @@ int main() // framerate of the game const int FRAME_RATE = 30; const float FRAME_TIME = 1.f/(float)FRAME_RATE; - startLog("log.txt"); + startLog("log.txt"); initGame(); float t = 0; @@ -27,8 +28,9 @@ int main() handleInput(ch); drawGame(); - drawLastPressed(ch); + //drawLastPressed(ch); drawCurPos(); + drawUI(); refresh(); @@ -38,6 +40,7 @@ int main() t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC; } + endUI(); endGame(); endLog(); return 0; diff --git a/ui.c b/ui.c index d39a0aa..294d92d 100644 --- a/ui.c +++ b/ui.c @@ -1,5 +1,6 @@ #include #include +#include #include "ui.h" typedef struct { @@ -11,20 +12,32 @@ static unsigned int ln_count = 0; static Linei* lines = NULL; -void initUI() -{ - lines = (Linei*)malloc(sizeof(Linei) * ++ln_count); -} +static void startUI(); void addLinei(char* msg, int* var) { - if (!lines) return; - if (ln_count >1) lines = realloc(lines, sizeof(Linei) * ++ln_count); + if (!lines) startUI(); + else lines = realloc(lines, sizeof(Linei) * ++ln_count); Linei line = { msg, var }; lines[ln_count-1] = line; } 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); + } + attroff(COLOR_PAIR(2)); } +void endUI() +{ + free(lines); +} + +static void startUI() +{ + lines = (Linei*)malloc(sizeof(Linei) * ++ln_count); +} diff --git a/ui.h b/ui.h index c6e75de..9f24b81 100644 --- a/ui.h +++ b/ui.h @@ -1,10 +1,12 @@ #ifndef UI_H #define UI_H -void initUI(); +//void startUI(); void addLinei(char* msg, int* var); void drawUI(); +void endUI(); + #endif