Skip to content

Commit 597f902

Browse files
Update/finish SDL_timer.inc
1 parent 459c601 commit 597f902

File tree

2 files changed

+283
-1
lines changed

2 files changed

+283
-1
lines changed

units/SDL3.pas

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ interface
8888
{$I SDL_surface.inc} // 3.1.6-prev
8989
{$I SDL_video.inc} // 3.1.6-prev
9090
{$I SDL_render.inc} // 3.1.6-prev
91-
{$I SDL_timer.inc} // 3.1.6-prev (unfinished)
91+
{$I SDL_timer.inc} // 3.1.6-prev
9292
{$I SDL_error.inc} // 3.1.6-prev
9393

9494

@@ -183,6 +183,37 @@ function SDL_RectsEqualFloat(const a: PSDL_FRect; b: PSDL_FRect): cbool;
183183
Result := SDL_RectsEqualEpsilon(a, b, SDL_FLT_EPSILON);
184184
end;
185185

186+
{ Macros from SDL_timer.h }
187+
function SDL_SECONDS_TO_NS(S: Integer): Integer;
188+
begin
189+
SDL_SECONDS_TO_NS:=(cuint64(S))*SDL_NS_PER_SECOND;
190+
end;
191+
192+
function SDL_NS_TO_SECONDS(NS: Integer): Integer;
193+
begin
194+
SDL_NS_TO_SECONDS:=NS div SDL_NS_PER_SECOND;
195+
end;
196+
197+
function SDL_MS_TO_NS(MS: Integer): Integer;
198+
begin
199+
SDL_MS_TO_NS:=(cuint64(MS))*SDL_NS_PER_MS;
200+
end;
201+
202+
function SDL_NS_TO_MS(NS: Integer): Integer;
203+
begin
204+
SDL_NS_TO_MS:=NS div SDL_NS_PER_MS;
205+
end;
206+
207+
function SDL_US_TO_NS(US: Integer): Integer;
208+
begin
209+
SDL_US_TO_NS:=(cuint64(US))*SDL_NS_PER_US;
210+
end;
211+
212+
function SDL_NS_TO_US(NS: Integer): Integer;
213+
begin
214+
SDL_NS_TO_US:=NS div SDL_NS_PER_US;
215+
end;
216+
186217
{ Macros from SDL_video.h }
187218
function SDL_WINDOWPOS_UNDEFINED_DISPLAY(X: Integer): Integer;
188219
begin

units/SDL_timer.inc

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,87 @@
66
SPDX-License-Identifier: Zlib
77
}
88

9+
{*
10+
* # CategoryTimer
11+
*
12+
* SDL time management routines.
13+
}
14+
15+
{ SDL time constants }
16+
17+
const
18+
SDL_MS_PER_SECOND = 1000;
19+
SDL_US_PER_SECOND = 1000000;
20+
SDL_NS_PER_SECOND = 1000000000;
21+
SDL_NS_PER_MS = 1000000;
22+
SDL_NS_PER_US = 1000;
23+
function SDL_SECONDS_TO_NS(S: Integer): Integer;
24+
function SDL_NS_TO_SECONDS(NS: Integer): Integer;
25+
function SDL_MS_TO_NS(MS: Integer): Integer;
26+
function SDL_NS_TO_MS(NS: Integer): Integer;
27+
function SDL_US_TO_NS(US: Integer): Integer;
28+
function SDL_NS_TO_US(NS: Integer): Integer;
29+
30+
{*
31+
* Get the number of milliseconds since SDL library initialization.
32+
*
33+
* \returns an unsigned 64-bit value representing the number of milliseconds
34+
* since the SDL library initialized.
35+
*
36+
* \threadsafety It is safe to call this function from any thread.
37+
*
38+
* \since This function is available since SDL 3.1.3.
39+
}
40+
function SDL_GetTicks: cuint64; cdecl;
41+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetTicks' {$ENDIF} {$ENDIF};
42+
43+
{*
44+
* Get the number of nanoseconds since SDL library initialization.
45+
*
46+
* \returns an unsigned 64-bit value representing the number of nanoseconds
47+
* since the SDL library initialized.
48+
*
49+
* \threadsafety It is safe to call this function from any thread.
50+
*
51+
* \since This function is available since SDL 3.1.3.
52+
}
53+
function SDL_GetTicksNS: cuint64; cdecl;
54+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetTicksNS' {$ENDIF} {$ENDIF};
55+
56+
{*
57+
* Get the current value of the high resolution counter.
58+
*
59+
* This function is typically used for profiling.
60+
*
61+
* The counter values are only meaningful relative to each other. Differences
62+
* between values can be converted to times by using
63+
* SDL_GetPerformanceFrequency().
64+
*
65+
* \returns the current counter value.
66+
*
67+
* \threadsafety It is safe to call this function from any thread.
68+
*
69+
* \since This function is available since SDL 3.1.3.
70+
*
71+
* \sa SDL_GetPerformanceFrequency
72+
}
73+
function SDL_GetPerformanceCounter: cuint64; cdecl;
74+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetPerformanceCounter' {$ENDIF} {$ENDIF};
75+
76+
{*
77+
* Get the count per second of the high resolution counter.
78+
*
79+
* \returns a platform-specific count per second.
80+
*
81+
* \threadsafety It is safe to call this function from any thread.
82+
*
83+
* \since This function is available since SDL 3.1.3.
84+
*
85+
* \sa SDL_GetPerformanceCounter
86+
}
87+
function SDL_GetPerformanceFrequency: cuint64; cdecl;
88+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetPerformanceFrequency' {$ENDIF} {$ENDIF};
89+
990
{*
1091
* Wait a specified number of milliseconds before returning.
1192
*
@@ -38,3 +119,173 @@ procedure SDL_Delay(ms: cuint32); cdecl;
38119
procedure SDL_DelayNS(ns: cuint64); cdecl;
39120
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_DelayNS' {$ENDIF} {$ENDIF};
40121

122+
{*
123+
* Wait a specified number of nanoseconds before returning.
124+
*
125+
* This function waits a specified number of nanoseconds before returning. It
126+
* will attempt to wait as close to the requested time as possible, busy
127+
* waiting if necessary, but could return later due to OS scheduling.
128+
*
129+
* \param ns the number of nanoseconds to delay.
130+
*
131+
* \threadsafety It is safe to call this function from any thread.
132+
*
133+
* \since This function is available since SDL 3.2.0.
134+
}
135+
procedure SDL_DelayPrecise(ns: cuint64); cdecl;
136+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_DelayPrecise' {$ENDIF} {$ENDIF};
137+
138+
{*
139+
* Definition of the timer ID type.
140+
*
141+
* \since This datatype is available since SDL 3.1.3.
142+
}
143+
type
144+
PPSDL_TimerID = ^PSDL_TimerID;
145+
PSDL_TimerID = ^TSDL_TimerID;
146+
TSDL_TimerID = cuint32;
147+
148+
{*
149+
* Function prototype for the millisecond timer callback function.
150+
*
151+
* The callback function is passed the current timer interval and returns the
152+
* next timer interval, in milliseconds. If the returned value is the same as
153+
* the one passed in, the periodic alarm continues, otherwise a new alarm is
154+
* scheduled. If the callback returns 0, the periodic alarm is canceled and
155+
* will be removed.
156+
*
157+
* \param userdata an arbitrary Pointer provided by the app through
158+
* SDL_AddTimer, for its own use.
159+
* \param timerID the current timer being processed.
160+
* \param interval the current callback time interval.
161+
* \returns the new callback time interval, or 0 to disable further runs of
162+
* the callback.
163+
*
164+
* \threadsafety SDL may call this callback at any time from a background
165+
* thread; the application is responsible for locking resources
166+
* the callback touches that need to be protected.
167+
*
168+
* \since This datatype is available since SDL 3.1.3.
169+
*
170+
* \sa SDL_AddTimer
171+
}
172+
type
173+
TSDL_TimerCallback = function (userdata: Pointer; timerID: TSDL_TimerID; interval: cuint32): cuint32; cdecl;
174+
175+
{*
176+
* Call a callback function at a future time.
177+
*
178+
* The callback function is passed the current timer interval and the user
179+
* supplied parameter from the SDL_AddTimer() call and should return the next
180+
* timer interval. If the value returned from the callback is 0, the timer is
181+
* canceled and will be removed.
182+
*
183+
* The callback is run on a separate thread, and for short timeouts can
184+
* potentially be called before this function returns.
185+
*
186+
* Timers take into account the amount of time it took to execute the
187+
* callback. For example, if the callback took 250 ms to execute and returned
188+
* 1000 (ms), the timer would only wait another 750 ms before its next
189+
* iteration.
190+
*
191+
* Timing may be inexact due to OS scheduling. Be sure to note the current
192+
* time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your
193+
* callback needs to adjust for variances.
194+
*
195+
* \param interval the timer delay, in milliseconds, passed to `callback`.
196+
* \param callback the SDL_TimerCallback function to call when the specified
197+
* `interval` elapses.
198+
* \param userdata a Pointer that is passed to `callback`.
199+
* \returns a timer ID or 0 on failure; call SDL_GetError() for more
200+
* information.
201+
*
202+
* \threadsafety It is safe to call this function from any thread.
203+
*
204+
* \since This function is available since SDL 3.1.3.
205+
*
206+
* \sa SDL_AddTimerNS
207+
* \sa SDL_RemoveTimer
208+
}
209+
function SDL_AddTimer(interval: cuint32; callback: TSDL_TimerCallback; userdata: Pointer): TSDL_TimerID; cdecl;
210+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AddTimer' {$ENDIF} {$ENDIF};
211+
212+
{*
213+
* Function prototype for the nanosecond timer callback function.
214+
*
215+
* The callback function is passed the current timer interval and returns the
216+
* next timer interval, in nanoseconds. If the returned value is the same as
217+
* the one passed in, the periodic alarm continues, otherwise a new alarm is
218+
* scheduled. If the callback returns 0, the periodic alarm is canceled and
219+
* will be removed.
220+
*
221+
* \param userdata an arbitrary Pointer provided by the app through
222+
* SDL_AddTimer, for its own use.
223+
* \param timerID the current timer being processed.
224+
* \param interval the current callback time interval.
225+
* \returns the new callback time interval, or 0 to disable further runs of
226+
* the callback.
227+
*
228+
* \threadsafety SDL may call this callback at any time from a background
229+
* thread; the application is responsible for locking resources
230+
* the callback touches that need to be protected.
231+
*
232+
* \since This datatype is available since SDL 3.1.3.
233+
*
234+
* \sa SDL_AddTimerNS
235+
}
236+
type
237+
TSDL_NSTimerCallback = function (userdata: Pointer; timerID: TSDL_TimerID; interval: cuint64): cuint64; cdecl;
238+
239+
{*
240+
* Call a callback function at a future time.
241+
*
242+
* The callback function is passed the current timer interval and the user
243+
* supplied parameter from the SDL_AddTimerNS() call and should return the
244+
* next timer interval. If the value returned from the callback is 0, the
245+
* timer is canceled and will be removed.
246+
*
247+
* The callback is run on a separate thread, and for short timeouts can
248+
* potentially be called before this function returns.
249+
*
250+
* Timers take into account the amount of time it took to execute the
251+
* callback. For example, if the callback took 250 ns to execute and returned
252+
* 1000 (ns), the timer would only wait another 750 ns before its next
253+
* iteration.
254+
*
255+
* Timing may be inexact due to OS scheduling. Be sure to note the current
256+
* time with SDL_GetTicksNS() or SDL_GetPerformanceCounter() in case your
257+
* callback needs to adjust for variances.
258+
*
259+
* \param interval the timer delay, in nanoseconds, passed to `callback`.
260+
* \param callback the SDL_TimerCallback function to call when the specified
261+
* `interval` elapses.
262+
* \param userdata a Pointer that is passed to `callback`.
263+
* \returns a timer ID or 0 on failure; call SDL_GetError() for more
264+
* information.
265+
*
266+
* \threadsafety It is safe to call this function from any thread.
267+
*
268+
* \since This function is available since SDL 3.1.3.
269+
*
270+
* \sa SDL_AddTimer
271+
* \sa SDL_RemoveTimer
272+
}
273+
function SDL_AddTimerNS(interval: cuint64; callback: TSDL_NSTimerCallback; userdata: Pointer): TSDL_TimerID; cdecl;
274+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AddTimerNS' {$ENDIF} {$ENDIF};
275+
276+
{*
277+
* Remove a timer created with SDL_AddTimer().
278+
*
279+
* \param id the ID of the timer to remove.
280+
* \returns true on success or false on failure; call SDL_GetError() for more
281+
* information.
282+
*
283+
* \threadsafety It is safe to call this function from any thread.
284+
*
285+
* \since This function is available since SDL 3.1.3.
286+
*
287+
* \sa SDL_AddTimer
288+
}
289+
function SDL_RemoveTimer(id: TSDL_TimerID): cbool; cdecl;
290+
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_RemoveTimer' {$ENDIF} {$ENDIF};
291+

0 commit comments

Comments
 (0)