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