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