]> git.tdb.fi Git - libs/gl.git/blob - source/animationplayer.cpp
Provide a getter for the number of active animations
[libs/gl.git] / source / animationplayer.cpp
1 #include "animatedobject.h"
2 #include "animationplayer.h"
3 #include "armature.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 AnimationPlayer::ObjectSlot &AnimationPlayer::get_slot(AnimatedObject &obj)
11 {
12         ObjectMap::iterator i = objects.find(&obj);
13         if(i!=objects.end())
14                 return i->second;
15
16         return objects.insert(ObjectMap::value_type(&obj, ObjectSlot(obj))).first->second;
17 }
18
19 void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim)
20 {
21         ObjectSlot &obj_slot = get_slot(obj);
22         obj_slot.animations.clear();
23         obj_slot.base_matrix = Matrix();
24         obj_slot.stacked = false;
25         obj_slot.armature = anim.get_armature();
26         obj_slot.animations.push_back(AnimationSlot(anim));
27 }
28
29 void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim)
30 {
31         ObjectSlot &obj_slot = get_slot(obj);
32         if(obj_slot.animations.empty())
33                 obj_slot.base_matrix = *obj.get_matrix();
34         // TODO check for incompatible armature
35         obj_slot.stacked = true;
36         obj_slot.armature = anim.get_armature();
37         obj_slot.animations.push_back(AnimationSlot(anim));
38 }
39
40 unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const
41 {
42         ObjectMap::const_iterator i = objects.find(&obj);
43         return (i!=objects.end() ? i->second.animations.size() : 0);
44 }
45
46 void AnimationPlayer::stop(AnimatedObject &obj)
47 {
48         objects.erase(&obj);
49 }
50
51 void AnimationPlayer::tick(const Time::TimeDelta &dt)
52 {
53         for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); )
54         {
55                 bool keep = false;
56                 if(i->second.stacked)
57                         keep = tick_stacked(i->second, dt);
58                 else
59                         keep = tick_single(i->second, dt);
60
61                 if(!keep)
62                         objects.erase(i++);
63                 else
64                         ++i;
65         }
66 }
67
68 bool AnimationPlayer::tick_single(ObjectSlot &slot, const Time::TimeDelta &dt)
69 {
70         AnimatedObject &obj = slot.object;
71         AnimationSlot &anim = slot.animations.front();
72         anim.iterator += dt;
73         obj.set_matrix(anim.iterator.get_matrix());
74         if(slot.armature)
75         {
76                 unsigned max_index = slot.armature->get_max_link_index();
77                 for(unsigned i=0; i<=max_index; ++i)
78                         obj.set_pose_matrix(i, anim.iterator.get_pose_matrix(i));
79         }
80
81         return !anim.iterator.is_end();
82 }
83
84 bool AnimationPlayer::tick_stacked(ObjectSlot &slot, const Time::TimeDelta &dt)
85 {
86         Matrix matrix = slot.base_matrix;
87         for(AnimationList::iterator i=slot.animations.begin(); i!=slot.animations.end(); ++i)
88         {
89                 i->iterator += dt;
90                 matrix *= i->iterator.get_matrix();
91         }
92         slot.object.set_matrix(matrix);
93
94         if(slot.armature)
95         {
96                 unsigned max_index = slot.armature->get_max_link_index();
97                 for(unsigned i=0; i<=max_index; ++i)
98                 {
99                         matrix = Matrix();
100                         /* XXX This is in all likelihood incorrect.  The stacking should be
101                         performed on local matrices. */
102                         for(AnimationList::iterator j=slot.animations.begin(); j!=slot.animations.end(); ++j)
103                                 if(j->animation.get_armature())
104                                         matrix *= j->iterator.get_pose_matrix(i);
105                         slot.object.set_pose_matrix(i, matrix);
106                 }
107         }
108
109         for(AnimationList::iterator i=slot.animations.begin(); i!=slot.animations.end(); )
110         {
111                 if(i->iterator.is_end())
112                         slot.animations.erase(i++);
113                 else
114                         ++i;
115         }
116
117         return !slot.animations.empty();
118 }
119
120
121 AnimationPlayer::ObjectSlot::ObjectSlot(AnimatedObject &o):
122         object(o),
123         armature(0),
124         stacked(false)
125 { }
126
127
128 AnimationPlayer::AnimationSlot::AnimationSlot(const Animation &a):
129         animation(a),
130         iterator(animation)
131 { }
132
133 } // namespace GL
134 } // namespace Msp