blob: 8c58be51fcb8d8295c549e21f5755a07a8eb5b64 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#ifndef TIMEDEVENT_H
#define TIMEDEVENT_H
#ifdef _WIN32
#pragma once
#endif
// This class triggers events at a specified rate. Just call NextEvent() and do an event until it
// returns false. For example, if you want to spawn particles 10 times per second, do this:
// pTimer->SetRate(10);
// float tempDelta = fTimeDelta;
// while(pTimer->NextEvent(tempDelta))
// spawn a particle
class TimedEvent
{
public:
TimedEvent()
{
m_TimeBetweenEvents = -1;
m_fNextEvent = 0;
}
// Rate is in events per second (ie: rate of 15 will trigger 15 events per second).
inline void Init(float rate)
{
m_TimeBetweenEvents = 1.0f / rate;
m_fNextEvent = 0;
}
inline void ResetRate(float rate)
{
m_TimeBetweenEvents = 1.0f / rate;
}
inline bool NextEvent(float &curDelta)
{
// If this goes off, you didn't call Init().
Assert( m_TimeBetweenEvents != -1 );
if(curDelta >= m_fNextEvent)
{
curDelta -= m_fNextEvent;
m_fNextEvent = m_TimeBetweenEvents;
return true;
}
else
{
m_fNextEvent -= curDelta;
return false;
}
}
private:
float m_TimeBetweenEvents;
float m_fNextEvent; // When the next event should be triggered.
};
#endif // TIMEDEVENT_H
|