annotate src/animate.c @ 50:c986e45c1e91

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