]> git.tdb.fi Git - libs/demoscene.git/blob - source/demo.cpp
Fix initialization issues in the Animate action
[libs/demoscene.git] / source / demo.cpp
1 #include <msp/time/utils.h>
2 #include "demo.h"
3 #include "stage.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace DemoScene {
9
10 Demo::Demo(Graphics::Window &window, Graphics::GLContext &gl_ctx, DataFile::Collection &r):
11         resources(r),
12         view(window, gl_ctx),
13         anim_action(anim_player),
14         music_source(0),
15         streamer(0),
16         music_io(0),
17         music_decoder(0)
18 {
19         things["window"] = static_cast<GL::View *>(&view);
20
21         sequencer.add_static_action(anim_action);
22         sequencer.signal_finished.connect(signal_finished);
23 }
24
25 Demo::~Demo()
26 {
27         delete streamer;
28         delete music_decoder;
29         delete music_io;
30         delete music_source;
31 }
32
33 void Demo::add_stage(const std::string &name, Stage &stage)
34 {
35         things[name] = &stage;
36         stage.add_things(things, name+".");
37 }
38
39 void Demo::set_fixed_framerate(float fps)
40 {
41         frame_interval = Time::sec/fps;
42 }
43
44 void Demo::enable_music()
45 {
46         if(!music_source)
47                 music_source = new AL::Source;
48         if(!streamer)
49                 streamer = new AL::Streamer(*music_source);
50 }
51
52 void Demo::play_music(const string &fn)
53 {
54         if(!streamer)
55                 return;
56
57         streamer->stop();
58         delete music_decoder;
59         delete music_io;
60         music_io = resources.open_raw(fn);
61         music_decoder = AL::SoundDecoder::open_io(*music_io);
62         float beat = sequencer.get_current_beat();
63         if(beat)
64         {
65                 float time = beat*60/sequencer.get_beats_per_minute();
66                 unsigned bytes_per_sec = music_decoder->get_frequency()*get_unit_size(music_decoder->get_format());
67                 music_decoder->seek(time*bytes_per_sec);
68         }
69         streamer->play(*music_decoder);
70 }
71
72 void Demo::tick()
73 {
74         Time::TimeStamp t = Time::now();
75         Time::TimeDelta dt = (last_tick ? frame_interval ? frame_interval : t-last_tick : Time::zero);
76         last_tick = t;
77
78         if(frame_interval)
79                 next_frame = t+frame_interval;
80
81         sequencer.tick(dt);
82         if(streamer)
83                 streamer->tick();
84         view.render();
85 }
86
87 void Demo::seek(const Time::TimeDelta &pos)
88 {
89         sequencer.seek(pos*sequencer.get_beats_per_minute()/Time::min);
90 }
91
92
93 Demo::AnimationAction::AnimationAction(GL::AnimationPlayer &p):
94         player(p)
95 { }
96
97 void Demo::AnimationAction::tick(float, float d)
98 {
99         player.tick(d*Time::sec);
100 }
101
102 } // namespace DemoScene
103 } // namespace Msp