view src/animate.c @ 64:c668c5c3ceae

M4 macro for C binding.
author Thinker K.F. Li <thinker@branda.to>
date Wed, 13 Aug 2008 02:07:40 +0800
parents ab028c9f0930
children 8feb89b19619
line wrap: on
line source

/*! \brief Animation tools.
 *
 * XXX: Program is a sequence of actions duration a perior.
 * Actions are grouped into words.  A program defines
 * the order and time of playing of words.  A word
 * defines how long to perform a set of actions.  Actions
 * in a word are performed concurrently.
 *
 * Animation shapes are updated periodically.  Every action
 * are working with start, step, and stop 3 calls.  start is
 * called when start time the word, contain it, due.  step is
 * called periodically in duration of playing time start at
 * 'start time'.  When the clock run out the playing time of
 * a word, it call stop of actions in the word.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mb_types.h"
#include "redraw_man.h"
#include "mb_timer.h"
#include "animate.h"


#define STEP_INTERVAL 100000
#define ASSERT(x)

/*! \brief A word is a set of concurrent actions in a program.
 */
struct _mb_word {
    mb_timeval_t start_time;	/*!< time to start the word */
    mb_timeval_t playing_time;	/*!< time duration of playing */
    mb_timeval_t abs_start, abs_stop;
    STAILQ(mb_action_t) actions;
};

/*! \brief A program describe a series of actions to animate shapes.
 *
 * first_playing is an index to one of words.  It always points to
 * the first of words that is playing or waiting for playing.
 */
struct _mb_progm {
    redraw_man_t *rdman;

    mb_timeval_t start_time;
    int first_playing;		/*!< first playing word. */
    mb_tman_t *tman;

    int n_words;
    int max_words;
    mb_word_t words[1];
};

/*! \brief Basic class of nnimation actions.
 *
 * A action must implement following 4 functions.
 * \li start,
 * \li step,
 * \li stop,
 * \li free, and
 * \li *_new().
 *
 * *_new() must invokes mb_word_add_action() to add new object
 * as one of actions in the word specified as an argument of it.
 * It also means *_new() must have an argument with type of
 * (mb_word_t *).
 */
struct _mb_action {
    void (*start)(mb_action_t *act,
		  const mb_timeval_t *now,
		  const mb_timeval_t *playing_time,
		  redraw_man_t *rdman);
    void (*step)(mb_action_t *act, const mb_timeval_t *now,
		 redraw_man_t *rdman);
    void (*stop)(mb_action_t *act, const mb_timeval_t *now,
		 redraw_man_t *rdman);
    void (*free)(mb_action_t *act);
    mb_action_t *next;
};

mb_progm_t *mb_progm_new(int max_words, redraw_man_t *rdman) {
    mb_progm_t *progm;
    int i;

    progm = (mb_progm_t *)malloc(sizeof(mb_progm_t) +
				 (sizeof(mb_word_t) * (max_words - 1)));
    if(progm == NULL)
	return NULL;

    progm->rdman = rdman;
    progm->n_words = 0;
    progm->max_words = max_words;
    for(i = 0; i < max_words; i++)
	STAILQ_INIT(progm->words[i].actions);
    return progm;
}

void mb_progm_free(mb_progm_t *progm) {
    int n_words;
    mb_word_t *word;
    mb_action_t *cur_act;
    int i;

    n_words = progm->n_words;
    for(i = 0; i < n_words; i++) {
	word = progm->words + i;
	for(cur_act = STAILQ_HEAD(word->actions);
	    cur_act != NULL;
	    cur_act = STAILQ_HEAD(word->actions)) {
	    STAILQ_REMOVE(word->actions, mb_action_t, next, cur_act);
	    cur_act->free(cur_act);
	}
    }
    free(progm);
}

/*! \brief Add a new word into a program.
 *
 * The start time of new word should bigger or equal to last one.
 * The time should be specified in incremental order.
 */
mb_word_t *mb_progm_next_word(mb_progm_t *progm,
			      const mb_timeval_t *start,
			      const mb_timeval_t *playing) {
    mb_word_t *word;
    if(progm->n_words >= progm->max_words)
	return NULL;
    if(progm->n_words &&
       MB_TIMEVAL_LATER(&progm->words[progm->n_words - 1].start_time, start))
	return NULL;
    word = progm->words + progm->n_words++;
    MB_TIMEVAL_CP(&word->start_time, start);
    MB_TIMEVAL_CP(&word->playing_time, playing);
    return word;
}

static void mb_word_add_action(mb_word_t *word, mb_action_t *act) {
    STAILQ_INS_TAIL(word->actions, mb_action_t, next, act);
}

static void mb_word_start(mb_word_t *word, const mb_timeval_t *tmo,
			  const mb_timeval_t *now, redraw_man_t *rdman) {
    mb_action_t *act;

    for(act = STAILQ_HEAD(word->actions);
	act != NULL;
	act = STAILQ_NEXT(mb_action_t, next, act)) {
	act->start(act, tmo, &word->playing_time, rdman);
    }
}

static void mb_word_step(mb_word_t *word, const mb_timeval_t *tmo,
			 const mb_timeval_t *now, redraw_man_t *rdman) {
    mb_action_t *act;

    for(act = STAILQ_HEAD(word->actions);
	act != NULL;
	act = STAILQ_NEXT(mb_action_t, next, act)) {
	act->step(act, tmo, rdman);
    }
}

static void mb_word_stop(mb_word_t *word, const mb_timeval_t *tmo,
			 const mb_timeval_t *now, redraw_man_t *rdman) {
    mb_action_t *act;

    for(act = STAILQ_HEAD(word->actions);
	act != NULL;
	act = STAILQ_NEXT(mb_action_t, next, act)) {
	act->stop(act, tmo, rdman);
    }
}

/*
 * Any two actions in concurrent words never change the same attribute.
 * Start time of words in a program are specified in incremental order.
 */
static void mb_progm_step(const mb_timeval_t *tmo,
			     const mb_timeval_t *now,
			     void *arg) {
    mb_progm_t *progm = (mb_progm_t *)arg;
    mb_timeval_t next_tmo;
    mb_word_t *word;
    mb_timer_t *timer;
    int i;

    MB_TIMEVAL_SET(&next_tmo, 0, STEP_INTERVAL);
    MB_TIMEVAL_ADD(&next_tmo, tmo);

    /* _step() or _stop() words that in playing. */
    i = progm->first_playing;
    for(word = progm->words + i;
	i < progm->n_words && MB_TIMEVAL_LATER(tmo, &word->abs_start);
	word = progm->words + ++i) {
	if(MB_TIMEVAL_LATER(tmo, &word->abs_stop))
	    continue;
	if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop))
	    mb_word_stop(word, tmo, now, progm->rdman);
	else
	    mb_word_step(word, tmo, now, progm->rdman);
    }

    /* Start words that their abs_start is in duration
     * from now to timeout for next update.
     */
    for(word = progm->words + i;
	i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_start);
	word = progm->words + ++i) {
	mb_word_start(word, tmo, now, progm->rdman);
	if(MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop))
	    mb_word_stop(word, tmo, now, progm->rdman);
    }

    /* Find a new first_playing if any consequence words, following current
     * first_playing word, are stoped.
     */
    i = progm->first_playing;
    for(word = progm->words + i;
	i < progm->n_words && MB_TIMEVAL_LATER(&next_tmo, &word->abs_stop);
	word = progm->words + ++i) {
	progm->first_playing++;
    }

    /* Setup timeout for next update. */
    if(progm->first_playing < progm->n_words) {
	word = progm->words + progm->first_playing;
	if(MB_TIMEVAL_LATER(&word->abs_start, &next_tmo))
	    MB_TIMEVAL_CP(&next_tmo, &word->abs_start);
	timer = mb_tman_timeout(progm->tman, &next_tmo,
				mb_progm_step, progm);	
    }
}

void mb_progm_start(mb_progm_t *progm, mb_tman_t *tman,
		    mb_timeval_t *now) {
    mb_timer_t *timer;
    mb_word_t *word;
    int i;

    if(progm->n_words == 0)
	return;

    progm->tman = tman;
    MB_TIMEVAL_CP(&progm->start_time, now);
    progm->first_playing = 0;

    for(i = 0; i < progm->n_words; i++) {
	word = progm->words + i;
	MB_TIMEVAL_CP(&word->abs_start, &word->start_time);
	MB_TIMEVAL_ADD(&word->abs_start, now);
	MB_TIMEVAL_CP(&word->abs_stop, &word->abs_start);
	MB_TIMEVAL_ADD(&word->abs_stop, &word->playing_time);
    }

    if(MB_TIMEVAL_EQ(&progm->words[0].abs_start, now)) {
	mb_progm_step(now, now, progm);
	return;
    }
    
    timer = mb_tman_timeout(tman, &progm->words[0].abs_start,
			    mb_progm_step, progm);
    ASSERT(timer != NULL);
}

void mb_progm_abort(mb_progm_t *progm, mb_tman_t *tman) {
}

typedef struct _mb_shift mb_shift_t;
/*! \brief Animation action for shift a coordination. */
struct _mb_shift {
    mb_action_t action;

    co_aix x, y;
    coord_t *coord;

    mb_timeval_t start_time;
    co_aix saved_matrix[6];
    const mb_timeval_t *playing_time;
};

static float comp_mb_timeval_ratio(const mb_timeval_t *a,
				   const mb_timeval_t *b) {
    float ratio;

    ratio = (float)MB_TIMEVAL_SEC(a) * 1000000.0 + (float)MB_TIMEVAL_USEC(a);
    ratio /= (float)MB_TIMEVAL_SEC(b) * 1000000.0 + (float)MB_TIMEVAL_USEC(b);
    return ratio;
}

static void mb_shift_start(mb_action_t *act,
			   const mb_timeval_t *now,
			   const mb_timeval_t *playing_time,
			   redraw_man_t *rdman) {
    mb_shift_t *shift = (mb_shift_t *)act;
    coord_t *coord;

    MB_TIMEVAL_CP(&shift->start_time, now);
    coord = shift->coord;
    memcpy(&shift->saved_matrix, coord->matrix, sizeof(co_aix[6]));
    shift->playing_time = playing_time;
}

static void mb_shift_step(mb_action_t *act, const mb_timeval_t *now,
			  redraw_man_t *rdman) {
    mb_shift_t *shift = (mb_shift_t *)act;
    mb_timeval_t diff;
    coord_t *coord;
    float ratio;

    
    MB_TIMEVAL_CP(&diff, now);
    MB_TIMEVAL_DIFF(&diff, &shift->start_time);
    ratio = comp_mb_timeval_ratio(&diff, shift->playing_time);

    coord = shift->coord;
    coord->matrix[2] = shift->saved_matrix[2] + shift->x * ratio;
    coord->matrix[5] = shift->saved_matrix[5] + shift->y * ratio;

    rdman_coord_changed(rdman, coord);
}

static void mb_shift_stop(mb_action_t *act, const mb_timeval_t *now,
			  redraw_man_t *rdman) {
    mb_shift_t *shift = (mb_shift_t *)act;
    coord_t *coord;

    coord = shift->coord;
    coord->matrix[2] = shift->saved_matrix[2] + shift->x;
    coord->matrix[5] = shift->saved_matrix[5] + shift->y;

    rdman_coord_changed(rdman, coord);
}


static void mb_shift_free(mb_action_t *act) {
    free(act);
}

mb_action_t *mb_shift_new(co_aix x, co_aix y, coord_t *coord,
			  mb_word_t *word) {
    mb_shift_t *shift;

    shift = (mb_shift_t *)malloc(sizeof(mb_shift_t));
    if(shift == NULL)
	return (mb_action_t *)shift;

    shift->x = x;
    shift->y = y;
    shift->coord = coord;

    shift->action.start = mb_shift_start;
    shift->action.step = mb_shift_step;
    shift->action.stop = mb_shift_stop;
    shift->action.free = mb_shift_free;

    mb_word_add_action(word, (mb_action_t *)shift);

    return (mb_action_t *)shift;
}


#include "paint.h"
typedef struct _mb_chgcolor mb_chgcolor_t;

struct _mb_chgcolor {
    mb_action_t action;

    co_comp_t r, g, b, a;
    paint_t *paint;

    mb_timeval_t start_time;
    const mb_timeval_t *playing_time;
    co_comp_t s_r, s_g, s_b, s_a; /*!< saved RGBA values. */
};

static void mb_chgcolor_start(mb_action_t *act,
			      const mb_timeval_t *now,
			      const mb_timeval_t *playing_time,
			      redraw_man_t *rdman) {
    mb_chgcolor_t *chg = (mb_chgcolor_t *)act;

    MB_TIMEVAL_CP(&chg->start_time, now);
    chg->playing_time = playing_time; /* playing_time is in word,
				       * it live time is as long as
				       * actions. */
    paint_color_get(chg->paint,
		    &chg->s_r, &chg->s_g,
		    &chg->s_b, &chg->s_a);
}

static void mb_chgcolor_step(mb_action_t *act,
			     const mb_timeval_t *now,
			     redraw_man_t *rdman) {
    mb_chgcolor_t *chg = (mb_chgcolor_t *)act;
    mb_timeval_t diff;
    co_comp_t r, g, b, a;
    float ratio, comp;

    MB_TIMEVAL_CP(&diff, now);
    MB_TIMEVAL_DIFF(&diff, &chg->start_time);
    ratio = comp_mb_timeval_ratio(&diff, chg->playing_time);
    comp = 1 - ratio;

    r = chg->s_r * comp + ratio * chg->r;
    g = chg->s_g * comp + ratio * chg->g;
    b = chg->s_b * comp + ratio * chg->b;
    a = chg->s_a * comp + ratio * chg->a;
    paint_color_set(chg->paint, r, g, b, a);

    rdman_paint_changed(rdman, chg->paint);
}

static void mb_chgcolor_stop(mb_action_t *act,
			     const mb_timeval_t *now,
			     redraw_man_t *rdman) {
    mb_chgcolor_t *chg = (mb_chgcolor_t *)act;

    paint_color_set(chg->paint, chg->r, chg->g, chg->b, chg->a);

    rdman_paint_changed(rdman, chg->paint);
}

static void mb_chgcolor_free(mb_action_t *act) {
    free(act);
}

mb_action_t *mb_chgcolor_new(co_comp_t r, co_comp_t g,
			     co_comp_t b, co_comp_t a,
			     paint_t *paint, mb_word_t *word) {
    mb_chgcolor_t *chg;

    chg = (mb_chgcolor_t *)malloc(sizeof(mb_chgcolor_t));
    if(chg == NULL)
	return NULL;

    chg->r = r;
    chg->g = g;
    chg->b = b;
    chg->a = a;

    chg->paint = paint;

    chg->action.start = mb_chgcolor_start;
    chg->action.step = mb_chgcolor_step;
    chg->action.stop = mb_chgcolor_stop;
    chg->action.free = mb_chgcolor_free;

    mb_word_add_action(word, (mb_action_t *)chg);

    return (mb_action_t *)chg;
}


typedef struct _mb_visibility mb_visibility_t;

struct _mb_visibility {
    mb_action_t action;
    int visibility;
    coord_t *coord;
};

static void mb_visibility_start(mb_action_t *act,
				const mb_timeval_t *now,
				const mb_timeval_t *playing_time,
				redraw_man_t *rdman) {
    mb_visibility_t *visibility = (mb_visibility_t *)act;

    switch(visibility->visibility) {
    case VIS_VISIBLE:
	coord_show(visibility->coord);
	break;
    case VIS_HIDDEN:
	coord_hide(visibility->coord);
	break;
    }
    rdman_coord_changed(rdman, visibility->coord);
}

static void mb_visibility_step(mb_action_t *act,
			       const mb_timeval_t *now,
			       redraw_man_t *rdman) {
}

static void mb_visibility_stop(mb_action_t *act,
			       const mb_timeval_t *now,
			       redraw_man_t *rdman) {
}

static void mb_visibility_free(mb_action_t *act) {
    free(act);
}

mb_action_t *mb_visibility_new(int visib, coord_t *coord,
			       mb_word_t *word) {
    mb_visibility_t *visibility;

    visibility = (mb_visibility_t *)malloc(sizeof(mb_visibility_t));
    if(visibility == NULL)
	return NULL;

    visibility->visibility = visib;
    visibility->coord = coord;

    visibility->action.start = mb_visibility_start;
    visibility->action.step = mb_visibility_step;
    visibility->action.stop = mb_visibility_stop;
    visibility->action.free = mb_visibility_free;

    mb_word_add_action(word, (mb_action_t *)visibility);

    return (mb_action_t *)visibility;
}

#ifdef UNITTEST

#include <CUnit/Basic.h>

typedef struct _mb_dummy mb_dummy_t;

struct _mb_dummy {
    mb_action_t action;
    int id;
    int *logidx;
    int *logs;
};


static void mb_dummy_start(mb_action_t *act,
			   const mb_timeval_t *now,
			   const mb_timeval_t *playing_time,
			   redraw_man_t *rdman) {
    mb_dummy_t *dummy = (mb_dummy_t *)act;

    dummy->logs[(*dummy->logidx)++] = dummy->id;
}

static void mb_dummy_step(mb_action_t *act,
			  const mb_timeval_t *now,
			  redraw_man_t *rdman) {
    mb_dummy_t *dummy = (mb_dummy_t *)act;

    dummy->logs[(*dummy->logidx)++] = dummy->id;
}

static void mb_dummy_stop(mb_action_t *act,
			  const mb_timeval_t *now,
			  redraw_man_t *rdman) {
    mb_dummy_t *dummy = (mb_dummy_t *)act;

    dummy->logs[(*dummy->logidx)++] = dummy->id;
}

static void mb_dummy_free(mb_action_t *act) {
    free(act);
}

mb_action_t *mb_dummy_new(int id, int *logidx, int *logs, mb_word_t *word) {
    mb_dummy_t *dummy;

    dummy = (mb_dummy_t *)malloc(sizeof(mb_dummy_t));
    if(dummy == NULL)
	return NULL;

    dummy->id = id;
    dummy->logidx = logidx;
    dummy->logs = logs;

    dummy->action.start = mb_dummy_start;
    dummy->action.step = mb_dummy_step;
    dummy->action.stop = mb_dummy_stop;
    dummy->action.free = mb_dummy_free;

    mb_word_add_action(word, (mb_action_t *)dummy);

    return (mb_action_t *)dummy;
}

void test_animate_words(void) {
    mb_progm_t *progm;
    mb_word_t *word;
    mb_action_t *acts[4];
    mb_tman_t *tman;
    mb_timeval_t tv1, tv2, now, tmo_after;
    int logcnt = 0;
    int logs[256];
    int r;

    tman = mb_tman_new();
    CU_ASSERT(tman != NULL);

    progm = mb_progm_new(3, NULL);
    CU_ASSERT(progm != NULL);

    MB_TIMEVAL_SET(&tv1, 1, 0);
    MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3);
    word = mb_progm_next_word(progm, &tv1, &tv2);
    CU_ASSERT(word != NULL);
    acts[0] = mb_dummy_new(0, &logcnt, logs, word);
    CU_ASSERT(acts[0] != NULL);

    MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 4 / 3);
    MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL / 3);
    word = mb_progm_next_word(progm, &tv1, &tv2);
    CU_ASSERT(word != NULL);
    acts[1] = mb_dummy_new(1, &logcnt, logs, word);
    CU_ASSERT(acts[1] != NULL);

    MB_TIMEVAL_SET(&tv1, 1, STEP_INTERVAL * 2);
    MB_TIMEVAL_SET(&tv2, 0, STEP_INTERVAL * 3);
    word = mb_progm_next_word(progm, &tv1, &tv2);
    CU_ASSERT(word != NULL);
    acts[2] = mb_dummy_new(2, &logcnt, logs, word);
    CU_ASSERT(acts[2] != NULL);

    MB_TIMEVAL_SET(&now, 0, 0);
    mb_progm_start(progm, tman, &now);

    r = mb_tman_next_timeout(tman, &now, &tmo_after);
    CU_ASSERT(r == 0);
    CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 1 &&
	      MB_TIMEVAL_USEC(&tmo_after) == 0);

    /* 1.0s */
    MB_TIMEVAL_ADD(&now, &tmo_after);
    mb_tman_handle_timeout(tman, &now);
    CU_ASSERT(logcnt == 1);

    r = mb_tman_next_timeout(tman, &now, &tmo_after);
    CU_ASSERT(r == 0);
    CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
	      MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
    
    /* 1.1s */
    MB_TIMEVAL_ADD(&now, &tmo_after);
    mb_tman_handle_timeout(tman, &now);
    CU_ASSERT(logcnt == 4);

    r = mb_tman_next_timeout(tman, &now, &tmo_after);
    CU_ASSERT(r == 0);
    CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
	      MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
    
    /* 1.2s */
    MB_TIMEVAL_ADD(&now, &tmo_after);
    mb_tman_handle_timeout(tman, &now);
    CU_ASSERT(logcnt == 6);

    r = mb_tman_next_timeout(tman, &now, &tmo_after);
    CU_ASSERT(r == 0);
    CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
	      MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
    
    /* 1.3s */
    MB_TIMEVAL_ADD(&now, &tmo_after);
    mb_tman_handle_timeout(tman, &now);
    CU_ASSERT(logcnt == 8);

    r = mb_tman_next_timeout(tman, &now, &tmo_after);
    CU_ASSERT(r == 0);
    CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
	      MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
    
    /* 1.4s */
    MB_TIMEVAL_ADD(&now, &tmo_after);
    mb_tman_handle_timeout(tman, &now);
    CU_ASSERT(logcnt == 9);

    r = mb_tman_next_timeout(tman, &now, &tmo_after);
    CU_ASSERT(r == 0);
    CU_ASSERT(MB_TIMEVAL_SEC(&tmo_after) == 0 &&
	      MB_TIMEVAL_USEC(&tmo_after) == STEP_INTERVAL);
    
    /* 1.5s */
    MB_TIMEVAL_ADD(&now, &tmo_after);
    mb_tman_handle_timeout(tman, &now);
    CU_ASSERT(logcnt == 10);

    r = mb_tman_next_timeout(tman, &now, &tmo_after);
    CU_ASSERT(r == -1);

    mb_progm_free(progm);
    mb_tman_free(tman);
}

CU_pSuite get_animate_suite(void) {
    CU_pSuite suite;

    suite = CU_add_suite("Suite_animate", NULL, NULL);
    if(suite == NULL)
	return NULL;

    CU_ADD_TEST(suite, test_animate_words);
    
    return suite;
}

#endif /* UNITTEST */