test/testtimer.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 25 Mar 2011 14:45:04 -0700
changeset 5514 6bd701987ba9
parent 1895 c121d94672cb
child 5535 96594ac5fd1a
permissions -rw-r--r--
Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
     1 
     2 /* Test program to check the resolution of the SDL timer on the current
     3    platform
     4 */
     5 
     6 #include <stdlib.h>
     7 #include <stdio.h>
     8 
     9 #include "SDL.h"
    10 
    11 #define DEFAULT_RESOLUTION	1
    12 
    13 static int ticks = 0;
    14 
    15 static Uint32 SDLCALL
    16 ticktock(Uint32 interval)
    17 {
    18     ++ticks;
    19     return (interval);
    20 }
    21 
    22 static Uint32 SDLCALL
    23 callback(Uint32 interval, void *param)
    24 {
    25     printf("Timer %d : param = %d\n", interval, (int) (uintptr_t) param);
    26     return interval;
    27 }
    28 
    29 int
    30 main(int argc, char *argv[])
    31 {
    32     int i, desired;
    33     SDL_TimerID t1, t2, t3;
    34     Uint64 start, now;
    35 
    36     if (SDL_Init(SDL_INIT_TIMER) < 0) {
    37         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    38         return (1);
    39     }
    40 
    41     /* Start the timer */
    42     desired = 0;
    43     if (argv[1]) {
    44         desired = atoi(argv[1]);
    45     }
    46     if (desired == 0) {
    47         desired = DEFAULT_RESOLUTION;
    48     }
    49     SDL_SetTimer(desired, ticktock);
    50 
    51     /* Wait 10 seconds */
    52     printf("Waiting 10 seconds\n");
    53     SDL_Delay(10 * 1000);
    54 
    55     /* Stop the timer */
    56     SDL_SetTimer(0, NULL);
    57 
    58     /* Print the results */
    59     if (ticks) {
    60         fprintf(stderr,
    61                 "Timer resolution: desired = %d ms, actual = %f ms\n",
    62                 desired, (double) (10 * 1000) / ticks);
    63     }
    64 
    65     /* Test multiple timers */
    66     printf("Testing multiple timers...\n");
    67     t1 = SDL_AddTimer(100, callback, (void *) 1);
    68     if (!t1)
    69         fprintf(stderr, "Could not create timer 1: %s\n", SDL_GetError());
    70     t2 = SDL_AddTimer(50, callback, (void *) 2);
    71     if (!t2)
    72         fprintf(stderr, "Could not create timer 2: %s\n", SDL_GetError());
    73     t3 = SDL_AddTimer(233, callback, (void *) 3);
    74     if (!t3)
    75         fprintf(stderr, "Could not create timer 3: %s\n", SDL_GetError());
    76 
    77     /* Wait 10 seconds */
    78     printf("Waiting 10 seconds\n");
    79     SDL_Delay(10 * 1000);
    80 
    81     printf("Removing timer 1 and waiting 5 more seconds\n");
    82     SDL_RemoveTimer(t1);
    83 
    84     SDL_Delay(5 * 1000);
    85 
    86     SDL_RemoveTimer(t2);
    87     SDL_RemoveTimer(t3);
    88 
    89     start = SDL_GetPerformanceCounter();
    90     for (i = 0; i < 1000000; ++i) {
    91         ticktock(0);
    92     }
    93     now = SDL_GetPerformanceCounter();
    94     printf("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
    95 
    96     SDL_Quit();
    97     return (0);
    98 }
    99 
   100 /* vi: set ts=4 sw=4 expandtab: */