Example #1
0
void EventManager::tick()
{
    
#ifdef USE_SDL
    DWORD now = SDL_GetTicks();
#else
    DWORD now = timeGetTime();
#endif
    while (m_EventHeap.size() && now >= m_EventHeap.front()->m_eventTime)// < 0x80000000)
    {
        pop_heap(m_EventHeap.begin(), m_EventHeap.end(), Event::Compare());
        Event* pEvent = m_EventHeap.back();

        assert(pEvent != NULL);

        assert(pEvent->m_loopTime != TIME_EVENT_INFINITE || pEvent->m_loopDelay > 0);

        Receiver* pReceiver = getReceiver(pEvent->m_Receiver);
        if (pReceiver == NULL) // receiver not found
        {
            m_EventHeap.pop_back();
            delete pEvent; // delete it
            continue;
        }

        if (pEvent->m_loopTime == TIME_EVENT_INFINITE || --pEvent->m_loopTime > 0)
        {
            // if the event is a loop event, and still need to be sent.
            //  push back it again
            pEvent->m_eventTime += pEvent->m_loopDelay;
            
            // refBuffer(pEvent->m_bufferid); // add ref

            push_heap(m_EventHeap.begin(), m_EventHeap.end(), Event::Compare());
            
            pReceiver->addEvent(TodoEvent(pEvent, 0)); // 0: can not release after event processed
        }
        else
        {
            // otherwise, pop it out
            m_EventHeap.pop_back();
            // only handle to the event is saved in receiver. the receiver should delete it when it processed
            
            pReceiver->addEvent(TodoEvent(pEvent, 1)); // 1: must release after event processed
        }
    }
}