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