clean wait for key function
authorLaurent Mazet <mazet@softndesign.org>
Fri, 18 Oct 2024 20:12:48 +0000 (22:12 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Fri, 18 Oct 2024 20:12:48 +0000 (22:12 +0200)
pacman.c
time.c
time.h

index 5ded060164bbe7826ee52fa71bf464ba931a9e0c..8bc68c6377c1e176001d5bf19934ae2087608379 100644 (file)
--- a/pacman.c
+++ b/pacman.c
@@ -306,6 +306,8 @@ int main (int argc, char *argv[])
             mode = 2;
             sprintf (msg, "End of game");
             msgwindow (msg, xboard + (board->width - strlen (msg)) / 2, yboard + (board->height - 1) / 2, strlen (msg), 1);
+            waitforkey ('q');
+            break;
         }
 
         int ch = getch ();
@@ -329,18 +331,14 @@ int main (int argc, char *argv[])
         case KEY_ESC:
         case 'h':
             helpwindow (help, xboard + (board->width - strmaxlen (help, '\n')) / 2, yboard + (board->height - strnbchr (help, '\n') - 1) / 2);
-            while (getch () == ERR) {
-                msleep (200);
-            }
+            waitforkey (0);
             tic (&turn);
             draw = 1;
             break;
         case 'p':
             sprintf (msg, "Game paused");
             msgwindow (msg, xboard + (board->width - strlen (msg)) / 2, yboard + (board->height - 1) / 2, strlen (msg), 1);
-            while (getch () == ERR) {
-                msleep (200);
-            }
+            waitforkey (0);
             tic (&turn);
             draw = 1;
             break;
@@ -361,10 +359,6 @@ int main (int argc, char *argv[])
             break;
         }
 
-        if (mode == 2) {
-            continue;
-        }
-
         /* game controls */
         switch (ch) {
         case KEY_UP:
diff --git a/time.c b/time.c
index 2ccb9f6e6dd9aa535c852b6a44e0a8e37b3fc15a..7c0d9d1b07c51accf0d69cc33ac00fbe0c91b5be 100644 (file)
--- a/time.c
+++ b/time.c
@@ -1,3 +1,4 @@
+#include <curses.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/time.h>
@@ -42,4 +43,15 @@ void msleep (int msec)
     usleep (msec * 1000);
 }
 
+void waitforkey (char key)
+{
+    char c;
+    while ((c = getch ()) == ERR) {
+        if ((key) && (c == key)) {
+            break;
+        }
+        msleep (200);
+    }
+}
+
 /* vim: set ts=4 sw=4 et: */
diff --git a/time.h b/time.h
index d991595fafab60c81b0541d771ee4a9142e6d601..f4a29e752b82c8790dd73d62fc023befe0912bfd 100644 (file)
--- a/time.h
+++ b/time.h
@@ -13,6 +13,8 @@ int toc (timeval_t *t0);
 
 void msleep (int msec);
 
+void waitforkey (char key);
+
 #endif /* __TIME_H__ */
 
 /* vim: set ts=4 sw=4 et: */