comparison src/shift.c @ 116:1d74eb3861b7

move animation actions from animate.c to files.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 14 Sep 2008 09:42:07 +0800
parents
children e4e47d2cdbcd
comparison
equal deleted inserted replaced
115:3895d2224e67 116:1d74eb3861b7
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "animate.h"
5
6 static float comp_mb_timeval_ratio(const mb_timeval_t *a,
7 const mb_timeval_t *b) {
8 float ratio;
9
10 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a);
11 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b);
12 return ratio;
13 }
14
15 typedef struct _mb_shift mb_shift_t;
16 /*! \brief Animation action for shift a coordination. */
17 struct _mb_shift {
18 mb_action_t action;
19
20 co_aix x, y;
21 coord_t *coord;
22
23 mb_timeval_t start_time;
24 co_aix saved_matrix[6];
25 const mb_timeval_t *playing_time;
26 };
27
28 static void mb_shift_start(mb_action_t *act,
29 const mb_timeval_t *now,
30 const mb_timeval_t *playing_time,
31 redraw_man_t *rdman) {
32 mb_shift_t *shift = (mb_shift_t *)act;
33 coord_t *coord;
34
35 MB_TIMEVAL_CP(&shift->start_time, now);
36 coord = shift->coord;
37 memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6]));
38 shift->playing_time = playing_time;
39 }
40
41 static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now,
42 redraw_man_t *rdman) {
43 mb_shift_t *shift = (mb_shift_t *)act;
44 mb_timeval_t diff;
45 coord_t *coord;
46 float ratio;
47
48
49 MB_TIMEVAL_CP(&diff, now);
50 MB_TIMEVAL_DIFF(&diff, &shift->start_time);
51 ratio = comp_mb_timeval_ratio(&diff, shift->playing_time);
52
53 coord = shift->coord;
54 coord->matrix[2] = shift->saved_matrix[2] + shift->x * ratio;
55 coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio;
56
57 rdman_coord_changed(rdman, coord);
58 }
59
60 static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now,
61 redraw_man_t *rdman) {
62 mb_shift_t *shift = (mb_shift_t *)act;
63 coord_t *coord;
64
65 coord = shift->coord;
66 coord->matrix[2] = shift->saved_matrix[2] + shift->x;
67 coord->matrix[5] = shift->saved_matrix[5] + shift->y;
68
69 rdman_coord_changed(rdman, coord);
70 }
71
72
73 static void mb_shift_free(mb_action_t *act) {
74 free(act);
75 }
76
77 mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord,
78 mb_word_t *word) {
79 mb_shift_t *shift;
80
81 shift = (mb_shift_t *)malloc(sizeof(mb_shift_t));
82 if(shift == NULL)
83 return (mb_action_t *)shift;
84
85 shift->x = x;
86 shift->y = y;
87 shift->coord = coord;
88
89 shift->action.start = mb_shift_start;
90 shift->action.step = mb_shift_step;
91 shift->action.stop = mb_shift_stop;
92 shift->action.free = mb_shift_free;
93
94 mb_word_add_action(word, (mb_action_t *)shift);
95
96 return (mb_action_t *)shift;
97 }
98
99