H3D API  2.4.1
PeriodicUpdate.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __PERIODIC_UPDATE_H__
30 #define __PERIODIC_UPDATE_H__
31 
32 #include <H3D/Scene.h>
33 
34 namespace H3D {
35 
40  class H3DAPI_API PeriodicUpdateField {
41  public:
43  virtual ~PeriodicUpdateField() {}
44 
48  virtual bool timeToUpdate() = 0;
49  };
50 
51 
72  template< class BaseFieldType >
73  class PeriodicUpdate: public BaseFieldType,
74  public PeriodicUpdateField {
75  public:
77  typedef enum {
78  TIME,
79  COUNT
81 
83  PeriodicUpdate( H3DFloat _period = 0, PeriodType _period_type = COUNT ):
84  period_type( _period_type ),
85  period( _period ),
87  counter( 0 ) {
88  this->route( Scene::eventSink );
89  }
90 
93  this->unroute( Scene::eventSink );
94  }
95 
99  virtual bool timeToUpdate() {
100  if( period_type == TIME ) {
101  return (TimeStamp() - last_up_to_date ) >= period;
102  } else {
103  if( counter >= period ) {
104  counter = 0;
105  return true;
106  } else {
107  ++counter;
108  return false;
109  }
110  }
111  }
112 
114  void setPeriodType( PeriodType type ) {
115  period_type = type;
116  }
117 
119  void setPeriod( H3DFloat _period ) {
120  period = _period;
121  }
122 
125  virtual void upToDate() {
126  BaseFieldType::upToDate();
128  }
129 
130  protected:
131 
139  unsigned int counter;
140  };
141 }
142 
143 #endif
Header file for Scene.
Abstract base class for all Fields that will be updated without anyone asking for a value.
Definition: PeriodicUpdate.h:40
virtual bool timeToUpdate()=0
Determines if it is time to check that the field is updated.
virtual ~PeriodicUpdateField()
Destructor.
Definition: PeriodicUpdate.h:43
The PeriodicUpdate is a template modifier that changes the way the field is updated.
Definition: PeriodicUpdate.h:74
void setPeriod(H3DFloat _period)
Set the period for the update.
Definition: PeriodicUpdate.h:119
PeriodicUpdate(H3DFloat _period=0, PeriodType _period_type=COUNT)
Constructor.
Definition: PeriodicUpdate.h:83
PeriodType
Type defining how the period should be interpreted.
Definition: PeriodicUpdate.h:77
@ COUNT
The period is specified in seconds.
Definition: PeriodicUpdate.h:79
~PeriodicUpdate()
Destructor.
Definition: PeriodicUpdate.h:92
TimeStamp last_up_to_date
The time of the last call to upToDate.
Definition: PeriodicUpdate.h:137
PeriodType period_type
The tye of the period.
Definition: PeriodicUpdate.h:133
H3DFloat period
The period of automatic updates/.
Definition: PeriodicUpdate.h:135
unsigned int counter
The number of calls to timeToUpdate() since the last update.
Definition: PeriodicUpdate.h:139
virtual bool timeToUpdate()
Determines if it is time to check that the field is updated.
Definition: PeriodicUpdate.h:99
void setPeriodType(PeriodType type)
Set the type of the period.
Definition: PeriodicUpdate.h:114
virtual void upToDate()
upToDate is specialized to record the time of the call to the function in the last_up_to_date member.
Definition: PeriodicUpdate.h:125
static H3DUniquePtr< EventSink > eventSink
Any field routed to this field will be updated once per frame.
Definition: Scene.h:520
float H3DFloat
H3D API namespace.
Definition: Anchor.h:38