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
38 void (* timerCallback)(void);
41 //let's try +1... nope.
42 static Event eventList[EVENT_LIST_SIZE];
43 static uint32_t nextEvent;
45 void InitializeEventList(void)
47 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
48 eventList[i].valid = false;
51 //We just slap the next event into the list, no checking, no nada...
52 void SetCallbackTime(void (* callback)(void), double time)
54 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
56 if (!eventList[i].valid)
58 //WriteLog("SCT: Found callback slot #%u...\n", i);
59 eventList[i].timerCallback = callback;
60 eventList[i].eventTime = time;
61 eventList[i].valid = true;
67 WriteLog("TIMING: SetCallbackTime() failed to find an empty slot in the list!\n");
70 void RemoveCallback(void (* callback)(void))
72 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
74 if (eventList[i].valid && eventList[i].timerCallback == callback)
76 eventList[i].valid = false;
83 void AdjustCallbackTime(void (* callback)(void), double time)
85 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
87 if (eventList[i].valid && eventList[i].timerCallback == callback)
89 eventList[i].eventTime = time;
96 double GetTimeToNextEvent(void)
98 // Find the next event. Since the events are not necessarily in order of
99 // increasing time, we have to search through the list for the lowest one.
101 //ALSO: There's a bug here--nextEvent is getting a bogus value/getting clobbered...
102 // Seems like it's getting clobbered somewhere other than here...
104 bool firstTime = true;
106 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
108 if (eventList[i].valid)
112 time = eventList[i].eventTime;
118 if (eventList[i].eventTime < time)
120 time = eventList[i].eventTime;
128 WriteLog("TIMING: GetTimeToNextEvent() failed to find next event!\n");
130 if (nextEvent >= EVENT_LIST_SIZE)
131 WriteLog("TIMING: GetTimeToNextEvent() has bad nextEvent (=%u)!\n", nextEvent);
136 void HandleNextEvent(void)
138 double elapsedTime = eventList[nextEvent].eventTime;
139 void (* event)(void) = eventList[nextEvent].timerCallback;
141 for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
142 if (eventList[i].valid)
143 eventList[i].eventTime -= elapsedTime;
145 eventList[nextEvent].valid = false; // Remove event from list...