Can now add lines to ui and draw.

This commit is contained in:
Sheldon Lee 2020-07-09 23:58:17 +01:00
parent 41db31e121
commit 3df203c588
4 changed files with 31 additions and 12 deletions

7
game.c
View File

@ -4,6 +4,7 @@
#include "grid.h" #include "grid.h"
#include "vect.h" #include "vect.h"
#include "log.h" #include "log.h"
#include "ui.h"
static Grid* grid = 0; static Grid* grid = 0;
@ -48,6 +49,9 @@ void initGame()
// stdscr is screen created by initscr() // stdscr is screen created by initscr()
getmaxyx(stdscr, height, width); getmaxyx(stdscr, height, width);
addLinei("x:", &cursor.x);
addLinei("y:", &cursor.y);
grid = initGrid(width, height); grid = initGrid(width, height);
//randomizeGrid(grid); //randomizeGrid(grid);
cursor.x = width/2; cursor.y = height/2; cursor.x = width/2; cursor.y = height/2;
@ -143,9 +147,6 @@ void drawLastPressed(char ch)
void drawCurPos() void drawCurPos()
{ {
attron(COLOR_PAIR(2));
mvprintw(1, 0, "curpos: %i, %i", cursor.x, cursor.y);
attroff(COLOR_PAIR(2));
attron(COLOR_PAIR(3)); attron(COLOR_PAIR(3));
mvaddch(cursor.y, cursor.x, ' '); mvaddch(cursor.y, cursor.x, ' ');
attroff(COLOR_PAIR(3)); attroff(COLOR_PAIR(3));

7
main.c
View File

@ -8,6 +8,7 @@
// game // game
#include "game.h" #include "game.h"
#include "log.h" #include "log.h"
#include "ui.h"
int main() int main()
@ -15,8 +16,8 @@ int main()
// framerate of the game // framerate of the game
const int FRAME_RATE = 30; const int FRAME_RATE = 30;
const float FRAME_TIME = 1.f/(float)FRAME_RATE; const float FRAME_TIME = 1.f/(float)FRAME_RATE;
startLog("log.txt");
startLog("log.txt");
initGame(); initGame();
float t = 0; float t = 0;
@ -27,8 +28,9 @@ int main()
handleInput(ch); handleInput(ch);
drawGame(); drawGame();
drawLastPressed(ch); //drawLastPressed(ch);
drawCurPos(); drawCurPos();
drawUI();
refresh(); refresh();
@ -38,6 +40,7 @@ int main()
t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC; t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;
} }
endUI();
endGame(); endGame();
endLog(); endLog();
return 0; return 0;

25
ui.c
View File

@ -1,5 +1,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <ncurses.h>
#include "ui.h" #include "ui.h"
typedef struct { typedef struct {
@ -11,20 +12,32 @@ static unsigned int ln_count = 0;
static Linei* lines = NULL; static Linei* lines = NULL;
void initUI() static void startUI();
{
lines = (Linei*)malloc(sizeof(Linei) * ++ln_count);
}
void addLinei(char* msg, int* var) void addLinei(char* msg, int* var)
{ {
if (!lines) return; if (!lines) startUI();
if (ln_count >1) lines = realloc(lines, sizeof(Linei) * ++ln_count); else lines = realloc(lines, sizeof(Linei) * ++ln_count);
Linei line = { msg, var }; Linei line = { msg, var };
lines[ln_count-1] = line; lines[ln_count-1] = line;
} }
void drawUI() 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);
}

4
ui.h
View File

@ -1,10 +1,12 @@
#ifndef UI_H #ifndef UI_H
#define UI_H #define UI_H
void initUI(); //void startUI();
void addLinei(char* msg, int* var); void addLinei(char* msg, int* var);
void drawUI(); void drawUI();
void endUI();
#endif #endif