Removed signal handler. Added method for user key input.

This commit is contained in:
Sheldon Lee 2020-06-18 01:44:21 +01:00
parent c003983ba3
commit c346dc0c27

31
main.c
View File

@ -2,33 +2,32 @@
#include <ncurses.h> #include <ncurses.h>
#include <unistd.h> #include <unistd.h>
#include <math.h> #include <math.h>
#include <signal.h> #include <time.h>
#include "grid.h" #include "grid.h"
#include "vect.h" #include "vect.h"
static volatile int running = 1;
static void contain(int* pos, int* velocity, int min, int max); static void contain(int* pos, int* velocity, int min, int max);
// if ^C is pressed, set running flag to false
static void signalHandler(int sig);
int main() int main()
{ {
signal(SIGINT, signalHandler);
int count = 0;
// init // init
bool running = true;
initscr(); initscr();
//noecho(); raw();
// doesn't wait for user to input.
// timeout(100) waits for 100ms for input.
timeout(0);
curs_set(FALSE); curs_set(FALSE);
int width = 0; int width = 0;
int height = 0; int height = 0;
// stdscr created by initscr() // stdscr is screen created by initscr()
getmaxyx(stdscr, height, width); getmaxyx(stdscr, height, width);
// hz // hz
const int FRAME_RATE = 10; const int FRAME_RATE = 100;
const int DELAY = (float)pow(10,6)/(float)FRAME_RATE; const int DELAY = (float)pow(10,6)/(float)FRAME_RATE;
@ -36,12 +35,18 @@ int main()
initGrid(&grid, width, height); initGrid(&grid, width, height);
randomizeGrid(&grid); randomizeGrid(&grid);
clock_t t = 0;
while (running) { while (running) {
clock_t start_t = clock();
char ch = getch();
if (ch == 'q') running = false;
drawGrid(&grid); drawGrid(&grid);
refresh(); refresh();
updateGrid(&grid); updateGrid(&grid);
t += clock()-start_t;
usleep(DELAY); usleep(DELAY);
} }
@ -61,9 +66,3 @@ static void contain(int* pos, int* velocity, int min, int max)
*velocity *= -1; *velocity *= -1;
} }
} }
static void signalHandler(int sig)
{
printf("interrupt\n");
running = 0;
}