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