Panda3D
 All Classes Functions Variables Enumerations
cMetaInterval.I
00001 // Filename: cMetaInterval.I
00002 // Created by:  drose (27Aug02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: CMetaInterval::set_precision
00018 //       Access: Published
00019 //  Description: Indicates the precision with which time measurements
00020 //               are compared.  For numerical accuracy, all
00021 //               floating-point time values are converted to integer
00022 //               values internally by scaling by the precision factor.
00023 //               The larger the number given here, the smaller the
00024 //               delta of time that can be differentiated; the
00025 //               limit is the maximum integer that can be represented
00026 //               in the system.
00027 ////////////////////////////////////////////////////////////////////
00028 INLINE void CMetaInterval::
00029 set_precision(double precision) {
00030   _precision = precision;
00031   mark_dirty();
00032 }
00033 
00034 ////////////////////////////////////////////////////////////////////
00035 //     Function: CMetaInterval::get_precision
00036 //       Access: Published
00037 //  Description: Returns the precision with which time measurements
00038 //               are compared.  See set_precision().
00039 ////////////////////////////////////////////////////////////////////
00040 INLINE double CMetaInterval::
00041 get_precision() const {
00042   return _precision;
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: CMetaInterval::get_num_defs
00047 //       Access: Published
00048 //  Description: Returns the number of interval and push/pop
00049 //               definitions that have been added to the meta
00050 //               interval.
00051 ////////////////////////////////////////////////////////////////////
00052 INLINE int CMetaInterval::
00053 get_num_defs() const {
00054   return (int)_defs.size();
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: CMetaInterval::get_def_type
00059 //       Access: Published
00060 //  Description: Returns the type of the nth interval definition that
00061 //               has been added.
00062 ////////////////////////////////////////////////////////////////////
00063 INLINE CMetaInterval::DefType CMetaInterval::
00064 get_def_type(int n) const {
00065   nassertr(n >= 0 && n < (int)_defs.size(), DT_c_interval);
00066   return _defs[n]._type;
00067 }
00068 
00069 ////////////////////////////////////////////////////////////////////
00070 //     Function: CMetaInterval::get_c_interval
00071 //       Access: Published
00072 //  Description: Return the CInterval pointer associated with the nth
00073 //               interval definition.  It is only valid to call this
00074 //               if get_def_type(n) returns DT_c_interval.
00075 ////////////////////////////////////////////////////////////////////
00076 INLINE CInterval *CMetaInterval::
00077 get_c_interval(int n) const {
00078   nassertr(n >= 0 && n < (int)_defs.size(), NULL);
00079   nassertr(_defs[n]._type == DT_c_interval, NULL);
00080   return _defs[n]._c_interval;
00081 }
00082 
00083 ////////////////////////////////////////////////////////////////////
00084 //     Function: CMetaInterval::get_ext_index
00085 //       Access: Published
00086 //  Description: Return the external interval index number associated
00087 //               with the nth interval definition.  It is only valid
00088 //               to call this if get_def_type(n) returns DT_ext_index.
00089 ////////////////////////////////////////////////////////////////////
00090 INLINE int CMetaInterval::
00091 get_ext_index(int n) const {
00092   nassertr(n >= 0 && n < (int)_defs.size(), -1);
00093   nassertr(_defs[n]._type == DT_ext_index, -1);
00094   return _defs[n]._ext_index;
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function: CMetaInterval::is_event_ready
00099 //       Access: Published
00100 //  Description: Returns true if a recent call to priv_initialize(),
00101 //               priv_step(), or priv_finalize() has left some external
00102 //               intervals ready to play.  If this returns true, call
00103 //               get_event_index(), get_event_t(), and pop_event() to
00104 //               retrieve the relevant information.
00105 ////////////////////////////////////////////////////////////////////
00106 INLINE bool CMetaInterval::
00107 is_event_ready() {
00108   return service_event_queue();
00109 }
00110 
00111 ////////////////////////////////////////////////////////////////////
00112 //     Function: CMetaInterval::get_event_index
00113 //       Access: Published
00114 //  Description: If a previous call to is_event_ready() returned
00115 //               true, this returns the index number (added via
00116 //               add_event_index()) of the external interval that needs
00117 //               to be played.
00118 ////////////////////////////////////////////////////////////////////
00119 INLINE int CMetaInterval::
00120 get_event_index() const {
00121   nassertr(!_event_queue.empty(), -1);
00122   const EventQueueEntry &entry = _event_queue.front();
00123   const IntervalDef &def = _defs[entry._n];
00124   nassertr(def._type == DT_ext_index, -1);
00125   return def._ext_index;
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: CMetaInterval::get_event_t
00130 //       Access: Published
00131 //  Description: If a previous call to is_event_ready() returned
00132 //               true, this returns the t value that should be fed to
00133 //               the given interval.
00134 ////////////////////////////////////////////////////////////////////
00135 INLINE double CMetaInterval::
00136 get_event_t() const {
00137   nassertr(!_event_queue.empty(), 0.0f);
00138   return int_to_double_time(_event_queue.front()._time);
00139 }
00140 
00141 ////////////////////////////////////////////////////////////////////
00142 //     Function: CMetaInterval::get_event_type
00143 //       Access: Published
00144 //  Description: If a previous call to is_event_ready() returned
00145 //               true, this returns the type of the event (initialize,
00146 //               step, finalize, etc.) for the given interval.
00147 ////////////////////////////////////////////////////////////////////
00148 INLINE CInterval::EventType CMetaInterval::
00149 get_event_type() const {
00150   nassertr(!_event_queue.empty(), ET_step);
00151   return _event_queue.front()._event_type;
00152 }
00153 
00154 ////////////////////////////////////////////////////////////////////
00155 //     Function: CMetaInterval::double_to_int_time
00156 //       Access: Private
00157 //  Description: Converts from an external double time value or offset
00158 //               in seconds to an internal integer value or offset.
00159 ////////////////////////////////////////////////////////////////////
00160 INLINE int CMetaInterval::
00161 double_to_int_time(double t) const {
00162   // Use floor() just in case there are negative values involved.
00163   return (int)floor(t * _precision + 0.5);
00164 }
00165 
00166 ////////////////////////////////////////////////////////////////////
00167 //     Function: CMetaInterval::int_to_double_time
00168 //       Access: Private
00169 //  Description: Converts from an internal integer time value or
00170 //               offset to an external double time value or offset in
00171 //               seconds.
00172 ////////////////////////////////////////////////////////////////////
00173 INLINE double CMetaInterval::
00174 int_to_double_time(int time) const {
00175   return (double)time / _precision;
00176 }
00177 
00178 ////////////////////////////////////////////////////////////////////
00179 //     Function: CMetaInterval::PlaybackEvent::Constructor
00180 //       Access: Public
00181 //  Description: 
00182 ////////////////////////////////////////////////////////////////////
00183 INLINE CMetaInterval::PlaybackEvent::
00184 PlaybackEvent(int time, int n, 
00185               CMetaInterval::PlaybackEventType type) :
00186   _time(time),
00187   _n(n),
00188   _type(type)
00189 {
00190   _begin_event = this;
00191 }
00192 
00193 ////////////////////////////////////////////////////////////////////
00194 //     Function: CMetaInterval::PlaybackEvent::Ordering operator
00195 //       Access: Public
00196 //  Description: 
00197 ////////////////////////////////////////////////////////////////////
00198 INLINE bool CMetaInterval::PlaybackEvent::
00199 operator < (const CMetaInterval::PlaybackEvent &other) const {
00200   return _time < other._time;
00201 }
00202 
00203 ////////////////////////////////////////////////////////////////////
00204 //     Function: CMetaInterval::EventQueueEntry::Constructor
00205 //       Access: Public
00206 //  Description: 
00207 ////////////////////////////////////////////////////////////////////
00208 INLINE CMetaInterval::EventQueueEntry::
00209 EventQueueEntry(int n, CInterval::EventType event_type, int time) :
00210   _n(n),
00211   _event_type(event_type),
00212   _time(time)
00213 {
00214 }
 All Classes Functions Variables Enumerations