blob: d682b3bdd0447760c14c90d4a52cf7869c684de6 (
plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
#pragma once
#include "h-basic.h"
#include <functional>
/*
* Timer descriptor and runtime data.
*/
struct timer_type
{
private:
std::function<void ()> m_callback;
public:
//
// XXX Currently need public access for loading and saving.
//
bool m_enabled;
s32b m_delay = 0;
s32b m_countdown = 0;
public:
/**
* Create a new timer
*/
timer_type(std::function<void()> callback, s32b delay)
: m_callback(callback)
, m_enabled(false)
, m_delay(delay)
, m_countdown(delay)
{
}
timer_type(timer_type const &other) = delete;
timer_type &operator =(timer_type const &other) = delete;
/**
* Enable the timer.
*/
void enable()
{
m_enabled = true;
}
/**
* Disable the timer.
*/
void disable()
{
m_enabled = false;
}
/**
* Change delay and reset.
*/
void set_delay_and_reset(s32b delay)
{
m_delay = delay;
m_countdown = delay;
}
/**
* Count down.
*/
void count_down()
{
if (!m_enabled)
{
return;
}
m_countdown--;
if (m_countdown <= 0)
{
m_countdown = m_delay;
m_callback();
}
}
};
|