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