Mercurial > MadButterfly
annotate src/animate.c @ 45:83ee36f19210
-
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 09 Aug 2008 11:21:52 +0800 |
parents | 6270230b9248 |
children | 46b77c92d118 |
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 | |
41 mb_timeval_t start_time, last_time; | |
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 | |
123 void mb_word_add_action(mb_word_t *word, mb_action_t *act) { | |
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 | |
160 static void mb_progm_step(const mb_timeval_t *tmo, | |
161 const mb_timeval_t *now, | |
162 void *arg) { | |
163 mb_progm_t *progm = (mb_progm_t *)arg; | |
45 | 164 mb_timeval_t next_tmo, w_stp_tm; |
165 mb_timeval_t tmo_diff, next_diff; | |
41 | 166 mb_word_t *word; |
167 mb_timer_t *timer; | |
168 int i; | |
169 | |
45 | 170 MB_TIMEVAL_CP(&tmo_diff, tmo); |
171 MB_TIMEVAL_DIFF(&tmo_diff, &progm->start_time); | |
41 | 172 |
173 i = progm->first_playing; | |
174 for(word = progm->words + i; | |
45 | 175 i < progm->n_words && MB_TIMEVAL_LATER(&tmo_diff, &word->start_time); |
41 | 176 word = progm->words + ++i) { |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
177 MB_TIMEVAL_CP(&w_stp_tm, &progm->start_time); |
42 | 178 MB_TIMEVAL_ADD(&w_stp_tm, &word->start_time); |
41 | 179 MB_TIMEVAL_ADD(&w_stp_tm, &word->playing_time); |
180 if(MB_TIMEVAL_LATER(&w_stp_tm, tmo)) | |
181 mb_word_step(word, tmo, now, progm->rdman); | |
182 else { | |
183 if(MB_TIMEVAL_LATER(&w_stp_tm, &progm->last_time)) | |
184 mb_word_stop(word, tmo, now, progm->rdman); | |
185 if(i == progm->first_playing) | |
186 progm->first_playing++; | |
187 } | |
188 } | |
189 | |
42 | 190 MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL); |
191 MB_TIMEVAL_ADD(&next_tmo, tmo); | |
192 | |
45 | 193 MB_TIMEVAL_CP(&next_diff, &next_tmo); |
194 MB_TIMEVAL_DIFF(&next_diff, &progm->start_time); | |
41 | 195 for(word = progm->words + i; |
45 | 196 i < progm->n_words && MB_TIMEVAL_LATER(&next_diff, &word->start_time); |
41 | 197 word = progm->words + ++i) { |
198 mb_word_start(word, tmo, now, progm->rdman); | |
199 } | |
200 | |
42 | 201 /* Setup next timeout. */ |
41 | 202 if(progm->first_playing < progm->n_words) { |
42 | 203 word = progm->words + progm->first_playing; |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
204 MB_TIMEVAL_CP(&w_stp_tm, &word->start_time); |
42 | 205 MB_TIMEVAL_ADD(&w_stp_tm, &progm->start_time); |
206 | |
207 if(MB_TIMEVAL_LATER(&w_stp_tm, &next_tmo)) | |
208 timer = mb_tman_timeout(progm->tman, &w_stp_tm, | |
209 mb_progm_step, progm); | |
210 else | |
211 timer = mb_tman_timeout(progm->tman, &next_tmo, | |
212 mb_progm_step, progm); | |
41 | 213 ASSERT(timer != NULL); |
214 } | |
42 | 215 |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
216 MB_TIMEVAL_CP(&progm->last_time, tmo); |
41 | 217 } |
218 | |
219 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman, | |
220 mb_timeval_t *now) { | |
221 mb_timeval_t next_time; | |
222 mb_timer_t *timer; | |
223 | |
224 if(progm->n_words == 0) | |
225 return; | |
226 | |
227 progm->tman = tman; | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
228 MB_TIMEVAL_CP(&progm->start_time, now); |
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
229 MB_TIMEVAL_CP(&progm->last_time, now); |
41 | 230 progm->first_playing = 0; |
231 | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
232 MB_TIMEVAL_CP(&next_time, &progm->words[0].start_time); |
41 | 233 MB_TIMEVAL_ADD(&next_time, now); |
234 if(!MB_TIMEVAL_LATER(&next_time, now)) { | |
235 mb_progm_step(&next_time, now, progm); | |
236 return; | |
237 } | |
238 | |
239 timer = mb_tman_timeout(tman, &next_time, mb_progm_step, progm); | |
240 ASSERT(timer != NULL); | |
241 } | |
242 | |
42 | 243 void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) { |
244 } | |
245 | |
41 | 246 typedef struct _mb_shift mb_shift_t; |
247 /*! \brief Animation action for shift a coordination. */ | |
248 struct _mb_shift { | |
249 mb_action_t action; | |
250 | |
251 co_aix x, y; | |
252 coord_t *coord; | |
253 | |
254 mb_timeval_t start_time; | |
255 co_aix saved_matrix[6]; | |
256 const mb_timeval_t *playing_time; | |
257 }; | |
258 | |
259 static float comp_mb_timeval_ratio(mb_timeval_t *a, const mb_timeval_t *b) { | |
260 float ratio; | |
261 | |
262 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a); | |
263 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b); | |
264 return ratio; | |
265 } | |
266 | |
267 static void mb_shift_start(mb_action_t *act, | |
268 const mb_timeval_t *now, | |
269 const mb_timeval_t *playing_time, | |
270 redraw_man_t *rdman) { | |
271 mb_shift_t *shift = (mb_shift_t *)act; | |
272 coord_t *coord; | |
273 | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
274 MB_TIMEVAL_CP(&shift->start_time, now); |
41 | 275 coord = shift->coord; |
276 memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6])); | |
277 shift->playing_time = playing_time; | |
278 } | |
279 | |
42 | 280 static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now, |
41 | 281 redraw_man_t *rdman) { |
282 mb_shift_t *shift = (mb_shift_t *)act; | |
283 mb_timeval_t diff; | |
284 coord_t *coord; | |
285 float ratio; | |
286 | |
287 | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
288 MB_TIMEVAL_CP(&diff, now); |
41 | 289 MB_TIMEVAL_DIFF(&diff, &shift->start_time); |
290 ratio = comp_mb_timeval_ratio(&diff, shift->playing_time); | |
291 | |
292 coord = shift->coord; | |
293 coord->matrix[2] = shift->saved_matrix[2] + shift->x * ratio; | |
294 coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio; | |
295 | |
296 rdman_coord_changed(rdman, coord); | |
297 } | |
298 | |
42 | 299 static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now, |
41 | 300 redraw_man_t *rdman) { |
301 mb_shift_t *shift = (mb_shift_t *)act; | |
302 coord_t *coord; | |
303 | |
304 coord = shift->coord; | |
305 coord->matrix[2] = shift->saved_matrix[2] + shift->x; | |
306 coord->matrix[5] = shift->saved_matrix[5] + shift->y; | |
307 | |
308 rdman_coord_changed(rdman, coord); | |
309 } | |
310 | |
311 | |
312 static void mb_shift_free(mb_action_t *act) { | |
313 free(act); | |
314 } | |
315 | |
42 | 316 mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord) { |
41 | 317 mb_shift_t *shift; |
318 | |
319 shift = (mb_shift_t *)malloc(sizeof(mb_shift_t)); | |
320 if(shift == NULL) | |
321 return (mb_action_t *)shift; | |
322 | |
323 shift->x = x; | |
324 shift->y = y; | |
42 | 325 shift->coord = coord; |
326 | |
41 | 327 shift->action.start = mb_shift_start; |
328 shift->action.step = mb_shift_step; | |
329 shift->action.stop = mb_shift_stop; | |
330 shift->action.free = mb_shift_free; | |
331 | |
332 return (mb_action_t *)shift; | |
333 } |