forked from lfeng1420/BrickGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerManager.cpp
142 lines (113 loc) · 2.62 KB
/
TimerManager.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "stdafx.h"
#include "TimerManager.h"
#include "ITimerHandler.h"
CTimerManager::CTimerManager()
{
}
CTimerManager::~CTimerManager()
{
}
bool CTimerManager::SetTimer(ITimerHandler* pHandler, int nTimerID, int nInterval, int nCallTimes, const char* szDesc)
{
if (pHandler == nullptr)
{
return false;
}
_TTimerData* pTimerData = __GetTimerData(nTimerID, pHandler);
if (pTimerData != nullptr)
{
// Update data
pTimerData->nInterval = nInterval;
pTimerData->nCallTimes = nCallTimes;
pTimerData->strDesc = szDesc;
return true;
}
_TTimerData stTimerData;
stTimerData.nTimerID = nTimerID;
stTimerData.nInterval = nInterval;
stTimerData.pHandler = pHandler;
stTimerData.strDesc = szDesc;
stTimerData.nCurInterval = 0;
stTimerData.nCallTimes = nCallTimes;
m_lsTimer.push_back(stTimerData);
return true;
}
void CTimerManager::KillTimer(ITimerHandler* pHandler, int nTimerID)
{
__RemoveTimerData(nTimerID, pHandler);
}
void CTimerManager::update(float dt)
{
__UpdateAllTimerData(dt * 1000);
}
//////////////////////////////////////////////////////////////////////////
bool CTimerManager::Create()
{
// Start update per frame
Scheduler* pScheduler = Director::getInstance()->getScheduler();
if (pScheduler == nullptr)
{
return false;
}
pScheduler->scheduleUpdate(this, 0, false);
return true;
}
void CTimerManager::Close()
{
Scheduler* pScheduler = Director::getInstance()->getScheduler();
if (pScheduler != nullptr)
{
pScheduler->unscheduleUpdate(this);
}
}
CTimerManager::_TTimerData* CTimerManager::__GetTimerData(int nTimerID, ITimerHandler* pHandler)
{
FOREACH_IN_CONTAINER(TList_Timer, m_lsTimer, itTimer)
{
if (itTimer->nTimerID == nTimerID && itTimer->pHandler == pHandler)
{
return &(*itTimer);
}
}
return nullptr;
}
void CTimerManager::__RemoveTimerData(int nTimerID, ITimerHandler* pHandler)
{
FOREACH_IN_CONTAINER(TList_Timer, m_lsTimer, itTimer)
{
if (itTimer->nTimerID == nTimerID && itTimer->pHandler == pHandler)
{
m_lsTimer.erase(itTimer);
return;
}
}
}
void CTimerManager::__UpdateAllTimerData(int nInterval)
{
TList_TimerIter itTimer = m_lsTimer.begin();
for (; itTimer != m_lsTimer.end(); )
{
itTimer->nCurInterval += nInterval;
if (itTimer->nCurInterval < itTimer->nInterval)
{
++itTimer;
continue;
}
// Trigger
itTimer->nCurInterval -= itTimer->nInterval;
(itTimer->pHandler)->OnTimer(itTimer->nTimerID);
// Infinity call
if (itTimer->nCallTimes == INFINITY_CALL)
{
++itTimer;
continue;
}
// Decrease call times
if (--itTimer->nCallTimes <= 0)
{
itTimer = m_lsTimer.erase(itTimer);
continue;
}
++itTimer;
}
}