Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
1.1 --- a/include/SDL_timer.h Fri Mar 25 13:48:48 2011 -0700
1.2 +++ b/include/SDL_timer.h Fri Mar 25 14:45:04 2011 -0700
1.3 @@ -48,6 +48,16 @@
1.4 extern DECLSPEC Uint32 SDLCALL SDL_GetTicks(void);
1.5
1.6 /**
1.7 + * \brief Get the current value of the high resolution counter
1.8 + */
1.9 +extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void);
1.10 +
1.11 +/**
1.12 + * \brief Get the count per second of the high resolution counter
1.13 + */
1.14 +extern DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void);
1.15 +
1.16 +/**
1.17 * \brief Wait a specified number of milliseconds before returning.
1.18 */
1.19 extern DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
2.1 --- a/src/timer/beos/SDL_systimer.c Fri Mar 25 13:48:48 2011 -0700
2.2 +++ b/src/timer/beos/SDL_systimer.c Fri Mar 25 14:45:04 2011 -0700
2.3 @@ -42,6 +42,18 @@
2.4 return ((system_time() - start) / 1000);
2.5 }
2.6
2.7 +Uint64
2.8 +SDL_GetPerformanceCounter(void)
2.9 +{
2.10 + return system_time();
2.11 +}
2.12 +
2.13 +Uint64
2.14 +SDL_GetPerformanceFrequency(void)
2.15 +{
2.16 + return 1000000;
2.17 +}
2.18 +
2.19 void
2.20 SDL_Delay(Uint32 ms)
2.21 {
3.1 --- a/src/timer/dummy/SDL_systimer.c Fri Mar 25 13:48:48 2011 -0700
3.2 +++ b/src/timer/dummy/SDL_systimer.c Fri Mar 25 14:45:04 2011 -0700
3.3 @@ -37,6 +37,18 @@
3.4 return 0;
3.5 }
3.6
3.7 +Uint64
3.8 +SDL_GetPerformanceCounter(void)
3.9 +{
3.10 + return SDL_GetTicks();
3.11 +}
3.12 +
3.13 +Uint64
3.14 +SDL_GetPerformanceFrequency(void)
3.15 +{
3.16 + return 1000;
3.17 +}
3.18 +
3.19 void
3.20 SDL_Delay(Uint32 ms)
3.21 {
4.1 --- a/src/timer/nds/SDL_systimer.c Fri Mar 25 13:48:48 2011 -0700
4.2 +++ b/src/timer/nds/SDL_systimer.c Fri Mar 25 14:45:04 2011 -0700
4.3 @@ -52,6 +52,18 @@
4.4 return timer_ticks;
4.5 }
4.6
4.7 +Uint64
4.8 +SDL_GetPerformanceCounter(void)
4.9 +{
4.10 + return SDL_GetTicks();
4.11 +}
4.12 +
4.13 +Uint64
4.14 +SDL_GetPerformanceFrequency(void)
4.15 +{
4.16 + return 1000;
4.17 +}
4.18 +
4.19 void
4.20 SDL_Delay(Uint32 ms)
4.21 {
5.1 --- a/src/timer/unix/SDL_systimer.c Fri Mar 25 13:48:48 2011 -0700
5.2 +++ b/src/timer/unix/SDL_systimer.c Fri Mar 25 14:45:04 2011 -0700
5.3 @@ -64,6 +64,7 @@
5.4 #if HAVE_CLOCK_GETTIME
5.5 Uint32 ticks;
5.6 struct timespec now;
5.7 +
5.8 clock_gettime(CLOCK_MONOTONIC, &now);
5.9 ticks =
5.10 (now.tv_sec - start.tv_sec) * 1000 + (now.tv_nsec -
5.11 @@ -72,6 +73,7 @@
5.12 #else
5.13 Uint32 ticks;
5.14 struct timeval now;
5.15 +
5.16 gettimeofday(&now, NULL);
5.17 ticks =
5.18 (now.tv_sec - start.tv_sec) * 1000 + (now.tv_usec -
5.19 @@ -80,6 +82,40 @@
5.20 #endif
5.21 }
5.22
5.23 +Uint64
5.24 +SDL_GetPerformanceCounter(void)
5.25 +{
5.26 +#if HAVE_CLOCK_GETTIME
5.27 + Uint64 ticks;
5.28 + struct timespec now;
5.29 +
5.30 + clock_gettime(CLOCK_MONOTONIC, &now);
5.31 + ticks = now.tv_sec;
5.32 + ticks *= 1000000000;
5.33 + ticks += now.tv_nsec;
5.34 + return (ticks);
5.35 +#else
5.36 + Uint64 ticks;
5.37 + struct timeval now;
5.38 +
5.39 + gettimeofday(&now, NULL);
5.40 + ticks = now.tv_sec;
5.41 + ticks *= 1000000;
5.42 + ticks += now.tv_usec;
5.43 + return (ticks);
5.44 +#endif
5.45 +}
5.46 +
5.47 +Uint64
5.48 +SDL_GetPerformanceFrequency(void)
5.49 +{
5.50 +#if HAVE_CLOCK_GETTIME
5.51 + return 1000000000;
5.52 +#else
5.53 + return 1000000;
5.54 +#endif
5.55 +}
5.56 +
5.57 void
5.58 SDL_Delay(Uint32 ms)
5.59 {
6.1 --- a/src/timer/wince/SDL_systimer.c Fri Mar 25 13:48:48 2011 -0700
6.2 +++ b/src/timer/wince/SDL_systimer.c Fri Mar 25 14:45:04 2011 -0700
6.3 @@ -87,6 +87,18 @@
6.4 return ((Uint32) wce_rel_ticks());
6.5 }
6.6
6.7 +Uint64
6.8 +SDL_GetPerformanceCounter(void)
6.9 +{
6.10 + return SDL_GetTicks();
6.11 +}
6.12 +
6.13 +Uint64
6.14 +SDL_GetPerformanceFrequency(void)
6.15 +{
6.16 + return 1000;
6.17 +}
6.18 +
6.19 /* Give up approx. givem milliseconds to the OS. */
6.20 void
6.21 SDL_Delay(Uint32 ms)
7.1 --- a/src/timer/windows/SDL_systimer.c Fri Mar 25 13:48:48 2011 -0700
7.2 +++ b/src/timer/windows/SDL_systimer.c Fri Mar 25 14:45:04 2011 -0700
7.3 @@ -99,6 +99,28 @@
7.4 return (ticks);
7.5 }
7.6
7.7 +Uint64
7.8 +SDL_GetPerformanceCounter(void)
7.9 +{
7.10 + LARGE_INTEGER counter;
7.11 +
7.12 + if (!QueryPerformanceCounter(&counter)) {
7.13 + return SDL_GetTicks();
7.14 + }
7.15 + return counter.QuadPart;
7.16 +}
7.17 +
7.18 +Uint64
7.19 +SDL_GetPerformanceFrequency(void)
7.20 +{
7.21 + LARGE_INTEGER frequency;
7.22 +
7.23 + if (!QueryPerformanceFrequency(&frequency)) {
7.24 + return 1000;
7.25 + }
7.26 + return frequency.QuadPart;
7.27 +}
7.28 +
7.29 void
7.30 SDL_Delay(Uint32 ms)
7.31 {
8.1 --- a/test/testtimer.c Fri Mar 25 13:48:48 2011 -0700
8.2 +++ b/test/testtimer.c Fri Mar 25 14:45:04 2011 -0700
8.3 @@ -29,8 +29,9 @@
8.4 int
8.5 main(int argc, char *argv[])
8.6 {
8.7 - int desired;
8.8 + int i, desired;
8.9 SDL_TimerID t1, t2, t3;
8.10 + Uint64 start, now;
8.11
8.12 if (SDL_Init(SDL_INIT_TIMER) < 0) {
8.13 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
8.14 @@ -85,6 +86,15 @@
8.15 SDL_RemoveTimer(t2);
8.16 SDL_RemoveTimer(t3);
8.17
8.18 + start = SDL_GetPerformanceCounter();
8.19 + for (i = 0; i < 1000000; ++i) {
8.20 + ticktock(0);
8.21 + }
8.22 + now = SDL_GetPerformanceCounter();
8.23 + printf("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
8.24 +
8.25 SDL_Quit();
8.26 return (0);
8.27 }
8.28 +
8.29 +/* vi: set ts=4 sw=4 expandtab: */