00001 // Filename: clockObject.cxx 00002 // Created by: drose (17Feb00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 00020 #include "clockObject.h" 00021 #include "config_express.h" 00022 00023 ClockObject *ClockObject::_global_clock = (ClockObject *)NULL; 00024 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: ClockObject::Constructor 00028 // Access: Published 00029 // Description: 00030 //////////////////////////////////////////////////////////////////// 00031 ClockObject:: 00032 ClockObject() { 00033 _true_clock = TrueClock::get_ptr(); 00034 _mode = M_normal; 00035 _start_short_time = _true_clock->get_short_time(); 00036 _start_long_time = _true_clock->get_long_time(); 00037 _frame_count = 0; 00038 _actual_frame_time = 0.0; 00039 _reported_frame_time = 0.0; 00040 _dt = 0.0; 00041 _max_dt = -1.0; 00042 } 00043 00044 //////////////////////////////////////////////////////////////////// 00045 // Function: ClockObject::set_real_time 00046 // Access: Published 00047 // Description: Resets the clock to the indicated time. This 00048 // changes only the real time of the clock as reported 00049 // by get_real_time(), but does not immediately change 00050 // the time reported by get_frame_time()--that will 00051 // change after the next call to tick(). Also see 00052 // reset(), set_frame_time(), and set_frame_count(). 00053 //////////////////////////////////////////////////////////////////// 00054 void ClockObject:: 00055 set_real_time(double time) { 00056 #ifdef NOTIFY_DEBUG 00057 if (this == _global_clock) { 00058 express_cat.warning() 00059 << "Adjusting global clock's real time by " << time - get_real_time() 00060 << " seconds.\n"; 00061 } 00062 #endif // NOTIFY_DEBUG 00063 _start_short_time = _true_clock->get_short_time() - time; 00064 _start_long_time = _true_clock->get_long_time() - time; 00065 } 00066 00067 //////////////////////////////////////////////////////////////////// 00068 // Function: ClockObject::set_frame_time 00069 // Access: Published 00070 // Description: Changes the time as reported for the current frame to 00071 // the indicated time. Normally, the way to adjust the 00072 // frame time is via tick(); this function is provided 00073 // only for occasional special adjustments. 00074 //////////////////////////////////////////////////////////////////// 00075 void ClockObject:: 00076 set_frame_time(double time) { 00077 #ifdef NOTIFY_DEBUG 00078 if (this == _global_clock) { 00079 express_cat.warning() 00080 << "Adjusting global clock's frame time by " << time - get_frame_time() 00081 << " seconds.\n"; 00082 } 00083 #endif // NOTIFY_DEBUG 00084 _actual_frame_time = time; 00085 _reported_frame_time = time; 00086 } 00087 00088 //////////////////////////////////////////////////////////////////// 00089 // Function: ClockObject::set_frame_count 00090 // Access: Published 00091 // Description: Resets the number of frames counted to the indicated 00092 // number. Also see reset(), set_real_time(), and 00093 // set_frame_time(). 00094 //////////////////////////////////////////////////////////////////// 00095 void ClockObject:: 00096 set_frame_count(int frame_count) { 00097 #ifdef NOTIFY_DEBUG 00098 if (this == _global_clock) { 00099 express_cat.warning() 00100 << "Adjusting global clock's frame count by " 00101 << frame_count - get_frame_count() << " frames.\n"; 00102 } 00103 #endif // NOTIFY_DEBUG 00104 _frame_count = frame_count; 00105 } 00106 00107 //////////////////////////////////////////////////////////////////// 00108 // Function: ClockObject::tick 00109 // Access: Published 00110 // Description: Instructs the clock that a new frame has just begun. 00111 // In normal, real-time mode, get_frame_time() will 00112 // henceforth report the time as of this instant as the 00113 // current start-of-frame time. In non-real-time mode, 00114 // get_frame_time() will be incremented by the value of 00115 // dt. 00116 //////////////////////////////////////////////////////////////////// 00117 void ClockObject:: 00118 tick() { 00119 double old_time = _actual_frame_time; 00120 _actual_frame_time = get_real_time(); 00121 00122 switch (_mode) { 00123 case M_normal: 00124 _dt = _actual_frame_time - old_time; 00125 if (_max_dt > 0.0) { 00126 _dt = min(_max_dt, _dt); 00127 } 00128 _reported_frame_time = _actual_frame_time; 00129 break; 00130 00131 case M_non_real_time: 00132 _reported_frame_time += _dt; 00133 break; 00134 } 00135 00136 _frame_count++; 00137 } 00138 00139 //////////////////////////////////////////////////////////////////// 00140 // Function: ClockObject::sync_frame_time 00141 // Access: Published 00142 // Description: Resets the frame time to the current real time. This 00143 // is similar to tick(), except that it does not advance 00144 // the frame counter and does not affect dt. This is 00145 // intended to be used in the middle of a particularly 00146 // long frame to compensate for the time that has 00147 // already elapsed. 00148 // 00149 // In non-real-time mode, this function has no effect 00150 // (because in this mode all frames take the same length 00151 // of time). 00152 //////////////////////////////////////////////////////////////////// 00153 void ClockObject:: 00154 sync_frame_time() { 00155 if (_mode == M_normal) { 00156 _reported_frame_time = get_real_time(); 00157 } 00158 } 00159 00160 //////////////////////////////////////////////////////////////////// 00161 // Function: get_time_of_day 00162 // Description: 00163 //////////////////////////////////////////////////////////////////// 00164 void get_time_of_day(TimeVal &tv) { 00165 get_true_time_of_day(tv.tv[0], tv.tv[1]); 00166 }