FPS now controlled by delay to reduce cpu usage. Added last key overlay.
This commit is contained in:
parent
75f670988c
commit
8361724ad5
1
grid.c
1
grid.c
@ -97,7 +97,6 @@ void drawGrid(Grid* grid)
|
||||
width = grid->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 x = 0; x < width; x++) {
|
||||
if (grid->state[toIndex(grid, x, y)]){
|
||||
|
53
main.c
53
main.c
@ -6,16 +6,28 @@
|
||||
#include "grid.h"
|
||||
#include "vect.h"
|
||||
|
||||
static bool running = true;
|
||||
|
||||
void handleInput(char ch);
|
||||
|
||||
void showLastPressed(char ch);
|
||||
|
||||
int main()
|
||||
{
|
||||
// init
|
||||
bool running = true;
|
||||
initscr();
|
||||
raw();
|
||||
|
||||
// Colors
|
||||
// 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();
|
||||
// cell
|
||||
init_pair(1, COLOR_BLUE, COLOR_WHITE);
|
||||
// text
|
||||
init_pair(2, COLOR_YELLOW, -1);
|
||||
|
||||
// doesn't wait for user to input.
|
||||
// timeout(100) waits for 100ms for input.
|
||||
timeout(0);
|
||||
@ -28,31 +40,32 @@ int main()
|
||||
getmaxyx(stdscr, height, width);
|
||||
|
||||
// framerate of the game
|
||||
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 FRAME_RATE = 144;
|
||||
const float FRAME_TIME = 1.f/(float)FRAME_RATE;
|
||||
|
||||
Grid grid;
|
||||
initGrid(&grid, width, height);
|
||||
randomizeGrid(&grid);
|
||||
|
||||
clock_t t = 0;
|
||||
float t = 0;
|
||||
while (running) {
|
||||
clock_t start_t = clock();
|
||||
char ch = getch();
|
||||
if (ch == 'q') running = false;
|
||||
|
||||
float actual_frame_time = (float) t / (float) CLOCKS_PER_SEC;
|
||||
handleInput(ch);
|
||||
|
||||
if (actual_frame_time >= FRAME_TIME) {
|
||||
drawGrid(&grid);
|
||||
refresh();
|
||||
updateGrid(&grid);
|
||||
t = 0;
|
||||
}
|
||||
// draw grid
|
||||
drawGrid(&grid);
|
||||
// draw overlays
|
||||
attron(COLOR_PAIR(2));
|
||||
showLastPressed(ch);
|
||||
attroff(COLOR_PAIR(2));
|
||||
|
||||
t += clock()-start_t;
|
||||
refresh();
|
||||
updateGrid(&grid);
|
||||
|
||||
usleep(pow(10,6)*(FRAME_TIME-t));
|
||||
float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;
|
||||
|
||||
}
|
||||
|
||||
@ -60,3 +73,13 @@ int main()
|
||||
return 0;
|
||||
}
|
||||
|
||||
void showLastPressed(char ch)
|
||||
{
|
||||
static char lastc = ' ';
|
||||
if (ch != -1) lastc = ch;
|
||||
mvprintw(0, 0, "LastPressed: %c", lastc);
|
||||
}
|
||||
void handleInput(char ch)
|
||||
{
|
||||
if (ch == 'q') running = false;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user