annotate src/rotate.c @ 779:8e9481bf1cc0

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