Mercurial > MadButterfly
comparison src/animate.c @ 42:e3295c07faa9
mb_shift is work
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 09 Aug 2008 08:06:45 +0800 |
parents | 400b4b5db0dc |
children | 6270230b9248 |
comparison
equal
deleted
inserted
replaced
41:400b4b5db0dc | 42:e3295c07faa9 |
---|---|
13 #include "redraw_man.h" | 13 #include "redraw_man.h" |
14 #include "mb_timer.h" | 14 #include "mb_timer.h" |
15 #include "animate.h" | 15 #include "animate.h" |
16 | 16 |
17 | 17 |
18 #define STEP_INTERVAL 500000 | 18 #define STEP_INTERVAL 100000 |
19 #define ASSERT(x) | 19 #define ASSERT(x) |
20 | 20 |
21 /*! \brief A word is a set of concurrent actions in a program. | 21 /*! \brief A word is a set of concurrent actions in a program. |
22 */ | 22 */ |
23 struct _mb_word { | 23 struct _mb_word { |
46 int act_type; | 46 int act_type; |
47 void (*start)(mb_action_t *act, | 47 void (*start)(mb_action_t *act, |
48 const mb_timeval_t *now, | 48 const mb_timeval_t *now, |
49 const mb_timeval_t *playing_time, | 49 const mb_timeval_t *playing_time, |
50 redraw_man_t *rdman); | 50 redraw_man_t *rdman); |
51 void (*step)(mb_action_t *act, mb_timeval_t *now, redraw_man_t *rdman); | 51 void (*step)(mb_action_t *act, const mb_timeval_t *now, |
52 void (*stop)(mb_action_t *act, mb_timeval_t *now, redraw_man_t *rdman); | 52 redraw_man_t *rdman); |
53 void (*stop)(mb_action_t *act, const mb_timeval_t *now, | |
54 redraw_man_t *rdman); | |
53 void (*free)(mb_action_t *act); | 55 void (*free)(mb_action_t *act); |
54 mb_action_t *next; | 56 mb_action_t *next; |
55 }; | 57 }; |
56 | 58 |
57 mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) { | 59 mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) { |
115 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act); | 117 STAILQ_INS_TAIL(word->actions, mb_action_t, next, act); |
116 } | 118 } |
117 | 119 |
118 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo, | 120 static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo, |
119 const mb_timeval_t *now, redraw_man_t *rdman) { | 121 const mb_timeval_t *now, redraw_man_t *rdman) { |
122 mb_action_t *act; | |
123 | |
124 for(act = STAILQ_HEAD(word->actions); | |
125 act != NULL; | |
126 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
127 act->start(act, tmo, &word->playing_time, rdman); | |
128 } | |
120 } | 129 } |
121 | 130 |
122 static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo, | 131 static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo, |
123 const mb_timeval_t *now, redraw_man_t *rdman) { | 132 const mb_timeval_t *now, redraw_man_t *rdman) { |
133 mb_action_t *act; | |
134 | |
135 for(act = STAILQ_HEAD(word->actions); | |
136 act != NULL; | |
137 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
138 act->step(act, tmo, rdman); | |
139 } | |
124 } | 140 } |
125 | 141 |
126 static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo, | 142 static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo, |
127 const mb_timeval_t *now, redraw_man_t *rdman) { | 143 const mb_timeval_t *now, redraw_man_t *rdman) { |
144 mb_action_t *act; | |
145 | |
146 for(act = STAILQ_HEAD(word->actions); | |
147 act != NULL; | |
148 act = STAILQ_NEXT(mb_action_t, next, act)) { | |
149 act->stop(act, tmo, rdman); | |
150 } | |
128 } | 151 } |
129 | 152 |
130 static void mb_progm_step(const mb_timeval_t *tmo, | 153 static void mb_progm_step(const mb_timeval_t *tmo, |
131 const mb_timeval_t *now, | 154 const mb_timeval_t *now, |
132 void *arg) { | 155 void *arg) { |
133 mb_progm_t *progm = (mb_progm_t *)arg; | 156 mb_progm_t *progm = (mb_progm_t *)arg; |
134 mb_timeval_t next_tmo, w_stp_tm; | 157 mb_timeval_t next_tmo, w_stp_tm, diff; |
135 mb_word_t *word; | 158 mb_word_t *word; |
136 mb_timer_t *timer; | 159 mb_timer_t *timer; |
137 int i; | 160 int i; |
138 | 161 |
139 MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL); | 162 memcpy(&diff, tmo, sizeof(mb_timeval_t)); |
140 MB_TIMEVAL_ADD(&next_tmo, tmo); | 163 MB_TIMEVAL_DIFF(&diff, &progm->start_time); |
141 | 164 |
142 i = progm->first_playing; | 165 i = progm->first_playing; |
143 for(word = progm->words + i; | 166 for(word = progm->words + i; |
144 i < progm->n_words && MB_TIMEVAL_LATER(tmo, &word->start_time); | 167 i < progm->n_words && MB_TIMEVAL_LATER(&diff, &word->start_time); |
145 word = progm->words + ++i) { | 168 word = progm->words + ++i) { |
146 memcpy(&w_stp_tm, &word->start_time, sizeof(mb_timeval_t)); | 169 memcpy(&w_stp_tm, &progm->start_time, sizeof(mb_timeval_t)); |
170 MB_TIMEVAL_ADD(&w_stp_tm, &word->start_time); | |
147 MB_TIMEVAL_ADD(&w_stp_tm, &word->playing_time); | 171 MB_TIMEVAL_ADD(&w_stp_tm, &word->playing_time); |
148 if(MB_TIMEVAL_LATER(&w_stp_tm, tmo)) | 172 if(MB_TIMEVAL_LATER(&w_stp_tm, tmo)) |
149 mb_word_step(word, tmo, now, progm->rdman); | 173 mb_word_step(word, tmo, now, progm->rdman); |
150 else { | 174 else { |
151 if(MB_TIMEVAL_LATER(&w_stp_tm, &progm->last_time)) | 175 if(MB_TIMEVAL_LATER(&w_stp_tm, &progm->last_time)) |
153 if(i == progm->first_playing) | 177 if(i == progm->first_playing) |
154 progm->first_playing++; | 178 progm->first_playing++; |
155 } | 179 } |
156 } | 180 } |
157 | 181 |
182 MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL); | |
183 MB_TIMEVAL_ADD(&next_tmo, tmo); | |
184 | |
185 memcpy(&diff, &next_tmo, sizeof(mb_timeval_t)); | |
186 MB_TIMEVAL_DIFF(&diff, &progm->start_time); | |
158 for(word = progm->words + i; | 187 for(word = progm->words + i; |
159 i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->start_time); | 188 i < progm->n_words && MB_TIMEVAL_LATER(&diff, &word->start_time); |
160 word = progm->words + ++i) { | 189 word = progm->words + ++i) { |
161 mb_word_start(word, tmo, now, progm->rdman); | 190 mb_word_start(word, tmo, now, progm->rdman); |
162 } | 191 } |
163 | 192 |
193 /* Setup next timeout. */ | |
164 if(progm->first_playing < progm->n_words) { | 194 if(progm->first_playing < progm->n_words) { |
165 timer = mb_tman_timeout(progm->tman, &next_tmo, | 195 word = progm->words + progm->first_playing; |
166 mb_progm_step, progm); | 196 memcpy(&w_stp_tm, &word->start_time, sizeof(mb_timeval_t)); |
197 MB_TIMEVAL_ADD(&w_stp_tm, &progm->start_time); | |
198 | |
199 if(MB_TIMEVAL_LATER(&w_stp_tm, &next_tmo)) | |
200 timer = mb_tman_timeout(progm->tman, &w_stp_tm, | |
201 mb_progm_step, progm); | |
202 else | |
203 timer = mb_tman_timeout(progm->tman, &next_tmo, | |
204 mb_progm_step, progm); | |
167 ASSERT(timer != NULL); | 205 ASSERT(timer != NULL); |
168 } | 206 } |
207 | |
208 memcpy(&progm->last_time, tmo, sizeof(mb_timeval_t)); | |
169 } | 209 } |
170 | 210 |
171 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman, | 211 void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman, |
172 mb_timeval_t *now) { | 212 mb_timeval_t *now) { |
173 mb_timeval_t next_time; | 213 mb_timeval_t next_time; |
188 return; | 228 return; |
189 } | 229 } |
190 | 230 |
191 timer = mb_tman_timeout(tman, &next_time, mb_progm_step, progm); | 231 timer = mb_tman_timeout(tman, &next_time, mb_progm_step, progm); |
192 ASSERT(timer != NULL); | 232 ASSERT(timer != NULL); |
233 } | |
234 | |
235 void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) { | |
193 } | 236 } |
194 | 237 |
195 typedef struct _mb_shift mb_shift_t; | 238 typedef struct _mb_shift mb_shift_t; |
196 /*! \brief Animation action for shift a coordination. */ | 239 /*! \brief Animation action for shift a coordination. */ |
197 struct _mb_shift { | 240 struct _mb_shift { |
218 const mb_timeval_t *playing_time, | 261 const mb_timeval_t *playing_time, |
219 redraw_man_t *rdman) { | 262 redraw_man_t *rdman) { |
220 mb_shift_t *shift = (mb_shift_t *)act; | 263 mb_shift_t *shift = (mb_shift_t *)act; |
221 coord_t *coord; | 264 coord_t *coord; |
222 | 265 |
223 memcpy(&shift->start_time, now, sizeof(now)); | 266 memcpy(&shift->start_time, now, sizeof(mb_timeval_t)); |
224 coord = shift->coord; | 267 coord = shift->coord; |
225 memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6])); | 268 memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6])); |
226 shift->playing_time = playing_time; | 269 shift->playing_time = playing_time; |
227 } | 270 } |
228 | 271 |
229 static void mb_shift_step(mb_action_t *act, mb_timeval_t *now, | 272 static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now, |
230 redraw_man_t *rdman) { | 273 redraw_man_t *rdman) { |
231 mb_shift_t *shift = (mb_shift_t *)act; | 274 mb_shift_t *shift = (mb_shift_t *)act; |
232 mb_timeval_t diff; | 275 mb_timeval_t diff; |
233 coord_t *coord; | 276 coord_t *coord; |
234 float ratio; | 277 float ratio; |
243 coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio; | 286 coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio; |
244 | 287 |
245 rdman_coord_changed(rdman, coord); | 288 rdman_coord_changed(rdman, coord); |
246 } | 289 } |
247 | 290 |
248 static void mb_shift_stop(mb_action_t *act, mb_timeval_t *now, | 291 static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now, |
249 redraw_man_t *rdman) { | 292 redraw_man_t *rdman) { |
250 mb_shift_t *shift = (mb_shift_t *)act; | 293 mb_shift_t *shift = (mb_shift_t *)act; |
251 coord_t *coord; | 294 coord_t *coord; |
252 | 295 |
253 coord = shift->coord; | 296 coord = shift->coord; |
260 | 303 |
261 static void mb_shift_free(mb_action_t *act) { | 304 static void mb_shift_free(mb_action_t *act) { |
262 free(act); | 305 free(act); |
263 } | 306 } |
264 | 307 |
265 mb_action_t *mb_shift_new(co_aix x, co_aix y) { | 308 mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord) { |
266 mb_shift_t *shift; | 309 mb_shift_t *shift; |
267 | 310 |
268 shift = (mb_shift_t *)malloc(sizeof(mb_shift_t)); | 311 shift = (mb_shift_t *)malloc(sizeof(mb_shift_t)); |
269 if(shift == NULL) | 312 if(shift == NULL) |
270 return (mb_action_t *)shift; | 313 return (mb_action_t *)shift; |
271 | 314 |
272 shift->x = x; | 315 shift->x = x; |
273 shift->y = y; | 316 shift->y = y; |
317 shift->coord = coord; | |
318 | |
274 shift->action.start = mb_shift_start; | 319 shift->action.start = mb_shift_start; |
275 shift->action.step = mb_shift_step; | 320 shift->action.step = mb_shift_step; |
276 shift->action.stop = mb_shift_stop; | 321 shift->action.stop = mb_shift_stop; |
277 shift->action.free = mb_shift_free; | 322 shift->action.free = mb_shift_free; |
278 | 323 |