From 0329ca5ceab912410058683681b12bad4846b57e Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 4 Nov 2011 12:45:32 +0000 Subject: [PATCH] Improved handling of resetting of the EventQueue::startTick(). --- include/osgGA/EventQueue | 4 +++- src/osgGA/EventQueue.cpp | 44 ++++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/include/osgGA/EventQueue b/include/osgGA/EventQueue index 3306acf87..ef253e329 100644 --- a/include/osgGA/EventQueue +++ b/include/osgGA/EventQueue @@ -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(); diff --git a/src/osgGA/EventQueue.cpp b/src/osgGA/EventQueue.cpp index 741d49a70..a5002927f 100644 --- a/src/osgGA/EventQueue.cpp +++ b/src/osgGA/EventQueue.cpp @@ -30,6 +30,13 @@ EventQueue::~EventQueue() { } +void EventQueue::clear() +{ + OpenThreads::ScopedLock lock(_eventQueueMutex); + _eventQueue.clear(); +} + + void EventQueue::setEvents(Events& events) { OpenThreads::ScopedLock lock(_eventQueueMutex); @@ -67,22 +74,41 @@ bool EventQueue::takeEvents(Events& events, double cutOffTime) OpenThreads::ScopedLock 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 "<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 {