test/testtimer.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Jul 2013 09:51:21 -0700
changeset 7517 965d57022c01
parent 7448 c1f9032d0634
child 7639 9406b7dd2f2d
permissions -rw-r--r--
Updated the copyright year for the test programs
     1 /*
     2   Copyright (C) 1997-2013 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, void *param)
    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     Uint32 start32, now32;
    46     Uint64 start, now;
    47 
    48     if (SDL_Init(SDL_INIT_TIMER) < 0) {
    49         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    50         return (1);
    51     }
    52 
    53     /* Start the timer */
    54     desired = 0;
    55     if (argv[1]) {
    56         desired = atoi(argv[1]);
    57     }
    58     if (desired == 0) {
    59         desired = DEFAULT_RESOLUTION;
    60     }
    61     t1 = SDL_AddTimer(desired, ticktock, NULL);
    62 
    63     /* Wait 10 seconds */
    64     printf("Waiting 10 seconds\n");
    65     SDL_Delay(10 * 1000);
    66 
    67     /* Stop the timer */
    68     SDL_RemoveTimer(t1);
    69 
    70     /* Print the results */
    71     if (ticks) {
    72         fprintf(stderr,
    73                 "Timer resolution: desired = %d ms, actual = %f ms\n",
    74                 desired, (double) (10 * 1000) / ticks);
    75     }
    76 
    77     /* Test multiple timers */
    78     printf("Testing multiple timers...\n");
    79     t1 = SDL_AddTimer(100, callback, (void *) 1);
    80     if (!t1)
    81         fprintf(stderr, "Could not create timer 1: %s\n", SDL_GetError());
    82     t2 = SDL_AddTimer(50, callback, (void *) 2);
    83     if (!t2)
    84         fprintf(stderr, "Could not create timer 2: %s\n", SDL_GetError());
    85     t3 = SDL_AddTimer(233, callback, (void *) 3);
    86     if (!t3)
    87         fprintf(stderr, "Could not create timer 3: %s\n", SDL_GetError());
    88 
    89     /* Wait 10 seconds */
    90     printf("Waiting 10 seconds\n");
    91     SDL_Delay(10 * 1000);
    92 
    93     printf("Removing timer 1 and waiting 5 more seconds\n");
    94     SDL_RemoveTimer(t1);
    95 
    96     SDL_Delay(5 * 1000);
    97 
    98     SDL_RemoveTimer(t2);
    99     SDL_RemoveTimer(t3);
   100 
   101     start = SDL_GetPerformanceCounter();
   102     for (i = 0; i < 1000000; ++i) {
   103         ticktock(0, NULL);
   104     }
   105     now = SDL_GetPerformanceCounter();
   106     printf("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
   107 
   108     printf("Performance counter frequency: %llu\n", (unsigned long long) SDL_GetPerformanceFrequency());
   109     start32 = SDL_GetTicks();
   110     start = SDL_GetPerformanceCounter();
   111     SDL_Delay(1000);
   112     now = SDL_GetPerformanceCounter();
   113     now32 = SDL_GetTicks();
   114     printf("Delay 1 second = %d ms in ticks, %f ms according to performance counter\n", (now32-start32), (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
   115 
   116     SDL_Quit();
   117     return (0);
   118 }
   119 
   120 /* vi: set ts=4 sw=4 expandtab: */