]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/clock.cpp
286ce966873af6b6735398ba1e5908f879460eda
[r2c2.git] / source / libr2c2 / clock.cpp
1 #include "clock.h"
2
3 using namespace std;
4 using namespace Msp;
5
6 namespace R2C2 {
7
8 Clock::Clock():
9         rate(1),
10         stopped(false)
11 { }
12
13 void Clock::set_rate(float s)
14 {
15         rate = s;
16 }
17
18 void Clock::set_current_time(const Time::TimeDelta &t)
19 {
20         current_time = t;
21 }
22
23 void Clock::stop(bool s)
24 {
25         stopped = s;
26 }
27
28 void Clock::tick(const Time::TimeDelta &dt)
29 {
30         if(stopped)
31                 return;
32
33         unsigned prev_minute = current_time/Time::min;
34         current_time += dt*rate;
35         if(current_time>=Time::day)
36                 current_time -= Time::day;
37         unsigned minute = current_time/Time::min;
38         if(minute!=prev_minute)
39                 signal_minute.emit();
40 }
41
42 void Clock::save(list<DataFile::Statement> &st) const
43 {
44         st.push_back((DataFile::Statement("rate"), rate));
45         st.push_back((DataFile::Statement("time"), current_time.raw()));
46 }
47
48
49 Clock::Loader::Loader(Clock &c):
50         DataFile::ObjectLoader<Clock>(c)
51 {
52         add("rate", &Clock::rate);
53         add("time", &Loader::time);
54 }
55
56 void Clock::Loader::time(Time::RawTime t)
57 {
58         obj.set_current_time(Time::TimeDelta(t));
59 }
60
61 } // namespace R2C2