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