annotate src/rotate.c @ 1453:0cb89e204824

Implement tree-traveling for JS
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 13 Apr 2011 17:34:09 +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
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include <stdio.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include <stdlib.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include <string.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 static float comp_mb_timeval_ratio(const mb_timeval_t *a,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 const mb_timeval_t *b) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 float ratio;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 return ratio;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 /*! \brief Animation action to rotate a coordinate.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 */
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 struct _mb_rotate {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 mb_action_t action;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 co_aix angle1, angle2;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 coord_t *coord;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 mb_timeval_t start_time;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 const mb_timeval_t *playing_time;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 };
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 typedef struct _mb_rotate mb_rotate_t;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 static void mb_rotate_start(mb_action_t *act,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 const mb_timeval_t *now,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 const mb_timeval_t *playing_time,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 redraw_man_t *rdman) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 mb_rotate_t *rotate = (mb_rotate_t *)act;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 co_aix *matrix;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 float _sin, _cos;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 _sin = sinf(rotate->angle1);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 _cos = cosf(rotate->angle1);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 matrix = rotate->coord->matrix;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 memset(matrix, 0, sizeof(co_aix) * 6);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 matrix[0] = _cos;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 matrix[1] = -_sin;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 matrix[3] = _sin;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 matrix[4] = _cos;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 rdman_coord_changed(rdman, rotate->coord);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 MB_TIMEVAL_CP(&rotate->start_time, now);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 rotate->playing_time = playing_time;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 static void mb_rotate_step(mb_action_t *act, const mb_timeval_t *now,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 redraw_man_t *rdman) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 mb_rotate_t *rotate = (mb_rotate_t *)act;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 mb_timeval_t diff;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 co_aix *matrix;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 float ratio;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 float angle;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 float _sin, _cos;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 MB_TIMEVAL_CP(&diff, now);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 MB_TIMEVAL_DIFF(&diff, &rotate->start_time);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 ratio = comp_mb_timeval_ratio(&diff, rotate->playing_time);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 angle = rotate->angle1 * (1 - ratio) + rotate->angle2 * ratio;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 _sin = sinf(angle);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 _cos = cosf(angle);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 matrix = rotate->coord->matrix;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 matrix[0] = _cos;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 matrix[1] = -_sin;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 matrix[3] = _sin;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 matrix[4] = _cos;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 rdman_coord_changed(rdman, rotate->coord);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 static void mb_rotate_stop(mb_action_t *act, const mb_timeval_t *now,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 redraw_man_t *rdman) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 mb_rotate_t *rotate = (mb_rotate_t *)act;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 co_aix *matrix;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 float _sin, _cos;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 _sin = sinf(rotate->angle2);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 _cos = cosf(rotate->angle2);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 matrix = rotate->coord->matrix;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 matrix[0] = _cos;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 matrix[1] = -_sin;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 matrix[3] = _sin;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 matrix[4] = _cos;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 rdman_coord_changed(rdman, rotate->coord);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 static void mb_rotate_free(mb_action_t *act) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 free(act);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 mb_action_t *mb_rotate_new(float angle1, float angle2,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 coord_t *coord,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 mb_word_t *word) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 mb_rotate_t *rotate;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 rotate = (mb_rotate_t *)malloc(sizeof(mb_rotate_t));
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 if(rotate == NULL)
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 return NULL;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109 rotate->angle1 = angle1;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 rotate->angle2 = angle2;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 rotate->coord = coord;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 rotate->action.start = mb_rotate_start;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114 rotate->action.step = mb_rotate_step;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 rotate->action.stop = mb_rotate_stop;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 rotate->action.free = mb_rotate_free;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 mb_word_add_action(word, (mb_action_t *)rotate);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 return (mb_action_t *)rotate;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 }