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
24 #define EVENT_LIST_SIZE 512
26 // NOTE ABOUT TIMING SYSTEM DATA STRUCTURES:
28 // A queue won't work for this system because we can't guarantee that an event will go
29 // in with a time that is later than the ones already queued up. So we just use a simple
32 // Although if we used an insertion sort we could, but it wouldn't work for adjusting
34 // [In that case, we could pull the event out, close the gap, then do insertion sort]
40 void (* timerCallback)(void);
43 static Event eventList[EVENT_LIST_SIZE];
44 static uint32 nextEvent;
46 void InitializeEventList(void)
48 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
49 eventList[i].valid = false;
52 //We just slap the next event into the list, no checking, no nada...
53 void SetCallbackTime(void (* callback)(void), double time)
55 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
57 if (!eventList[i].valid)
59 //WriteLog("SCT: Found callback slot #%u...\n", i);
60 eventList[i].timerCallback = callback;
61 eventList[i].eventTime = time;
62 eventList[i].valid = true;
68 WriteLog("SetCallbackTime() failed to find an empty slot in the list!\n");
71 void RemoveCallback(void (* callback)(void))
73 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
75 if (eventList[i].valid && eventList[i].timerCallback == callback)
77 eventList[i].valid = false;
84 void AdjustCallbackTime(void (* callback)(void), double time)
86 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
88 if (eventList[i].valid && eventList[i].timerCallback == callback)
90 eventList[i].eventTime = time;
97 double GetTimeToNextEvent(void)
100 bool firstTime = true;
102 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
104 if (eventList[i].valid)
107 time = eventList[i].eventTime, nextEvent = i, firstTime = false;
110 if (eventList[i].eventTime < time)
111 time = eventList[i].eventTime, nextEvent = i;
119 void HandleNextEvent(void)
121 double elapsedTime = eventList[nextEvent].eventTime;
122 void (* event)(void) = eventList[nextEvent].timerCallback;
124 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
125 if (eventList[i].valid)
126 eventList[i].eventTime -= elapsedTime;
128 eventList[nextEvent].valid = false; // Remove event from list...