test/testtimer.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5514 6bd701987ba9
child 6256 1d905b13b102
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Test program to check the resolution of the SDL timer on the current
    14    platform
    15 */
    16 
    17 #include <stdlib.h>
    18 #include <stdio.h>
    19 
    20 #include "SDL.h"
    21 
    22 #define DEFAULT_RESOLUTION	1
    23 
    24 static int ticks = 0;
    25 
    26 static Uint32 SDLCALL
    27 ticktock(Uint32 interval)
    28 {
    29     ++ticks;
    30     return (interval);
    31 }
    32 
    33 static Uint32 SDLCALL
    34 callback(Uint32 interval, void *param)
    35 {
    36     printf("Timer %d : param = %d\n", interval, (int) (uintptr_t) param);
    37     return interval;
    38 }
    39 
    40 int
    41 main(int argc, char *argv[])
    42 {
    43     int i, desired;
    44     SDL_TimerID t1, t2, t3;
    45     Uint64 start, now;
    46 
    47     if (SDL_Init(SDL_INIT_TIMER) < 0) {
    48         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    49         return (1);
    50     }
    51 
    52     /* Start the timer */
    53     desired = 0;
    54     if (argv[1]) {
    55         desired = atoi(argv[1]);
    56     }
    57     if (desired == 0) {
    58         desired = DEFAULT_RESOLUTION;
    59     }
    60     SDL_SetTimer(desired, ticktock);
    61 
    62     /* Wait 10 seconds */
    63     printf("Waiting 10 seconds\n");
    64     SDL_Delay(10 * 1000);
    65 
    66     /* Stop the timer */
    67     SDL_SetTimer(0, NULL);
    68 
    69     /* Print the results */
    70     if (ticks) {
    71         fprintf(stderr,
    72                 "Timer resolution: desired = %d ms, actual = %f ms\n",
    73                 desired, (double) (10 * 1000) / ticks);
    74     }
    75 
    76     /* Test multiple timers */
    77     printf("Testing multiple timers...\n");
    78     t1 = SDL_AddTimer(100, callback, (void *) 1);
    79     if (!t1)
    80         fprintf(stderr, "Could not create timer 1: %s\n", SDL_GetError());
    81     t2 = SDL_AddTimer(50, callback, (void *) 2);
    82     if (!t2)
    83         fprintf(stderr, "Could not create timer 2: %s\n", SDL_GetError());
    84     t3 = SDL_AddTimer(233, callback, (void *) 3);
    85     if (!t3)
    86         fprintf(stderr, "Could not create timer 3: %s\n", SDL_GetError());
    87 
    88     /* Wait 10 seconds */
    89     printf("Waiting 10 seconds\n");
    90     SDL_Delay(10 * 1000);
    91 
    92     printf("Removing timer 1 and waiting 5 more seconds\n");
    93     SDL_RemoveTimer(t1);
    94 
    95     SDL_Delay(5 * 1000);
    96 
    97     SDL_RemoveTimer(t2);
    98     SDL_RemoveTimer(t3);
    99 
   100     start = SDL_GetPerformanceCounter();
   101     for (i = 0; i < 1000000; ++i) {
   102         ticktock(0);
   103     }
   104     now = SDL_GetPerformanceCounter();
   105     printf("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
   106 
   107     SDL_Quit();
   108     return (0);
   109 }
   110 
   111 /* vi: set ts=4 sw=4 expandtab: */