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