libanim

view 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 source
1 /* An animation track defines the values of a single scalar over time
2 * and supports various interpolation and extrapolation modes.
3 */
4 #ifndef LIBANIM_TRACK_H_
5 #define LIBANIM_TRACK_H_
7 #include <limits.h>
8 #include "config.h"
10 enum anm_interpolator {
11 ANM_INTERP_STEP,
12 ANM_INTERP_LINEAR,
13 ANM_INTERP_CUBIC
14 };
16 enum anm_extrapolator {
17 ANM_EXTRAP_EXTEND, /* extend to infinity */
18 ANM_EXTRAP_CLAMP, /* clamp to last value */
19 ANM_EXTRAP_REPEAT /* repeat motion */
20 };
22 typedef long anm_time_t;
23 #define ANM_TIME_INVAL LONG_MIN
25 #define ANM_SEC2TM(x) ((anm_time_t)((x) * 1000))
26 #define ANM_MSEC2TM(x) ((anm_time_t)(x))
27 #define ANM_TM2SEC(x) ((x) / 1000.0)
28 #define ANM_TM2MSEC(x) (x)
30 struct anm_keyframe {
31 anm_time_t time;
32 float val;
33 };
35 struct anm_track {
36 char *name;
37 int count;
38 struct anm_keyframe *keys;
40 float def_val;
42 enum anm_interpolator interp;
43 enum anm_extrapolator extrap;
44 };
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
50 /* track constructor and destructor */
51 int anm_init_track(struct anm_track *track);
52 void anm_destroy_track(struct anm_track *track);
54 /* helper functions that use anm_init_track and anm_destroy_track internally */
55 struct anm_track *anm_create_track(void);
56 void anm_free_track(struct anm_track *track);
58 int anm_set_track_name(struct anm_track *track, const char *name);
59 const char *anm_get_track_name(struct anm_track *track);
61 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in);
62 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex);
64 anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end);
66 void anm_set_track_default(struct anm_track *track, float def);
68 /* set or update a keyframe */
69 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key);
71 /* get the idx-th keyframe, returns null if it doesn't exist */
72 struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx);
74 /* Finds the 0-based index of the intra-keyframe interval which corresponds
75 * to the specified time. If the time falls exactly onto the N-th keyframe
76 * the function returns N.
77 *
78 * Special cases:
79 * - if the time is before the first keyframe -1 is returned.
80 * - if the time is after the last keyframe, the index of the last keyframe
81 * is returned.
82 */
83 int anm_get_key_interval(struct anm_track *track, anm_time_t tm);
85 int anm_set_value(struct anm_track *track, anm_time_t tm, float val);
87 /* evaluates and returns the value of the track for a particular time */
88 float anm_get_value(struct anm_track *track, anm_time_t tm);
90 #ifdef __cplusplus
91 }
92 #endif
95 #endif /* LIBANIM_TRACK_H_ */