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