Added SDL_DelayPrecise()

SDL_DelayNS() now passes through to the high precision OS delay function, and SDL_DelayPrecise() tries to busy wait to get as close as possible to the desired wait time.

Fixes https://github.com/libsdl-org/SDL/issues/11141
This commit is contained in:
Sam Lantinga
2024-10-10 07:43:34 -07:00
parent 28a70a5b71
commit c8f5f6d47a
8 changed files with 53 additions and 4 deletions

View File

@@ -1177,6 +1177,7 @@ SDL3_0.0.0 {
SDL_wcsstr;
SDL_wcstol;
SDL_StepBackUTF8;
SDL_DelayPrecise;
# extra symbols go here (don't modify this line)
local: *;
};

View File

@@ -1202,3 +1202,4 @@
#define SDL_wcsstr SDL_wcsstr_REAL
#define SDL_wcstol SDL_wcstol_REAL
#define SDL_StepBackUTF8 SDL_StepBackUTF8_REAL
#define SDL_DelayPrecise SDL_DelayPrecise_REAL

View File

@@ -1208,3 +1208,4 @@ SDL_DYNAPI_PROC(wchar_t*,SDL_wcsnstr,(const wchar_t *a, const wchar_t *b, size_t
SDL_DYNAPI_PROC(wchar_t*,SDL_wcsstr,(const wchar_t *a, const wchar_t *b),(a,b),return)
SDL_DYNAPI_PROC(long,SDL_wcstol,(const wchar_t *a, wchar_t **b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(Uint32,SDL_StepBackUTF8,(const char *a, const char **b),(a,b),return)
SDL_DYNAPI_PROC(void,SDL_DelayPrecise,(Uint64 a),(a),)

View File

@@ -65,7 +65,7 @@ int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit,
} else {
const Uint64 now = SDL_GetTicksNS();
if (next_iteration > now) { // Running faster than the limit, sleep a little.
SDL_DelayNS(next_iteration - now);
SDL_DelayPrecise(next_iteration - now);
} else {
next_iteration = now; // running behind (or just lost the window)...reset the timer.
}

View File

@@ -4948,7 +4948,7 @@ static void SDL_SimulateRenderVSync(SDL_Renderer *renderer)
elapsed = (now - renderer->last_present);
if (elapsed < interval) {
Uint64 duration = (interval - elapsed);
SDL_DelayNS(duration);
SDL_DelayPrecise(duration);
now = SDL_GetTicksNS();
}

View File

@@ -657,6 +657,11 @@ void SDL_Delay(Uint32 ms)
}
void SDL_DelayNS(Uint64 ns)
{
SDL_SYS_DelayNS(ns);
}
void SDL_DelayPrecise(Uint64 ns)
{
Uint64 current_value = SDL_GetTicksNS();
Uint64 target_value = current_value + ns;