Mercurial > MadButterfly
annotate src/animate.c @ 51:5f5bd3ac9316
documentation
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 09 Aug 2008 21:34:07 +0800 |
parents | c986e45c1e91 |
children | 59a295651480 |
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. | |
51 | 38 * |
39 * first_playing is an index to one of words. It always points to | |
40 * the first of words that is playing or waiting for playing. | |
41 | 41 */ |
42 struct _mb_progm { | |
43 redraw_man_t *rdman; | |
44 | |
46 | 45 mb_timeval_t start_time; |
41 | 46 int first_playing; /*!< first playing word. */ |
47 mb_tman_t *tman; | |
48 | |
49 int n_words; | |
50 int max_words; | |
51 mb_word_t words[1]; | |
52 }; | |
53 | |
54 /*! \brief Basic class of nnimation actions. | |
51 | 55 * |
56 * A action must implement following 4 functions. | |
57 * \li start, | |
58 * \li step, | |
59 * \li stop, | |
60 * \li free, and | |
61 * \li *_new(). | |
62 * | |
63 * *_new() must invokes mb_word_add_action() to add new object | |
64 * as one of actions in the word specified as an argument of it. | |
65 * It also means *_new() must have an argument with type of | |
66 * (mb_word_t *). | |
41 | 67 */ |
68 struct _mb_action { | |
69 void (*start)(mb_action_t *act, | |
70 const mb_timeval_t *now, | |
71 const mb_timeval_t *playing_time, | |
72 redraw_man_t *rdman); | |
42 | 73 void (*step)(mb_action_t *act, const mb_timeval_t *now, |
74 redraw_man_t *rdman); | |
75 void (*stop)(mb_action_t *act, const mb_timeval_t *now, | |
76 redraw_man_t *rdman); | |
41 | 77 void (*free)(mb_action_t *act); |
78 mb_action_t *next; | |
79 }; | |
80 | |
81 mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) { | |
82 mb_progm_t *progm; | |
83 int i; | |
84 | |
85 progm = (mb_progm_t *)malloc(sizeof(mb_progm_t) + | |
86 (sizeof(mb_word_t) * (max_words - 1))); | |
87 if(progm == NULL) | |
88 return NULL; | |
89 | |
90 progm->rdman = rdman; | |
91 progm->n_words = 0; | |
92 progm->max_words = max_words; | |
93 for(i = 0; i < max_words; i++) | |
94 STAILQ_INIT(progm->words[i].actions); | |
95 return progm; | |
96 } | |
97 | |
98 void mb_progm_free(mb_progm_t *progm) { | |
99 int n_words; | |
100 mb_word_t *word; | |
101 mb_action_t *cur_act; | |
102 int i; | |
103 | |
104 n_words = progm->n_words; | |
105 for(i = 0; i < n_words; i++) { | |
106 word = progm->words + i; | |
107 for(cur_act = STAILQ_HEAD(word->actions); | |
108 cur_act != NULL; | |
109 cur_act = STAILQ_HEAD(word->actions)) { | |
110 STAILQ_REMOVE(word->actions, mb_action_t, next, cur_act); | |
111 cur_act->free(cur_act); | |
112 } | |
113 } | |
114 free(progm); | |
115 } | |
116 | |
117 /*! \brief Add a new word into a program. | |
118 * | |
119 * The start time of new word should bigger or equal to last one. | |
120 * The time should be specified in incremental order. | |
121 */ | |
122 mb_word_t *mb_progm_next_word(mb_progm_t *progm, | |
123 const mb_timeval_t *start, | |
124 const mb_timeval_t *playing) { | |
125 mb_word_t *word; | |
126 if(progm->n_words >= progm->max_words) | |
127 return NULL; | |
128 if(progm->n_words && | |
129 MB_TIMEVAL_LATER(&progm->words[progm->n_words - 1].start_time, start)) | |
130 return NULL; | |
131 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
|
132 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
|
133 MB_TIMEVAL_CP(&word->playing_time, playing); |
41 | 134 return word; |
135 } | |
136 | |
47
f3818d996f4f
change interface of creating a animation action
Thinker K.F. Li <thinker@branda.to>
parents:
46
diff
changeset
|
137 static void mb_word_add_action(mb_word_t *word, mb_action_t *act) { |
41 | 138 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act); |
139 } | |
140 | |
141 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo, | |
142 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 143 mb_action_t *act; |
144 | |
145 for(act = STAILQ_HEAD(word->actions); | |
146 act != NULL; | |
147 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
148 act->start(act, tmo, &word->playing_time, rdman); | |
149 } | |
41 | 150 } |
151 | |
152 static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo, | |
153 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 154 mb_action_t *act; |
155 | |
156 for(act = STAILQ_HEAD(word->actions); | |
157 act != NULL; | |
158 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
159 act->step(act, tmo, rdman); | |
160 } | |
41 | 161 } |
162 | |
163 static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo, | |
164 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 165 mb_action_t *act; |
166 | |
167 for(act = STAILQ_HEAD(word->actions); | |
168 act != NULL; | |
169 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
170 act->stop(act, tmo, rdman); | |
171 } | |
41 | 172 } |
173 | |
46 | 174 /* |
175 * Any two actions in concurrent words never change the same attribute. | |
176 * Start time of words in a program are specified in incremental order. | |
177 */ | |
41 | 178 static void mb_progm_step(const mb_timeval_t *tmo, |
46 | 179 const mb_timeval_t *now, |
180 void *arg) { | |
41 | 181 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
|
182 mb_timeval_t next_tmo; |
41 | 183 mb_word_t *word; |
184 mb_timer_t *timer; | |
185 int i; | |
186 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
187 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
|
188 MB_TIMEVAL_ADD(&next_tmo, tmo); |
41 | 189 |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
190 /* _step() or _stop() words that in playing. */ |
41 | 191 i = progm->first_playing; |
192 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
|
193 i < progm->n_words && MB_TIMEVAL_LATER(tmo, &word->abs_start); |
41 | 194 word = progm->words + ++i) { |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
195 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
|
196 continue; |
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); |
199 else | |
41 | 200 mb_word_step(word, tmo, now, progm->rdman); |
201 } | |
202 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
203 /* 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
|
204 * 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
|
205 */ |
41 | 206 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
|
207 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_start); |
41 | 208 word = progm->words + ++i) { |
209 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
|
210 if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop)) |
46 | 211 mb_word_stop(word, tmo, now, progm->rdman); |
41 | 212 } |
213 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
214 /* 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
|
215 * first_playing word, are stoped. |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
216 */ |
46 | 217 i = progm->first_playing; |
218 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
|
219 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop); |
46 | 220 word = progm->words + ++i) { |
221 progm->first_playing++; | |
222 } | |
223 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
224 /* Setup timeout for next update. */ |
41 | 225 if(progm->first_playing < progm->n_words) { |
42 | 226 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
|
227 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
|
228 MB_TIMEVAL_CP(&next_tmo, &word->abs_start); |
46 | 229 timer = mb_tman_timeout(progm->tman, &next_tmo, |
230 mb_progm_step, progm); | |
41 | 231 } |
232 } | |
233 | |
234 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman, | |
235 mb_timeval_t *now) { | |
236 mb_timer_t *timer; | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
237 mb_word_t *word; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
238 int i; |
41 | 239 |
240 if(progm->n_words == 0) | |
241 return; | |
242 | |
243 progm->tman = tman; | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
244 MB_TIMEVAL_CP(&progm->start_time, now); |
41 | 245 progm->first_playing = 0; |
246 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
247 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
|
248 word = progm->words + i; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 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
|
253 } |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
254 |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
255 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
|
256 mb_progm_step(now, now, progm); |
41 | 257 return; |
258 } | |
259 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
260 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
|
261 mb_progm_step, progm); |
41 | 262 ASSERT(timer != NULL); |
263 } | |
264 | |
42 | 265 void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) { |
266 } | |
267 | |
41 | 268 typedef struct _mb_shift mb_shift_t; |
269 /*! \brief Animation action for shift a coordination. */ | |
270 struct _mb_shift { | |
271 mb_action_t action; | |
272 | |
273 co_aix x, y; | |
274 coord_t *coord; | |
275 | |
276 mb_timeval_t start_time; | |
277 co_aix saved_matrix[6]; | |
278 const mb_timeval_t *playing_time; | |
279 }; | |
280 | |
51 | 281 static float comp_mb_timeval_ratio(const mb_timeval_t *a, |
282 const mb_timeval_t *b) { | |
41 | 283 float ratio; |
284 | |
285 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a); | |
286 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b); | |
287 return ratio; | |
288 } | |
289 | |
290 static void mb_shift_start(mb_action_t *act, | |
291 const mb_timeval_t *now, | |
292 const mb_timeval_t *playing_time, | |
293 redraw_man_t *rdman) { | |
294 mb_shift_t *shift = (mb_shift_t *)act; | |
295 coord_t *coord; | |
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(&shift->start_time, now); |
41 | 298 coord = shift->coord; |
299 memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6])); | |
300 shift->playing_time = playing_time; | |
301 } | |
302 | |
42 | 303 static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now, |
41 | 304 redraw_man_t *rdman) { |
305 mb_shift_t *shift = (mb_shift_t *)act; | |
306 mb_timeval_t diff; | |
307 coord_t *coord; | |
308 float ratio; | |
309 | |
310 | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
311 MB_TIMEVAL_CP(&diff, now); |
41 | 312 MB_TIMEVAL_DIFF(&diff, &shift->start_time); |
313 ratio = comp_mb_timeval_ratio(&diff, shift->playing_time); | |
314 | |
315 coord = shift->coord; | |
316 coord->matrix[2] = shift->saved_matrix[2] + shift->x * ratio; | |
317 coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio; | |
318 | |
319 rdman_coord_changed(rdman, coord); | |
320 } | |
321 | |
42 | 322 static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now, |
41 | 323 redraw_man_t *rdman) { |
324 mb_shift_t *shift = (mb_shift_t *)act; | |
325 coord_t *coord; | |
326 | |
327 coord = shift->coord; | |
328 coord->matrix[2] = shift->saved_matrix[2] + shift->x; | |
329 coord->matrix[5] = shift->saved_matrix[5] + shift->y; | |
330 | |
331 rdman_coord_changed(rdman, coord); | |
332 } | |
333 | |
334 | |
335 static void mb_shift_free(mb_action_t *act) { | |
336 free(act); | |
337 } | |
338 | |
47
f3818d996f4f
change interface of creating a animation action
Thinker K.F. Li <thinker@branda.to>
parents:
46
diff
changeset
|
339 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
|
340 mb_word_t *word) { |
41 | 341 mb_shift_t *shift; |
342 | |
343 shift = (mb_shift_t *)malloc(sizeof(mb_shift_t)); | |
344 if(shift == NULL) | |
345 return (mb_action_t *)shift; | |
346 | |
347 shift->x = x; | |
348 shift->y = y; | |
42 | 349 shift->coord = coord; |
350 | |
41 | 351 shift->action.start = mb_shift_start; |
352 shift->action.step = mb_shift_step; | |
353 shift->action.stop = mb_shift_stop; | |
354 shift->action.free = mb_shift_free; | |
355 | |
47
f3818d996f4f
change interface of creating a animation action
Thinker K.F. Li <thinker@branda.to>
parents:
46
diff
changeset
|
356 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
|
357 |
41 | 358 return (mb_action_t *)shift; |
359 } | |
50 | 360 |
361 #ifdef UNITTEST | |
362 | |
363 #include <CUnit/Basic.h> | |
364 | |
365 typedef struct _mb_dummy mb_dummy_t; | |
366 | |
367 struct _mb_dummy { | |
368 mb_action_t action; | |
369 int id; | |
370 int *logidx; | |
371 int *logs; | |
372 }; | |
373 | |
374 | |
375 static void mb_dummy_start(mb_action_t *act, | |
376 const mb_timeval_t *now, | |
377 const mb_timeval_t *playing_time, | |
378 redraw_man_t *rdman) { | |
379 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
380 | |
381 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
382 } | |
383 | |
384 static void mb_dummy_step(mb_action_t *act, | |
385 const mb_timeval_t *now, | |
386 redraw_man_t *rdman) { | |
387 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
388 | |
389 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
390 } | |
391 | |
392 static void mb_dummy_stop(mb_action_t *act, | |
393 const mb_timeval_t *now, | |
394 redraw_man_t *rdman) { | |
395 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
396 | |
397 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
398 } | |
399 | |
400 static void mb_dummy_free(mb_action_t *act) { | |
401 free(act); | |
402 } | |
403 | |
404 mb_action_t *mb_dummy_new(int id, int *logidx, int *logs, mb_word_t *word) { | |
405 mb_dummy_t *dummy; | |
406 | |
407 dummy = (mb_dummy_t *)malloc(sizeof(mb_dummy_t)); | |
408 if(dummy == NULL) | |
409 return NULL; | |
410 | |
411 dummy->id = id; | |
412 dummy->logidx = logidx; | |
413 dummy->logs = logs; | |
414 | |
415 dummy->action.start = mb_dummy_start; | |
416 dummy->action.step = mb_dummy_step; | |
417 dummy->action.stop = mb_dummy_stop; | |
418 dummy->action.free = mb_dummy_free; | |
419 | |
420 mb_word_add_action(word, (mb_action_t *)dummy); | |
421 | |
422 return (mb_action_t *)dummy; | |
423 } | |
424 | |
425 void test_animate_words(void) { | |
426 mb_progm_t *progm; | |
427 mb_word_t *word; | |
428 mb_action_t *acts[4]; | |
429 mb_tman_t *tman; | |
430 mb_timeval_t tv1, tv2, now, tmo_after; | |
431 int logcnt = 0; | |
432 int logs[256]; | |
433 int r; | |
434 | |
435 tman = mb_tman_new(); | |
436 CU_ASSERT(tman != NULL); | |
437 | |
438 progm = mb_progm_new(3, NULL); | |
439 CU_ASSERT(progm != NULL); | |
440 | |
441 MB_TIMEVAL_SET(&tv1, 1, 0); | |
442 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3); | |
443 word = mb_progm_next_word(progm, &tv1, &tv2); | |
444 CU_ASSERT(word != NULL); | |
445 acts[0] = mb_dummy_new(0, &logcnt, logs, word); | |
446 CU_ASSERT(acts[0] != NULL); | |
447 | |
448 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 4 / 3); | |
449 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL / 3); | |
450 word = mb_progm_next_word(progm, &tv1, &tv2); | |
451 CU_ASSERT(word != NULL); | |
452 acts[1] = mb_dummy_new(1, &logcnt, logs, word); | |
453 CU_ASSERT(acts[1] != NULL); | |
454 | |
455 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 2); | |
456 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3); | |
457 word = mb_progm_next_word(progm, &tv1, &tv2); | |
458 CU_ASSERT(word != NULL); | |
459 acts[2] = mb_dummy_new(2, &logcnt, logs, word); | |
460 CU_ASSERT(acts[2] != NULL); | |
461 | |
462 MB_TIMEVAL_SET(&now, 0, 0); | |
463 mb_progm_start(progm, tman, &now); | |
464 | |
465 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
466 CU_ASSERT(r == 0); | |
467 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 1 && | |
468 MB_TIMEVAL_USEC(&tmo_after) == 0); | |
469 | |
470 /* 1.0s */ | |
471 MB_TIMEVAL_ADD(&now, &tmo_after); | |
472 mb_tman_handle_timeout(tman, &now); | |
473 CU_ASSERT(logcnt == 1); | |
474 | |
475 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
476 CU_ASSERT(r == 0); | |
477 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
478 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
479 | |
480 /* 1.1s */ | |
481 MB_TIMEVAL_ADD(&now, &tmo_after); | |
482 mb_tman_handle_timeout(tman, &now); | |
483 CU_ASSERT(logcnt == 4); | |
484 | |
485 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
486 CU_ASSERT(r == 0); | |
487 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
488 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
489 | |
490 /* 1.2s */ | |
491 MB_TIMEVAL_ADD(&now, &tmo_after); | |
492 mb_tman_handle_timeout(tman, &now); | |
493 CU_ASSERT(logcnt == 6); | |
494 | |
495 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
496 CU_ASSERT(r == 0); | |
497 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
498 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
499 | |
500 /* 1.3s */ | |
501 MB_TIMEVAL_ADD(&now, &tmo_after); | |
502 mb_tman_handle_timeout(tman, &now); | |
503 CU_ASSERT(logcnt == 8); | |
504 | |
505 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
506 CU_ASSERT(r == 0); | |
507 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
508 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
509 | |
510 /* 1.4s */ | |
511 MB_TIMEVAL_ADD(&now, &tmo_after); | |
512 mb_tman_handle_timeout(tman, &now); | |
513 CU_ASSERT(logcnt == 9); | |
514 | |
515 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
516 CU_ASSERT(r == 0); | |
517 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
518 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
519 | |
520 /* 1.5s */ | |
521 MB_TIMEVAL_ADD(&now, &tmo_after); | |
522 mb_tman_handle_timeout(tman, &now); | |
523 CU_ASSERT(logcnt == 10); | |
524 | |
525 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
526 CU_ASSERT(r == -1); | |
527 | |
528 mb_progm_free(progm); | |
529 mb_tman_free(tman); | |
530 } | |
531 | |
532 CU_pSuite get_animate_suite(void) { | |
533 CU_pSuite suite; | |
534 | |
535 suite = CU_add_suite("Suite_animate", NULL, NULL); | |
536 if(suite == NULL) | |
537 return NULL; | |
538 | |
539 CU_ADD_TEST(suite, test_animate_words); | |
540 | |
541 return suite; | |
542 } | |
543 | |
544 #endif /* UNITTEST */ |