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