Panda3D
timedCycle.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file timedCycle.I
10  * @author jason
11  * @date 2000-08-01
12  */
13 
14 /**
15  *
16  */
17 INLINE TimedCycle::
18 TimedCycle() :
19  _cycle_time(30),
20  _inv_cycle_time(1./30),
21  _next_switch(-1),
22  _current_child(0),
23  _element_count(0)
24 {
25  _global_clock = ClockObject::get_global_clock();
26 }
27 
28 /**
29  *
30  */
31 INLINE TimedCycle::
32 TimedCycle(PN_stdfloat cycle_time, int element_count) :
33  _cycle_time(cycle_time),
34  _current_child(0),
35  _element_count(element_count)
36 {
37  nassertv(_cycle_time > 0);
38  _global_clock = ClockObject::get_global_clock();
39  _next_switch = _global_clock->get_frame_time() + _cycle_time;
40  _inv_cycle_time = 1. / _cycle_time;
41 }
42 
43 /**
44  * Set the number of elements being cycled through
45  */
46 INLINE void TimedCycle::
47 set_element_count(int element_count)
48 {
49  _element_count = element_count;
50 }
51 
52 /**
53  * Set the number of elements being cycled through
54  */
55 INLINE void TimedCycle::
56 set_cycle_time(PN_stdfloat cycle_time)
57 {
58  nassertv(cycle_time > 0);
59  if (_next_switch == -1)
60  {
61  _next_switch = _global_clock->get_frame_time() + cycle_time;
62  }
63  else
64  {
65  _next_switch = _next_switch - _cycle_time + cycle_time;
66  }
67  _cycle_time = cycle_time;
68  _inv_cycle_time = 1. / _cycle_time;
69 }
70 
71 
72 /**
73  * Set the number of elements being cycled through
74  */
75 INLINE int TimedCycle::
77 {
78  double current_time = _global_clock->get_frame_time();
79  unsigned int increment = (unsigned int) ((current_time - _next_switch)
80  * _inv_cycle_time);
81 
82  _next_switch += _cycle_time * increment;
83  _current_child = (_current_child + increment) % _element_count;
84 
85  return _current_child;
86 }
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition: clockObject.I:215
void set_element_count(int element_count)
Set the number of elements being cycled through.
Definition: timedCycle.I:47
int next_element()
Set the number of elements being cycled through.
Definition: timedCycle.I:76
get_frame_time
Returns the time in seconds as of the last time tick() was called (typically, this will be as of the ...
Definition: clockObject.h:91
void set_cycle_time(PN_stdfloat cycle_time)
Set the number of elements being cycled through.
Definition: timedCycle.I:56