X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=ansitris;a=blobdiff_plain;f=src%2Fmain.c;h=dc240fd9a5a4dfa99cb663389654ff872c212803;hp=6f27d73c04d1d774df69cde37b0279744c240cfb;hb=0bdf1f5608fc18fe9d999bd400e951df9342c831;hpb=b9e8d87bd8fe53a3ca4dae3020c3df7bc117f7c7 diff --git a/src/main.c b/src/main.c index 6f27d73..dc240fd 100644 --- a/src/main.c +++ b/src/main.c @@ -5,16 +5,24 @@ #include #include #include +#include +#include #include "game.h" int init(void); void cleanup(void); +long get_msec(void); -const char *termfile = "/dev/tty"; -struct termios saved_term; +static const char *termfile = "/dev/tty"; +static struct termios saved_term; +static struct timeval tv0; int main(int argc, char **argv) { + int res, c; + long msec, next; + struct timeval tv; + if(argc > 1) { termfile = argv[1]; } @@ -23,11 +31,33 @@ int main(int argc, char **argv) return 1; } + gettimeofday(&tv0, 0); + + tv.tv_sec = tick_interval / 1000; + tv.tv_usec = (tick_interval % 1000) * 1000; + for(;;) { - proc_input(); - if(quit) break; + fd_set rdset; + FD_ZERO(&rdset); + FD_SET(0, &rdset); + + while((res = select(1, &rdset, 0, 0, &tv)) == -1 && errno == EINTR); + + if(res > 0 && FD_ISSET(0, &rdset)) { + while((c = fgetc(stdin)) >= 0) { + game_input(c); + if(quit) goto end; + } + } + + msec = get_msec(); + next = update(msec); + + tv.tv_sec = next / 1000; + tv.tv_usec = (next % 1000) * 1000; } +end: cleanup(); return 0; } @@ -37,7 +67,7 @@ int init(void) int fd; struct termios term; - if((fd = open(termfile, O_RDWR)) == -1) { + if((fd = open(termfile, O_RDWR | O_NONBLOCK)) == -1) { fprintf(stderr, "failed to open terminal device: %s: %s\n", termfile, strerror(errno)); return -1; } @@ -79,3 +109,12 @@ void cleanup(void) cleanup_game(); tcsetattr(0, TCSAFLUSH, &saved_term); } + +long get_msec(void) +{ + struct timeval tv; + + gettimeofday(&tv, 0); + + return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; +}