]> git.tdb.fi Git - libs/gl.git/blob - source/animationplayer.h
Provide a getter for the number of active animations
[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<const 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         /// Returns the number of animations currently affecting an object.
56         unsigned get_n_active_animations(const AnimatedObject &) const;
57
58         /// Stops any animations affecting an object.
59         void stop(AnimatedObject &);
60
61         /** Advances all playing animations.  Should be called in a regular manner,
62         preferably just before rendering. */
63         void tick(const Time::TimeDelta &);
64
65 private:
66         bool tick_single(ObjectSlot &, const Time::TimeDelta &);
67         bool tick_stacked(ObjectSlot &, const Time::TimeDelta &);
68 };
69
70 } // namespace GL
71 } // namespace Msp
72
73 #endif