Panda3D
|
00001 // Filename: timedCycle.I 00002 // Created by: jason (01Aug00) 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 // Function: TimedCycle::Constructor 00017 // Access: Public 00018 // Description: 00019 //////////////////////////////////////////////////////////////////// 00020 INLINE TimedCycle:: 00021 TimedCycle() : 00022 _cycle_time(30), 00023 _inv_cycle_time(1./30), 00024 _next_switch(-1), 00025 _current_child(0), 00026 _element_count(0) 00027 { 00028 _global_clock = ClockObject::get_global_clock(); 00029 } 00030 00031 //////////////////////////////////////////////////////////////////// 00032 // Function: TimedCycle::Constructor 00033 // Access: Public 00034 // Description: 00035 //////////////////////////////////////////////////////////////////// 00036 INLINE TimedCycle:: 00037 TimedCycle(PN_stdfloat cycle_time, int element_count) : 00038 _cycle_time(cycle_time), 00039 _current_child(0), 00040 _element_count(element_count) 00041 { 00042 nassertv(_cycle_time > 0); 00043 _global_clock = ClockObject::get_global_clock(); 00044 _next_switch = _global_clock->get_frame_time() + _cycle_time; 00045 _inv_cycle_time = 1. / _cycle_time; 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: TimedCycle::set_element_count 00050 // Access: Public 00051 // Description: Set the number of elements being cycled through 00052 //////////////////////////////////////////////////////////////////// 00053 INLINE void TimedCycle:: 00054 set_element_count(int element_count) 00055 { 00056 _element_count = element_count; 00057 } 00058 00059 //////////////////////////////////////////////////////////////////// 00060 // Function: TimedCycle::set_cycle_time 00061 // Access: Public 00062 // Description: Set the number of elements being cycled through 00063 //////////////////////////////////////////////////////////////////// 00064 INLINE void TimedCycle:: 00065 set_cycle_time(PN_stdfloat cycle_time) 00066 { 00067 nassertv(cycle_time > 0); 00068 if (_next_switch == -1) 00069 { 00070 _next_switch = _global_clock->get_frame_time() + cycle_time; 00071 } 00072 else 00073 { 00074 _next_switch = _next_switch - _cycle_time + cycle_time; 00075 } 00076 _cycle_time = cycle_time; 00077 _inv_cycle_time = 1. / _cycle_time; 00078 } 00079 00080 00081 //////////////////////////////////////////////////////////////////// 00082 // Function: TimedCycle::next_element 00083 // Access: Public 00084 // Description: Set the number of elements being cycled through 00085 //////////////////////////////////////////////////////////////////// 00086 INLINE int TimedCycle:: 00087 next_element() 00088 { 00089 double current_time = _global_clock->get_frame_time(); 00090 unsigned int increment = (unsigned int) ((current_time - _next_switch) 00091 * _inv_cycle_time); 00092 00093 _next_switch += _cycle_time * increment; 00094 _current_child = (_current_child + increment) % _element_count; 00095 00096 return _current_child; 00097 } 00098