From 7c70cb4eb45214d6867081744e3af6a0f715b6b0 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Fri, 12 Jun 2020 22:50:03 +0100 Subject: [PATCH] added terminal interrupt handlingw --- main.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 16598e5..d4d07a1 100644 --- a/main.c +++ b/main.c @@ -1,14 +1,21 @@ #include -#include #include #include #include +#include #include "grid.h" #include "vect.h" -void contain(int* pos, int* velocity, int min, int max); +static volatile int running = 1; + +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() { + signal(SIGINT, signalHandler); + int count = 0; //init initscr(); //noecho(); @@ -29,9 +36,7 @@ int main() initGrid(&grid, width, height); putPixel(&grid, pos.x, pos.y); - while (1) { - clear(); - + while (running) { clearGrid(&grid); putPixel(&grid, pos.x, pos.y); drawGrid(&grid); @@ -49,7 +54,7 @@ int main() return 0; } -void contain(int* pos, int* velocity, int min, int max) +static void contain(int* pos, int* velocity, int min, int max) { bool above_max = max < *pos; bool below_min = *pos < min; @@ -60,3 +65,9 @@ void contain(int* pos, int* velocity, int min, int max) *velocity *= -1; } } + +static void signalHandler(int sig) +{ + printf("interrupt\n"); + running = 0; +}