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