Mercurial > MadButterfly
annotate src/rotate.c @ 1172:178b126edd2c
Implement the correct normal tween. We will duplicate the node in the start scene. Insted of deleting all nodes which is not in the stop scene, we should delete the object which is not in the start scene instead. If we delete objecvt the the stop scene, the object should appear until we reach the stop scene.
author | wycc |
---|---|
date | Thu, 30 Dec 2010 11:50:02 +0800 |
parents | 586e50f82c1f |
children |
rev | line source |
---|---|
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
186
diff
changeset
|
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- |
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
186
diff
changeset
|
2 // vim: sw=4:ts=8:sts=4 |
117 | 3 #include <stdio.h> |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 #include <math.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
|
7 #include "mb_animate.h" |
117 | 8 |
9 static float comp_mb_timeval_ratio(const mb_timeval_t *a, | |
10 const mb_timeval_t *b) { | |
11 float ratio; | |
12 | |
13 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a); | |
14 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b); | |
15 return ratio; | |
16 } | |
17 | |
18 /*! \brief Animation action to rotate a coordinate. | |
19 */ | |
20 struct _mb_rotate { | |
21 mb_action_t action; | |
22 | |
23 co_aix angle1, angle2; | |
24 coord_t *coord; | |
25 | |
26 mb_timeval_t start_time; | |
27 const mb_timeval_t *playing_time; | |
28 }; | |
29 typedef struct _mb_rotate mb_rotate_t; | |
30 | |
31 static void mb_rotate_start(mb_action_t *act, | |
32 const mb_timeval_t *now, | |
33 const mb_timeval_t *playing_time, | |
34 redraw_man_t *rdman) { | |
35 mb_rotate_t *rotate = (mb_rotate_t *)act; | |
36 co_aix *matrix; | |
37 float _sin, _cos; | |
38 | |
39 _sin = sinf(rotate->angle1); | |
40 _cos = cosf(rotate->angle1); | |
41 | |
42 matrix = rotate->coord->matrix; | |
43 memset(matrix, 0, sizeof(co_aix) * 6); | |
44 matrix[0] = _cos; | |
45 matrix[1] = -_sin; | |
46 matrix[3] = _sin; | |
47 matrix[4] = _cos; | |
48 rdman_coord_changed(rdman, rotate->coord); | |
49 | |
50 MB_TIMEVAL_CP(&rotate->start_time, now); | |
51 rotate->playing_time = playing_time; | |
52 } | |
53 | |
54 static void mb_rotate_step(mb_action_t *act, const mb_timeval_t *now, | |
55 redraw_man_t *rdman) { | |
56 mb_rotate_t *rotate = (mb_rotate_t *)act; | |
57 mb_timeval_t diff; | |
58 co_aix *matrix; | |
59 float ratio; | |
60 float angle; | |
61 float _sin, _cos; | |
62 | |
63 MB_TIMEVAL_CP(&diff, now); | |
64 MB_TIMEVAL_DIFF(&diff, &rotate->start_time); | |
65 ratio = comp_mb_timeval_ratio(&diff, rotate->playing_time); | |
66 | |
67 angle = rotate->angle1 * (1 - ratio) + rotate->angle2 * ratio; | |
68 _sin = sinf(angle); | |
69 _cos = cosf(angle); | |
70 | |
71 matrix = rotate->coord->matrix; | |
72 matrix[0] = _cos; | |
73 matrix[1] = -_sin; | |
74 matrix[3] = _sin; | |
75 matrix[4] = _cos; | |
76 rdman_coord_changed(rdman, rotate->coord); | |
77 } | |
78 | |
79 static void mb_rotate_stop(mb_action_t *act, const mb_timeval_t *now, | |
80 redraw_man_t *rdman) { | |
81 mb_rotate_t *rotate = (mb_rotate_t *)act; | |
82 co_aix *matrix; | |
83 float _sin, _cos; | |
84 | |
85 _sin = sinf(rotate->angle2); | |
86 _cos = cosf(rotate->angle2); | |
87 | |
88 matrix = rotate->coord->matrix; | |
89 matrix[0] = _cos; | |
90 matrix[1] = -_sin; | |
91 matrix[3] = _sin; | |
92 matrix[4] = _cos; | |
93 rdman_coord_changed(rdman, rotate->coord); | |
94 } | |
95 | |
96 static void mb_rotate_free(mb_action_t *act) { | |
97 free(act); | |
98 } | |
99 | |
100 mb_action_t *mb_rotate_new(float angle1, float angle2, | |
101 coord_t *coord, | |
102 mb_word_t *word) { | |
103 mb_rotate_t *rotate; | |
104 | |
105 rotate = (mb_rotate_t *)malloc(sizeof(mb_rotate_t)); | |
106 if(rotate == NULL) | |
107 return NULL; | |
108 | |
109 rotate->angle1 = angle1; | |
110 rotate->angle2 = angle2; | |
111 rotate->coord = coord; | |
112 | |
113 rotate->action.start = mb_rotate_start; | |
114 rotate->action.step = mb_rotate_step; | |
115 rotate->action.stop = mb_rotate_stop; | |
116 rotate->action.free = mb_rotate_free; | |
117 | |
118 mb_word_add_action(word, (mb_action_t *)rotate); | |
119 | |
120 return (mb_action_t *)rotate; | |
121 } |