Mercurial > MadButterfly
comparison src/animate.c @ 116:1d74eb3861b7
move animation actions from animate.c to files.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 14 Sep 2008 09:42:07 +0800 |
parents | 8feb89b19619 |
children | e4e47d2cdbcd |
comparison
equal
deleted
inserted
replaced
115:3895d2224e67 | 116:1d74eb3861b7 |
---|---|
58 mb_tman_t *tman; | 58 mb_tman_t *tman; |
59 | 59 |
60 int n_words; | 60 int n_words; |
61 int max_words; | 61 int max_words; |
62 mb_word_t words[1]; | 62 mb_word_t words[1]; |
63 }; | |
64 | |
65 /*! \brief Basic class of nnimation actions. | |
66 * | |
67 * A action must implement following 4 functions. | |
68 * \li start, | |
69 * \li step, | |
70 * \li stop, | |
71 * \li free, and | |
72 * \li *_new(). | |
73 * | |
74 * *_new() must invokes mb_word_add_action() to add new object | |
75 * as one of actions in the word specified as an argument of it. | |
76 * It also means *_new() must have an argument with type of | |
77 * (mb_word_t *). | |
78 */ | |
79 struct _mb_action { | |
80 void (*start)(mb_action_t *act, | |
81 const mb_timeval_t *now, | |
82 const mb_timeval_t *playing_time, | |
83 redraw_man_t *rdman); | |
84 void (*step)(mb_action_t *act, const mb_timeval_t *now, | |
85 redraw_man_t *rdman); | |
86 void (*stop)(mb_action_t *act, const mb_timeval_t *now, | |
87 redraw_man_t *rdman); | |
88 void (*free)(mb_action_t *act); | |
89 mb_action_t *next; | |
90 }; | 63 }; |
91 | 64 |
92 mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) { | 65 mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) { |
93 mb_progm_t *progm; | 66 mb_progm_t *progm; |
94 int i; | 67 int i; |
143 MB_TIMEVAL_CP(&word->start_time, start); | 116 MB_TIMEVAL_CP(&word->start_time, start); |
144 MB_TIMEVAL_CP(&word->playing_time, playing); | 117 MB_TIMEVAL_CP(&word->playing_time, playing); |
145 return word; | 118 return word; |
146 } | 119 } |
147 | 120 |
148 static void mb_word_add_action(mb_word_t *word, mb_action_t *act) { | 121 void mb_word_add_action(mb_word_t *word, mb_action_t *act) { |
149 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act); | 122 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act); |
150 } | 123 } |
151 | 124 |
152 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo, | 125 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo, |
153 const mb_timeval_t *now, redraw_man_t *rdman) { | 126 const mb_timeval_t *now, redraw_man_t *rdman) { |
274 } | 247 } |
275 | 248 |
276 void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) { | 249 void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) { |
277 } | 250 } |
278 | 251 |
279 typedef struct _mb_shift mb_shift_t; | |
280 /*! \brief Animation action for shift a coordination. */ | |
281 struct _mb_shift { | |
282 mb_action_t action; | |
283 | |
284 co_aix x, y; | |
285 coord_t *coord; | |
286 | |
287 mb_timeval_t start_time; | |
288 co_aix saved_matrix[6]; | |
289 const mb_timeval_t *playing_time; | |
290 }; | |
291 | |
292 static float comp_mb_timeval_ratio(const mb_timeval_t *a, | |
293 const mb_timeval_t *b) { | |
294 float ratio; | |
295 | |
296 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a); | |
297 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b); | |
298 return ratio; | |
299 } | |
300 | |
301 static void mb_shift_start(mb_action_t *act, | |
302 const mb_timeval_t *now, | |
303 const mb_timeval_t *playing_time, | |
304 redraw_man_t *rdman) { | |
305 mb_shift_t *shift = (mb_shift_t *)act; | |
306 coord_t *coord; | |
307 | |
308 MB_TIMEVAL_CP(&shift->start_time, now); | |
309 coord = shift->coord; | |
310 memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6])); | |
311 shift->playing_time = playing_time; | |
312 } | |
313 | |
314 static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now, | |
315 redraw_man_t *rdman) { | |
316 mb_shift_t *shift = (mb_shift_t *)act; | |
317 mb_timeval_t diff; | |
318 coord_t *coord; | |
319 float ratio; | |
320 | |
321 | |
322 MB_TIMEVAL_CP(&diff, now); | |
323 MB_TIMEVAL_DIFF(&diff, &shift->start_time); | |
324 ratio = comp_mb_timeval_ratio(&diff, shift->playing_time); | |
325 | |
326 coord = shift->coord; | |
327 coord->matrix[2] = shift->saved_matrix[2] + shift->x * ratio; | |
328 coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio; | |
329 | |
330 rdman_coord_changed(rdman, coord); | |
331 } | |
332 | |
333 static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now, | |
334 redraw_man_t *rdman) { | |
335 mb_shift_t *shift = (mb_shift_t *)act; | |
336 coord_t *coord; | |
337 | |
338 coord = shift->coord; | |
339 coord->matrix[2] = shift->saved_matrix[2] + shift->x; | |
340 coord->matrix[5] = shift->saved_matrix[5] + shift->y; | |
341 | |
342 rdman_coord_changed(rdman, coord); | |
343 } | |
344 | |
345 | |
346 static void mb_shift_free(mb_action_t *act) { | |
347 free(act); | |
348 } | |
349 | |
350 mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord, | |
351 mb_word_t *word) { | |
352 mb_shift_t *shift; | |
353 | |
354 shift = (mb_shift_t *)malloc(sizeof(mb_shift_t)); | |
355 if(shift == NULL) | |
356 return (mb_action_t *)shift; | |
357 | |
358 shift->x = x; | |
359 shift->y = y; | |
360 shift->coord = coord; | |
361 | |
362 shift->action.start = mb_shift_start; | |
363 shift->action.step = mb_shift_step; | |
364 shift->action.stop = mb_shift_stop; | |
365 shift->action.free = mb_shift_free; | |
366 | |
367 mb_word_add_action(word, (mb_action_t *)shift); | |
368 | |
369 return (mb_action_t *)shift; | |
370 } | |
371 | |
372 | |
373 #include "paint.h" | |
374 typedef struct _mb_chgcolor mb_chgcolor_t; | |
375 | |
376 struct _mb_chgcolor { | |
377 mb_action_t action; | |
378 | |
379 co_comp_t r, g, b, a; | |
380 paint_t *paint; | |
381 | |
382 mb_timeval_t start_time; | |
383 const mb_timeval_t *playing_time; | |
384 co_comp_t s_r, s_g, s_b, s_a; /*!< saved RGBA values. */ | |
385 }; | |
386 | |
387 static void mb_chgcolor_start(mb_action_t *act, | |
388 const mb_timeval_t *now, | |
389 const mb_timeval_t *playing_time, | |
390 redraw_man_t *rdman) { | |
391 mb_chgcolor_t *chg = (mb_chgcolor_t *)act; | |
392 | |
393 MB_TIMEVAL_CP(&chg->start_time, now); | |
394 chg->playing_time = playing_time; /* playing_time is in word, | |
395 * it live time is as long as | |
396 * actions. */ | |
397 paint_color_get(chg->paint, | |
398 &chg->s_r, &chg->s_g, | |
399 &chg->s_b, &chg->s_a); | |
400 } | |
401 | |
402 static void mb_chgcolor_step(mb_action_t *act, | |
403 const mb_timeval_t *now, | |
404 redraw_man_t *rdman) { | |
405 mb_chgcolor_t *chg = (mb_chgcolor_t *)act; | |
406 mb_timeval_t diff; | |
407 co_comp_t r, g, b, a; | |
408 float ratio, comp; | |
409 | |
410 MB_TIMEVAL_CP(&diff, now); | |
411 MB_TIMEVAL_DIFF(&diff, &chg->start_time); | |
412 ratio = comp_mb_timeval_ratio(&diff, chg->playing_time); | |
413 comp = 1 - ratio; | |
414 | |
415 r = chg->s_r * comp + ratio * chg->r; | |
416 g = chg->s_g * comp + ratio * chg->g; | |
417 b = chg->s_b * comp + ratio * chg->b; | |
418 a = chg->s_a * comp + ratio * chg->a; | |
419 paint_color_set(chg->paint, r, g, b, a); | |
420 | |
421 rdman_paint_changed(rdman, chg->paint); | |
422 } | |
423 | |
424 static void mb_chgcolor_stop(mb_action_t *act, | |
425 const mb_timeval_t *now, | |
426 redraw_man_t *rdman) { | |
427 mb_chgcolor_t *chg = (mb_chgcolor_t *)act; | |
428 | |
429 paint_color_set(chg->paint, chg->r, chg->g, chg->b, chg->a); | |
430 | |
431 rdman_paint_changed(rdman, chg->paint); | |
432 } | |
433 | |
434 static void mb_chgcolor_free(mb_action_t *act) { | |
435 free(act); | |
436 } | |
437 | |
438 mb_action_t *mb_chgcolor_new(co_comp_t r, co_comp_t g, | |
439 co_comp_t b, co_comp_t a, | |
440 paint_t *paint, mb_word_t *word) { | |
441 mb_chgcolor_t *chg; | |
442 | |
443 chg = (mb_chgcolor_t *)malloc(sizeof(mb_chgcolor_t)); | |
444 if(chg == NULL) | |
445 return NULL; | |
446 | |
447 chg->r = r; | |
448 chg->g = g; | |
449 chg->b = b; | |
450 chg->a = a; | |
451 | |
452 chg->paint = paint; | |
453 | |
454 chg->action.start = mb_chgcolor_start; | |
455 chg->action.step = mb_chgcolor_step; | |
456 chg->action.stop = mb_chgcolor_stop; | |
457 chg->action.free = mb_chgcolor_free; | |
458 | |
459 mb_word_add_action(word, (mb_action_t *)chg); | |
460 | |
461 return (mb_action_t *)chg; | |
462 } | |
463 | |
464 | |
465 typedef struct _mb_visibility mb_visibility_t; | |
466 | |
467 struct _mb_visibility { | |
468 mb_action_t action; | |
469 int visibility; | |
470 coord_t *coord; | |
471 }; | |
472 | |
473 static void mb_visibility_start(mb_action_t *act, | |
474 const mb_timeval_t *now, | |
475 const mb_timeval_t *playing_time, | |
476 redraw_man_t *rdman) { | |
477 mb_visibility_t *visibility = (mb_visibility_t *)act; | |
478 | |
479 switch(visibility->visibility) { | |
480 case VIS_VISIBLE: | |
481 coord_show(visibility->coord); | |
482 break; | |
483 case VIS_HIDDEN: | |
484 coord_hide(visibility->coord); | |
485 break; | |
486 } | |
487 rdman_coord_changed(rdman, visibility->coord); | |
488 } | |
489 | |
490 static void mb_visibility_step(mb_action_t *act, | |
491 const mb_timeval_t *now, | |
492 redraw_man_t *rdman) { | |
493 } | |
494 | |
495 static void mb_visibility_stop(mb_action_t *act, | |
496 const mb_timeval_t *now, | |
497 redraw_man_t *rdman) { | |
498 } | |
499 | |
500 static void mb_visibility_free(mb_action_t *act) { | |
501 free(act); | |
502 } | |
503 | |
504 mb_action_t *mb_visibility_new(int visib, coord_t *coord, | |
505 mb_word_t *word) { | |
506 mb_visibility_t *visibility; | |
507 | |
508 visibility = (mb_visibility_t *)malloc(sizeof(mb_visibility_t)); | |
509 if(visibility == NULL) | |
510 return NULL; | |
511 | |
512 visibility->visibility = visib; | |
513 visibility->coord = coord; | |
514 | |
515 visibility->action.start = mb_visibility_start; | |
516 visibility->action.step = mb_visibility_step; | |
517 visibility->action.stop = mb_visibility_stop; | |
518 visibility->action.free = mb_visibility_free; | |
519 | |
520 mb_word_add_action(word, (mb_action_t *)visibility); | |
521 | |
522 return (mb_action_t *)visibility; | |
523 } | |
524 | |
525 #ifdef UNITTEST | 252 #ifdef UNITTEST |
526 | 253 |
527 #include <CUnit/Basic.h> | 254 #include <CUnit/Basic.h> |
528 | 255 |
529 typedef struct _mb_dummy mb_dummy_t; | 256 typedef struct _mb_dummy mb_dummy_t; |