Get the count per second of the high resolution counter.
void); Uint64 SDL_GetPerformanceFrequency(
Returns a platform-specific count per second.
This function is available since SDL 3.0.0.
#include "SDL.h"
#define DEFAULT_RESOLUTION 1
static int ticks = 0;
static Uint32 SDLCALL
void *param)
ticktock(Uint32 interval,
{
++ticks;return (interval);
}
static Uint32 SDLCALL
void *param)
callback(Uint32 interval,
{"Timer %d : param = %d", interval, (int) (uintptr_t) param);
SDL_Log(return interval;
}
int
int argc, char *argv[])
main(
{int i, desired;
SDL_TimerID t1, t2, t3;
Uint32 start32, now32;
Uint64 start, now;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
if (SDL_Init(SDL_INIT_TIMER) < 0) {
"Couldn't initialize SDL: %s", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, return (1);
}
/* Start the timer */
0;
desired = if (argv[1]) {
1]);
desired = SDL_atoi(argv[
}if (desired == 0) {
desired = DEFAULT_RESOLUTION;
}
t1 = SDL_AddTimer(desired, ticktock, NULL);
/* Wait 10 seconds */
"Waiting 10 seconds");
SDL_Log(10 * 1000);
SDL_Delay(
/* Stop the timer */
SDL_RemoveTimer(t1);
/* Print the results */
if (ticks) {
"Timer resolution: desired = %d ms, actual = %f ms",
SDL_Log(double) (10 * 1000) / ticks);
desired, (
}
/* Test multiple timers */
"Testing multiple timers...");
SDL_Log(100, callback, (void *) 1);
t1 = SDL_AddTimer(if (!t1)
"Could not create timer 1: %s", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,50, callback, (void *) 2);
t2 = SDL_AddTimer(if (!t2)
"Could not create timer 2: %s", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,233, callback, (void *) 3);
t3 = SDL_AddTimer(if (!t3)
"Could not create timer 3: %s", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
/* Wait 10 seconds */
"Waiting 10 seconds");
SDL_Log(10 * 1000);
SDL_Delay(
"Removing timer 1 and waiting 5 more seconds");
SDL_Log(
SDL_RemoveTimer(t1);
5 * 1000);
SDL_Delay(
SDL_RemoveTimer(t2);
SDL_RemoveTimer(t3);
start = SDL_GetPerformanceCounter();for (i = 0; i < 1000000; ++i) {
0, NULL);
ticktock(
}
now = SDL_GetPerformanceCounter();"1 million iterations of ticktock took %f ms", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
SDL_Log(
"Performance counter frequency: %"SDL_PRIu64"", (unsigned long long) SDL_GetPerformanceFrequency());
SDL_Log(
start32 = SDL_GetTicks();
start = SDL_GetPerformanceCounter();1000);
SDL_Delay(
now = SDL_GetPerformanceCounter();
now32 = SDL_GetTicks();"Delay 1 second = %d ms in ticks, %f ms according to performance counter", (now32-start32), (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
SDL_Log(
SDL_Quit();return (0);
}