annotate src/animate.c @ 775:caa2fdb5f220

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