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