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;
20 SGTimerQueue* q = simtime ? &_simQueue : &_rtQueue;
36 void SGEventMgr::update(double delta_time_sec)
38 _simQueue.update(delta_time_sec);
40 double rt = _rtProp ? _rtProp->getDoubleValue() : 0;
44 void SGEventMgr::removeTask(const std::string& name)
46 SGTimer* t = _simQueue.findByName(name);
49 } else if ((t = _rtQueue.findByName(name))) {
52 SG_LOG(SG_GENERAL, SG_WARN, "removeTask: no task found with name:" << name);
59 ////////////////////////////////////////////////////////////////////////
61 // This is the priority queue implementation:
62 ////////////////////////////////////////////////////////////////////////
64 SGTimerQueue::SGTimerQueue(int size)
69 while(size > _tableSize)
70 _tableSize = ((_tableSize + 1)<<1) - 1;
72 _table = new HeapEntry[_tableSize];
73 for(int i=0; i<_tableSize; i++) {
80 SGTimerQueue::~SGTimerQueue()
82 for(int i=0; i<_numEntries; i++) {
83 delete _table[i].timer;
92 void SGTimerQueue::update(double deltaSecs)
95 while(_numEntries && nextTime() <= _now) {
96 SGTimer* t = remove();
98 insert(t, t->interval);
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;