Improved handling of resetting of the EventQueue::startTick().

This commit is contained in:
Robert Osfield 2011-11-04 12:45:32 +00:00
parent 747654fb6d
commit 0329ca5cea
2 changed files with 38 additions and 10 deletions

View File

@ -196,11 +196,13 @@ class OSGGA_EXPORT EventQueue : public osg::Referenced
void frame(double time);
void setStartTick(osg::Timer_t tick) { _startTick = tick; }
void setStartTick(osg::Timer_t tick) { _startTick = tick; clear(); }
osg::Timer_t getStartTick() const { return _startTick; }
double getTime() const { return osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); }
/** clear all events from queue. */
void clear();
/** convenience method for create an event ready to fill in. Clones the getCurrentEventState() to produce a up to date event state. */
GUIEventAdapter* createEvent();

View File

@ -30,6 +30,13 @@ EventQueue::~EventQueue()
{
}
void EventQueue::clear()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
_eventQueue.clear();
}
void EventQueue::setEvents(Events& events)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
@ -67,22 +74,41 @@ bool EventQueue::takeEvents(Events& events, double cutOffTime)
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
if (!_eventQueue.empty())
{
bool eventsTaken = false;
Events::iterator itr = _eventQueue.begin();
for(;
itr != _eventQueue.end() && ((*itr)->getTime() <= cutOffTime);
// find last event if queue that came in before the cuttof.
Events::reverse_iterator ritr = _eventQueue.rbegin();
for(; ritr != _eventQueue.rend() && ((*ritr)->getTime() > cutOffTime); ++ritr) {}
if (ritr==_eventQueue.rend()) return false;
for(Events::iterator itr = _eventQueue.begin();
itr != ritr.base();
++itr)
{
events.push_back(*itr);
eventsTaken = true;
}
if (eventsTaken)
// make sure that the events are in ascending time order, and any out of out events
// have their time reset to the next valid time after them in the events list.
double previousTime = cutOffTime;
for(Events::reverse_iterator itr = events.rbegin();
itr != events.rend();
++itr)
{
_eventQueue.erase(_eventQueue.begin(), itr);
if ((*itr)->getTime() > previousTime)
{
OSG_NOTICE<<"Reset event time from "<<(*itr)->getTime()<<" to "<<previousTime<<std::endl;
(*itr)->setTime(previousTime);
}
else
{
previousTime = (*itr)->getTime();
}
}
return eventsTaken;
// remove the events we are taking from the original event queue.
_eventQueue.erase(_eventQueue.begin(), ritr.base());
return true;
}
else
{