ncurses-gameoflife/main.c

63 lines
1.1 KiB
C
Raw Normal View History

2020-06-12 09:46:44 +08:00
#include <ncurses.h>
// Delay and timing.
2020-06-12 09:46:44 +08:00
#include <unistd.h>
#include <time.h>
// math
#include <math.h>
// game
#include "game.h"
2020-06-12 09:46:44 +08:00
2020-06-12 09:46:44 +08:00
int main()
{
// init
2020-06-12 09:46:44 +08:00
initscr();
raw();
2020-06-23 09:34:29 +08:00
noecho();
// 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);
2020-06-23 09:34:29 +08:00
// cursor
init_pair(3, COLOR_RED, COLOR_RED);
// doesn't wait for user to input.
// timeout(100) waits for 100ms for input.
timeout(0);
2020-06-12 09:46:44 +08:00
curs_set(FALSE);
// framerate of the game
2020-06-23 09:34:29 +08:00
const int FRAME_RATE = 30;
const float FRAME_TIME = 1.f/(float)FRAME_RATE;
2020-06-12 09:46:44 +08:00
initGame();
2020-06-23 09:34:29 +08:00
float t = 0;
while (isRunning()) {
clock_t start_t = clock();
char ch = getch();
handleInput(ch);
drawGame();
drawLastPressed(ch);
drawCurPos();
refresh();
2020-06-24 11:05:49 +08:00
updateGame();
2020-06-12 09:46:44 +08:00
usleep(pow(10,6)*(FRAME_TIME-t));
float t = (float) (clock()-start_t) / (float) CLOCKS_PER_SEC;
2020-06-12 09:46:44 +08:00
}
endwin();
return 0;
}