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 static Event eventList[EVENT_LIST_SIZE];
42 static uint32 nextEvent;
44 void InitializeEventList(void)
46 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
47 eventList[i].valid = false;
50 //We just slap the next event into the list, no checking, no nada...
51 void SetCallbackTime(void (* callback)(void), double time)
53 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
55 if (!eventList[i].valid)
57 //WriteLog("SCT: Found callback slot #%u...\n", i);
58 eventList[i].timerCallback = callback;
59 eventList[i].eventTime = time;
60 eventList[i].valid = true;
66 WriteLog("SetCallbackTime() failed to find an empty slot in the list!\n");
69 void RemoveCallback(void (* callback)(void))
71 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
73 if (eventList[i].valid && eventList[i].timerCallback == callback)
75 eventList[i].valid = false;
82 void AdjustCallbackTime(void (* callback)(void), double time)
84 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
86 if (eventList[i].valid && eventList[i].timerCallback == callback)
88 eventList[i].eventTime = time;
95 double GetTimeToNextEvent(void)
98 bool firstTime = true;
100 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
102 if (eventList[i].valid)
105 time = eventList[i].eventTime, nextEvent = i, firstTime = false;
108 if (eventList[i].eventTime < time)
109 time = eventList[i].eventTime, nextEvent = i;
117 void HandleNextEvent(void)
119 double elapsedTime = eventList[nextEvent].eventTime;
120 void (* event)(void) = eventList[nextEvent].timerCallback;
122 for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
123 if (eventList[i].valid)
124 eventList[i].eventTime -= elapsedTime;
126 eventList[nextEvent].valid = false; // Remove event from list...