From: Armaan Bhojwani Date: Sun, 16 May 2021 02:34:30 +0000 (-0400) Subject: Sleep during loops X-Git-Url: https://git.armaanb.net/?p=pong.git;a=commitdiff_plain;h=c8a444f1416c62879ad1d3ffb97019c25d24d79d Sleep during loops Doesn't max out the CPU --- diff --git a/pong.c b/pong.c index 0a75050..c158607 100644 --- a/pong.c +++ b/pong.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -35,6 +36,14 @@ randsign(void) return pow(-1, rand()); } +void +msleep(void) +{ + struct timeval timeout; + timeout.tv_usec = 10000; + select(NULL, NULL, NULL, NULL, &timeout); // Sleep 100 ms +} + int main(void) { srand((unsigned int)time(NULL)); @@ -95,6 +104,7 @@ main(void) { } else if (key == 'q') { die(0); } + msleep(); } } @@ -177,5 +187,6 @@ main(void) { mvprintw(y, x, " "); y = (y == max_y) ? max_y - 1 : 1; } + msleep(); } }