Mercurial > MadButterfly
annotate src/animate.c @ 123:9e2316dc6ecb
Program completion events
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 16 Sep 2008 14:19:26 +0800 |
parents | 17e97e92b76e |
children | ad5ab8e61c2b |
rev | line source |
---|---|
111 | 1 /*! \file |
2 * \brief Animation tools. | |
3 * | |
117 | 4 * \sa \ref ani |
111 | 5 */ |
6 /*! \page ani What is Animation? | |
41 | 7 * |
117 | 8 * Animation is a program to move, resize, rotate, ..., changing |
9 * graphics on the output screen. | |
10 * | |
11 * \image html program.png | |
12 * | |
41 | 13 * XXX: Program is a sequence of actions duration a perior. |
14 * Actions are grouped into words. A program defines | |
15 * the order and time of playing of words. A word | |
16 * defines how long to perform a set of actions. Actions | |
17 * in a word are performed concurrently. | |
45 | 18 * |
19 * Animation shapes are updated periodically. Every action | |
20 * are working with start, step, and stop 3 calls. start is | |
21 * called when start time the word, contain it, due. step is | |
22 * called periodically in duration of playing time start at | |
23 * 'start time'. When the clock run out the playing time of | |
24 * a word, it call stop of actions in the word. | |
111 | 25 * |
26 * A program is driven by a timer. Once a program is started, it registers | |
27 * with timer for periodic running. \ref mb_tman_t is timer of | |
28 * MadButterfly. The update frequence is 10fps by default, now. | |
29 * | |
117 | 30 * \section use_progm How to Use Animation Program? |
31 * Following code block creates a program with 2 words. First word is | |
32 * started immediately after the program been started. It is consisted | |
33 * for 1 second. Second word is started 1 second after the program been | |
34 * started. It is consisted for 2 seconds. There are 2 action in | |
35 * first word, they shift graphics managed by coord1 & coord2 by (50,50) and | |
36 * (-50,50) pixels, respectly. The shifting is performed incrementally | |
37 * in 1 second. Second word shifts coord1 and coord2, too. And, graphics | |
38 * managed by coord3 are hidden at end of the word. At end of code in the | |
39 * block, mb_progm_start() starts the program. 3rd argument of | |
40 * mb_progm_start() must be current wall time. | |
41 * | |
42 * \code | |
43 * progm = mb_progm_new(10, &rdman); | |
44 * | |
45 * MB_TIMEVAL_SET(&start, 0, 0); | |
46 * MB_TIMEVAL_SET(&playing, 1, 0); | |
47 * word = mb_progm_next_word(progm, &start, &playing); | |
48 * | |
49 * act = mb_shift_new(50, 50, coord1, word); | |
50 * act = mb_shift_new(-50, 50, coord2, word); | |
51 * | |
52 * MB_TIMEVAL_SET(&start, 1, 0); | |
53 * MB_TIMEVAL_SET(&playing, 2, 0); | |
54 * word = mb_progm_next_word(progm, &start, &playing); | |
55 * | |
56 * act = mb_shift_new(0, 20, coord1, word); | |
57 * act = mb_shift_new(0, -20, coord2, word); | |
58 * act = mb_visibility_new(VIS_HIDDEN, coord3, word); | |
59 * | |
60 * gettimeofday(&tv, NULL); | |
61 * MB_TIMEVAL_SET(&mbtv, tv.tv_sec, tv.tv_usec); | |
62 * mb_progm_start(progm, tman, &mbtv); | |
63 * \endcode | |
64 * | |
65 * | |
66 * \sa \ref animate.c | |
41 | 67 */ |
68 #include <stdio.h> | |
69 #include <stdlib.h> | |
70 #include <string.h> | |
71 #include "mb_types.h" | |
72 #include "redraw_man.h" | |
73 #include "mb_timer.h" | |
74 #include "animate.h" | |
75 | |
76 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
77 #define STEP_INTERVAL 90000 |
41 | 78 #define ASSERT(x) |
79 | |
80 /*! \brief A word is a set of concurrent actions in a program. | |
81 */ | |
82 struct _mb_word { | |
83 mb_timeval_t start_time; /*!< time to start the word */ | |
84 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
|
85 mb_timeval_t abs_start, abs_stop; |
41 | 86 STAILQ(mb_action_t) actions; |
87 }; | |
88 | |
89 /*! \brief A program describe a series of actions to animate shapes. | |
51 | 90 * |
91 * first_playing is an index to one of words. It always points to | |
92 * the first of words that is playing or waiting for playing. | |
41 | 93 */ |
94 struct _mb_progm { | |
95 redraw_man_t *rdman; | |
96 | |
46 | 97 mb_timeval_t start_time; |
41 | 98 int first_playing; /*!< first playing word. */ |
99 mb_tman_t *tman; | |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
100 subject_t *complete; /*!< notify when a program is completed. */ |
41 | 101 |
102 int n_words; | |
103 int max_words; | |
104 mb_word_t words[1]; | |
105 }; | |
106 | |
119 | 107 /*! \brief Create a program object. |
108 * | |
109 * \param max_words is maximum number of words the program can hold. | |
110 * \param rdman is a rdman that show graphics manipulated by it. | |
111 */ | |
41 | 112 mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) { |
113 mb_progm_t *progm; | |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
114 ob_factory_t *factory; |
41 | 115 int i; |
116 | |
117 progm = (mb_progm_t *)malloc(sizeof(mb_progm_t) + | |
118 (sizeof(mb_word_t) * (max_words - 1))); | |
119 if(progm == NULL) | |
120 return NULL; | |
121 | |
122 progm->rdman = rdman; | |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
123 |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
124 factory = rdman_get_ob_factory(rdman); |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
125 progm->complete = subject_new(factory, progm, OBJT_PROGM); |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
126 |
41 | 127 progm->n_words = 0; |
128 progm->max_words = max_words; | |
129 for(i = 0; i < max_words; i++) | |
130 STAILQ_INIT(progm->words[i].actions); | |
131 return progm; | |
132 } | |
133 | |
134 void mb_progm_free(mb_progm_t *progm) { | |
135 int n_words; | |
136 mb_word_t *word; | |
137 mb_action_t *cur_act; | |
138 int i; | |
139 | |
140 n_words = progm->n_words; | |
141 for(i = 0; i < n_words; i++) { | |
142 word = progm->words + i; | |
143 for(cur_act = STAILQ_HEAD(word->actions); | |
144 cur_act != NULL; | |
145 cur_act = STAILQ_HEAD(word->actions)) { | |
146 STAILQ_REMOVE(word->actions, mb_action_t, next, cur_act); | |
147 cur_act->free(cur_act); | |
148 } | |
149 } | |
150 free(progm); | |
151 } | |
152 | |
153 /*! \brief Add a new word into a program. | |
154 * | |
155 * The start time of new word should bigger or equal to last one. | |
156 * The time should be specified in incremental order. | |
157 */ | |
158 mb_word_t *mb_progm_next_word(mb_progm_t *progm, | |
159 const mb_timeval_t *start, | |
160 const mb_timeval_t *playing) { | |
161 mb_word_t *word; | |
162 if(progm->n_words >= progm->max_words) | |
163 return NULL; | |
164 if(progm->n_words && | |
165 MB_TIMEVAL_LATER(&progm->words[progm->n_words - 1].start_time, start)) | |
166 return NULL; | |
167 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
|
168 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
|
169 MB_TIMEVAL_CP(&word->playing_time, playing); |
41 | 170 return word; |
171 } | |
172 | |
116
1d74eb3861b7
move animation actions from animate.c to files.
Thinker K.F. Li <thinker@branda.to>
parents:
111
diff
changeset
|
173 void mb_word_add_action(mb_word_t *word, mb_action_t *act) { |
41 | 174 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act); |
175 } | |
176 | |
177 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo, | |
178 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 179 mb_action_t *act; |
180 | |
181 for(act = STAILQ_HEAD(word->actions); | |
182 act != NULL; | |
183 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
184 act->start(act, tmo, &word->playing_time, rdman); | |
185 } | |
41 | 186 } |
187 | |
188 static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo, | |
189 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 190 mb_action_t *act; |
191 | |
192 for(act = STAILQ_HEAD(word->actions); | |
193 act != NULL; | |
194 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
195 act->step(act, tmo, rdman); | |
196 } | |
41 | 197 } |
198 | |
199 static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo, | |
200 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 201 mb_action_t *act; |
202 | |
203 for(act = STAILQ_HEAD(word->actions); | |
204 act != NULL; | |
205 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
206 act->stop(act, tmo, rdman); | |
207 } | |
41 | 208 } |
209 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
210 /*! \brief Time stepping for a program. |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
211 * |
46 | 212 * Any two actions in concurrent words never change the same attribute. |
213 * Start time of words in a program are specified in incremental order. | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
214 * |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
215 * \note Program will take a big step at last monent. It is because |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
216 * mb_progm_step() running mb_word_stop() if the word being stop |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
217 * between now and next_tmo. It is not obviously if time stepping |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
218 * small. |
46 | 219 */ |
41 | 220 static void mb_progm_step(const mb_timeval_t *tmo, |
120 | 221 const mb_timeval_t *now, |
222 void *arg) { | |
41 | 223 mb_progm_t *progm = (mb_progm_t *)arg; |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
224 ob_factory_t *factory; |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
225 mb_progm_complete_t comp_evt; |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
226 mb_timeval_t next_tmo; |
41 | 227 mb_word_t *word; |
228 mb_timer_t *timer; | |
229 int i; | |
230 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
231 MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL); |
120 | 232 MB_TIMEVAL_ADD(&next_tmo, now); |
41 | 233 |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
234 /* _step() or _stop() words that in playing. */ |
41 | 235 i = progm->first_playing; |
236 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
|
237 i < progm->n_words && MB_TIMEVAL_LATER(tmo, &word->abs_start); |
41 | 238 word = progm->words + ++i) { |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
239 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
|
240 continue; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
241 if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop)) |
46 | 242 mb_word_stop(word, tmo, now, progm->rdman); |
243 else | |
41 | 244 mb_word_step(word, tmo, now, progm->rdman); |
245 } | |
246 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
247 /* 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
|
248 * 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
|
249 */ |
41 | 250 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
|
251 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_start); |
41 | 252 word = progm->words + ++i) { |
253 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
|
254 if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop)) |
46 | 255 mb_word_stop(word, tmo, now, progm->rdman); |
41 | 256 } |
257 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
258 /* 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
|
259 * first_playing word, are stoped. |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
260 */ |
46 | 261 i = progm->first_playing; |
262 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
|
263 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop); |
46 | 264 word = progm->words + ++i) { |
265 progm->first_playing++; | |
266 } | |
267 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
268 /* Setup timeout for next update. */ |
41 | 269 if(progm->first_playing < progm->n_words) { |
42 | 270 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
|
271 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
|
272 MB_TIMEVAL_CP(&next_tmo, &word->abs_start); |
46 | 273 timer = mb_tman_timeout(progm->tman, &next_tmo, |
274 mb_progm_step, progm); | |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
275 } else { |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
276 factory = rdman_get_ob_factory(progm->rdman); |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
277 comp_evt.event.type = EVT_PROGM_COMPLETE; |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
278 comp_evt.event.tgt = comp_evt.event.cur_tgt = progm->complete; |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
279 comp_evt.progm = progm; |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
280 subject_notify(factory, progm->complete, &comp_evt.event); |
41 | 281 } |
282 } | |
283 | |
284 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman, | |
285 mb_timeval_t *now) { | |
286 mb_timer_t *timer; | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
287 mb_word_t *word; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
288 int i; |
41 | 289 |
290 if(progm->n_words == 0) | |
291 return; | |
292 | |
293 progm->tman = tman; | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
294 MB_TIMEVAL_CP(&progm->start_time, now); |
41 | 295 progm->first_playing = 0; |
296 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
297 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
|
298 word = progm->words + i; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
299 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
|
300 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
|
301 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
|
302 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
|
303 } |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
304 |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
305 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
|
306 mb_progm_step(now, now, progm); |
41 | 307 return; |
308 } | |
309 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
310 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
|
311 mb_progm_step, progm); |
41 | 312 ASSERT(timer != NULL); |
313 } | |
314 | |
42 | 315 void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) { |
316 } | |
317 | |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
318 /*! \brief Return event subject for completion of a program. |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
319 */ |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
320 subject_t *mb_progm_get_complete(mb_progm_t *progm) { |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
321 return progm->complete; |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
322 } |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
323 |
50 | 324 #ifdef UNITTEST |
325 | |
326 #include <CUnit/Basic.h> | |
327 | |
328 typedef struct _mb_dummy mb_dummy_t; | |
329 | |
330 struct _mb_dummy { | |
331 mb_action_t action; | |
332 int id; | |
333 int *logidx; | |
334 int *logs; | |
335 }; | |
336 | |
337 | |
338 static void mb_dummy_start(mb_action_t *act, | |
339 const mb_timeval_t *now, | |
340 const mb_timeval_t *playing_time, | |
341 redraw_man_t *rdman) { | |
342 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
343 | |
344 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
345 } | |
346 | |
347 static void mb_dummy_step(mb_action_t *act, | |
348 const mb_timeval_t *now, | |
349 redraw_man_t *rdman) { | |
350 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
351 | |
352 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
353 } | |
354 | |
355 static void mb_dummy_stop(mb_action_t *act, | |
356 const mb_timeval_t *now, | |
357 redraw_man_t *rdman) { | |
358 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
359 | |
360 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
361 } | |
362 | |
363 static void mb_dummy_free(mb_action_t *act) { | |
364 free(act); | |
365 } | |
366 | |
367 mb_action_t *mb_dummy_new(int id, int *logidx, int *logs, mb_word_t *word) { | |
368 mb_dummy_t *dummy; | |
369 | |
370 dummy = (mb_dummy_t *)malloc(sizeof(mb_dummy_t)); | |
371 if(dummy == NULL) | |
372 return NULL; | |
373 | |
374 dummy->id = id; | |
375 dummy->logidx = logidx; | |
376 dummy->logs = logs; | |
377 | |
378 dummy->action.start = mb_dummy_start; | |
379 dummy->action.step = mb_dummy_step; | |
380 dummy->action.stop = mb_dummy_stop; | |
381 dummy->action.free = mb_dummy_free; | |
382 | |
383 mb_word_add_action(word, (mb_action_t *)dummy); | |
384 | |
385 return (mb_action_t *)dummy; | |
386 } | |
387 | |
388 void test_animate_words(void) { | |
389 mb_progm_t *progm; | |
390 mb_word_t *word; | |
391 mb_action_t *acts[4]; | |
392 mb_tman_t *tman; | |
393 mb_timeval_t tv1, tv2, now, tmo_after; | |
394 int logcnt = 0; | |
395 int logs[256]; | |
396 int r; | |
397 | |
398 tman = mb_tman_new(); | |
399 CU_ASSERT(tman != NULL); | |
400 | |
401 progm = mb_progm_new(3, NULL); | |
402 CU_ASSERT(progm != NULL); | |
403 | |
404 MB_TIMEVAL_SET(&tv1, 1, 0); | |
405 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3); | |
406 word = mb_progm_next_word(progm, &tv1, &tv2); | |
407 CU_ASSERT(word != NULL); | |
408 acts[0] = mb_dummy_new(0, &logcnt, logs, word); | |
409 CU_ASSERT(acts[0] != NULL); | |
410 | |
411 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 4 / 3); | |
412 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL / 3); | |
413 word = mb_progm_next_word(progm, &tv1, &tv2); | |
414 CU_ASSERT(word != NULL); | |
415 acts[1] = mb_dummy_new(1, &logcnt, logs, word); | |
416 CU_ASSERT(acts[1] != NULL); | |
417 | |
418 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 2); | |
419 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3); | |
420 word = mb_progm_next_word(progm, &tv1, &tv2); | |
421 CU_ASSERT(word != NULL); | |
422 acts[2] = mb_dummy_new(2, &logcnt, logs, word); | |
423 CU_ASSERT(acts[2] != NULL); | |
424 | |
425 MB_TIMEVAL_SET(&now, 0, 0); | |
426 mb_progm_start(progm, tman, &now); | |
427 | |
428 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
429 CU_ASSERT(r == 0); | |
430 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 1 && | |
431 MB_TIMEVAL_USEC(&tmo_after) == 0); | |
432 | |
433 /* 1.0s */ | |
434 MB_TIMEVAL_ADD(&now, &tmo_after); | |
435 mb_tman_handle_timeout(tman, &now); | |
436 CU_ASSERT(logcnt == 1); | |
437 | |
438 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
439 CU_ASSERT(r == 0); | |
440 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
441 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
442 | |
443 /* 1.1s */ | |
444 MB_TIMEVAL_ADD(&now, &tmo_after); | |
445 mb_tman_handle_timeout(tman, &now); | |
446 CU_ASSERT(logcnt == 4); | |
447 | |
448 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
449 CU_ASSERT(r == 0); | |
450 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
451 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
452 | |
453 /* 1.2s */ | |
454 MB_TIMEVAL_ADD(&now, &tmo_after); | |
455 mb_tman_handle_timeout(tman, &now); | |
456 CU_ASSERT(logcnt == 6); | |
457 | |
458 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
459 CU_ASSERT(r == 0); | |
460 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
461 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
462 | |
463 /* 1.3s */ | |
464 MB_TIMEVAL_ADD(&now, &tmo_after); | |
465 mb_tman_handle_timeout(tman, &now); | |
466 CU_ASSERT(logcnt == 8); | |
467 | |
468 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
469 CU_ASSERT(r == 0); | |
470 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
471 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
472 | |
473 /* 1.4s */ | |
474 MB_TIMEVAL_ADD(&now, &tmo_after); | |
475 mb_tman_handle_timeout(tman, &now); | |
476 CU_ASSERT(logcnt == 9); | |
477 | |
478 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
479 CU_ASSERT(r == 0); | |
480 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
481 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
482 | |
483 /* 1.5s */ | |
484 MB_TIMEVAL_ADD(&now, &tmo_after); | |
485 mb_tman_handle_timeout(tman, &now); | |
486 CU_ASSERT(logcnt == 10); | |
487 | |
488 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
489 CU_ASSERT(r == -1); | |
490 | |
491 mb_progm_free(progm); | |
492 mb_tman_free(tman); | |
493 } | |
494 | |
495 CU_pSuite get_animate_suite(void) { | |
496 CU_pSuite suite; | |
497 | |
498 suite = CU_add_suite("Suite_animate", NULL, NULL); | |
499 if(suite == NULL) | |
500 return NULL; | |
501 | |
502 CU_ADD_TEST(suite, test_animate_words); | |
503 | |
504 return suite; | |
505 } | |
506 | |
507 #endif /* UNITTEST */ |