~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/environment/caelum/src/UniversalClock.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This file is part of Caelum.
 
3
See http://www.ogre3d.org/wiki/index.php/Caelum 
 
4
 
 
5
Copyright (c) 2006-2008 Caelum team. See Contributors.txt for details.
 
6
 
 
7
Caelum is free software: you can redistribute it and/or modify
 
8
it under the terms of the GNU Lesser General Public License as published
 
9
by the Free Software Foundation, either version 3 of the License, or
 
10
(at your option) any later version.
 
11
 
 
12
Caelum is distributed in the hope that it will be useful,
 
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
GNU Lesser General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU Lesser General Public License
 
18
along with Caelum. If not, see <http://www.gnu.org/licenses/>.
 
19
*/
 
20
 
 
21
#include "CaelumPrecompiled.h"
 
22
#include "UniversalClock.h"
 
23
#include "Astronomy.h"
 
24
 
 
25
namespace caelum {
 
26
 
 
27
const caelum::LongReal UniversalClock::SECONDS_PER_DAY = 86400.0;
 
28
 
 
29
UniversalClock::UniversalClock () {
 
30
    setJulianDay (Astronomy::J2000);        
 
31
        setTimeScale (1.0);
 
32
        setUpdateRate (0);
 
33
    // The above call does forceUpdate but let's be explicit for clarity.
 
34
    forceUpdate ();
 
35
}
 
36
 
 
37
void UniversalClock::setJulianDay (caelum::LongReal value) {
 
38
    mJulianDayBase = value;
 
39
    mCurrentTime = 0;
 
40
    mLastUpdateTime = 0;
 
41
}
 
42
 
 
43
void UniversalClock::setGregorianDateTime(
 
44
        int year, int month, int day,
 
45
        int hour, int minute, double second)
 
46
{
 
47
    int fpmode = Astronomy::enterHighPrecissionFloatingPointMode ();
 
48
    setJulianDay(Astronomy::getJulianDayFromGregorianDateTime(year, month, day, hour, minute, second));
 
49
    Astronomy::restoreFloatingPointMode(fpmode);
 
50
}
 
51
 
 
52
caelum::LongReal UniversalClock::getJulianDay () const
 
53
{
 
54
    int fpmode = Astronomy::enterHighPrecissionFloatingPointMode ();
 
55
    caelum::LongReal res = mJulianDayBase + mCurrentTime / SECONDS_PER_DAY;
 
56
    Astronomy::restoreFloatingPointMode(fpmode);
 
57
    return res;
 
58
}
 
59
 
 
60
caelum::LongReal UniversalClock::getJulianDayDifference () const {
 
61
    return (mCurrentTime - mLastUpdateTime) / SECONDS_PER_DAY;
 
62
}
 
63
 
 
64
caelum::LongReal UniversalClock::getJulianSecond () const {
 
65
    int fpmode = Astronomy::enterHighPrecissionFloatingPointMode ();
 
66
    caelum::LongReal res = mJulianDayBase * SECONDS_PER_DAY + mCurrentTime;
 
67
    Astronomy::restoreFloatingPointMode(fpmode);
 
68
    return res;
 
69
}
 
70
 
 
71
caelum::LongReal UniversalClock::getJulianSecondDifference () const {
 
72
    return mCurrentTime - mLastUpdateTime;
 
73
}
 
74
 
 
75
void UniversalClock::setTimeScale (const Ogre::Real scale) {
 
76
        mTimeScale = scale;
 
77
}
 
78
 
 
79
Ogre::Real UniversalClock::getTimeScale () const {
 
80
        return mTimeScale;
 
81
}
 
82
 
 
83
void UniversalClock::setUpdateRate (const Ogre::Real rate) {
 
84
        mUpdateRate = rate;
 
85
 
 
86
    if (mUpdateRate < 0) {
 
87
                mUpdateRate = 0;
 
88
    }
 
89
 
 
90
    forceUpdate();
 
91
}
 
92
 
 
93
Ogre::Real UniversalClock::getUpdateRate () const {
 
94
        return mUpdateRate;
 
95
}
 
96
 
 
97
bool UniversalClock::update (const Ogre::Real time) {
 
98
        mTimeSinceLastUpdate += time;
 
99
 
 
100
        if (mTimeSinceLastUpdate > mUpdateRate) {
 
101
        mLastUpdateTime = mCurrentTime;
 
102
        mCurrentTime += mTimeSinceLastUpdate * mTimeScale;
 
103
        mTimeSinceLastUpdate = 0;
 
104
                return true;
 
105
        } else {
 
106
                return false;
 
107
        }
 
108
}
 
109
 
 
110
void UniversalClock::forceUpdate () {
 
111
    mTimeSinceLastUpdate = mUpdateRate;
 
112
}
 
113
 
 
114
} // namespace caelum