test/testtimer.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    15
static Uint32 SDLCALL
slouken@1662
    16
ticktock (Uint32 interval)
slouken@0
    17
{
slouken@1662
    18
    ++ticks;
slouken@1662
    19
    return (interval);
slouken@0
    20
}
slouken@0
    21
slouken@1662
    22
static Uint32 SDLCALL
slouken@1662
    23
callback (Uint32 interval, void *param)
slouken@0
    24
{
slouken@1662
    25
    printf ("Timer %d : param = %d\n", interval, (int) (uintptr_t) param);
slouken@1662
    26
    return interval;
slouken@0
    27
}
slouken@0
    28
slouken@1662
    29
int
slouken@1662
    30
main (int argc, char *argv[])
slouken@0
    31
{
slouken@1662
    32
    int desired;
slouken@1662
    33
    SDL_TimerID t1, t2, t3;
slouken@0
    34
slouken@1662
    35
    if (SDL_Init (SDL_INIT_TIMER) < 0) {
slouken@1662
    36
        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
slouken@1662
    37
        return (1);
slouken@1662
    38
    }
slouken@0
    39
slouken@1662
    40
    /* Start the timer */
slouken@1662
    41
    desired = 0;
slouken@1662
    42
    if (argv[1]) {
slouken@1662
    43
        desired = atoi (argv[1]);
slouken@1662
    44
    }
slouken@1662
    45
    if (desired == 0) {
slouken@1662
    46
        desired = DEFAULT_RESOLUTION;
slouken@1662
    47
    }
slouken@1662
    48
    SDL_SetTimer (desired, ticktock);
slouken@0
    49
slouken@1662
    50
    /* Wait 10 seconds */
slouken@1662
    51
    printf ("Waiting 10 seconds\n");
slouken@1662
    52
    SDL_Delay (10 * 1000);
slouken@0
    53
slouken@1662
    54
    /* Stop the timer */
slouken@1662
    55
    SDL_SetTimer (0, NULL);
slouken@0
    56
slouken@1662
    57
    /* Print the results */
slouken@1662
    58
    if (ticks) {
slouken@1662
    59
        fprintf (stderr,
slouken@1662
    60
                 "Timer resolution: desired = %d ms, actual = %f ms\n",
slouken@1662
    61
                 desired, (double) (10 * 1000) / ticks);
slouken@1662
    62
    }
slouken@0
    63
slouken@1662
    64
    /* Test multiple timers */
slouken@1662
    65
    printf ("Testing multiple timers...\n");
slouken@1662
    66
    t1 = SDL_AddTimer (100, callback, (void *) 1);
slouken@1662
    67
    if (!t1)
slouken@1662
    68
        fprintf (stderr, "Could not create timer 1: %s\n", SDL_GetError ());
slouken@1662
    69
    t2 = SDL_AddTimer (50, callback, (void *) 2);
slouken@1662
    70
    if (!t2)
slouken@1662
    71
        fprintf (stderr, "Could not create timer 2: %s\n", SDL_GetError ());
slouken@1662
    72
    t3 = SDL_AddTimer (233, callback, (void *) 3);
slouken@1662
    73
    if (!t3)
slouken@1662
    74
        fprintf (stderr, "Could not create timer 3: %s\n", SDL_GetError ());
slouken@0
    75
slouken@1662
    76
    /* Wait 10 seconds */
slouken@1662
    77
    printf ("Waiting 10 seconds\n");
slouken@1662
    78
    SDL_Delay (10 * 1000);
slouken@0
    79
slouken@1662
    80
    printf ("Removing timer 1 and waiting 5 more seconds\n");
slouken@1662
    81
    SDL_RemoveTimer (t1);
slouken@0
    82
slouken@1662
    83
    SDL_Delay (5 * 1000);
slouken@1662
    84
slouken@1662
    85
    SDL_RemoveTimer (t2);
slouken@1662
    86
    SDL_RemoveTimer (t3);
slouken@1662
    87
slouken@1662
    88
    SDL_Quit ();
slouken@1662
    89
    return (0);
slouken@0
    90
}