libanim

diff src/track.h @ 0:fad4701f484e

libanim mercurial repo
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 08 Jan 2012 05:13:13 +0200
parents
children f561282b13e8
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/track.h	Sun Jan 08 05:13:13 2012 +0200
     1.3 @@ -0,0 +1,95 @@
     1.4 +/* An animation track defines the values of a single scalar over time
     1.5 + * and supports various interpolation and extrapolation modes.
     1.6 + */
     1.7 +#ifndef LIBANIM_TRACK_H_
     1.8 +#define LIBANIM_TRACK_H_
     1.9 +
    1.10 +#include <limits.h>
    1.11 +#include "config.h"
    1.12 +
    1.13 +enum anm_interpolator {
    1.14 +	ANM_INTERP_STEP,
    1.15 +	ANM_INTERP_LINEAR,
    1.16 +	ANM_INTERP_CUBIC
    1.17 +};
    1.18 +
    1.19 +enum anm_extrapolator {
    1.20 +	ANM_EXTRAP_EXTEND,	/* extend to infinity */
    1.21 +	ANM_EXTRAP_CLAMP,	/* clamp to last value */
    1.22 +	ANM_EXTRAP_REPEAT	/* repeat motion */
    1.23 +};
    1.24 +
    1.25 +typedef long anm_time_t;
    1.26 +#define ANM_TIME_INVAL	LONG_MIN
    1.27 +
    1.28 +#define ANM_SEC2TM(x)	((anm_time_t)((x) * 1000))
    1.29 +#define ANM_MSEC2TM(x)	((anm_time_t)(x))
    1.30 +#define ANM_TM2SEC(x)	((x) / 1000.0)
    1.31 +#define ANM_TM2MSEC(x)	(x)
    1.32 +
    1.33 +struct anm_keyframe {
    1.34 +	anm_time_t time;
    1.35 +	float val;
    1.36 +};
    1.37 +
    1.38 +struct anm_track {
    1.39 +	char *name;
    1.40 +	int count;
    1.41 +	struct anm_keyframe *keys;
    1.42 +
    1.43 +	float def_val;
    1.44 +
    1.45 +	enum anm_interpolator interp;
    1.46 +	enum anm_extrapolator extrap;
    1.47 +};
    1.48 +
    1.49 +#ifdef __cplusplus
    1.50 +extern "C" {
    1.51 +#endif
    1.52 +
    1.53 +/* track constructor and destructor */
    1.54 +int anm_init_track(struct anm_track *track);
    1.55 +void anm_destroy_track(struct anm_track *track);
    1.56 +
    1.57 +/* helper functions that use anm_init_track and anm_destroy_track internally */
    1.58 +struct anm_track *anm_create_track(void);
    1.59 +void anm_free_track(struct anm_track *track);
    1.60 +
    1.61 +int anm_set_track_name(struct anm_track *track, const char *name);
    1.62 +const char *anm_get_track_name(struct anm_track *track);
    1.63 +
    1.64 +void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in);
    1.65 +void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex);
    1.66 +
    1.67 +anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end);
    1.68 +
    1.69 +void anm_set_track_default(struct anm_track *track, float def);
    1.70 +
    1.71 +/* set or update a keyframe */
    1.72 +int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key);
    1.73 +
    1.74 +/* get the idx-th keyframe, returns null if it doesn't exist */
    1.75 +struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx);
    1.76 +
    1.77 +/* Finds the 0-based index of the intra-keyframe interval which corresponds
    1.78 + * to the specified time. If the time falls exactly onto the N-th keyframe
    1.79 + * the function returns N.
    1.80 + *
    1.81 + * Special cases:
    1.82 + * - if the time is before the first keyframe -1 is returned.
    1.83 + * - if the time is after the last keyframe, the index of the last keyframe
    1.84 + *   is returned.
    1.85 + */
    1.86 +int anm_get_key_interval(struct anm_track *track, anm_time_t tm);
    1.87 +
    1.88 +int anm_set_value(struct anm_track *track, anm_time_t tm, float val);
    1.89 +
    1.90 +/* evaluates and returns the value of the track for a particular time */
    1.91 +float anm_get_value(struct anm_track *track, anm_time_t tm);
    1.92 +
    1.93 +#ifdef __cplusplus
    1.94 +}
    1.95 +#endif
    1.96 +
    1.97 +
    1.98 +#endif	/* LIBANIM_TRACK_H_ */