]> git.tdb.fi Git - libs/gl.git/blob - source/animation.h
Basic animation support
[libs/gl.git] / source / animation.h
1 #ifndef MSP_GL_ANIMATION_H_
2 #define MSP_GL_ANIMATION_H_
3
4 #include <msp/core/refptr.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/time/timedelta.h>
7
8 namespace Msp {
9 namespace GL {
10
11 class KeyFrame;
12 class Matrix;
13
14 /**
15 An Animation is a sequence of KeyFrames combined with timing information.  The
16 state at any point in the animation can be interpolated from the keyframes.
17 */
18 class Animation
19 {
20 public:
21         class Loader: public DataFile::CollectionObjectLoader<Animation>
22         {
23         private:
24                 Time::TimeDelta current_time;
25
26         public:
27                 Loader(Animation &);
28                 Loader(Animation &, Collection &);
29         private:
30                 void init();
31
32                 void keyframe(const std::string &);
33                 void keyframe_inline();
34                 void interval(float);
35         };
36
37 private:
38         struct AxisInterpolation
39         {
40                 float slope;
41                 float scale;
42
43                 AxisInterpolation();
44         };
45
46         struct TimedKeyFrame
47         {
48                 const TimedKeyFrame *prev;
49                 Time::TimeDelta time;
50                 Time::TimeDelta delta_t;
51                 RefPtr<const KeyFrame> keyframe;
52                 AxisInterpolation axes[3];
53         };
54
55         typedef std::list<TimedKeyFrame> KeyFrameList;
56
57 public:
58         class Iterator
59         {
60         private:
61                 const Animation &animation;
62                 KeyFrameList::const_iterator iter;
63                 Time::TimeDelta time_since_keyframe;
64                 bool end;
65
66         public:
67                 Iterator(const Animation &);
68
69                 Iterator &operator+=(const Time::TimeDelta &);
70
71                 bool is_end() const { return end; }
72                 Matrix get_matrix() const;
73         };
74
75 private:
76         KeyFrameList keyframes;
77         bool looping;
78
79 public:
80         Animation();
81
82         void add_keyframe(const Time::TimeDelta &, const KeyFrame &);
83         void set_looping(bool);
84 private:
85         void prepare_keyframe(TimedKeyFrame &);
86
87         Matrix compute_matrix(const TimedKeyFrame &, const Time::TimeDelta &) const;
88 };
89
90 } // namespace GL
91 } // namespace Msp
92
93 #endif