]> Shamusworld >> Repos - stargem2/blob - src/timing.cpp
Converted to SDL 2, added fullscreen support (F12 to toggle).
[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 #include <stdint.h>
21 #include "log.h"
22
23 #define EVENT_LIST_SIZE       512
24
25 // NOTE ABOUT TIMING SYSTEM DATA STRUCTURES:
26
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
29 // list.
30
31 // Although if we used an insertion sort we could, but it wouldn't work for adjusting
32 // times...
33 // [In that case, we could pull the event out, close the gap, then do insertion sort]
34
35 struct Event
36 {
37     bool valid;
38     double eventTime;
39     void (* timerCallback)(void);
40 };
41
42
43 static Event eventList[EVENT_LIST_SIZE];
44 static uint32_t nextEvent;
45
46
47 void InitializeEventList(void)
48 {
49     for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
50         eventList[i].valid = false;
51 }
52
53
54 //We just slap the next event into the list, no checking, no nada...
55 void SetCallbackTime(void (* callback)(void), double time)
56 {
57     for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
58     {
59         if (!eventList[i].valid)
60         {
61 //WriteLog("SCT: Found callback slot #%u...\n", i);
62             eventList[i].timerCallback = callback;
63             eventList[i].eventTime = time;
64             eventList[i].valid = true;
65
66             return;
67         }
68     }
69
70     WriteLog("SetCallbackTime() failed to find an empty slot in the list!\n");
71 }
72
73
74 void RemoveCallback(void (* callback)(void))
75 {
76     for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
77     {
78         if (eventList[i].valid && eventList[i].timerCallback == callback)
79         {
80             eventList[i].valid = false;
81
82             return;
83         }
84     }
85 }
86
87
88 void AdjustCallbackTime(void (* callback)(void), double time)
89 {
90     for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
91     {
92         if (eventList[i].valid && eventList[i].timerCallback == callback)
93         {
94             eventList[i].eventTime = time;
95
96             return;
97         }
98     }
99 }
100
101
102 double GetTimeToNextEvent(void)
103 {
104     double time = 0;
105     bool firstTime = true;
106
107     for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
108     {
109         if (eventList[i].valid)
110         {
111             if (firstTime)
112                 time = eventList[i].eventTime, nextEvent = i, firstTime = false;
113             else
114             {
115                 if (eventList[i].eventTime < time)
116                     time = eventList[i].eventTime, nextEvent = i;
117             }
118         }
119     }
120
121     return time;
122 }
123
124
125 void HandleNextEvent(void)
126 {
127     double elapsedTime = eventList[nextEvent].eventTime;
128     void (* event)(void) = eventList[nextEvent].timerCallback;
129
130     for(uint32_t i=0; i<EVENT_LIST_SIZE; i++)
131         if (eventList[i].valid)
132             eventList[i].eventTime -= elapsedTime;
133
134     eventList[nextEvent].valid = false;      // Remove event from list...
135
136     (*event)();
137 }
138