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