]> git.tdb.fi Git - libs/demoscene.git/blob - source/animate.cpp
Fix initialization issues in the Animate action
[libs/demoscene.git] / source / animate.cpp
1 #include "animate.h"
2 #include "demo.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace DemoScene {
8
9 Animate::Animate():
10         target(0),
11         target_obj(0),
12         anim(0),
13         stacked(false),
14         player(0)
15 { }
16
17 Animate::Animate(GL::Placeable &t, const GL::Animation &a, GL::AnimationPlayer &p, bool s):
18         target(&t),
19         target_obj(0),
20         anim(&a),
21         stacked(s),
22         player(&p)
23 { }
24
25 void Animate::validate() const
26 {
27         if(!target)
28                 throw logic_error("null target");
29         if(!anim)
30                 throw logic_error("null animation");
31         if(!player)
32                 throw logic_error("null player");
33 }
34
35 void Animate::start(float, float d)
36 {
37         float speed = 1.0f;
38         if(!anim->is_looping() && d)
39                 speed = (anim->get_duration()/Time::sec)/d;
40         if(target_obj)
41         {
42                 if(stacked)
43                         player->play_stacked(*target_obj, *anim, speed);
44                 else
45                         player->play(*target_obj, *anim, speed);
46         }
47         else
48         {
49                 if(stacked)
50                         player->play_stacked(*target, *anim, speed);
51                 else
52                         player->play(*target, *anim, speed);
53         }
54 }
55
56
57 Animate::Loader::Loader(Animate &a, Demo &d):
58         DataFile::DerivedObjectLoader<Animate, Action::Loader>(a, d)
59 {
60         a.player = &demo.get_animation_player();
61         add("animation", &Loader::animation);
62         add("stacked", &Animate::stacked);
63         add("target", &Loader::target);
64 }
65
66 void Animate::Loader::animation(const string &n)
67 {
68         obj.anim = &demo.get_resources().get<GL::Animation>(n);
69 }
70
71 void Animate::Loader::target(const string &n)
72 {
73         obj.target = &demo.get_thing<GL::Placeable>(n);
74         obj.target_obj = dynamic_cast<GL::AnimatedObject *>(obj.target);
75 }
76
77 } // namespace DemoScene
78 } // namespace Msp