2 // System time handlers
5 // (C) 2005 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 01/04/2006 Cosmetic changes (like this one ;-)
16 // - Handling for an event that occurs NOW
23 #define EVENT_LIST_SIZE 512
25 // NOTE ABOUT TIMING SYSTEM DATA STRUCTURES:
27 // A queue won't work for this system because we can't guarantee that an event will go
28 // in with a time that is later than the ones already queued up. So we just use a simple
31 // Although if we used an insertion sort we could, but it wouldn't work for adjusting
33 // [In that case, we could pull the event out, close the gap, then do insertion sort]
39 void (* timerCallback)(void);
43 static Event eventList[EVENT_LIST_SIZE];
44 static uint32_t nextEvent;
47 void InitializeEventList(void)
49 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
50 eventList[i].valid = false;
54 //We just slap the next event into the list, no checking, no nada...
55 void SetCallbackTime(void (* callback)(void), double time)
57 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
59 if (!eventList[i].valid)
61 //WriteLog("SCT: Found callback slot #%u...\n", i);
62 eventList[i].timerCallback = callback;
63 eventList[i].eventTime = time;
64 eventList[i].valid = true;
70 WriteLog("SetCallbackTime() failed to find an empty slot in the list!\n");
74 void RemoveCallback(void (* callback)(void))
76 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
78 if (eventList[i].valid && eventList[i].timerCallback == callback)
80 eventList[i].valid = false;
88 void AdjustCallbackTime(void (* callback)(void), double time)
90 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
92 if (eventList[i].valid && eventList[i].timerCallback == callback)
94 eventList[i].eventTime = time;
102 double GetTimeToNextEvent(void)
105 bool firstTime = true;
107 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
109 if (eventList[i].valid)
112 time = eventList[i].eventTime, nextEvent = i, firstTime = false;
115 if (eventList[i].eventTime < time)
116 time = eventList[i].eventTime, nextEvent = i;
125 void HandleNextEvent(void)
127 double elapsedTime = eventList[nextEvent].eventTime;
128 void (* event)(void) = eventList[nextEvent].timerCallback;
130 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
131 if (eventList[i].valid)
132 eventList[i].eventTime -= elapsedTime;
134 eventList[nextEvent].valid = false; // Remove event from list...