1 #include "event_mgr.hxx"
3 #include <simgear/math/SGMath.hxx>
4 #include <simgear/debug/logstream.hxx>
6 void SGEventMgr::add(const std::string& name, SGCallback* cb,
7 double interval, double delay,
8 bool repeat, bool simtime)
10 // Clamp the delay value to 1 usec, so that user code can use
11 // "zero" as a synonym for "next frame".
12 if(delay <= 0) delay = 0.000001;
14 SGTimer* t = new SGTimer;
15 t->interval = interval;
22 SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
32 SGTimerQueue* q = simtime ? &mgr->_simQueue : &mgr->_rtQueue;
33 q->insert(this, interval);
40 void SGEventMgr::update(double delta_time_sec)
42 _simQueue.update(delta_time_sec);
44 double rt = _rtProp ? _rtProp->getDoubleValue() : 0;
48 void SGEventMgr::removeTask(const std::string& name)
50 SGTimer* t = _simQueue.findByName(name);
53 } else if ((t = _rtQueue.findByName(name))) {
56 SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
63 ////////////////////////////////////////////////////////////////////////
65 // This is the priority queue implementation:
66 ////////////////////////////////////////////////////////////////////////
68 SGTimerQueue::SGTimerQueue(int size)
73 while(size > _tableSize)
74 _tableSize = ((_tableSize + 1)<<1) - 1;
76 _table = new HeapEntry[_tableSize];
77 for(int i=0; i<_tableSize; i++) {
84 SGTimerQueue::~SGTimerQueue()
86 for(int i=0; i<_numEntries; i++) {
87 delete _table[i].timer;
96 void SGTimerQueue::update(double deltaSecs)
99 while(_numEntries && nextTime() <= _now) {
100 SGTimer* t = remove();
105 void SGTimerQueue::insert(SGTimer* timer, double time)
107 if(_numEntries >= _tableSize)
111 _table[_numEntries-1].pri = -(_now + time);
112 _table[_numEntries-1].timer = timer;
114 siftUp(_numEntries-1);
117 SGTimer* SGTimerQueue::remove(SGTimer* t)
120 for(entry=0; entry<_numEntries; entry++)
121 if(_table[entry].timer == t)
123 if(entry == _numEntries)
126 // Swap in the last item in the table, and sift down
127 swap(entry, _numEntries-1);
134 SGTimer* SGTimerQueue::remove()
136 if(_numEntries == 0) {
138 } else if(_numEntries == 1) {
140 return _table[0].timer;
143 SGTimer *result = _table[0].timer;
144 _table[0] = _table[_numEntries - 1];
150 void SGTimerQueue::siftDown(int n)
152 // While we have children bigger than us, swap us with the biggest
154 while(lchild(n) < _numEntries) {
155 int bigc = lchild(n);
156 if(rchild(n) < _numEntries && pri(rchild(n)) > pri(bigc))
158 if(pri(bigc) <= pri(n))
165 void SGTimerQueue::siftUp(int n)
167 while((n != 0) && (_table[n].pri > _table[parent(n)].pri)) {
174 void SGTimerQueue::growArray()
176 _tableSize = ((_tableSize+1)<<1) - 1;
177 HeapEntry *newTable = new HeapEntry[_tableSize];
178 for(int i=0; i<_numEntries; i++) {
179 newTable[i].pri = _table[i].pri;
180 newTable[i].timer = _table[i].timer;
186 SGTimer* SGTimerQueue::findByName(const std::string& name) const
188 for (int i=0; i < _numEntries; ++i) {
189 if (_table[i].timer->name == name) {
190 return _table[i].timer;