]> git.tdb.fi Git - libs/gl.git/blob - source/animationplayer.h
Add support for playing multiple stacked animations on an object
[libs/gl.git] / source / animationplayer.h
1 #ifndef MSP_GL_ANIMATIONPLAYER_H_
2 #define MSP_GL_ANIMATIONPLAYER_H_
3
4 #include <msp/time/timedelta.h>
5 #include "animation.h"
6
7 namespace Msp {
8 namespace GL {
9
10 class AnimatedObject;
11
12 /**
13 The bridge between Animations and AnimatedObjects.  A single AnimationPlayer
14 can handle an arbitrary number of animations simultaneously.
15 */
16 class AnimationPlayer
17 {
18 private:
19         struct AnimationSlot
20         {
21                 const Animation &animation;
22                 Animation::Iterator iterator;
23
24                 AnimationSlot(const Animation &);
25         };
26
27         typedef std::list<AnimationSlot> AnimationList;
28
29         struct ObjectSlot
30         {
31                 AnimatedObject &object;
32                 Matrix base_matrix;
33                 const Armature *armature;
34                 AnimationList animations;
35                 bool stacked;
36
37                 ObjectSlot(AnimatedObject &);
38         };
39
40         typedef std::map<AnimatedObject *, ObjectSlot> ObjectMap;
41
42         ObjectMap objects;
43
44 private:
45         ObjectSlot &get_slot(AnimatedObject &);
46
47 public:
48         /// Plays an animation on an object.  Any previous animations are replaced.
49         void play(AnimatedObject &, const Animation &);
50
51         /** Plays an animation, stacked with other animations.  If no animations are
52         playing yet, the object's current matrix is used as the base. */
53         void play_stacked(AnimatedObject &, const Animation &);
54
55         /// Stops any animations affecting an object.
56         void stop(AnimatedObject &);
57
58         /** Advances all playing animations.  Should be called in a regular manner,
59         preferably just before rendering. */
60         void tick(const Time::TimeDelta &);
61
62 private:
63         bool tick_single(ObjectSlot &, const Time::TimeDelta &);
64         bool tick_stacked(ObjectSlot &, const Time::TimeDelta &);
65 };
66
67 } // namespace GL
68 } // namespace Msp
69
70 #endif