Logging & timeSDL 3.2

SDL_GetTicksNS

Nanoseconds since SDL started. The clock to compute deltaTime with.

Signature

Uint64 SDL_GetTicksNS(void);

How it works

The same clock as SDL_GetTicks with a million times finer units. It is monotonic: it never jumps backwards, even if the user changes the system time.

Subtract two readings and divide by 1e9 to get seconds. Do the subtraction in integers first, then convert: the difference is small, so the float keeps all its precision.

Return value

Nanoseconds since SDL was initialized.

Example

example.cpp
1Uint64 last = SDL_GetTicksNS();
2while (running) {
3    Uint64 now = SDL_GetTicksNS();
4    float dt = float(now - last) / 1e9f;   // seconds, subtract first
5    last = now;
6    update(dt);
7}
Official SDL wiki