test/testtimer.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 05 Jun 2013 22:56:42 -0700
changeset 7288 d9fcd19ec866
parent 7191 75360622e65f
child 7316 c3052ed2c310
permissions -rw-r--r--
Fixed building tests on iOS
     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 #if 1 /* FIXME: Rework this using the 2.0 API */
    18 #include <stdio.h>
    19 #include "SDL.h"
    20 
    21 int main(int argc, char *argv[])
    22 {
    23     printf("FIXME\n");
    24     return 0;
    25 }
    26 #else
    27 #include <stdlib.h>
    28 #include <stdio.h>
    29 
    30 #include "SDL.h"
    31 
    32 #define DEFAULT_RESOLUTION  1
    33 
    34 static int ticks = 0;
    35 
    36 static Uint32 SDLCALL
    37 ticktock(Uint32 interval)
    38 {
    39     ++ticks;
    40     return (interval);
    41 }
    42 
    43 static Uint32 SDLCALL
    44 callback(Uint32 interval, void *param)
    45 {
    46     printf("Timer %d : param = %d\n", interval, (int) (uintptr_t) param);
    47     return interval;
    48 }
    49 
    50 int
    51 main(int argc, char *argv[])
    52 {
    53     int i, desired;
    54     SDL_TimerID t1, t2, t3;
    55     Uint64 start, now;
    56 
    57     if (SDL_Init(SDL_INIT_TIMER) < 0) {
    58         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    59         return (1);
    60     }
    61 
    62     /* Start the timer */
    63     desired = 0;
    64     if (argv[1]) {
    65         desired = atoi(argv[1]);
    66     }
    67     if (desired == 0) {
    68         desired = DEFAULT_RESOLUTION;
    69     }
    70     SDL_SetTimer(desired, ticktock);
    71 
    72     /* Wait 10 seconds */
    73     printf("Waiting 10 seconds\n");
    74     SDL_Delay(10 * 1000);
    75 
    76     /* Stop the timer */
    77     SDL_SetTimer(0, NULL);
    78 
    79     /* Print the results */
    80     if (ticks) {
    81         fprintf(stderr,
    82                 "Timer resolution: desired = %d ms, actual = %f ms\n",
    83                 desired, (double) (10 * 1000) / ticks);
    84     }
    85 
    86     /* Test multiple timers */
    87     printf("Testing multiple timers...\n");
    88     t1 = SDL_AddTimer(100, callback, (void *) 1);
    89     if (!t1)
    90         fprintf(stderr, "Could not create timer 1: %s\n", SDL_GetError());
    91     t2 = SDL_AddTimer(50, callback, (void *) 2);
    92     if (!t2)
    93         fprintf(stderr, "Could not create timer 2: %s\n", SDL_GetError());
    94     t3 = SDL_AddTimer(233, callback, (void *) 3);
    95     if (!t3)
    96         fprintf(stderr, "Could not create timer 3: %s\n", SDL_GetError());
    97 
    98     /* Wait 10 seconds */
    99     printf("Waiting 10 seconds\n");
   100     SDL_Delay(10 * 1000);
   101 
   102     printf("Removing timer 1 and waiting 5 more seconds\n");
   103     SDL_RemoveTimer(t1);
   104 
   105     SDL_Delay(5 * 1000);
   106 
   107     SDL_RemoveTimer(t2);
   108     SDL_RemoveTimer(t3);
   109 
   110     start = SDL_GetPerformanceCounter();
   111     for (i = 0; i < 1000000; ++i) {
   112         ticktock(0);
   113     }
   114     now = SDL_GetPerformanceCounter();
   115     printf("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
   116 
   117     SDL_Quit();
   118     return (0);
   119 }
   120 #endif
   121 
   122 /* vi: set ts=4 sw=4 expandtab: */