Panda3D
cMetaInterval.I
1 // Filename: cMetaInterval.I
2 // Created by: drose (27Aug02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: CMetaInterval::set_precision
18 // Access: Published
19 // Description: Indicates the precision with which time measurements
20 // are compared. For numerical accuracy, all
21 // floating-point time values are converted to integer
22 // values internally by scaling by the precision factor.
23 // The larger the number given here, the smaller the
24 // delta of time that can be differentiated; the
25 // limit is the maximum integer that can be represented
26 // in the system.
27 ////////////////////////////////////////////////////////////////////
28 INLINE void CMetaInterval::
29 set_precision(double precision) {
30  _precision = precision;
31  mark_dirty();
32 }
33 
34 ////////////////////////////////////////////////////////////////////
35 // Function: CMetaInterval::get_precision
36 // Access: Published
37 // Description: Returns the precision with which time measurements
38 // are compared. See set_precision().
39 ////////////////////////////////////////////////////////////////////
40 INLINE double CMetaInterval::
41 get_precision() const {
42  return _precision;
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: CMetaInterval::get_num_defs
47 // Access: Published
48 // Description: Returns the number of interval and push/pop
49 // definitions that have been added to the meta
50 // interval.
51 ////////////////////////////////////////////////////////////////////
52 INLINE int CMetaInterval::
53 get_num_defs() const {
54  return (int)_defs.size();
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: CMetaInterval::get_def_type
59 // Access: Published
60 // Description: Returns the type of the nth interval definition that
61 // has been added.
62 ////////////////////////////////////////////////////////////////////
63 INLINE CMetaInterval::DefType CMetaInterval::
64 get_def_type(int n) const {
65  nassertr(n >= 0 && n < (int)_defs.size(), DT_c_interval);
66  return _defs[n]._type;
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function: CMetaInterval::get_c_interval
71 // Access: Published
72 // Description: Return the CInterval pointer associated with the nth
73 // interval definition. It is only valid to call this
74 // if get_def_type(n) returns DT_c_interval.
75 ////////////////////////////////////////////////////////////////////
77 get_c_interval(int n) const {
78  nassertr(n >= 0 && n < (int)_defs.size(), NULL);
79  nassertr(_defs[n]._type == DT_c_interval, NULL);
80  return _defs[n]._c_interval;
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: CMetaInterval::get_ext_index
85 // Access: Published
86 // Description: Return the external interval index number associated
87 // with the nth interval definition. It is only valid
88 // to call this if get_def_type(n) returns DT_ext_index.
89 ////////////////////////////////////////////////////////////////////
90 INLINE int CMetaInterval::
91 get_ext_index(int n) const {
92  nassertr(n >= 0 && n < (int)_defs.size(), -1);
93  nassertr(_defs[n]._type == DT_ext_index, -1);
94  return _defs[n]._ext_index;
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: CMetaInterval::is_event_ready
99 // Access: Published
100 // Description: Returns true if a recent call to priv_initialize(),
101 // priv_step(), or priv_finalize() has left some external
102 // intervals ready to play. If this returns true, call
103 // get_event_index(), get_event_t(), and pop_event() to
104 // retrieve the relevant information.
105 ////////////////////////////////////////////////////////////////////
106 INLINE bool CMetaInterval::
108  return service_event_queue();
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: CMetaInterval::get_event_index
113 // Access: Published
114 // Description: If a previous call to is_event_ready() returned
115 // true, this returns the index number (added via
116 // add_event_index()) of the external interval that needs
117 // to be played.
118 ////////////////////////////////////////////////////////////////////
119 INLINE int CMetaInterval::
121  nassertr(!_event_queue.empty(), -1);
122  const EventQueueEntry &entry = _event_queue.front();
123  const IntervalDef &def = _defs[entry._n];
124  nassertr(def._type == DT_ext_index, -1);
125  return def._ext_index;
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: CMetaInterval::get_event_t
130 // Access: Published
131 // Description: If a previous call to is_event_ready() returned
132 // true, this returns the t value that should be fed to
133 // the given interval.
134 ////////////////////////////////////////////////////////////////////
135 INLINE double CMetaInterval::
136 get_event_t() const {
137  nassertr(!_event_queue.empty(), 0.0f);
138  return int_to_double_time(_event_queue.front()._time);
139 }
140 
141 ////////////////////////////////////////////////////////////////////
142 // Function: CMetaInterval::get_event_type
143 // Access: Published
144 // Description: If a previous call to is_event_ready() returned
145 // true, this returns the type of the event (initialize,
146 // step, finalize, etc.) for the given interval.
147 ////////////////////////////////////////////////////////////////////
148 INLINE CInterval::EventType CMetaInterval::
149 get_event_type() const {
150  nassertr(!_event_queue.empty(), ET_step);
151  return _event_queue.front()._event_type;
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: CMetaInterval::double_to_int_time
156 // Access: Private
157 // Description: Converts from an external double time value or offset
158 // in seconds to an internal integer value or offset.
159 ////////////////////////////////////////////////////////////////////
160 INLINE int CMetaInterval::
161 double_to_int_time(double t) const {
162  // Use floor() just in case there are negative values involved.
163  return (int)floor(t * _precision + 0.5);
164 }
165 
166 ////////////////////////////////////////////////////////////////////
167 // Function: CMetaInterval::int_to_double_time
168 // Access: Private
169 // Description: Converts from an internal integer time value or
170 // offset to an external double time value or offset in
171 // seconds.
172 ////////////////////////////////////////////////////////////////////
173 INLINE double CMetaInterval::
174 int_to_double_time(int time) const {
175  return (double)time / _precision;
176 }
177 
178 ////////////////////////////////////////////////////////////////////
179 // Function: CMetaInterval::PlaybackEvent::Constructor
180 // Access: Public
181 // Description:
182 ////////////////////////////////////////////////////////////////////
183 INLINE CMetaInterval::PlaybackEvent::
184 PlaybackEvent(int time, int n,
185  CMetaInterval::PlaybackEventType type) :
186  _time(time),
187  _n(n),
188  _type(type)
189 {
190  _begin_event = this;
191 }
192 
193 ////////////////////////////////////////////////////////////////////
194 // Function: CMetaInterval::PlaybackEvent::Ordering operator
195 // Access: Public
196 // Description:
197 ////////////////////////////////////////////////////////////////////
198 INLINE bool CMetaInterval::PlaybackEvent::
199 operator < (const CMetaInterval::PlaybackEvent &other) const {
200  return _time < other._time;
201 }
202 
203 ////////////////////////////////////////////////////////////////////
204 // Function: CMetaInterval::EventQueueEntry::Constructor
205 // Access: Public
206 // Description:
207 ////////////////////////////////////////////////////////////////////
208 INLINE CMetaInterval::EventQueueEntry::
209 EventQueueEntry(int n, CInterval::EventType event_type, int time) :
210  _n(n),
211  _event_type(event_type),
212  _time(time)
213 {
214 }
DefType get_def_type(int n) const
Returns the type of the nth interval definition that has been added.
Definition: cMetaInterval.I:64
void set_precision(double precision)
Indicates the precision with which time measurements are compared.
Definition: cMetaInterval.I:29
int get_num_defs() const
Returns the number of interval and push/pop definitions that have been added to the meta interval...
Definition: cMetaInterval.I:53
The base class for timeline components.
Definition: cInterval.h:39
EventType get_event_type() const
If a previous call to is_event_ready() returned true, this returns the type of the event (initialize...
int get_ext_index(int n) const
Return the external interval index number associated with the nth interval definition.
Definition: cMetaInterval.I:91
CInterval * get_c_interval(int n) const
Return the CInterval pointer associated with the nth interval definition.
Definition: cMetaInterval.I:77
bool is_event_ready()
Returns true if a recent call to priv_initialize(), priv_step(), or priv_finalize() has left some ext...
int get_event_index() const
If a previous call to is_event_ready() returned true, this returns the index number (added via add_ev...
double get_precision() const
Returns the precision with which time measurements are compared.
Definition: cMetaInterval.I:41
double get_event_t() const
If a previous call to is_event_ready() returned true, this returns the t value that should be fed to ...
void mark_dirty()
Called by a derived class to indicate the interval has been changed internally and must be recomputed...
Definition: cInterval.cxx:746