changeset 120:5df7403b6fbc

Fix bug of get_now()
author Thinker K.F. Li <thinker@branda.to>
date Sun, 14 Sep 2008 23:05:30 +0800
parents 257a1d314bcd
children 76ba6fd61c7d
files examples/tank/tank_main.c src/X_supp.c src/animate.c src/timertool.c
diffstat 4 files changed, 38 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/examples/tank/tank_main.c	Sun Sep 14 21:08:25 2008 +0800
+++ b/examples/tank/tank_main.c	Sun Sep 14 23:05:30 2008 +0800
@@ -1,3 +1,4 @@
+#include <sys/time.h>
 #include <mb/mb.h>
 #include "svgs.h"
 
@@ -39,6 +40,7 @@
     tank_en_t *tank_enemies[10];
     void *map[12][16];
     X_MB_runtime_t *mb_rt;
+    mb_progm_t *tank1_progm;
 };
 
 #define CHANGE_POS(g, x, y) do {			\
@@ -56,6 +58,9 @@
     brick_t *brick;
     rock_t *rock;
     bush_t *bush;
+    mb_word_t *word;
+    mb_timeval_t start, playing;
+    mb_timeval_t mbtv;
     int i, j;
 
     rdman = mb_rt->rdman;
@@ -97,6 +102,25 @@
 	CHANGE_POS(tank_rt->tank_enemies[i], (2 + i * 3) * 50, 0);
     }
     tank_rt->n_enemy = i;
+
+    tank_rt->tank1_progm = mb_progm_new(4, rdman);
+
+    MB_TIMEVAL_SET(&start, 1, 0);
+    MB_TIMEVAL_SET(&playing, 3, 0);
+    word = mb_progm_next_word(tank_rt->tank1_progm, &start, &playing);
+
+    mb_shift_new(0, -150, tank_rt->tank1->root_coord, word);
+    mb_shift_new(0, -150, tank_rt->tank2->root_coord, word);
+
+    MB_TIMEVAL_SET(&start, 5, 0);
+    MB_TIMEVAL_SET(&playing, 3, 0);
+    word = mb_progm_next_word(tank_rt->tank1_progm, &start, &playing);
+
+    mb_shift_new(0, 150, tank_rt->tank1->root_coord, word);
+    mb_shift_new(0, 150, tank_rt->tank2->root_coord, word);
+
+    get_now(&mbtv);
+    mb_progm_start(tank_rt->tank1_progm, mb_rt->tman, &mbtv);
 }
 
 int
--- a/src/X_supp.c	Sun Sep 14 21:08:25 2008 +0800
+++ b/src/X_supp.c	Sun Sep 14 23:05:30 2008 +0800
@@ -196,7 +196,7 @@
     struct timeval tv;
     fd_set rfds;
     int nfds;
-    int r;
+    int r, r1;
 
     rdman_redraw_all(rdman);
     XFlush(display);
@@ -213,16 +213,16 @@
 	if(r == 0) {
 	    tv.tv_sec = MB_TIMEVAL_SEC(&tmo);
 	    tv.tv_usec = MB_TIMEVAL_USEC(&tmo);
-	    r = select(nfds, &rfds, NULL, NULL, &tv);
+	    r1 = select(nfds, &rfds, NULL, NULL, &tv);
 	} else
-	    r = select(nfds, &rfds, NULL, NULL, NULL);
+	    r1 = select(nfds, &rfds, NULL, NULL, NULL);
 
-	if(r == -1) {
+	if(r1 == -1) {
 	    perror("select");
 	    break;
 	}
 
-	if(r == 0) {
+	if(r1 == 0) {
 	    get_now(&now);
 	    mb_tman_handle_timeout(tman, &now);
 	    rdman_redraw_changed(rdman);
--- a/src/animate.c	Sun Sep 14 21:08:25 2008 +0800
+++ b/src/animate.c	Sun Sep 14 23:05:30 2008 +0800
@@ -74,7 +74,7 @@
 #include "animate.h"
 
 
-#define STEP_INTERVAL 100000
+#define STEP_INTERVAL 50000
 #define ASSERT(x)
 
 /*! \brief A word is a set of concurrent actions in a program.
@@ -206,8 +206,8 @@
  * Start time of words in a program are specified in incremental order.
  */
 static void mb_progm_step(const mb_timeval_t *tmo,
-			     const mb_timeval_t *now,
-			     void *arg) {
+			  const mb_timeval_t *now,
+			  void *arg) {
     mb_progm_t *progm = (mb_progm_t *)arg;
     mb_timeval_t next_tmo;
     mb_word_t *word;
@@ -215,7 +215,7 @@
     int i;
 
     MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL);
-    MB_TIMEVAL_ADD(&next_tmo, tmo);
+    MB_TIMEVAL_ADD(&next_tmo, now);
 
     /* _step() or _stop() words that in playing. */
     i = progm->first_playing;
--- a/src/timertool.c	Sun Sep 14 21:08:25 2008 +0800
+++ b/src/timertool.c	Sun Sep 14 23:05:30 2008 +0800
@@ -12,10 +12,11 @@
 void get_now(mb_timeval_t *tmo) {
     struct timeval tv;
     static uint64_t cpufreq;
+    static uint64_t diff;
     static mb_timeval_t tm = {0, 0};
     static uint64_t last_ts;
     mb_timeval_t diff_tm;
-    uint64_t ts, diff, udiff, sdiff;
+    uint64_t ts, udiff, sdiff;
     size_t sysctl_sz;
     int r;
 
@@ -37,7 +38,7 @@
 	diff = 0;
     } else {
 	ts = rdtsc();
-	diff = ts - last_ts;
+	diff += ts - last_ts;
 	sdiff = diff / cpufreq;
 	udiff = (diff % cpufreq) * 1000000 / cpufreq;
 
@@ -48,7 +49,8 @@
 	MB_TIMEVAL_SET(&diff_tm, sdiff, 0);
 	MB_TIMEVAL_ADD(&tm, &diff_tm);
 
-	last_ts += sdiff;
+	diff %= cpufreq;
+	last_ts = ts;
     }
 }
 #else /* __FreeBSD__ */