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

7
main.c
View File

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

25
ui.c
View File

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

4
ui.h
View File

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