SDL_SetTimer(3)

✨ SDL_SetTimer ✨

okay so bestie, this is the SDL API Reference page for SDL_SetTimer and it's giving "set a callback to run after the specified number of milliseconds has elapsed" πŸ•°οΈπŸ’– that's literally the whole vibe, and we're gonna walk through it together, no cap.


🌸 NAME 🌸

SDL_SetTimer β€” set a callback to run after the specified number of milliseconds has elapsed. πŸ¦‹


πŸŽ€ SYNOPSIS πŸŽ€

#include "SDL.h"

int SDL_SetTimer(Uint32 interval, SDL_TimerCallback callback);

πŸ’… CALLBACK πŸ’…

/* Function prototype for the timer callback function */ typedef Uint32
(*SDL_TimerCallback)(Uint32 interval);

πŸ‘‘ DESCRIPTION πŸ‘‘

So here's the tea, bestie: you set a callback to run after the specified number of milliseconds has elapsed. The callback function is passed the current timer interval and returns the next timer interval. If the returned value is the same as the one passed in, the periodic alarm continues, otherwise a new alarm is scheduled. that's the whole rhythm, it's giving metronome energy ⏱️✨

To cancel a currently running timer, just call:

SDL_SetTimer(0, NULL);

and boom, she's gone πŸ’¨

The timer callback function may run in a different thread than your main constant, and so shouldn't call any functions from within itself. (yes, the page literally says "main constant" β€” we don't make the rules πŸ™ˆ) this is the part where you do NOT get cute, okay? no sneaky function calls in there fr fr 😭

The maximum resolution of this timer is 10 ms, which means that if you request a 16 ms timer, your callback will run approximately 20 ms later on an unloaded system. lowkey wild, highkey important.

If you wanted to set a flag signaling a frame update at 30 frames per second (every 33 ms), you might set a timer for 30 ms (see example below πŸ‘‡). the math is mathing, trust πŸ’―

If you use this function, you need to pass SDL_INIT_TIMER to SDL_Init(). don't forget it or she simply will not run, periodt 🚫

Note: This function is kept for compatibility but has been superseded by the new timer functions SDL_AddTimer and SDL_RemoveTimer which support multiple timers. so like… she still works, she's just a legacy icon πŸ’€πŸ‘‘

🌈 EXAMPLES 🌈

SDL_SetTimer((33/10)*10, my_callback);

look at her. iconic. slay πŸ’…


πŸ’– SEE ALSO πŸ’–

  • SDL_AddTimer

and that's the whole page bestie, you ate that 🫢 from the SDL crew, this lil slice of the docs is dated Tue 11 Sep 2001, 23:01 β€” ancient, but still serving. mwah πŸ’‹β­

πŸ“¦ where this page comes from