-
Notifications
You must be signed in to change notification settings - Fork 6
/
csdm_timer.cpp
127 lines (107 loc) · 1.89 KB
/
csdm_timer.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "amxxmodule.h"
#include "csdm_timer.h"
#include "sh_stack.h"
Timer g_Timer;
CStack<tasksel *> *g_FreeTasks;
Timer::~Timer()
{
Clear();
tasksel *pTask;
while (!g_FreeTasks->empty())
{
pTask = g_FreeTasks->front();
if (pTask->task)
pTask->task->deleteThis();
delete pTask;
g_FreeTasks->pop();
}
m_Tasks.clear();
delete g_FreeTasks;
g_FreeTasks = NULL;
}
Timer::Timer()
{
g_FreeTasks = new CStack<tasksel *>();
}
void Timer::AddTask(ITask *pTask, float interval)
{
tasksel *pSel;
if (g_FreeTasks->empty())
{
pSel = new tasksel;
} else {
pSel = g_FreeTasks->front();
g_FreeTasks->pop();
}
pSel->task = pTask;
pSel->interval = interval;
pSel->think = gpGlobals->time;
m_Tasks.push_back(pSel);
}
void Timer::RemoveTask(ITask *pTask)
{
List<tasksel *>::iterator iter;
iter = m_Tasks.begin();
while (iter != m_Tasks.end())
{
if ( (*iter)->task == pTask )
{
pTask->deleteThis();
(*iter)->task = NULL;
g_FreeTasks->push( (*iter) );
iter = m_Tasks.erase(iter);
return;
}
iter++;
}
}
void Timer::Clear()
{
List<tasksel *>::iterator iter;
iter = m_Tasks.begin();
while (iter != m_Tasks.end())
{
(*iter)->task->deleteThis();
(*iter)->task = NULL;
g_FreeTasks->push( (*iter) );
iter++;
}
m_Tasks.clear();
}
void Timer::TaskInfo(int &num, int &avail)
{
num = m_Tasks.size();
avail = g_FreeTasks->size();
}
size_t Timer::Tick(float curTime)
{
List<tasksel *>::iterator iter;
iter = m_Tasks.begin();
bool done;
tasksel *pSel;
size_t num = 0;
ITask *pRun;
while (iter != m_Tasks.end())
{
done = false;
pSel = (*iter);
pRun = (*iter)->task;
if (pSel->think + pSel->interval < curTime)
{
pRun->Run();
done = true;
num++;
pSel->think = curTime;
}
if (done)
{
pRun->deleteThis();
pSel->task = NULL;
g_FreeTasks->push(pSel);
iter = m_Tasks.erase(iter);
} else {
iter++;
}
}
return num;
}