00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <math.h>
00005 #include "mb_animate.h"
00006
00007 static float comp_mb_timeval_ratio(const mb_timeval_t *a,
00008 const mb_timeval_t *b) {
00009 float ratio;
00010
00011 ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a);
00012 ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b);
00013 return ratio;
00014 }
00015
00016
00017
00018 struct _mb_rotate {
00019 mb_action_t action;
00020
00021 co_aix angle1, angle2;
00022 coord_t *coord;
00023
00024 mb_timeval_t start_time;
00025 const mb_timeval_t *playing_time;
00026 };
00027 typedef struct _mb_rotate mb_rotate_t;
00028
00029 static void mb_rotate_start(mb_action_t *act,
00030 const mb_timeval_t *now,
00031 const mb_timeval_t *playing_time,
00032 redraw_man_t *rdman) {
00033 mb_rotate_t *rotate = (mb_rotate_t *)act;
00034 co_aix *matrix;
00035 float _sin, _cos;
00036
00037 _sin = sinf(rotate->angle1);
00038 _cos = cosf(rotate->angle1);
00039
00040 matrix = rotate->coord->matrix;
00041 memset(matrix, 0, sizeof(co_aix) * 6);
00042 matrix[0] = _cos;
00043 matrix[1] = -_sin;
00044 matrix[3] = _sin;
00045 matrix[4] = _cos;
00046 rdman_coord_changed(rdman, rotate->coord);
00047
00048 MB_TIMEVAL_CP(&rotate->start_time, now);
00049 rotate->playing_time = playing_time;
00050 }
00051
00052 static void mb_rotate_step(mb_action_t *act, const mb_timeval_t *now,
00053 redraw_man_t *rdman) {
00054 mb_rotate_t *rotate = (mb_rotate_t *)act;
00055 mb_timeval_t diff;
00056 co_aix *matrix;
00057 float ratio;
00058 float angle;
00059 float _sin, _cos;
00060
00061 MB_TIMEVAL_CP(&diff, now);
00062 MB_TIMEVAL_DIFF(&diff, &rotate->start_time);
00063 ratio = comp_mb_timeval_ratio(&diff, rotate->playing_time);
00064
00065 angle = rotate->angle1 * (1 - ratio) + rotate->angle2 * ratio;
00066 _sin = sinf(angle);
00067 _cos = cosf(angle);
00068
00069 matrix = rotate->coord->matrix;
00070 matrix[0] = _cos;
00071 matrix[1] = -_sin;
00072 matrix[3] = _sin;
00073 matrix[4] = _cos;
00074 rdman_coord_changed(rdman, rotate->coord);
00075 }
00076
00077 static void mb_rotate_stop(mb_action_t *act, const mb_timeval_t *now,
00078 redraw_man_t *rdman) {
00079 mb_rotate_t *rotate = (mb_rotate_t *)act;
00080 co_aix *matrix;
00081 float _sin, _cos;
00082
00083 _sin = sinf(rotate->angle2);
00084 _cos = cosf(rotate->angle2);
00085
00086 matrix = rotate->coord->matrix;
00087 matrix[0] = _cos;
00088 matrix[1] = -_sin;
00089 matrix[3] = _sin;
00090 matrix[4] = _cos;
00091 rdman_coord_changed(rdman, rotate->coord);
00092 }
00093
00094 static void mb_rotate_free(mb_action_t *act) {
00095 free(act);
00096 }
00097
00098 mb_action_t *mb_rotate_new(float angle1, float angle2,
00099 coord_t *coord,
00100 mb_word_t *word) {
00101 mb_rotate_t *rotate;
00102
00103 rotate = (mb_rotate_t *)malloc(sizeof(mb_rotate_t));
00104 if(rotate == NULL)
00105 return NULL;
00106
00107 rotate->angle1 = angle1;
00108 rotate->angle2 = angle2;
00109 rotate->coord = coord;
00110
00111 rotate->action.start = mb_rotate_start;
00112 rotate->action.step = mb_rotate_step;
00113 rotate->action.stop = mb_rotate_stop;
00114 rotate->action.free = mb_rotate_free;
00115
00116 mb_word_add_action(word, (mb_action_t *)rotate);
00117
00118 return (mb_action_t *)rotate;
00119 }