]> Shamusworld >> Repos - stargem2/blob - src/timing.cpp
Added config file key bindings, general code cleanup
[stargem2] / src / timing.cpp
1 //
2 // System time handlers
3 //
4 // by James L. Hammons
5 // (C) 2005 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  01/04/2006  Cosmetic changes (like this one ;-)
12 //
13
14 // STILL TO DO:
15 //
16 // - Handling for an event that occurs NOW
17 //
18
19 #include "timing.h"
20
21 #include "types.h"
22 #include "log.h"
23
24 #define EVENT_LIST_SIZE       512
25
26 // NOTE ABOUT TIMING SYSTEM DATA STRUCTURES:
27
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
30 // list.
31
32 // Although if we used an insertion sort we could, but it wouldn't work for adjusting
33 // times...
34 // [In that case, we could pull the event out, close the gap, then do insertion sort]
35
36 struct Event
37 {
38     bool valid;
39     double eventTime;
40     void (* timerCallback)(void);
41 };
42
43 static Event eventList[EVENT_LIST_SIZE];
44 static uint32 nextEvent;
45
46 void InitializeEventList(void)
47 {
48     for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
49         eventList[i].valid = false;
50 }
51
52 //We just slap the next event into the list, no checking, no nada...
53 void SetCallbackTime(void (* callback)(void), double time)
54 {
55     for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
56     {
57         if (!eventList[i].valid)
58         {
59 //WriteLog("SCT: Found callback slot #%u...\n", i);
60             eventList[i].timerCallback = callback;
61             eventList[i].eventTime = time;
62             eventList[i].valid = true;
63
64             return;
65         }
66     }
67
68     WriteLog("SetCallbackTime() failed to find an empty slot in the list!\n");
69 }
70
71 void RemoveCallback(void (* callback)(void))
72 {
73     for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
74     {
75         if (eventList[i].valid && eventList[i].timerCallback == callback)
76         {
77             eventList[i].valid = false;
78
79             return;
80         }
81     }
82 }
83
84 void AdjustCallbackTime(void (* callback)(void), double time)
85 {
86     for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
87     {
88         if (eventList[i].valid && eventList[i].timerCallback == callback)
89         {
90             eventList[i].eventTime = time;
91
92             return;
93         }
94     }
95 }
96
97 double GetTimeToNextEvent(void)
98 {
99     double time = 0;
100     bool firstTime = true;
101
102     for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
103     {
104         if (eventList[i].valid)
105         {
106             if (firstTime)
107                 time = eventList[i].eventTime, nextEvent = i, firstTime = false;
108             else
109             {
110                 if (eventList[i].eventTime < time)
111                     time = eventList[i].eventTime, nextEvent = i;
112             }
113         }
114     }
115
116     return time;
117 }
118
119 void HandleNextEvent(void)
120 {
121     double elapsedTime = eventList[nextEvent].eventTime;
122     void (* event)(void) = eventList[nextEvent].timerCallback;
123
124     for(uint32 i=0; i<EVENT_LIST_SIZE; i++)
125         if (eventList[i].valid)
126             eventList[i].eventTime -= elapsedTime;
127
128     eventList[nextEvent].valid = false;      // Remove event from list...
129
130     (*event)();
131 }