Added "color" retaining transparancy. Pixels are now drawn solid

This commit is contained in:
Sheldon Lee 2020-06-18 22:26:14 +01:00
parent 5a8285eb31
commit 59f7bfa061
2 changed files with 22 additions and 8 deletions

10
grid.c
View File

@ -96,10 +96,16 @@ void drawGrid(Grid* grid)
unsigned int width, height; unsigned int width, height;
width = grid->width; width = grid->width;
height = grid->size/width; height = grid->size/width;
// Init color pair init_pair(index, fg, bg);
init_pair(1, COLOR_BLUE, COLOR_WHITE);
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
if (grid->state[toIndex(grid, x, y)]) mvprintw(y, x, "X"); if (grid->state[toIndex(grid, x, y)]){
else mvprintw(y, x, " "); attron(COLOR_PAIR(1));
mvaddch(y, x, ' ');
attroff(COLOR_PAIR(1));
}
else mvaddch(y, x, ' ');
} }
} }
} }

20
main.c
View File

@ -15,6 +15,10 @@ int main()
bool running = true; bool running = true;
initscr(); initscr();
raw(); raw();
// allows for transparancy when color values in init_pair(); are set to -1 or, no pair specified
// e.g init_pair(1, COLOR_WHITE, -1) would be transparent background but white text
use_default_colors();
start_color();
// doesn't wait for user to input. // doesn't wait for user to input.
// timeout(100) waits for 100ms for input. // timeout(100) waits for 100ms for input.
timeout(0); timeout(0);
@ -26,8 +30,9 @@ int main()
// stdscr is screen created by initscr() // stdscr is screen created by initscr()
getmaxyx(stdscr, height, width); getmaxyx(stdscr, height, width);
// hz // framerate of the game
const int FRAME_RATE = 100; const int FRAME_RATE = 30;
const float FRAME_TIME = 1.f/(double)FRAME_RATE;
const int DELAY = (float)pow(10,6)/(float)FRAME_RATE; const int DELAY = (float)pow(10,6)/(float)FRAME_RATE;
@ -41,14 +46,17 @@ int main()
char ch = getch(); char ch = getch();
if (ch == 'q') running = false; if (ch == 'q') running = false;
drawGrid(&grid); float actual_frame_time = (float) t / (float) CLOCKS_PER_SEC;
refresh(); if (actual_frame_time >= FRAME_TIME) {
drawGrid(&grid);
refresh();
updateGrid(&grid);
t = 0;
}
updateGrid(&grid);
t += clock()-start_t; t += clock()-start_t;
usleep(DELAY);
} }
endwin(); endwin();