Mercurial > MadButterfly
annotate src/animate.c @ 99:4aa1c9673363
Arc in path pass the test in example svg2code_ex.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 09 Sep 2008 22:17:17 +0800 |
parents | ab028c9f0930 |
children | 8feb89b19619 |
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 |
52
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
361 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
362 #include "paint.h" |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
363 typedef struct _mb_chgcolor mb_chgcolor_t; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
364 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
365 struct _mb_chgcolor { |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
366 mb_action_t action; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
367 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
368 co_comp_t r, g, b, a; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
369 paint_t *paint; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
370 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
371 mb_timeval_t start_time; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
372 const mb_timeval_t *playing_time; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
373 co_comp_t s_r, s_g, s_b, s_a; /*!< saved RGBA values. */ |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
374 }; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
375 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
376 static void mb_chgcolor_start(mb_action_t *act, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
377 const mb_timeval_t *now, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
378 const mb_timeval_t *playing_time, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
379 redraw_man_t *rdman) { |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
380 mb_chgcolor_t *chg = (mb_chgcolor_t *)act; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
381 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
382 MB_TIMEVAL_CP(&chg->start_time, now); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
383 chg->playing_time = playing_time; /* playing_time is in word, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
384 * it live time is as long as |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
385 * actions. */ |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
386 paint_color_get(chg->paint, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
387 &chg->s_r, &chg->s_g, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
388 &chg->s_b, &chg->s_a); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
389 } |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
390 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
391 static void mb_chgcolor_step(mb_action_t *act, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
392 const mb_timeval_t *now, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
393 redraw_man_t *rdman) { |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
394 mb_chgcolor_t *chg = (mb_chgcolor_t *)act; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
395 mb_timeval_t diff; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
396 co_comp_t r, g, b, a; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
397 float ratio, comp; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
398 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
399 MB_TIMEVAL_CP(&diff, now); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
400 MB_TIMEVAL_DIFF(&diff, &chg->start_time); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
401 ratio = comp_mb_timeval_ratio(&diff, chg->playing_time); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
402 comp = 1 - ratio; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
403 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
404 r = chg->s_r * comp + ratio * chg->r; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
405 g = chg->s_g * comp + ratio * chg->g; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
406 b = chg->s_b * comp + ratio * chg->b; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
407 a = chg->s_a * comp + ratio * chg->a; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
408 paint_color_set(chg->paint, r, g, b, a); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
409 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
410 rdman_paint_changed(rdman, chg->paint); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
411 } |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
412 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
413 static void mb_chgcolor_stop(mb_action_t *act, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
414 const mb_timeval_t *now, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
415 redraw_man_t *rdman) { |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
416 mb_chgcolor_t *chg = (mb_chgcolor_t *)act; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
417 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
418 paint_color_set(chg->paint, chg->r, chg->g, chg->b, chg->a); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
419 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
420 rdman_paint_changed(rdman, chg->paint); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
421 } |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
422 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
423 static void mb_chgcolor_free(mb_action_t *act) { |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
424 free(act); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
425 } |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
426 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
427 mb_action_t *mb_chgcolor_new(co_comp_t r, co_comp_t g, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
428 co_comp_t b, co_comp_t a, |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
429 paint_t *paint, mb_word_t *word) { |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
430 mb_chgcolor_t *chg; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
431 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
432 chg = (mb_chgcolor_t *)malloc(sizeof(mb_chgcolor_t)); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
433 if(chg == NULL) |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
434 return NULL; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
435 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
436 chg->r = r; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
437 chg->g = g; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
438 chg->b = b; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
439 chg->a = a; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
440 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
441 chg->paint = paint; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
442 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
443 chg->action.start = mb_chgcolor_start; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
444 chg->action.step = mb_chgcolor_step; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
445 chg->action.stop = mb_chgcolor_stop; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
446 chg->action.free = mb_chgcolor_free; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
447 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
448 mb_word_add_action(word, (mb_action_t *)chg); |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
449 |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
450 return (mb_action_t *)chg; |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
451 } |
59a295651480
Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents:
51
diff
changeset
|
452 |
57
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
453 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
454 typedef struct _mb_visibility mb_visibility_t; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
455 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
456 struct _mb_visibility { |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
457 mb_action_t action; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
458 int visibility; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
459 coord_t *coord; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
460 }; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
461 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
462 static void mb_visibility_start(mb_action_t *act, |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
463 const mb_timeval_t *now, |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
464 const mb_timeval_t *playing_time, |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
465 redraw_man_t *rdman) { |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
466 mb_visibility_t *visibility = (mb_visibility_t *)act; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
467 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
468 switch(visibility->visibility) { |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
469 case VIS_VISIBLE: |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
470 coord_show(visibility->coord); |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
471 break; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
472 case VIS_HIDDEN: |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
473 coord_hide(visibility->coord); |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
474 break; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
475 } |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
476 rdman_coord_changed(rdman, visibility->coord); |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
477 } |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
478 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
479 static void mb_visibility_step(mb_action_t *act, |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
480 const mb_timeval_t *now, |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
481 redraw_man_t *rdman) { |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
482 } |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
483 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
484 static void mb_visibility_stop(mb_action_t *act, |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
485 const mb_timeval_t *now, |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
486 redraw_man_t *rdman) { |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
487 } |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
488 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
489 static void mb_visibility_free(mb_action_t *act) { |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
490 free(act); |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
491 } |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
492 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
493 mb_action_t *mb_visibility_new(int visib, coord_t *coord, |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
494 mb_word_t *word) { |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
495 mb_visibility_t *visibility; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
496 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
497 visibility = (mb_visibility_t *)malloc(sizeof(mb_visibility_t)); |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
498 if(visibility == NULL) |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
499 return NULL; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
500 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
501 visibility->visibility = visib; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
502 visibility->coord = coord; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
503 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
504 visibility->action.start = mb_visibility_start; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
505 visibility->action.step = mb_visibility_step; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
506 visibility->action.stop = mb_visibility_stop; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
507 visibility->action.free = mb_visibility_free; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
508 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
509 mb_word_add_action(word, (mb_action_t *)visibility); |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
510 |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
511 return (mb_action_t *)visibility; |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
512 } |
ab028c9f0930
Ability to hidden shapes and action of visibility.
Thinker K.F. Li <thinker@branda.to>
parents:
52
diff
changeset
|
513 |
50 | 514 #ifdef UNITTEST |
515 | |
516 #include <CUnit/Basic.h> | |
517 | |
518 typedef struct _mb_dummy mb_dummy_t; | |
519 | |
520 struct _mb_dummy { | |
521 mb_action_t action; | |
522 int id; | |
523 int *logidx; | |
524 int *logs; | |
525 }; | |
526 | |
527 | |
528 static void mb_dummy_start(mb_action_t *act, | |
529 const mb_timeval_t *now, | |
530 const mb_timeval_t *playing_time, | |
531 redraw_man_t *rdman) { | |
532 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
533 | |
534 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
535 } | |
536 | |
537 static void mb_dummy_step(mb_action_t *act, | |
538 const mb_timeval_t *now, | |
539 redraw_man_t *rdman) { | |
540 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
541 | |
542 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
543 } | |
544 | |
545 static void mb_dummy_stop(mb_action_t *act, | |
546 const mb_timeval_t *now, | |
547 redraw_man_t *rdman) { | |
548 mb_dummy_t *dummy = (mb_dummy_t *)act; | |
549 | |
550 dummy->logs[(*dummy->logidx)++] = dummy->id; | |
551 } | |
552 | |
553 static void mb_dummy_free(mb_action_t *act) { | |
554 free(act); | |
555 } | |
556 | |
557 mb_action_t *mb_dummy_new(int id, int *logidx, int *logs, mb_word_t *word) { | |
558 mb_dummy_t *dummy; | |
559 | |
560 dummy = (mb_dummy_t *)malloc(sizeof(mb_dummy_t)); | |
561 if(dummy == NULL) | |
562 return NULL; | |
563 | |
564 dummy->id = id; | |
565 dummy->logidx = logidx; | |
566 dummy->logs = logs; | |
567 | |
568 dummy->action.start = mb_dummy_start; | |
569 dummy->action.step = mb_dummy_step; | |
570 dummy->action.stop = mb_dummy_stop; | |
571 dummy->action.free = mb_dummy_free; | |
572 | |
573 mb_word_add_action(word, (mb_action_t *)dummy); | |
574 | |
575 return (mb_action_t *)dummy; | |
576 } | |
577 | |
578 void test_animate_words(void) { | |
579 mb_progm_t *progm; | |
580 mb_word_t *word; | |
581 mb_action_t *acts[4]; | |
582 mb_tman_t *tman; | |
583 mb_timeval_t tv1, tv2, now, tmo_after; | |
584 int logcnt = 0; | |
585 int logs[256]; | |
586 int r; | |
587 | |
588 tman = mb_tman_new(); | |
589 CU_ASSERT(tman != NULL); | |
590 | |
591 progm = mb_progm_new(3, NULL); | |
592 CU_ASSERT(progm != NULL); | |
593 | |
594 MB_TIMEVAL_SET(&tv1, 1, 0); | |
595 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3); | |
596 word = mb_progm_next_word(progm, &tv1, &tv2); | |
597 CU_ASSERT(word != NULL); | |
598 acts[0] = mb_dummy_new(0, &logcnt, logs, word); | |
599 CU_ASSERT(acts[0] != NULL); | |
600 | |
601 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 4 / 3); | |
602 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL / 3); | |
603 word = mb_progm_next_word(progm, &tv1, &tv2); | |
604 CU_ASSERT(word != NULL); | |
605 acts[1] = mb_dummy_new(1, &logcnt, logs, word); | |
606 CU_ASSERT(acts[1] != NULL); | |
607 | |
608 MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 2); | |
609 MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3); | |
610 word = mb_progm_next_word(progm, &tv1, &tv2); | |
611 CU_ASSERT(word != NULL); | |
612 acts[2] = mb_dummy_new(2, &logcnt, logs, word); | |
613 CU_ASSERT(acts[2] != NULL); | |
614 | |
615 MB_TIMEVAL_SET(&now, 0, 0); | |
616 mb_progm_start(progm, tman, &now); | |
617 | |
618 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
619 CU_ASSERT(r == 0); | |
620 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 1 && | |
621 MB_TIMEVAL_USEC(&tmo_after) == 0); | |
622 | |
623 /* 1.0s */ | |
624 MB_TIMEVAL_ADD(&now, &tmo_after); | |
625 mb_tman_handle_timeout(tman, &now); | |
626 CU_ASSERT(logcnt == 1); | |
627 | |
628 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
629 CU_ASSERT(r == 0); | |
630 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
631 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
632 | |
633 /* 1.1s */ | |
634 MB_TIMEVAL_ADD(&now, &tmo_after); | |
635 mb_tman_handle_timeout(tman, &now); | |
636 CU_ASSERT(logcnt == 4); | |
637 | |
638 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
639 CU_ASSERT(r == 0); | |
640 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
641 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
642 | |
643 /* 1.2s */ | |
644 MB_TIMEVAL_ADD(&now, &tmo_after); | |
645 mb_tman_handle_timeout(tman, &now); | |
646 CU_ASSERT(logcnt == 6); | |
647 | |
648 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
649 CU_ASSERT(r == 0); | |
650 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
651 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
652 | |
653 /* 1.3s */ | |
654 MB_TIMEVAL_ADD(&now, &tmo_after); | |
655 mb_tman_handle_timeout(tman, &now); | |
656 CU_ASSERT(logcnt == 8); | |
657 | |
658 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
659 CU_ASSERT(r == 0); | |
660 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
661 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
662 | |
663 /* 1.4s */ | |
664 MB_TIMEVAL_ADD(&now, &tmo_after); | |
665 mb_tman_handle_timeout(tman, &now); | |
666 CU_ASSERT(logcnt == 9); | |
667 | |
668 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
669 CU_ASSERT(r == 0); | |
670 CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 && | |
671 MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL); | |
672 | |
673 /* 1.5s */ | |
674 MB_TIMEVAL_ADD(&now, &tmo_after); | |
675 mb_tman_handle_timeout(tman, &now); | |
676 CU_ASSERT(logcnt == 10); | |
677 | |
678 r = mb_tman_next_timeout(tman, &now, &tmo_after); | |
679 CU_ASSERT(r == -1); | |
680 | |
681 mb_progm_free(progm); | |
682 mb_tman_free(tman); | |
683 } | |
684 | |
685 CU_pSuite get_animate_suite(void) { | |
686 CU_pSuite suite; | |
687 | |
688 suite = CU_add_suite("Suite_animate", NULL, NULL); | |
689 if(suite == NULL) | |
690 return NULL; | |
691 | |
692 CU_ADD_TEST(suite, test_animate_words); | |
693 | |
694 return suite; | |
695 } | |
696 | |
697 #endif /* UNITTEST */ |