bool EventManager::processEvents(Time max_process_time)
{
	Time curr_time = g_app->getCurrentTime();
	Time max_time = curr_time + max_process_time;


	// swap active queues and clear the new queue after the swap
	int queueToProcess = m_active_queue;
	m_active_queue = (m_active_queue + 1) % EVENTMANAGER_NUM_QUEUES;
	m_queues[m_active_queue].clear();

	// Process the queue
	while (!m_queues[queueToProcess].empty())
	{
		// pop the front of the queue
		EventPtr event = m_queues[queueToProcess].front();
		m_queues[queueToProcess].pop_front();
		cout<<"Send event \""<<event->getName()<<"\""<<endl;

		// find all the delegate functions registered for this event
		auto findIt = m_event_listeners.find(typeid(*event));
		if (findIt != m_event_listeners.end())
		{
			const EventListenerList& eventListeners = findIt->second;

			// call each listener
			for (auto it = eventListeners.begin(); it != eventListeners.end();++it)
			{
				EventListenerFunction event_function = (*it);
				if (event_function.getOwner() != event->getSender())
					event_function.execute(event);
			}
		}
		// check to see if time ran out
		curr_time = g_app->getCurrentTime();
		if (curr_time >= max_time)
		{
			cout<<"EventLoop: Aborting event processing; time ran out. Already "<<(curr_time-max_time).asMilliseconds()<<"ms too long."<<endl;
			break;
		}
	}

	// If we couldn’t process all of the events, push the remaining events to
	// the new active queue.
	// Note: To preserve sequencing, go back-to-front, inserting them at the
	// head of the active queue.
	bool queueFlushed = (m_queues[queueToProcess].empty());
	if (!queueFlushed)
	{
		while (!m_queues[queueToProcess].empty())
		{
			EventPtr event = m_queues[queueToProcess].back();
			m_queues[queueToProcess].pop_back();
			m_queues[m_active_queue].push_front(event);
		}
	}
	return queueFlushed;
}