Mercurial > MadButterfly
annotate src/animate.c @ 124:ad5ab8e61c2b
Free subject of complete when a program is freed
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 16 Sep 2008 14:55:24 +0800 |
parents | 9e2316dc6ecb |
children | 07dc9ec71221 |
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; | |
124
ad5ab8e61c2b
Free subject of complete when a program is freed
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
138 ob_factory_t *factory; |
41 | 139 int i; |
140 | |
141 n_words = progm->n_words; | |
142 for(i = 0; i < n_words; i++) { | |
143 word = progm->words + i; | |
144 for(cur_act = STAILQ_HEAD(word->actions); | |
145 cur_act != NULL; | |
146 cur_act = STAILQ_HEAD(word->actions)) { | |
147 STAILQ_REMOVE(word->actions, mb_action_t, next, cur_act); | |
148 cur_act->free(cur_act); | |
149 } | |
150 } | |
124
ad5ab8e61c2b
Free subject of complete when a program is freed
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
151 |
ad5ab8e61c2b
Free subject of complete when a program is freed
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
152 factory = rdman_get_ob_factory(progm->rdman); |
ad5ab8e61c2b
Free subject of complete when a program is freed
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
153 subject_free(factory, progm->complete); |
ad5ab8e61c2b
Free subject of complete when a program is freed
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
154 |
41 | 155 free(progm); |
156 } | |
157 | |
158 /*! \brief Add a new word into a program. | |
159 * | |
160 * The start time of new word should bigger or equal to last one. | |
161 * The time should be specified in incremental order. | |
162 */ | |
163 mb_word_t *mb_progm_next_word(mb_progm_t *progm, | |
164 const mb_timeval_t *start, | |
165 const mb_timeval_t *playing) { | |
166 mb_word_t *word; | |
167 if(progm->n_words >= progm->max_words) | |
168 return NULL; | |
169 if(progm->n_words && | |
170 MB_TIMEVAL_LATER(&progm->words[progm->n_words - 1].start_time, start)) | |
171 return NULL; | |
172 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
|
173 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
|
174 MB_TIMEVAL_CP(&word->playing_time, playing); |
41 | 175 return word; |
176 } | |
177 | |
116
1d74eb3861b7
move animation actions from animate.c to files.
Thinker K.F. Li <thinker@branda.to>
parents:
111
diff
changeset
|
178 void mb_word_add_action(mb_word_t *word, mb_action_t *act) { |
41 | 179 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act); |
180 } | |
181 | |
182 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo, | |
183 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 184 mb_action_t *act; |
185 | |
186 for(act = STAILQ_HEAD(word->actions); | |
187 act != NULL; | |
188 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
189 act->start(act, tmo, &word->playing_time, rdman); | |
190 } | |
41 | 191 } |
192 | |
193 static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo, | |
194 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 195 mb_action_t *act; |
196 | |
197 for(act = STAILQ_HEAD(word->actions); | |
198 act != NULL; | |
199 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
200 act->step(act, tmo, rdman); | |
201 } | |
41 | 202 } |
203 | |
204 static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo, | |
205 const mb_timeval_t *now, redraw_man_t *rdman) { | |
42 | 206 mb_action_t *act; |
207 | |
208 for(act = STAILQ_HEAD(word->actions); | |
209 act != NULL; | |
210 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
211 act->stop(act, tmo, rdman); | |
212 } | |
41 | 213 } |
214 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
215 /*! \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
|
216 * |
46 | 217 * Any two actions in concurrent words never change the same attribute. |
218 * 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
|
219 * |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
220 * \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
|
221 * 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
|
222 * 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
|
223 * small. |
46 | 224 */ |
41 | 225 static void mb_progm_step(const mb_timeval_t *tmo, |
120 | 226 const mb_timeval_t *now, |
227 void *arg) { | |
41 | 228 mb_progm_t *progm = (mb_progm_t *)arg; |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
229 ob_factory_t *factory; |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
230 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
|
231 mb_timeval_t next_tmo; |
41 | 232 mb_word_t *word; |
233 mb_timer_t *timer; | |
234 int i; | |
235 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
236 MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL); |
120 | 237 MB_TIMEVAL_ADD(&next_tmo, now); |
41 | 238 |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
239 /* _step() or _stop() words that in playing. */ |
41 | 240 i = progm->first_playing; |
241 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
|
242 i < progm->n_words && MB_TIMEVAL_LATER(tmo, &word->abs_start); |
41 | 243 word = progm->words + ++i) { |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
244 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
|
245 continue; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
246 if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop)) |
46 | 247 mb_word_stop(word, tmo, now, progm->rdman); |
248 else | |
41 | 249 mb_word_step(word, tmo, now, progm->rdman); |
250 } | |
251 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
252 /* 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
|
253 * 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
|
254 */ |
41 | 255 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
|
256 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_start); |
41 | 257 word = progm->words + ++i) { |
258 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
|
259 if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop)) |
46 | 260 mb_word_stop(word, tmo, now, progm->rdman); |
41 | 261 } |
262 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
263 /* 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
|
264 * first_playing word, are stoped. |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
265 */ |
46 | 266 i = progm->first_playing; |
267 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
|
268 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop); |
46 | 269 word = progm->words + ++i) { |
270 progm->first_playing++; | |
271 } | |
272 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
273 /* Setup timeout for next update. */ |
41 | 274 if(progm->first_playing < progm->n_words) { |
42 | 275 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
|
276 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
|
277 MB_TIMEVAL_CP(&next_tmo, &word->abs_start); |
46 | 278 timer = mb_tman_timeout(progm->tman, &next_tmo, |
279 mb_progm_step, progm); | |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
280 } else { |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
281 factory = rdman_get_ob_factory(progm->rdman); |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
282 comp_evt.event.type = EVT_PROGM_COMPLETE; |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
283 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
|
284 comp_evt.progm = progm; |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
285 subject_notify(factory, progm->complete, &comp_evt.event); |
41 | 286 } |
287 } | |
288 | |
289 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman, | |
290 mb_timeval_t *now) { | |
291 mb_timer_t *timer; | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
292 mb_word_t *word; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
293 int i; |
41 | 294 |
295 if(progm->n_words == 0) | |
296 return; | |
297 | |
298 progm->tman = tman; | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
42
diff
changeset
|
299 MB_TIMEVAL_CP(&progm->start_time, now); |
41 | 300 progm->first_playing = 0; |
301 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
302 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
|
303 word = progm->words + i; |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
304 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
|
305 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
|
306 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
|
307 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
|
308 } |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
309 |
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
310 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
|
311 mb_progm_step(now, now, progm); |
41 | 312 return; |
313 } | |
314 | |
48
bdf711cbf0fb
Use absolute time to dispatch animation actions.
Thinker K.F. Li <thinker@branda.to>
parents:
47
diff
changeset
|
315 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
|
316 mb_progm_step, progm); |
41 | 317 ASSERT(timer != NULL); |
318 } | |
319 | |
42 | 320 void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) { |
321 } | |
322 | |
123
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
323 /*! \brief Return event subject for completion of a program. |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
324 */ |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
325 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
|
326 return progm->complete; |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
327 } |
9e2316dc6ecb
Program completion events
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
328 |
50 | 329 #ifdef UNITTEST |
330 | |
331 #include <CUnit/Basic.h> | |
332 | |
333 typedef struct _mb_dummy mb_dummy_t; | |
334 | |
335 struct _mb_dummy { | |
336 mb_action_t action; | |
337 int id; | |
338 int *logidx; | |
339 int *logs; | |
340 }; | |
341 | |
342 | |
343 static void mb_dummy_start(mb_action_t *act, | |
344 const mb_timeval_t *now, | |
345 const mb_timeval_t *playing_time, | |
346 redraw_man_t *rdman) { | |
347 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
348 | |
349 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
350 } | |
351 | |
352 static void mb_dummy_step(mb_action_t *act, | |
353 const mb_timeval_t *now, | |
354 redraw_man_t *rdman) { | |
355 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
356 | |
357 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
358 } | |
359 | |
360 static void mb_dummy_stop(mb_action_t *act, | |
361 const mb_timeval_t *now, | |
362 redraw_man_t *rdman) { | |
363 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
364 | |
365 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
366 } | |
367 | |
368 static void mb_dummy_free(mb_action_t *act) { | |
369 free(act); | |
370 } | |
371 | |
372 mb_action_t *mb_dummy_new(int id, int *logidx, int *logs, mb_word_t *word) { | |
373 mb_dummy_t *dummy; | |
374 | |
375 dummy = (mb_dummy_t *)malloc(sizeof(mb_dummy_t)); | |
376 if(dummy == NULL) | |
377 return NULL; | |
378 | |
379 dummy->id = id; | |
380 dummy->logidx = logidx; | |
381 dummy->logs = logs; | |
382 | |
383 dummy->action.start = mb_dummy_start; | |
384 dummy->action.step = mb_dummy_step; | |
385 dummy->action.stop = mb_dummy_stop; | |
386 dummy->action.free = mb_dummy_free; | |
387 | |
388 mb_word_add_action(word, (mb_action_t *)dummy); | |
389 | |
390 return (mb_action_t *)dummy; | |
391 } | |
392 | |
393 void test_animate_words(void) { | |
394 mb_progm_t *progm; | |
395 mb_word_t *word; | |
396 mb_action_t *acts[4]; | |
397 mb_tman_t *tman; | |
398 mb_timeval_t tv1, tv2, now, tmo_after; | |
399 int logcnt = 0; | |
400 int logs[256]; | |
401 int r; | |
402 | |
403 tman = mb_tman_new(); | |
404 CU_ASSERT(tman != NULL); | |
405 | |
406 progm = mb_progm_new(3, NULL); | |
407 CU_ASSERT(progm != NULL); | |
408 | |
409 MB_TIMEVAL_SET(&tv1, 1, 0); | |
410 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3); | |
411 word = mb_progm_next_word(progm, &tv1, &tv2); | |
412 CU_ASSERT(word != NULL); | |
413 acts[0] = mb_dummy_new(0, &logcnt, logs, word); | |
414 CU_ASSERT(acts[0] != NULL); | |
415 | |
416 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 4 / 3); | |
417 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL / 3); | |
418 word = mb_progm_next_word(progm, &tv1, &tv2); | |
419 CU_ASSERT(word != NULL); | |
420 acts[1] = mb_dummy_new(1, &logcnt, logs, word); | |
421 CU_ASSERT(acts[1] != NULL); | |
422 | |
423 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 2); | |
424 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3); | |
425 word = mb_progm_next_word(progm, &tv1, &tv2); | |
426 CU_ASSERT(word != NULL); | |
427 acts[2] = mb_dummy_new(2, &logcnt, logs, word); | |
428 CU_ASSERT(acts[2] != NULL); | |
429 | |
430 MB_TIMEVAL_SET(&now, 0, 0); | |
431 mb_progm_start(progm, tman, &now); | |
432 | |
433 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
434 CU_ASSERT(r == 0); | |
435 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 1 && | |
436 MB_TIMEVAL_USEC(&tmo_after) == 0); | |
437 | |
438 /* 1.0s */ | |
439 MB_TIMEVAL_ADD(&now, &tmo_after); | |
440 mb_tman_handle_timeout(tman, &now); | |
441 CU_ASSERT(logcnt == 1); | |
442 | |
443 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
444 CU_ASSERT(r == 0); | |
445 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
446 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
447 | |
448 /* 1.1s */ | |
449 MB_TIMEVAL_ADD(&now, &tmo_after); | |
450 mb_tman_handle_timeout(tman, &now); | |
451 CU_ASSERT(logcnt == 4); | |
452 | |
453 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
454 CU_ASSERT(r == 0); | |
455 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
456 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
457 | |
458 /* 1.2s */ | |
459 MB_TIMEVAL_ADD(&now, &tmo_after); | |
460 mb_tman_handle_timeout(tman, &now); | |
461 CU_ASSERT(logcnt == 6); | |
462 | |
463 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
464 CU_ASSERT(r == 0); | |
465 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
466 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
467 | |
468 /* 1.3s */ | |
469 MB_TIMEVAL_ADD(&now, &tmo_after); | |
470 mb_tman_handle_timeout(tman, &now); | |
471 CU_ASSERT(logcnt == 8); | |
472 | |
473 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
474 CU_ASSERT(r == 0); | |
475 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
476 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
477 | |
478 /* 1.4s */ | |
479 MB_TIMEVAL_ADD(&now, &tmo_after); | |
480 mb_tman_handle_timeout(tman, &now); | |
481 CU_ASSERT(logcnt == 9); | |
482 | |
483 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
484 CU_ASSERT(r == 0); | |
485 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
486 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
487 | |
488 /* 1.5s */ | |
489 MB_TIMEVAL_ADD(&now, &tmo_after); | |
490 mb_tman_handle_timeout(tman, &now); | |
491 CU_ASSERT(logcnt == 10); | |
492 | |
493 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
494 CU_ASSERT(r == -1); | |
495 | |
496 mb_progm_free(progm); | |
497 mb_tman_free(tman); | |
498 } | |
499 | |
500 CU_pSuite get_animate_suite(void) { | |
501 CU_pSuite suite; | |
502 | |
503 suite = CU_add_suite("Suite_animate", NULL, NULL); | |
504 if(suite == NULL) | |
505 return NULL; | |
506 | |
507 CU_ADD_TEST(suite, test_animate_words); | |
508 | |
509 return suite; | |
510 } | |
511 | |
512 #endif /* UNITTEST */ |