changeset 42:e3295c07faa9

mb_shift is work
author Thinker K.F. Li <thinker@branda.to>
date Sat, 09 Aug 2008 08:06:45 +0800
parents 400b4b5db0dc
children 6270230b9248
files src/X_main.c src/animate.c src/animate.h
diffstat 3 files changed, 92 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/X_main.c	Fri Aug 08 21:34:53 2008 +0800
+++ b/src/X_main.c	Sat Aug 09 08:06:45 2008 +0800
@@ -12,6 +12,7 @@
 #include "redraw_man.h"
 #include "paint.h"
 #include "mb_timer.h"
+#include "animate.h"
 
 #define OK 0
 #define ERR -1
@@ -127,6 +128,7 @@
 	    }
 	    MB_TIMEVAL_SET(&mb_tmo, tmo.tv_sec, tmo.tv_usec);
 	    mb_tman_handle_timeout(tman, &mb_tmo);
+	    rdman_redraw_changed(rdman);
 	    XFlush(display);
 	} else if(FD_ISSET(xcon, &rds)) {
 	    event_interaction(display, rdman, w, h);
@@ -148,7 +150,10 @@
     struct test_motion_data mdata;
     struct timeval tv;
     mb_tman_t *tman;
-    mb_timeval_t mbtv;
+    mb_timeval_t mbtv, start, playing;
+    mb_progm_t *progm;
+    mb_word_t *word;
+    mb_action_t *act;
     int i;
 
     tmpsuf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
@@ -296,6 +301,24 @@
 
     tman = mb_tman_new();
     if(tman) {
+	progm = mb_progm_new(2, &rdman);
+	
+	MB_TIMEVAL_SET(&start, 1, 0);
+	MB_TIMEVAL_SET(&playing, 2, 0);
+	word = mb_progm_next_word(progm, &start, &playing);
+	act = mb_shift_new(0, 20, coord1);
+	mb_word_add_action(word, act);
+	
+	MB_TIMEVAL_SET(&start, 3, 0);
+	MB_TIMEVAL_SET(&playing, 2, 0);
+	word = mb_progm_next_word(progm, &start, &playing);
+	act = mb_shift_new(0, 20, coord2);
+	mb_word_add_action(word, act);
+	
+	gettimeofday(&tv, NULL);
+	MB_TIMEVAL_SET(&mbtv, tv.tv_sec, tv.tv_usec);
+	mb_progm_start(progm, tman, &mbtv);
+
 	mdata.text_stroke = text_stroke;
 	mdata.rdman = &rdman;
 	gettimeofday(&tv, NULL);
--- a/src/animate.c	Fri Aug 08 21:34:53 2008 +0800
+++ b/src/animate.c	Sat Aug 09 08:06:45 2008 +0800
@@ -15,7 +15,7 @@
 #include "animate.h"
 
 
-#define STEP_INTERVAL 500000
+#define STEP_INTERVAL 100000
 #define ASSERT(x)
 
 /*! \brief A word is a set of concurrent actions in a program.
@@ -48,8 +48,10 @@
 		  const mb_timeval_t *now,
 		  const mb_timeval_t *playing_time,
 		  redraw_man_t *rdman);
-    void (*step)(mb_action_t *act, mb_timeval_t *now, redraw_man_t *rdman);
-    void (*stop)(mb_action_t *act, mb_timeval_t *now, redraw_man_t *rdman);
+    void (*step)(mb_action_t *act, const mb_timeval_t *now,
+		 redraw_man_t *rdman);
+    void (*stop)(mb_action_t *act, const mb_timeval_t *now,
+		 redraw_man_t *rdman);
     void (*free)(mb_action_t *act);
     mb_action_t *next;
 };
@@ -117,33 +119,55 @@
 
 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo,
 			  const mb_timeval_t *now, redraw_man_t *rdman) {
+    mb_action_t *act;
+
+    for(act = STAILQ_HEAD(word->actions);
+	act != NULL;
+	act = STAILQ_NEXT(mb_action_t, next, act)) {
+	act->start(act, tmo, &word->playing_time, rdman);
+    }
 }
 
 static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo,
 			 const mb_timeval_t *now, redraw_man_t *rdman) {
+    mb_action_t *act;
+
+    for(act = STAILQ_HEAD(word->actions);
+	act != NULL;
+	act = STAILQ_NEXT(mb_action_t, next, act)) {
+	act->step(act, tmo, rdman);
+    }
 }
 
 static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo,
 			 const mb_timeval_t *now, redraw_man_t *rdman) {
+    mb_action_t *act;
+
+    for(act = STAILQ_HEAD(word->actions);
+	act != NULL;
+	act = STAILQ_NEXT(mb_action_t, next, act)) {
+	act->stop(act, tmo, rdman);
+    }
 }
 
 static void mb_progm_step(const mb_timeval_t *tmo,
 			  const mb_timeval_t *now,
 			  void *arg) {
     mb_progm_t *progm = (mb_progm_t *)arg;
-    mb_timeval_t next_tmo, w_stp_tm;
+    mb_timeval_t next_tmo, w_stp_tm, diff;
     mb_word_t *word;
     mb_timer_t *timer;
     int i;
 
-    MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL);
-    MB_TIMEVAL_ADD(&next_tmo, tmo);
+    memcpy(&diff, tmo, sizeof(mb_timeval_t));
+    MB_TIMEVAL_DIFF(&diff, &progm->start_time);
 
     i = progm->first_playing;
     for(word = progm->words + i;
-	i < progm->n_words && MB_TIMEVAL_LATER(tmo, &word->start_time);
+	i < progm->n_words && MB_TIMEVAL_LATER(&diff, &word->start_time);
 	word = progm->words + ++i) {
-	memcpy(&w_stp_tm, &word->start_time, sizeof(mb_timeval_t));
+	memcpy(&w_stp_tm, &progm->start_time, sizeof(mb_timeval_t));
+	MB_TIMEVAL_ADD(&w_stp_tm, &word->start_time);
 	MB_TIMEVAL_ADD(&w_stp_tm, &word->playing_time);
 	if(MB_TIMEVAL_LATER(&w_stp_tm, tmo))
 	    mb_word_step(word, tmo, now, progm->rdman);
@@ -155,17 +179,33 @@
 	}
     }
 
+    MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL);
+    MB_TIMEVAL_ADD(&next_tmo, tmo);
+
+    memcpy(&diff, &next_tmo, sizeof(mb_timeval_t));
+    MB_TIMEVAL_DIFF(&diff, &progm->start_time);
     for(word = progm->words + i;
-	i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->start_time);
+	i < progm->n_words && MB_TIMEVAL_LATER(&diff, &word->start_time);
 	word = progm->words + ++i) {
 	mb_word_start(word, tmo, now, progm->rdman);
     }
 
+    /* Setup next timeout. */
     if(progm->first_playing < progm->n_words) {
-	timer = mb_tman_timeout(progm->tman, &next_tmo,
-				mb_progm_step, progm);
+	word = progm->words + progm->first_playing;
+	memcpy(&w_stp_tm, &word->start_time, sizeof(mb_timeval_t));
+	MB_TIMEVAL_ADD(&w_stp_tm, &progm->start_time);
+
+	if(MB_TIMEVAL_LATER(&w_stp_tm, &next_tmo))
+	    timer = mb_tman_timeout(progm->tman, &w_stp_tm,
+				    mb_progm_step, progm);
+	else
+	    timer = mb_tman_timeout(progm->tman, &next_tmo,
+				    mb_progm_step, progm);
 	ASSERT(timer != NULL);
     }
+
+    memcpy(&progm->last_time, tmo, sizeof(mb_timeval_t));
 }
 
 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman,
@@ -192,6 +232,9 @@
     ASSERT(timer != NULL);
 }
 
+void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) {
+}
+
 typedef struct _mb_shift mb_shift_t;
 /*! \brief Animation action for shift a coordination. */
 struct _mb_shift {
@@ -220,13 +263,13 @@
     mb_shift_t *shift = (mb_shift_t *)act;
     coord_t *coord;
 
-    memcpy(&shift->start_time, now, sizeof(now));
+    memcpy(&shift->start_time, now, sizeof(mb_timeval_t));
     coord = shift->coord;
     memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6]));
     shift->playing_time = playing_time;
 }
 
-static void mb_shift_step(mb_action_t *act, mb_timeval_t *now,
+static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now,
 			  redraw_man_t *rdman) {
     mb_shift_t *shift = (mb_shift_t *)act;
     mb_timeval_t diff;
@@ -245,7 +288,7 @@
     rdman_coord_changed(rdman, coord);
 }
 
-static void mb_shift_stop(mb_action_t *act, mb_timeval_t *now,
+static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now,
 			  redraw_man_t *rdman) {
     mb_shift_t *shift = (mb_shift_t *)act;
     coord_t *coord;
@@ -262,7 +305,7 @@
     free(act);
 }
 
-mb_action_t *mb_shift_new(co_aix x, co_aix y) {
+mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord) {
     mb_shift_t *shift;
 
     shift = (mb_shift_t *)malloc(sizeof(mb_shift_t));
@@ -271,6 +314,8 @@
 
     shift->x = x;
     shift->y = y;
+    shift->coord = coord;
+
     shift->action.start = mb_shift_start;
     shift->action.step = mb_shift_step;
     shift->action.stop = mb_shift_stop;
--- a/src/animate.h	Fri Aug 08 21:34:53 2008 +0800
+++ b/src/animate.h	Sat Aug 09 08:06:45 2008 +0800
@@ -1,6 +1,9 @@
 #ifndef __ANIMATE_H_
 #define __ANIMATE_H_
 
+#include "mb_types.h"
+#include "mb_timer.h"
+
 typedef struct _mb_progm mb_progm_t;
 typedef struct _mb_word mb_word_t;
 typedef struct _mb_action mb_action_t;
@@ -12,6 +15,10 @@
 				     const mb_timeval_t *start,
 				     const mb_timeval_t *playing);
 extern void mb_word_add_action(mb_word_t *word, mb_action_t *act);
-extern mb_action_t *mb_shift_new(co_aix x, co_aix y);
+extern void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman,
+			   mb_timeval_t *now);
+extern void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman);
+extern mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord);
+
 
 #endif /* __ANIMATE_H_ */