From b2ccc0bb1d331342b5fa0b11715590a87bb294ac Mon Sep 17 00:00:00 2001 From: girst Date: Mon, 30 Apr 2018 16:40:28 +0200 Subject: [PATCH] stomp radius bugfix go figure: the signal handler interrupting the poll(2) call makes it go right though. that meant, the stomp radius might not be shown the full 150ms, but only a fraction of it if SIGALRM comes in at a unfavourable time. --- mines.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/mines.c b/mines.c index 330082f..c5b26ce 100644 --- a/mines.c +++ b/mines.c @@ -18,6 +18,7 @@ #define _POSIX_C_SOURCE 2 /*for getopt, sigaction in c99*/ #include +#include #include #include #include @@ -35,6 +36,7 @@ #define COL_OFFSET 2 #define BIG_MOVE 5 #define MOUSE_MAX 231 +#define STOMP_TIMEOUT 150 /*ms*/ #define MIN(a,b) (a>b?b:a) #define MAX(a,b) (a>b?a:b) @@ -204,6 +206,7 @@ int everything_opened (void) { } int wait_mouse_up (int l, int c) { /* TODO: should not take minefield-coords but absolute ones */ + //TODO: make stomp radius visible while mouse is down instead of after mouse up unsigned char mouse2[3]; int level = 1; int l2, c2; @@ -309,11 +312,21 @@ int do_uncover (int* is_newgame) { AROUND(g.p[0], g.p[1]) if (AR_CELL.o == CLOSED && AR_CELL.f !=FLAG) partial_show_minefield (ROW, COL, HIGHLIGHT); - /* TODO: save g.p, then in ~.1s execute: - AROUND(saved_l, saved_r) partial_show_minefield (ROW, COL, NORMAL); - using setitimer is tricky, because we already use SIGALRM; and it - should also trigger when a key is pressed. maybe a getchar() with - a read(2)-timeout + ungetch()? (usleep did not show the stomp at all?!) */ + fflush(stdout); /* won't display without */ + + /* block SIGALRM, otherwise poll gets cancelled by the timer: */ + sigset_t sig; + sigemptyset (&sig); + sigaddset(&sig, SIGALRM); + sigprocmask (SIG_BLOCK, &sig, NULL); + /* wait for timout or keypress: */ + struct pollfd fds; + fds.fd = 0; fds.events = POLLIN; + poll(&fds, 1, STOMP_TIMEOUT); + /* restore signal mask: */ + sigprocmask (SIG_UNBLOCK, &sig, NULL); + + AROUND(g.p[0], g.p[1]) partial_show_minefield(ROW, COL, NORMAL); } if (everything_opened()) return GAME_WON; -- 2.39.3