separated time functions from main file
authorLaurent Mazet <mazet@softndesign.org>
Wed, 31 Jul 2024 11:31:36 +0000 (13:31 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Wed, 31 Jul 2024 11:31:36 +0000 (13:31 +0200)
tetris.c
time.c [new file with mode: 0644]
time.h [new file with mode: 0644]

index 8b4b63dc998cc9229a385e551817a22f3fb8f10c..7465c8afcbe185967d447538aecda10cdd75b6b7 100644 (file)
--- 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 <curses.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
-#include <sys/time.h>
-#include <unistd.h>
 
 #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 (file)
index 0000000..4381e48
--- /dev/null
+++ b/time.c
@@ -0,0 +1,34 @@
+#include <stdlib.h>
+#include <sys/time.h>
+#include <time.h>
+#include <unistd.h>
+
+#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 (file)
index 0000000..75c2307
--- /dev/null
+++ b/time.h
@@ -0,0 +1,18 @@
+#ifndef __TIME_H__
+#define __TIME_H__
+
+#include <sys/time.h>
+
+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: */