test/testtimer.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
slouken@5535
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 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
{
aschiffler@7639
    36
    SDL_Log("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
philipp@9922
    48
    /* Enable standard application logging */
aschiffler@7639
    49
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    50
slouken@1895
    51
    if (SDL_Init(SDL_INIT_TIMER) < 0) {
aschiffler@7639
    52
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    53
        return (1);
slouken@1895
    54
    }
slouken@0
    55
slouken@1895
    56
    /* Start the timer */
slouken@1895
    57
    desired = 0;
slouken@1895
    58
    if (argv[1]) {
slouken@1895
    59
        desired = atoi(argv[1]);
slouken@1895
    60
    }
slouken@1895
    61
    if (desired == 0) {
slouken@1895
    62
        desired = DEFAULT_RESOLUTION;
slouken@1895
    63
    }
slouken@7316
    64
    t1 = SDL_AddTimer(desired, ticktock, NULL);
slouken@0
    65
slouken@1895
    66
    /* Wait 10 seconds */
aschiffler@7639
    67
    SDL_Log("Waiting 10 seconds\n");
slouken@1895
    68
    SDL_Delay(10 * 1000);
slouken@0
    69
slouken@1895
    70
    /* Stop the timer */
slouken@7316
    71
    SDL_RemoveTimer(t1);
slouken@0
    72
slouken@1895
    73
    /* Print the results */
slouken@1895
    74
    if (ticks) {
aschiffler@7639
    75
        SDL_Log("Timer resolution: desired = %d ms, actual = %f ms\n",
slouken@1895
    76
                desired, (double) (10 * 1000) / ticks);
slouken@1895
    77
    }
slouken@0
    78
slouken@1895
    79
    /* Test multiple timers */
aschiffler@7639
    80
    SDL_Log("Testing multiple timers...\n");
slouken@1895
    81
    t1 = SDL_AddTimer(100, callback, (void *) 1);
slouken@1895
    82
    if (!t1)
aschiffler@7639
    83
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Could not create timer 1: %s\n", SDL_GetError());
slouken@1895
    84
    t2 = SDL_AddTimer(50, callback, (void *) 2);
slouken@1895
    85
    if (!t2)
aschiffler@7639
    86
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Could not create timer 2: %s\n", SDL_GetError());
slouken@1895
    87
    t3 = SDL_AddTimer(233, callback, (void *) 3);
slouken@1895
    88
    if (!t3)
aschiffler@7639
    89
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Could not create timer 3: %s\n", SDL_GetError());
slouken@0
    90
slouken@1895
    91
    /* Wait 10 seconds */
aschiffler@7639
    92
    SDL_Log("Waiting 10 seconds\n");
slouken@1895
    93
    SDL_Delay(10 * 1000);
slouken@1895
    94
aschiffler@7639
    95
    SDL_Log("Removing timer 1 and waiting 5 more seconds\n");
slouken@1895
    96
    SDL_RemoveTimer(t1);
slouken@0
    97
slouken@1895
    98
    SDL_Delay(5 * 1000);
slouken@1895
    99
slouken@1895
   100
    SDL_RemoveTimer(t2);
slouken@1895
   101
    SDL_RemoveTimer(t3);
slouken@1895
   102
slouken@5514
   103
    start = SDL_GetPerformanceCounter();
slouken@5514
   104
    for (i = 0; i < 1000000; ++i) {
slouken@7316
   105
        ticktock(0, NULL);
slouken@5514
   106
    }
slouken@5514
   107
    now = SDL_GetPerformanceCounter();
aschiffler@7639
   108
    SDL_Log("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
slouken@5514
   109
philipp@9939
   110
    SDL_Log("Performance counter frequency: %"SDL_PRIu64"\n", (unsigned long long) SDL_GetPerformanceFrequency());
slouken@7316
   111
    start32 = SDL_GetTicks();
slouken@7316
   112
    start = SDL_GetPerformanceCounter();
slouken@7316
   113
    SDL_Delay(1000);
slouken@7316
   114
    now = SDL_GetPerformanceCounter();
slouken@7316
   115
    now32 = SDL_GetTicks();
aschiffler@7639
   116
    SDL_Log("Delay 1 second = %d ms in ticks, %f ms according to performance counter\n", (now32-start32), (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
slouken@7316
   117
slouken@1895
   118
    SDL_Quit();
slouken@1895
   119
    return (0);
slouken@0
   120
}
slouken@5514
   121
slouken@5514
   122
/* vi: set ts=4 sw=4 expandtab: */