JNR
laTimeTrig.h
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 #ifndef M_TIME_TRIG_H
30 #define M_TIME_TRIG_H
31 
33 
34 
40 
42 {
43 protected:
44  // Parameters of the time-trigger
45  //
46  double _dElapse, _dHold;
47  M_BOOL _bCyclic;
48 
49  // Current status
50  //
51  M_BOOL _bEnabled, _bEvent, _bFired;
52  double _nTimeLeft;
53 
54  // Event callback
55  //
56  void (*_pEventHandler)(void*);
57  void *_pEventObject;
58 
59 public:
60  // Constructor/destructor
61  //
62  laTimeTrig(void);
63  //~laTimeTrig(void);
64 
65  // Control methods
66  //
67  inline void enable(M_BOOL bEnable=M_TRUE) { _bEnabled = bEnable; }
68 
69  inline void force() {
70  _bFired = _bEvent = M_TRUE;
71  };
72 
73  inline void reset() {
74  _nTimeLeft = _dElapse;
75  _bEvent = M_FALSE;
76  _bFired = M_FALSE;
77  }
78 
79  // Parameter setup methods
80  //
81  void parameters(double dElapse, double dHold=1, M_BOOL bCyclic=M_FALSE){
82  _dElapse = dElapse;
83  _dHold = dHold;
84  _bCyclic = bCyclic;
85  reset();
86  }
87 
88  void setElapseTime(double sec) { _dElapse = sec; reset();}
89  void setHoldTime(double sec) { _dHold = sec; reset();}
90  void setCyclic(M_BOOL bCylcic=M_TRUE) { _bCyclic = bCylcic; reset();}
91 
92  double getElapseTime() const{ return _dElapse; }
93  double getHoldTime() const{ return _dHold; }
94 
95  // Query parameters and status
96  //
97  inline M_BOOL isEnabled() const{ return _bEnabled; }
98  inline M_BOOL isElapsed() const{ return _bEvent; }
99 
100  inline double timeRemaining() const{ return _bEvent ? 0 : _nTimeLeft; } //< remaining between [t - 0]
101  inline double timeElapsed() const{ return _bEvent ? _dElapse : _dElapse-_nTimeLeft; } //< elapsed between [0 - t]
102 
103  inline double progress() const{ return timeElapsed()/_dElapse; } //< progress varies between [0 - 1]
104  inline double reminder() const{ return timeRemaining()/_dElapse; } //< reminder varies between [1 - 0]
105 
106  // Event callback function
107  //
108  void setEventHandler( void (*pEventHandler)(void*), void* pObject ){
109  _pEventHandler = pEventHandler;
110  _pEventObject = pObject;
111  }
112 
113  // "Animate" the time triggered event; This method must be called on every frame,
114  // when the time-trigger needs to be active
115  //
116  void animate(class laTimer &t);
117 
118 };
120 #endif //#ifndef M_TIME_TRIG_H
Time-triggered events.
Definition: laTimeTrig.h:41