From 782bb3da9d2e2ba1cc467ae2548e10b08af7c84d Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Wed, 31 Jul 2024 13:31:36 +0200 Subject: [PATCH] separated time functions from main file --- tetris.c | 40 +++++++++++----------------------------- time.c | 34 ++++++++++++++++++++++++++++++++++ time.h | 18 ++++++++++++++++++ 3 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 time.c create mode 100644 time.h diff --git a/tetris.c b/tetris.c index 8b4b63d..7465c8a 100644 --- a/tetris.c +++ b/tetris.c @@ -1,21 +1,19 @@ /* depend: */ /* cflags: */ -/* linker: block.o color.c constant.o debug.o display.o function.o -lcurses */ -/* doslnk: block.o color.c constant.o debug.o display.o function.o -lpdc~1 */ -/* winlnk: block.o color.c constant.o debug.o display.o function.o -lpdcurses */ +/* linker: block.o color.c constant.o debug.o display.o function.o time.o -lcurses */ +/* doslnk: block.o color.c constant.o debug.o display.o function.o time.o -lpdc~1 */ +/* winlnk: block.o color.c constant.o debug.o display.o function.o time.o -lpdcurses */ #include #include #include #include -#include -#include -#include #include "constant.h" #include "debug.h" #include "display.h" #include "function.h" +#include "time.h" #include "type.h" /* static variables */ @@ -103,22 +101,6 @@ block_t *drawblock (board_t *board, block_t *blocks, int nb_blocks, block_t *cbl return cblock; } -void setendtime (struct timeval *t, int s) -{ - gettimeofday (t, NULL); - t->tv_usec += s * 1000l; - t->tv_sec += t->tv_usec / 1000000l; - t->tv_usec = t->tv_usec % 1000000l; -} - -int isovertime (struct timeval *t) -{ - struct timeval _c; - struct timeval *c = &_c; - gettimeofday (c, NULL); - return ((t->tv_sec < c->tv_sec) || ((t->tv_sec == c->tv_sec) && ( t->tv_usec < c->tv_usec))); -} - /* main function */ int main (int argc, char *argv[]) { @@ -246,7 +228,7 @@ int main (int argc, char *argv[]) setscale (nextblock, scale); /* init seed */ - srand (time (NULL)); + newseed (); /* get first bloc */ int xblock, yblock; @@ -303,9 +285,9 @@ int main (int argc, char *argv[]) int speed = 5000; float chronoratio = 5.0; int stop = 0; - struct timeval blockend = {0, 0}; - struct timeval blockend2 = {0, 0}; - struct timeval lineend = {0, 0}; + timeval_t blockend = {0, 0}; + timeval_t blockend2 = {0, 0}; + timeval_t lineend = {0, 0}; while (!stop) { char msg[128] = {0}; msgwindow (msg, xmsg, ymsg, lmsg); @@ -350,7 +332,7 @@ int main (int argc, char *argv[]) while (nblines-- > 0) { boardwindow (board, 0); stackboard (board); - usleep ((200 + 2000 / (lines / 10 + 1)) * 100); + msleep (20 + 200 / (lines / 10 + 1)); refresh (); } if (chrono) { @@ -395,7 +377,7 @@ int main (int argc, char *argv[]) while (nblines2-- > 0) { boardwindow (board2, 0); stackboard (board2); - usleep ((200 + 2000 / (lines / 10 + 1)) * 100); + msleep (20 + 200 / (lines / 10 + 1)); refresh (); } } @@ -425,7 +407,7 @@ int main (int argc, char *argv[]) if (*msg) { halfdelay (0); msgwindow (msg, xmsg, ymsg, lmsg); - usleep (800000); + msleep (800); } boardwindow (board, 0); diff --git a/time.c b/time.c new file mode 100644 index 0000000..4381e48 --- /dev/null +++ b/time.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +#include "time.h" + +void newseed () +{ + srand (time (NULL)); +} + +void setendtime (timeval_t *t, int s) +{ + gettimeofday (t, NULL); + t->tv_usec += s * 1000l; + t->tv_sec += t->tv_usec / 1000000l; + t->tv_usec = t->tv_usec % 1000000l; +} + +int isovertime (timeval_t *t) +{ + struct timeval _c; + struct timeval *c = &_c; + gettimeofday (c, NULL); + return ((t->tv_sec < c->tv_sec) || ((t->tv_sec == c->tv_sec) && ( t->tv_usec < c->tv_usec))); +} + +void msleep (int msec) +{ + usleep (msec * 1000); +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/time.h b/time.h new file mode 100644 index 0000000..75c2307 --- /dev/null +++ b/time.h @@ -0,0 +1,18 @@ +#ifndef __TIME_H__ +#define __TIME_H__ + +#include + +typedef struct timeval timeval_t; + +void newseed (); + +void setendtime (timeval_t *t, int s); + +int isovertime (timeval_t *t); + +void msleep (int msec); + +#endif /* __TIME_H__ */ + +/* vim: set ts=4 sw=4 et: */ -- 2.30.2