annotate src/animate.c @ 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
rev   line source
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 /*! \brief Animation tools.
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 *
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 * XXX: Program is a sequence of actions duration a perior.
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 * Actions are grouped into words. A program defines
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 * the order and time of playing of words. A word
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 * defines how long to perform a set of actions. Actions
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 * in a word are performed concurrently.
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 */
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 #include <stdio.h>
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 #include <stdlib.h>
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 #include <string.h>
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 #include "mb_types.h"
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 #include "redraw_man.h"
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 #include "mb_timer.h"
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 #include "animate.h"
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
18 #define STEP_INTERVAL 100000
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 #define ASSERT(x)
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 /*! \brief A word is a set of concurrent actions in a program.
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 */
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 struct _mb_word {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 mb_timeval_t start_time; /*!< time to start the word */
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 mb_timeval_t playing_time; /*!< time duration of playing */
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 STAILQ(mb_action_t) actions;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 };
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 /*! \brief A program describe a series of actions to animate shapes.
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 */
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 struct _mb_progm {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 redraw_man_t *rdman;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 mb_timeval_t start_time, last_time;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 int first_playing; /*!< first playing word. */
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 mb_tman_t *tman;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 int n_words;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 int max_words;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 mb_word_t words[1];
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 };
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 /*! \brief Basic class of nnimation actions.
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 */
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 struct _mb_action {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 int act_type;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 void (*start)(mb_action_t *act,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 const mb_timeval_t *now,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 const mb_timeval_t *playing_time,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 redraw_man_t *rdman);
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
51 void (*step)(mb_action_t *act, const mb_timeval_t *now,
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
52 redraw_man_t *rdman);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
53 void (*stop)(mb_action_t *act, const mb_timeval_t *now,
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
54 redraw_man_t *rdman);
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 void (*free)(mb_action_t *act);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 mb_action_t *next;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 };
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 mb_progm_t *progm;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 int i;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 progm = (mb_progm_t *)malloc(sizeof(mb_progm_t) +
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 (sizeof(mb_word_t) * (max_words - 1)));
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 if(progm == NULL)
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 return NULL;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 progm->rdman = rdman;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 progm->n_words = 0;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 progm->max_words = max_words;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 for(i = 0; i < max_words; i++)
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 STAILQ_INIT(progm->words[i].actions);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 return progm;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 void mb_progm_free(mb_progm_t *progm) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 int n_words;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 mb_word_t *word;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 mb_action_t *cur_act;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 int i;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 n_words = progm->n_words;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 for(i = 0; i < n_words; i++) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 word = progm->words + i;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 for(cur_act = STAILQ_HEAD(word->actions);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 cur_act != NULL;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 cur_act = STAILQ_HEAD(word->actions)) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 STAILQ_REMOVE(word->actions, mb_action_t, next, cur_act);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 cur_act->free(cur_act);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 free(word);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 free(progm);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 /*! \brief Add a new word into a program.
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 *
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 * The start time of new word should bigger or equal to last one.
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 * The time should be specified in incremental order.
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 */
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 mb_word_t *mb_progm_next_word(mb_progm_t *progm,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 const mb_timeval_t *start,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 const mb_timeval_t *playing) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 mb_word_t *word;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 if(progm->n_words >= progm->max_words)
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 return NULL;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 if(progm->n_words &&
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 MB_TIMEVAL_LATER(&progm->words[progm->n_words - 1].start_time, start))
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109 return NULL;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 word = progm->words + progm->n_words++;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 memcpy(&word->start_time, start, sizeof(mb_timeval_t));
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 memcpy(&word->playing_time, playing, sizeof(mb_timeval_t));
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 return word;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 void mb_word_add_action(mb_word_t *word, mb_action_t *act) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 const mb_timeval_t *now, redraw_man_t *rdman) {
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
122 mb_action_t *act;
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
123
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
124 for(act = STAILQ_HEAD(word->actions);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
125 act != NULL;
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
126 act = STAILQ_NEXT(mb_action_t, next, act)) {
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
127 act->start(act, tmo, &word->playing_time, rdman);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
128 }
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132 const mb_timeval_t *now, redraw_man_t *rdman) {
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
133 mb_action_t *act;
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
134
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
135 for(act = STAILQ_HEAD(word->actions);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
136 act != NULL;
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
137 act = STAILQ_NEXT(mb_action_t, next, act)) {
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
138 act->step(act, tmo, rdman);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
139 }
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
141
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
142 static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
143 const mb_timeval_t *now, redraw_man_t *rdman) {
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
144 mb_action_t *act;
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
145
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
146 for(act = STAILQ_HEAD(word->actions);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
147 act != NULL;
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
148 act = STAILQ_NEXT(mb_action_t, next, act)) {
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
149 act->stop(act, tmo, rdman);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
150 }
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
151 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
152
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
153 static void mb_progm_step(const mb_timeval_t *tmo,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
154 const mb_timeval_t *now,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
155 void *arg) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
156 mb_progm_t *progm = (mb_progm_t *)arg;
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
157 mb_timeval_t next_tmo, w_stp_tm, diff;
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
158 mb_word_t *word;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
159 mb_timer_t *timer;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
160 int i;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
161
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
162 memcpy(&diff, tmo, sizeof(mb_timeval_t));
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
163 MB_TIMEVAL_DIFF(&diff, &progm->start_time);
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
164
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
165 i = progm->first_playing;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
166 for(word = progm->words + i;
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
167 i < progm->n_words && MB_TIMEVAL_LATER(&diff, &word->start_time);
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
168 word = progm->words + ++i) {
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
169 memcpy(&w_stp_tm, &progm->start_time, sizeof(mb_timeval_t));
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
170 MB_TIMEVAL_ADD(&w_stp_tm, &word->start_time);
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
171 MB_TIMEVAL_ADD(&w_stp_tm, &word->playing_time);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
172 if(MB_TIMEVAL_LATER(&w_stp_tm, tmo))
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
173 mb_word_step(word, tmo, now, progm->rdman);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
174 else {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
175 if(MB_TIMEVAL_LATER(&w_stp_tm, &progm->last_time))
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
176 mb_word_stop(word, tmo, now, progm->rdman);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
177 if(i == progm->first_playing)
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
178 progm->first_playing++;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
179 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
180 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
181
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
182 MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
183 MB_TIMEVAL_ADD(&next_tmo, tmo);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
184
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
185 memcpy(&diff, &next_tmo, sizeof(mb_timeval_t));
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
186 MB_TIMEVAL_DIFF(&diff, &progm->start_time);
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
187 for(word = progm->words + i;
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
188 i < progm->n_words && MB_TIMEVAL_LATER(&diff, &word->start_time);
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
189 word = progm->words + ++i) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
190 mb_word_start(word, tmo, now, progm->rdman);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
191 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
192
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
193 /* Setup next timeout. */
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
194 if(progm->first_playing < progm->n_words) {
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
195 word = progm->words + progm->first_playing;
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
196 memcpy(&w_stp_tm, &word->start_time, sizeof(mb_timeval_t));
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
197 MB_TIMEVAL_ADD(&w_stp_tm, &progm->start_time);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
198
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
199 if(MB_TIMEVAL_LATER(&w_stp_tm, &next_tmo))
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
200 timer = mb_tman_timeout(progm->tman, &w_stp_tm,
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
201 mb_progm_step, progm);
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
202 else
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
203 timer = mb_tman_timeout(progm->tman, &next_tmo,
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
204 mb_progm_step, progm);
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
205 ASSERT(timer != NULL);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
206 }
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
207
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
208 memcpy(&progm->last_time, tmo, sizeof(mb_timeval_t));
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
209 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
210
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
211 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
212 mb_timeval_t *now) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
213 mb_timeval_t next_time;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
214 mb_timer_t *timer;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
215
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
216 if(progm->n_words == 0)
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
217 return;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
218
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
219 progm->tman = tman;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
220 memcpy(&progm->start_time, now, sizeof(mb_timeval_t));
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
221 memcpy(&progm->last_time, now, sizeof(mb_timeval_t));
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
222 progm->first_playing = 0;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
223
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
224 memcpy(&next_time, &progm->words[0].start_time, sizeof(mb_timeval_t));
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
225 MB_TIMEVAL_ADD(&next_time, now);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
226 if(!MB_TIMEVAL_LATER(&next_time, now)) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
227 mb_progm_step(&next_time, now, progm);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
228 return;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
229 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
230
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
231 timer = mb_tman_timeout(tman, &next_time, mb_progm_step, progm);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
232 ASSERT(timer != NULL);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
233 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
234
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
235 void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) {
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
236 }
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
237
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
238 typedef struct _mb_shift mb_shift_t;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
239 /*! \brief Animation action for shift a coordination. */
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
240 struct _mb_shift {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
241 mb_action_t action;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
242
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
243 co_aix x, y;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
244 coord_t *coord;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
245
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
246 mb_timeval_t start_time;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
247 co_aix saved_matrix[6];
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
248 const mb_timeval_t *playing_time;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
249 };
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
250
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
251 static float comp_mb_timeval_ratio(mb_timeval_t *a, const mb_timeval_t *b) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
252 float ratio;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
253
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
254 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
255 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
256 return ratio;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
257 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
258
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
259 static void mb_shift_start(mb_action_t *act,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
260 const mb_timeval_t *now,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
261 const mb_timeval_t *playing_time,
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
262 redraw_man_t *rdman) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
263 mb_shift_t *shift = (mb_shift_t *)act;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
264 coord_t *coord;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
265
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
266 memcpy(&shift->start_time, now, sizeof(mb_timeval_t));
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
267 coord = shift->coord;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
268 memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6]));
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
269 shift->playing_time = playing_time;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
270 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
271
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
272 static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now,
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
273 redraw_man_t *rdman) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
274 mb_shift_t *shift = (mb_shift_t *)act;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
275 mb_timeval_t diff;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
276 coord_t *coord;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
277 float ratio;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
278
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
279
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
280 memcpy(&diff, now, sizeof(mb_timeval_t));
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
281 MB_TIMEVAL_DIFF(&diff, &shift->start_time);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
282 ratio = comp_mb_timeval_ratio(&diff, shift->playing_time);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
283
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
284 coord = shift->coord;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
285 coord->matrix[2] = shift->saved_matrix[2] + shift->x * ratio;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
286 coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
287
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
288 rdman_coord_changed(rdman, coord);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
289 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
290
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
291 static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now,
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
292 redraw_man_t *rdman) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
293 mb_shift_t *shift = (mb_shift_t *)act;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
294 coord_t *coord;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
295
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
296 coord = shift->coord;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
297 coord->matrix[2] = shift->saved_matrix[2] + shift->x;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
298 coord->matrix[5] = shift->saved_matrix[5] + shift->y;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
299
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
300 rdman_coord_changed(rdman, coord);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
301 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
302
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
303
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
304 static void mb_shift_free(mb_action_t *act) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
305 free(act);
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
306 }
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
307
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
308 mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord) {
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
309 mb_shift_t *shift;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
310
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
311 shift = (mb_shift_t *)malloc(sizeof(mb_shift_t));
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
312 if(shift == NULL)
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
313 return (mb_action_t *)shift;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
314
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
315 shift->x = x;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
316 shift->y = y;
42
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
317 shift->coord = coord;
e3295c07faa9 mb_shift is work
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
318
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
319 shift->action.start = mb_shift_start;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
320 shift->action.step = mb_shift_step;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
321 shift->action.stop = mb_shift_stop;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
322 shift->action.free = mb_shift_free;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
323
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
324 return (mb_action_t *)shift;
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
325 }