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);
42 static Event eventList[EVENT_LIST_SIZE];
43 static uint32 nextEvent;
45 void InitializeEventList(void)
47 for(uint32 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 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("SetCallbackTime() failed to find an empty slot in the list!\n");
70 void RemoveCallback(void (* callback)(void))
72 for(uint32 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 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)
99 bool firstTime = true;
101 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
103 if (eventList[i].valid)
106 time = eventList[i].eventTime, nextEvent = i, firstTime = false;
109 if (eventList[i].eventTime < time)
110 time = eventList[i].eventTime, nextEvent = i;
118 void HandleNextEvent(void)
120 double elapsedTime = eventList[nextEvent].eventTime;
121 void (* event)(void) = eventList[nextEvent].timerCallback;
123 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
124 if (eventList[i].valid)
125 eventList[i].eventTime -= elapsedTime;
127 eventList[nextEvent].valid = false; // Remove event from list...